1. Introducción

Este cuaderno tiene la finalidad de ilustrar y representar una interpolación espacial usando un conjunto de datos de precipitación mensual sobre el departamento de Boyaca.

rm(list=ls())
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.3.2, PROJ 7.2.1; sf_use_s2() is TRUE
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.3, released 2022/04/22
## Path to GDAL shared files: C:/Users/GERARDO/AppData/Local/R/win-library/4.2/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/GERARDO/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.5-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(rasterVis)
## Loading required package: lattice
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(raster)
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
library(gstat)
library(berryFunctions)
## 
## Attaching package: 'berryFunctions'
## The following object is masked from 'package:raster':
## 
##     distance
## The following object is masked from 'package:dplyr':
## 
##     between
(boyaca <- st_read("C:/Users/GERARDO/Desktop/GB2022/Datos/15_BOYACA/ADMINISTRATIVO/MGN_MPIO_POLITICO.shp"))
## Reading layer `MGN_MPIO_POLITICO' from data source 
##   `C:\Users\GERARDO\Desktop\GB2022\Datos\15_BOYACA\ADMINISTRATIVO\MGN_MPIO_POLITICO.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 123 features and 9 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.66496 ymin: 4.655196 xmax: -71.94885 ymax: 7.055557
## Geodetic CRS:  WGS 84
## Simple feature collection with 123 features and 9 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.66496 ymin: 4.655196 xmax: -71.94885 ymax: 7.055557
## Geodetic CRS:  WGS 84
## First 10 features:
##    DPTO_CCDGO MPIO_CCDGO MPIO_CNMBR                           MPIO_CRSLC
## 1          15      15001      TUNJA                                 1541
## 2          15      15022    ALMEIDA                                 1908
## 3          15      15047  AQUITANIA                                 1789
## 4          15      15051   ARCABUCO                                 1856
## 5          15      15087      BELÉN                                 1756
## 6          15      15090     BERBEO      Ordenanza 28 de Abril 9 de 1913
## 7          15      15092  BETÉITIVA                                 1754
## 8          15      15097    BOAVITA                                 1613
## 9          15      15104     BOYACÁ                                 1537
## 10         15      15106    BRICEÑO Ordenanza 14 del 25 de Julio de 1890
##    MPIO_NAREA MPIO_NANO DPTO_CNMBR Shape_Leng  Shape_Area
## 1   119.68957      2017     BOYACÁ  0.5723744 0.009766301
## 2    57.67212      2017     BOYACÁ  0.3484692 0.004701759
## 3   942.14660      2017     BOYACÁ  1.8003115 0.076843504
## 4   137.89859      2017     BOYACÁ  0.7527090 0.011256738
## 5   163.08822      2017     BOYACÁ  0.6293489 0.013314920
## 6    58.01301      2017     BOYACÁ  0.4291743 0.004730850
## 7   101.89955      2017     BOYACÁ  0.4738184 0.008317810
## 8   145.30529      2017     BOYACÁ  0.6597822 0.011867743
## 9    48.02287      2017     BOYACÁ  0.3256140 0.003918022
## 10   64.59970      2017     BOYACÁ  0.4849753 0.005273255
##                          geometry
## 1  POLYGON ((-73.34014 5.58308...
## 2  POLYGON ((-73.36793 5.01349...
## 3  POLYGON ((-72.76242 5.63856...
## 4  POLYGON ((-73.50487 5.84347...
## 5  POLYGON ((-72.91692 6.08612...
## 6  POLYGON ((-73.0677 5.27048,...
## 7  POLYGON ((-72.81796 5.97422...
## 8  POLYGON ((-72.64907 6.43640...
## 9  POLYGON ((-73.34806 5.47411...
## 10 POLYGON ((-73.89118 5.73749...
(precip <- raster("C:/Users/GERARDO/Desktop/GB2022/Datos/rep_boyaca_dem.tif"))
## class      : RasterLayer 
## dimensions : 680, 288, 195840  (nrow, ncol, ncell)
## resolution : 180, 180  (x, y)
## extent     : 560865, 612705, 412335, 534735  (xmin, xmax, ymin, ymax)
## crs        : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs 
## source     : rep_boyaca_dem.tif 
## names      : rep_boyaca_dem 
## values     : -258, 4201  (min, max)
names(precip) <- "X2019.07"
library(leaflet)
library(RColorBrewer)
pal <- colorNumeric(c("red", "orange", "#fcc000","yellow", "cyan", "blue", "#3240cd"), values(precip$X2019.07), 
                    na.color = "transparent")

leaflet() %>% addTiles() %>%
  addRasterImage(precip$X2019.07 , colors = pal, opacity = 0.8) %>%
  addLegend(pal = pal, values = values(precip$X2019.07),
    title = "Rainfall-Jul.2019 [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
(precip.mask <- mask(x = precip, mask = boyaca))
## class      : RasterLayer 
## dimensions : 680, 288, 195840  (nrow, ncol, ncell)
## resolution : 180, 180  (x, y)
## extent     : 560865, 612705, 412335, 534735  (xmin, xmax, ymin, ymax)
## crs        : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs 
## source     : memory
## names      : X2019.07 
## values     : NA, NA  (min, max)
leaflet() %>% addTiles() %>%
  addRasterImage(precip.mask$X2019.07 , colors = pal, opacity = 0.8) %>%
  addLegend(pal = pal, values = values(precip.mask$X2019.07),
    title = "Rainfall in Boyacá-Jul.2019 [mm]")
## Warning in min(x): ningún argumento finito para min; retornando Inf
## Warning in max(x): ningun argumento finito para max; retornando -Inf
## Warning in pal(c(r[1], cuts, r[2])): Some values were outside the color scale
## and will be treated as NA