library(rgdal)
## Warning: package 'rgdal' was built under R version 4.1.2
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.1.2
## 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-28, (SVN revision 1158)
## 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/JUPA/Documents/R/win-library/4.1/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/JUPA/Documents/R/win-library/4.1/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was C:/Users/JUPA/Documents/R/win-library/4.1/rgdal/proj
library(sf)
## Warning: package 'sf' was built under R version 4.1.2
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(raster)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.1.1 v forcats 0.5.1
## Warning: package 'tibble' was built under R version 4.1.2
## Warning: package 'readr' was built under R version 4.1.2
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x tidyr::extract() masks raster::extract()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x dplyr::select() masks raster::select()
library(gstat)
## Warning: package 'gstat' was built under R version 4.1.2
library(tmap)
## Warning: package 'tmap' was built under R version 4.1.2
library(sp)
library(geojsonio)
## Warning: package 'geojsonio' was built under R version 4.1.2
## Registered S3 method overwritten by 'geojsonsf':
## method from
## print.geojson geojson
##
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
##
## pretty
(Precip <- raster("C:/Users/JUPA/Documents/geomatica/chirps-v2.0.2021.12.6.tif"))
## class : RasterLayer
## dimensions : 2000, 7200, 14400000 (nrow, ncol, ncell)
## resolution : 0.05, 0.05 (x, y)
## extent : -180, 180, -50, 50 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : chirps-v2.0.2021.12.6.tif
## names : chirps.v2.0.2021.12.6
(s <- shapefile("C:/Users/JUPA/Documents/geomatica/68_SANTANDER/ADMINISTRATIVO/MGN_MPIO_POLITICO.shp"))
## class : SpatialPolygonsDataFrame
## features : 87
## extent : -74.52895, -72.47706, 5.707536, 8.14501 (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 : 68, 68001, AGUADA, 1539, 19.69444164, 2017, SANTANDER, 0.270018178002, 0.00160984367921
## max values : 68, 68895, ZAPATOCA, Ordenanza 33 de 1968, 3174.27867176, 2017, SANTANDER, 4.36038638488, 0.259459176725
corte.precipitacion <- raster::crop(Precip, extent(s))
(precip.mask <- mask(x = corte.precipitacion, mask = s))
## class : RasterLayer
## dimensions : 49, 41, 2009 (nrow, ncol, ncell)
## resolution : 0.05, 0.05 (x, y)
## extent : -74.55, -72.5, 5.699999, 8.149999 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : memory
## names : chirps.v2.0.2021.12.6
## values : 3.385371, 108.3905 (min, max)
plot(precip.mask, main= "Precipitaciones CHIRPS [mm]")
plot(s, add=TRUE)

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 [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.points <- rasterToPoints(precip.mask, spatial = TRUE))
## class : SpatialPointsDataFrame
## features : 998
## extent : -74.475, -72.525, 5.774999, 8.124999 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## variables : 1
## names : chirps.v2.0.2021.12.6
## min values : 3.38537120819092
## max values : 108.390510559082
names(precip.points) <- "lluvia"
precip.points
## class : SpatialPointsDataFrame
## features : 998
## extent : -74.475, -72.525, 5.774999, 8.124999 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## variables : 1
## names : lluvia
## min values : 3.38537120819092
## max values : 108.390510559082
str(precip.points)
## Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
## ..@ data :'data.frame': 998 obs. of 1 variable:
## .. ..$ lluvia: num [1:998] 6.89 6.59 7.15 6.22 6.71 ...
## ..@ coords.nrs : num(0)
## ..@ coords : num [1:998, 1:2] -73.8 -73.8 -73.8 -73.8 -73.8 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : NULL
## .. .. ..$ : chr [1:2] "x" "y"
## ..@ bbox : num [1:2, 1:2] -74.47 5.77 -72.52 8.12
## .. ..- 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__
plot(precip.mask, main= "Precipitaciones CHIRPS [mm]")
plot(s, add=TRUE)
points(precip.points$x, precip.points$y, col = "Blue", cex = .1)

geojsonio::geojson_write(precip.points, file = "C:/Users/JUPA/Documents/geomatica/ppoints.geojson11")
## Success! File is at C:/Users/JUPA/Documents/geomatica/ppoints.geojson11.geojson
## <geojson-file>
## Path: C:/Users/JUPA/Documents/geomatica/ppoints.geojson11
## From class: SpatialPointsDataFrame