library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(rgdal)
## Warning: package 'rgdal' was built under R version 4.0.5
## Loading required package: sp
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/Juan Narvaez/OneDrive/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/Juan Narvaez/OneDrive/Documents/R/win-library/4.0/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/Juan Narvaez/OneDrive/Documents/R/win-library/4.0/rgdal/proj
library(rasterVis)
## Warning: package 'rasterVis' was built under R version 4.0.5
## Loading required package: raster
## Warning: package 'raster' was built under R version 4.0.5
## Loading required package: terra
## Warning: package 'terra' was built under R version 4.0.5
## terra version 1.1.4
## 
## Attaching package: 'terra'
## The following object is masked from 'package:rgdal':
## 
##     project
## Loading required package: lattice
## Loading required package: latticeExtra
## Warning: package 'latticeExtra' was built under R version 4.0.5
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:terra':
## 
##     collapse, desc, intersect, near, select, union
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(raster)
library(gstat)
## Warning: package 'gstat' was built under R version 4.0.5
library(geoR)
## Warning: package 'geoR' was built under R version 4.0.5
## --------------------------------------------------------------
##  Analysis of Geostatistical Data
##  For an Introduction to geoR go to http://www.leg.ufpr.br/geoR
##  geoR version 1.8-1 (built on 2020-02-08) is now loaded
## --------------------------------------------------------------
library(berryFunctions)
## Warning: package 'berryFunctions' was built under R version 4.0.5
## 
## Attaching package: 'berryFunctions'
## The following object is masked from 'package:dplyr':
## 
##     between
## The following objects are masked from 'package:terra':
## 
##     classify, distance, rescale
## The following object is masked from 'package:raster':
## 
##     distance
(Guainia <- st_read("geomatica/94_GUAINIA/ADMINISTRATIVO/MGN_MPIO_POLITICO.shp"))
## Reading layer `MGN_MPIO_POLITICO' from data source `C:\Users\Juan Narvaez\OneDrive\Documents\geomatica\94_GUAINIA\ADMINISTRATIVO\MGN_MPIO_POLITICO.shp' using driver `ESRI Shapefile'
## Simple feature collection with 9 features and 9 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -70.94249 ymin: 1.165633 xmax: -66.84722 ymax: 4.045026
## geographic CRS: WGS 84
## Simple feature collection with 9 features and 9 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -70.94249 ymin: 1.165633 xmax: -66.84722 ymax: 4.045026
## geographic CRS: WGS 84
##   DPTO_CCDGO MPIO_CCDGO                MPIO_CNMBR
## 1         94      94001                   INÍRIDA
## 2         94      94343             BARRANCO MINA
## 3         94      94663                MAPIRIPANA
## 4         94      94883                SAN FELIPE
## 5         94      94884           PUERTO COLOMBIA
## 6         94      94885              LA GUADALUPE
## 7         94      94886                  CACAHUAL
## 8         94      94887  PANÁ-PANÁ (Campo Alegre)
## 9         94      94888 MORICHAL (Morichal Nuevo)
##                                                    MPIO_CRSLC MPIO_NAREA
## 1                            Decreto 1593 de Agosto 5 de 1974  15970.268
## 2 Resolución 83 del 1 de Febrero de 1988 que aprueba con modi   9467.804
## 3             ACUERDO COMISARIAL 018 DEL 25 DE AGOSTO DE 1990   4927.971
## 4                                               Resolución 83   2926.365
## 5                                               Resolución 83  15700.545
## 6                                               Resolución 83   1178.868
## 7                                               Resolución 83   2335.323
## 8                                       Resolución 83 de 1988  10227.139
## 9                                                        1988   8554.996
##   MPIO_NANO DPTO_CNMBR Shape_Leng Shape_Area                       geometry
## 1      2017    GUAINÍA   9.256685  1.2869895 POLYGON ((-67.67638 3.91228...
## 2      2017    GUAINÍA   6.825914  0.7652557 POLYGON ((-68.91332 3.68215...
## 3      2017    GUAINÍA   5.010789  0.3990354 POLYGON ((-70.10453 3.38436...
## 4      2017    GUAINÍA   4.029719  0.2346116 POLYGON ((-67.34976 2.50451...
## 5      2017    GUAINÍA   9.166487  1.2632740 POLYGON ((-67.5385 3.177568...
## 6      2017    GUAINÍA   1.750810  0.0943385 POLYGON ((-66.96243 1.66858...
## 7      2017    GUAINÍA   2.364174  0.1876089 POLYGON ((-67.49529 3.75681...
## 8      2017    GUAINÍA   7.805305  0.8250362 POLYGON ((-68.77132 2.42182...
## 9      2017    GUAINÍA   6.916544  0.6917881 POLYGON ((-69.46493 2.88960...
library(climateR)
tc_prcp.guai.16 = getTerraClim(Guainia, param = "prcp", startDate = "2016-06-01")
tc_tmp.16 <- tc_prcp.guai.16[[1]]
tc_tmp.16
## class      : RasterStack 
## dimensions : 71, 99, 7029, 1  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -70.95833, -66.83333, 1.125, 4.083333  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## names      : X2016.06 
## min values :    217.3 
## max values :      647
library(leaflet)
library(RColorBrewer)
pal <- colorNumeric(c("red", "orange", "#fcc000","yellow", "cyan", "blue", "#3240cd"), values(tc_tmp.16$X2016.06), 
                    na.color = "transparent")

leaflet() %>% addTiles() %>%
  addRasterImage(tc_tmp.16$X2016.06 , colors = pal, opacity = 0.8) %>%
  addLegend(pal = pal, values = values(tc_tmp.16$X2016.06),
    title = "Precipitacion Guainia 2016 [mm]")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
(tc_prcp.guai.16.mask <- mask(x = tc_tmp.16, mask = Guainia))
## class      : RasterBrick 
## dimensions : 71, 99, 7029, 1  (nrow, ncol, ncell, nlayers)
## resolution : 0.04166667, 0.04166667  (x, y)
## extent     : -70.95833, -66.83333, 1.125, 4.083333  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : X2016.06 
## min values :    295.8 
## max values :    613.6
leaflet() %>% addTiles() %>%
  addRasterImage(tc_prcp.guai.16.mask$X2016.06 , colors = pal, opacity = 0.8) %>%
  addLegend(pal = pal, values = values(tc_prcp.guai.16.mask$X2016.06),
    title = "Precipitaciones en Guainia-Jun.2016 [mm]")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
(precip.points <- rasterToPoints(tc_prcp.guai.16.mask, spatial = TRUE))
## class       : SpatialPointsDataFrame 
## features    : 3316 
## extent      : -70.9375, -66.85417, 1.1875, 4.020833  (xmin, xmax, ymin, ymax)
## crs         : +proj=longlat +datum=WGS84 +no_defs 
## variables   : 1
## names       : X2016.06 
## min values  :    295.8 
## max values  :    613.6
names(precip.points) <- "rain"
(p.sf.magna <- st_transform(st_as_sf(precip.points), crs=32618))
## Simple feature collection with 3316 features and 1 field
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 951970.6 ymin: 132532.6 xmax: 1409307 ymax: 448045.4
## projected CRS:  WGS 84 / UTM zone 18N
## First 10 features:
##     rain                 geometry
## 1  397.3 POINT (1309252 448045.4)
## 2  372.2   POINT (1258028 442963)
## 3  375.5 POINT (1262687 443001.8)
## 4  385.6 POINT (1299968 443320.4)
## 5  384.8 POINT (1304630 443361.4)
## 6  387.1 POINT (1309293 443402.5)
## 7  366.6 POINT (1244093 438211.2)
## 8  367.6 POINT (1248751 438248.8)
## 9  370.8 POINT (1253408 438286.7)
## 10 375.1 POINT (1258067 438324.8)
(Guainia.sf.magna <- st_transform(Guainia, crs=32618))
## Simple feature collection with 9 features and 9 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 951413.4 ymin: 130083.7 xmax: 1410054 ymax: 450762.9
## projected CRS:  WGS 84 / UTM zone 18N
##   DPTO_CCDGO MPIO_CCDGO                MPIO_CNMBR
## 1         94      94001                   INÍRIDA
## 2         94      94343             BARRANCO MINA
## 3         94      94663                MAPIRIPANA
## 4         94      94883                SAN FELIPE
## 5         94      94884           PUERTO COLOMBIA
## 6         94      94885              LA GUADALUPE
## 7         94      94886                  CACAHUAL
## 8         94      94887  PANÁ-PANÁ (Campo Alegre)
## 9         94      94888 MORICHAL (Morichal Nuevo)
##                                                    MPIO_CRSLC MPIO_NAREA
## 1                            Decreto 1593 de Agosto 5 de 1974  15970.268
## 2 Resolución 83 del 1 de Febrero de 1988 que aprueba con modi   9467.804
## 3             ACUERDO COMISARIAL 018 DEL 25 DE AGOSTO DE 1990   4927.971
## 4                                               Resolución 83   2926.365
## 5                                               Resolución 83  15700.545
## 6                                               Resolución 83   1178.868
## 7                                               Resolución 83   2335.323
## 8                                       Resolución 83 de 1988  10227.139
## 9                                                        1988   8554.996
##   MPIO_NANO DPTO_CNMBR Shape_Leng Shape_Area                       geometry
## 1      2017    GUAINÍA   9.256685  1.2869895 POLYGON ((1315267 436001.5,...
## 2      2017    GUAINÍA   6.825914  0.7652557 POLYGON ((1177179 409311.4,...
## 3      2017    GUAINÍA   5.010789  0.3990354 POLYGON ((1044463 375453.9,...
## 4      2017    GUAINÍA   4.029719  0.2346116 POLYGON ((1353019 279325.7,...
## 5      2017    GUAINÍA   9.166487  1.2632740 POLYGON ((1331365 354234, 1...
## 6      2017    GUAINÍA   1.750810  0.0943385 POLYGON ((1396965 186270.7,...
## 7      2017    GUAINÍA   2.364174  0.1876089 POLYGON ((1335692 418848.3,...
## 8      2017    GUAINÍA   7.805305  0.8250362 POLYGON ((1193854 269284.5,...
## 9      2017    GUAINÍA   6.916544  0.6917881 POLYGON ((1116097 320894.8,...
precip2 <- as(p.sf.magna, 'Spatial')
Guainia2 <- as(Guainia.sf.magna, 'Spatial')
precip2@bbox <- Guainia2@bbox
train_index <- sample(1:nrow(precip2), 0.7 * nrow(precip2))
test_index <- setdiff(1:nrow(precip2), train_index)
ptos_train <- precip2[train_index, ]
ptos_test  <- precip2[test_index,]
ptos_train
## class       : SpatialPointsDataFrame 
## features    : 2321 
## extent      : 956614.2, 1409307, 132532.6, 448045.4  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs 
## variables   : 1
## names       :  rain 
## min values  : 295.8 
## max values  : 613.3
sessionInfo()
## R version 4.0.4 (2021-02-15)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19042)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Spanish_Colombia.1252  LC_CTYPE=Spanish_Colombia.1252   
## [3] LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C                     
## [5] LC_TIME=Spanish_Colombia.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] RColorBrewer_1.1-2    leaflet_2.0.4.1       climateR_0.1.0       
##  [4] berryFunctions_1.20.1 geoR_1.8-1            gstat_2.0-7          
##  [7] dplyr_1.0.5           rasterVis_0.50.1      latticeExtra_0.6-29  
## [10] lattice_0.20-41       terra_1.1-4           raster_3.4-5         
## [13] rgdal_1.5-23          sp_1.4-5              sf_0.9-7             
## 
## loaded via a namespace (and not attached):
##  [1] httr_1.4.2              sass_0.3.1              jsonlite_1.7.2         
##  [4] viridisLite_0.3.0       foreach_1.5.1           bslib_0.2.4            
##  [7] assertthat_0.2.1        yaml_2.2.1              pillar_1.5.1           
## [10] glue_1.4.2              digest_0.6.27           colorspace_2.0-0       
## [13] htmltools_0.5.1.1       pkgconfig_2.0.3         purrr_0.3.4            
## [16] scales_1.1.1            intervals_0.15.2        jpeg_0.1-8.1           
## [19] tibble_3.1.0            proxy_0.4-25            farver_2.1.0           
## [22] generics_0.1.0          ellipsis_0.3.1          hexbin_1.28.2          
## [25] magrittr_2.0.1          crayon_1.4.1            evaluate_0.14          
## [28] fansi_0.4.2             doParallel_1.0.16       MASS_7.3-53            
## [31] xts_0.12.1              class_7.3-18            RandomFieldsUtils_0.5.3
## [34] FNN_1.1.3               tools_4.0.4             lifecycle_1.0.0        
## [37] stringr_1.4.0           munsell_0.5.0           compiler_4.0.4         
## [40] jquerylib_0.1.3         e1071_1.7-5             RNetCDF_2.4-2          
## [43] spacetime_1.2-5         rlang_0.4.10            classInt_0.4-3         
## [46] units_0.7-0             grid_4.0.4              RandomFields_3.3.8     
## [49] iterators_1.0.13        htmlwidgets_1.5.3       crosstalk_1.1.1        
## [52] base64enc_0.1-3         tcltk_4.0.4             rmarkdown_2.7          
## [55] codetools_0.2-18        abind_1.4-5             DBI_1.1.1              
## [58] R6_2.5.0                splancs_2.01-42         zoo_1.8-9              
## [61] knitr_1.31              utf8_1.1.4              KernSmooth_2.23-18     
## [64] stringi_1.5.3           parallel_4.0.4          Rcpp_1.0.6             
## [67] vctrs_0.3.6             png_0.1-7               tidyselect_1.1.0       
## [70] xfun_0.22