Este cuaderno contiene el primer anexo del informe final correspondiente a la asignatura geomática básica. En este escrito, se ilustra la obtención de datos de precipitación chirps para el departamento de Valle del Cauca.
Se procede a cargar las librerias necesarias para elaborar el cuaderno
library(rgdal)
library(sf)
library(raster)
library(tidyverse)
library(gstat)
library(tmap)
library(sp)
Para continuar, es necesario descargar los datos de precipitación CHIRPS correspondendientes a los ultimos 5 días del mes de abril del presente año. Luego, se leen los datos y se crea el objeto “Precip”.
(Precip <- raster("C:/Users/LUISA CARRION/Downloads/chirps-v2.0.2021.03.6.tif"))
Ahora, se lee el archivo shapefile que corresponde a la zona de estudio.
(valle <- shapefile("C:/Users/LUISA CARRION/Downloads/76_VALLE_DEL_CAUCA/ADMINISTRATIVO/MGN_MPIO_POLITICO.shp"))
class : SpatialPolygonsDataFrame
features : 42
extent : -77.54977, -75.70724, 3.091239, 5.047394 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
variables : 9
names : DPTO_CCDGO, MPIO_CCDGO, MPIO_CNMBR, MPIO_CRSLC, MPIO_NAREA, MPIO_NANO, DPTO_CNMBR, Shape_Leng, Shape_Area
min values : 76, 76001, ALCALÃ, 1536, 41.86090736, 2017, VALLE DEL CAUCA, 0.453826056161, 0.00340901158098
max values : 76, 76895, ZARZAL, Ordenanza 9 de Diciembre 1954, 6292.50083741, 2017, VALLE DEL CAUCA, 6.59527269127, 0.510778519244
Empleando la funcion “crop” se recortan los datos de precicipitación con el fin de que tenga la extensión de la zona de interés.
corte.precipitacion <- raster::crop(Precip, extent(valle))
Con los valores obtenidos en el objeto se realiza un raster.
(precip.mask <- mask(x = corte.precipitacion, mask = valle))
class : RasterLayer
dimensions : 39, 37, 1443 (nrow, ncol, ncell)
resolution : 0.05, 0.05 (x, y)
extent : -77.55, -75.7, 3.099999, 5.049999 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : chirps.v2.0.2021.03.6
values : 17.53901, 182.8078 (min, max)
Se plotea el shapefile y el raster de precipitaiones del departamento.
plot(precip.mask, main= "Precipitaciones CHIRPS del 26 al 30 de Abril en 2021 [mm]")
plot(Vichada, add=TRUE)
Haciendo uso de las librerías “leaflet” y “RCcolorBrewer” se puede realizar un mapa que permita una mejor visualización
library(leaflet)
library(RColorBrewer)
pal <- colorNumeric(c("Paired"), values(precip.mask),
na.color = "transparent")
leaflet() %>% addTiles() %>%
addRasterImage(precip.mask, colors = pal, opacity = 0.6) %>%
addLegend(pal = pal, values = values(precip.mask),
title = "Datos CHIRPS de precipitación del 26 al 30 de abril 2021 [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
Con ayuda de la función rasterToPoints() contenida en la librería “raster” se puede convertir un raster a puntos
(precip.points <- rasterToPoints(precip.mask, spatial = TRUE))
class : SpatialPointsDataFrame
features : 677
extent : -77.475, -75.725, 3.124999, 5.024999 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
variables : 1
names : chirps.v2.0.2021.03.6
min values : 17.5390129089355
max values : 182.807846069336
Se cambia el nombre de “chirps-v2.0.2021.03.6” a “lluvia”.
names(precip.points) <- "lluvia"
precip.points
class : SpatialPointsDataFrame
features : 677
extent : -77.475, -75.725, 3.124999, 5.024999 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
variables : 1
names : lluvia
min values : 17.5390129089355
max values : 182.807846069336
str(precip.points)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 677 obs. of 1 variable:
.. ..$ lluvia: num [1:677] 59.3 65.3 68.4 65.9 64.7 ...
..@ coords.nrs : num(0)
..@ coords : num [1:677, 1:2] -76.1 -76.1 -76.1 -76 -76.1 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "x" "y"
..@ bbox : num [1:2, 1:2] -77.47 3.12 -75.72 5.02
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
.. .. ..$ comment: chr "GEOGCRS[\"WGS 84 (with axis order normalized for visualization)\",\n DATUM[\"World Geodetic System 1984\",\n"| __truncated__
Ahora, se grafican los puntos obtenidos sobre el objeto ráster, debido a que cada punto se ubica en el centro de cada celda encontrada en el raster creado.
plot(precip.mask, main= "Precipitaciones CHIRPS del 1 al 06.04.2021 [mm]")
plot(valle, add=TRUE)
points(precip.points$x, precip.points$y, col = "Blue", cex = .1)
Con ayuda de la libreria “Rgdal” se guarda el objeto espacial en un archivo geojson.
geojsonio::geojson_write(precip.points, file = "chirps/ppoints.geojson11")
Registered S3 method overwritten by 'geojsonsf':
method from
print.geojson geojson
Success! File is at chirps/ppoints.geojson11.geojson
<geojson-file>
Path: chirps/ppoints.geojson11
From class: SpatialPointsDataFrame
sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
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 tmap_3.3-2
[4] gstat_2.0-7 forcats_0.5.1 stringr_1.4.0
[7] dplyr_1.0.7 purrr_0.3.4 readr_2.0.0
[10] tidyr_1.1.3 tibble_3.1.3 ggplot2_3.3.5
[13] tidyverse_1.3.1 raster_3.4-13 sf_1.0-1
[16] rgdal_1.5-23 sp_1.4-5
loaded via a namespace (and not attached):
[1] leafem_0.1.6 colorspace_2.0-2
[3] ellipsis_0.3.2 class_7.3-19
[5] base64enc_0.1-3 fs_1.5.0
[7] dichromat_2.0-0 rstudioapi_0.13
[9] httpcode_0.3.0 proxy_0.4-26
[11] farver_2.1.0 fansi_0.5.0
[13] lubridate_1.7.10 xml2_1.3.2
[15] codetools_0.2-18 knitr_1.33
[17] jsonlite_1.7.2 tmaptools_3.1-1
[19] broom_0.7.9 dbplyr_2.1.1
[21] png_0.1-7 rgeos_0.5-5
[23] compiler_4.1.0 httr_1.4.2
[25] backports_1.2.1 assertthat_0.2.1
[27] lazyeval_0.2.2 cli_3.0.1
[29] leaflet.providers_1.9.0 htmltools_0.5.1.1
[31] tools_4.1.0 gtable_0.3.0
[33] glue_1.4.2 geojson_0.3.4
[35] V8_3.4.2 Rcpp_1.0.7
[37] cellranger_1.1.0 vctrs_0.3.8
[39] crul_1.1.0 leafsync_0.1.0
[41] crosstalk_1.1.1 lwgeom_0.2-6
[43] xfun_0.24 rvest_1.0.1
[45] lifecycle_1.0.0 XML_3.99-0.6
[47] jqr_1.2.1 zoo_1.8-9
[49] scales_1.1.1 hms_1.1.0
[51] parallel_4.1.0 yaml_2.2.1
[53] curl_4.3.2 stringi_1.7.3
[55] maptools_1.1-1 e1071_1.7-7
[57] intervals_0.15.2 rlang_0.4.11
[59] pkgconfig_2.0.3 evaluate_0.14
[61] lattice_0.20-44 htmlwidgets_1.5.3
[63] tidyselect_1.1.1 magrittr_2.0.1
[65] geojsonsf_2.0.1 R6_2.5.0
[67] geojsonio_0.9.4 generics_0.1.0
[69] DBI_1.1.1 pillar_1.6.2
[71] haven_2.4.1 foreign_0.8-81
[73] withr_2.4.2 units_0.7-2
[75] stars_0.5-3 xts_0.12.1
[77] abind_1.4-5 spacetime_1.2-5
[79] modelr_0.1.8 crayon_1.4.1
[81] KernSmooth_2.23-20 utf8_1.2.2
[83] tzdb_0.1.2 rmarkdown_2.9
[85] grid_4.1.0 readxl_1.3.1
[87] FNN_1.1.3 reprex_2.0.0
[89] digest_0.6.27 classInt_0.4-3
[91] munsell_0.5.0 viridisLite_0.4.0