Extracción y mapeo de datos de SoilGrids

Bibliotecas

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(gdalUtils)
## Warning: package 'gdalUtils' was built under R version 4.1.2
library(gdalUtilities)
## Warning: package 'gdalUtilities' was built under R version 4.1.2
## 
## Attaching package: 'gdalUtilities'
## The following objects are masked from 'package:gdalUtils':
## 
##     gdal_grid, gdal_rasterize, gdal_translate, gdalbuildvrt, gdaldem,
##     gdalinfo, gdalwarp, nearblack, ogr2ogr
library(raster)
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
## 
## Attaching package: 'sf'
## The following object is masked from 'package:gdalUtilities':
## 
##     gdal_rasterize
## The following object is masked from 'package:gdalUtils':
## 
##     gdal_rasterize
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(ggspatial)
## Warning: package 'ggspatial' was built under R version 4.1.2
library(dplyr)
library(magrittr)
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
## 
##     set_names
## The following object is masked from 'package:tidyr':
## 
##     extract
## The following object is masked from 'package:raster':
## 
##     extract
library(knitr)
## Warning: package 'knitr' was built under R version 4.1.2

Raster Nitrógeno

nitro <- raster("https://files.isric.org/soilgrids/latest/data/nitrogen/nitrogen_15-30cm_mean.vrt")
nitro
## class      : RasterLayer 
## dimensions : 58034, 159246, 9241682364  (nrow, ncol, ncell)
## resolution : 250, 250  (x, y)
## extent     : -19949750, 19861750, -6147500, 8361000  (xmin, xmax, ymin, ymax)
## crs        : +proj=igh +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
## source     : nitrogen_15-30cm_mean.vrt 
## names      : nitrogen_15.30cm_mean 
## values     : -32768, 32767  (min, max)

Shp departamentos de Colombia

dpto= st_read("C:/Users/JUPA/Documents/geomatica/MGN_DPTO_POLITICO.shp")
## Reading layer `MGN_DPTO_POLITICO' from data source 
##   `C:\Users\JUPA\Documents\geomatica\MGN_DPTO_POLITICO.shp' using driver `ESRI Shapefile'
## Simple feature collection with 33 features and 9 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -81.73562 ymin: -4.229406 xmax: -66.84722 ymax: 13.39473
## Geodetic CRS:  WGS 84

Proyeccion de coordenadas

igh <- '+proj=igh +lat_0=0 +lon_0=0 +datum=WGS84 +units=m +no_defs'
colombia_igh <- st_transform(dpto, igh)
colombia_igh
## Simple feature collection with 33 features and 9 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -9147200 ymin: -470815.4 xmax: -7442381 ymax: 1491094
## CRS:           +proj=igh +lat_0=0 +lon_0=0 +datum=WGS84 +units=m +no_defs
## First 10 features:
##    DPTO_CCDGO DPTO_CNMBR DPTO_ANO_C
## 1          05  ANTIOQUIA       1886
## 2          23    CÓRDOBA       1951
## 3          27      CHOCÓ       1947
## 4          70      SUCRE       1966
## 5          08  ATLÁNTICO       1910
## 6          13    BOLÍVAR       1886
## 7          47  MAGDALENA       1964
## 8          20      CESAR       1967
## 9          44 LA GUAJIRA       1964
## 10         19      CAUCA       1857
##                                        DPTO_ACT_A DPTO_NAREA DPTO_CSMBL
## 1                   Constitucion Politica de 1886   62804.71          3
## 2               Ley 9 del 18 de Diciembre de 1951   25086.55          3
## 3               Ley 13 del 3 de Noviembre de 1947   48353.22          3
## 4                  Ley 47 del 8 de Agosto de 1966   10591.85          3
## 5                                  Ley 21 de 1910    3313.81          3
## 6                   Constitucion Politica de 1886   26719.21          3
## 7                                            1964   23135.94          3
## 8                     Ley 25  21 de junio de 1967   22565.31          3
## 9  Acto Legislativo No. 1 de Diciembre 28 de 1964   20619.01          3
## 10                            15 de junio de 1857   31242.91          3
##    DPTO_VGNC SHAPE_AREA SHAPE_LEN                       geometry
## 1       2020  5.1349154 21.443794 MULTIPOLYGON (((-8537745 98...
## 2       2020  2.0575332  9.691516 MULTIPOLYGON (((-8487485 10...
## 3       2020  3.9397431 20.634463 MULTIPOLYGON (((-8641428 96...
## 4       2020  0.8708103  8.570869 MULTIPOLYGON (((-8449360 11...
## 5       2020  0.2737698  2.544651 MULTIPOLYGON (((-8384559 12...
## 6       2020  2.1955769 16.234817 MULTIPOLYGON (((-8426196 12...
## 7       2020  1.9092663 10.815958 MULTIPOLYGON (((-8298708 12...
## 8       2020  1.8582044 12.578459 MULTIPOLYGON (((-8229790 12...
## 9       2020  1.7068745 10.786905 MULTIPOLYGON (((-8050536 13...
## 10      2020  2.5344192 13.950263 MULTIPOLYGON (((-8515823 37...
area <- st_bbox(colombia_igh)
area
##       xmin       ymin       xmax       ymax 
## -9147200.3  -470815.4 -7442381.4  1491094.3
ulx <- area$xmin
uly <- area$ymax
lrx <- area$xmax
lry <- area$ymin
limites <- c(ulx, uly, lrx, lry)
limites
##       xmin       ymax       xmax       ymin 
## -9147200.3  1491094.3 -7442381.4  -470815.4

N de Colombia

sg_url <- "/vsicurl/https://files.isric.org/soilgrids/latest/data/nitrogen/nitrogen_15-30cm_mean.vrt"
lfile <- "C:/Users/JUPA/Documents/geomatica/eda/nitrogeno_colombia.tif"
gdal_translate(sg_url, lfile ,
               tr = c(250,250),
               projwin = limites,
               projwin_srs = igh)
nitro_colombia <- raster("C:/Users/JUPA/Documents/geomatica/eda/nitrogeno_colombia.tif")/ 100
plot(nitro_colombia)

corte_colombia <- nitro_colombia %>% 
  crop(colombia_igh) %>% 
  mask(colombia_igh)
plot(corte_colombia)

sant <- colombia_igh %>% 
  filter(DPTO_CNMBR == "SANTANDER")
corte_sant <- nitro_colombia %>% 
  crop(sant) %>% 
  mask(sant)
plot(corte_sant, colNA = "black")

sant_df <- corte_sant %>% 
  as("SpatialPixelsDataFrame") %>% 
  as_tibble()

sant_df %>% head()
## # A tibble: 6 x 3
##   nitrogeno_colombia        x      y
##                <dbl>    <dbl>  <dbl>
## 1               2    -8242125 905625
## 2               2.23 -8242875 905375
## 3               2.16 -8242625 905375
## 4               2.45 -8242125 905375
## 5               1.73 -8243875 905125
## 6               1.79 -8243625 905125
ggplot() +
  geom_raster(data = sant_df, aes(x = x, y = y, fill = nitrogeno_colombia)) +
  geom_sf(data = sant, alpha = 0, fill = "transparent") +
  labs(fill = "Nitrógeno (cg/kg)",
       x = "Longitud", y = "Latitud") +
  annotation_north_arrow(location = "tl") +
  scale_fill_viridis_c(direction = -1) +
  theme_bw()