Instalar librerias
library(terra)
## terra 1.8.5
library(sf)
## Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
library(leaflet)
library(XML)
library(gdalUtilities)
##
## Adjuntando el paquete: 'gdalUtilities'
## The following object is masked from 'package:sf':
##
## gdal_rasterize
library(dplyr)
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:terra':
##
## intersect, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(RColorBrewer)
url = "https://files.isric.org/soilgrids/latest/data/"
voi = "soc"
depth = "15-30cm"
quantile = "mean"
# concatenate the strings
(variable = paste(url, voi, sep="_"))
## [1] "https://files.isric.org/soilgrids/latest/data/_soc"
(layer = paste(variable,depth,quantile, sep="_"))
## [1] "https://files.isric.org/soilgrids/latest/data/_soc_15-30cm_mean"
(vrt_layer = paste(layer, '.vrt', sep=""))
## [1] "https://files.isric.org/soilgrids/latest/data/_soc_15-30cm_mean.vrt"
(Narino <- st_read("C:/Users/danna/Desktop/Geomática Básica 2024-2/Proyecto5/GB_sem_12/Datos/NARINO_.gpkg"))
## Reading layer `municipios_colombia' from data source
## `C:\Users\danna\Desktop\Geomática Básica 2024-2\Proyecto5\GB_sem_12\Datos\NARINO_.gpkg'
## using driver `GPKG'
## Simple feature collection with 64 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -79.01021 ymin: 0.3613481 xmax: -76.83368 ymax: 2.683898
## Geodetic CRS: MAGNA-SIRGAS
igh='+proj=igh +lat_0=0 +lon_0=0 +datum=WGS84 +units=m +no_defs'
Narino_igh <- st_transform(Narino, igh)
(bbox <- st_bbox(Narino_igh))
## xmin ymin xmax ymax
## -8796326.48 40225.08 -8554103.93 298770.17
ulx = bbox$xmin
uly = bbox$ymax
lrx= bbox$xmax
lry = bbox$ymin
(bb <- c(ulx, uly, lrx, lry))
## xmin ymax xmax ymin
## -8796326.48 298770.17 -8554103.93 40225.08
sg_url="/vsicurl/https://files.isric.org/soilgrids/latest/data/"
datos = 'soc/soc_15-30cm_mean.vrt'
file = "soc_igh_15_30.tif"
gdal_translate(paste0(sg_url,datos), file ,
tr=c(250,250),
projwin=bb,
projwin_srs =igh)
(Narino_soc <- terra::rast(file)/10)
## class : SpatRaster
## dimensions : 1034, 969, 1 (nrow, ncol, nlyr)
## resolution : 250, 250 (x, y)
## extent : -8796500, -8554250, 40500, 299000 (xmin, xmax, ymin, ymax)
## coord. ref. : Interrupted_Goode_Homolosine
## source(s) : memory
## varname : soc_igh_15_30
## name : soc_igh_15_30
## min value : 13.1
## max value : 262.3
terra::hist(Narino_soc)
## Warning: [hist] a sample of 100% of the cells was used (of which 10% was NA)

summary(Narino_soc)
## Warning: [summary] used a sample
## soc_igh_15_30
## Min. : 14.90
## 1st Qu.: 41.50
## Median : 62.30
## Mean : 79.73
## 3rd Qu.:110.10
## Max. :253.40
## NA's :10445
(names(Narino_soc) <- "soc")
## [1] "soc"
valores <- values(Narino_soc, na.rm=TRUE)
orangecyan <- colorNumeric(c("orange","yellow2", "cyan", "darkseagreen" ), valores,
na.color = "transparent")
leaflet::leaflet(Narino) %>%
addTiles() %>%
setView(-78, 1.55, 8) %>%
addPolygons(color = "gray", weight = 1.0, smoothFactor = 0.5,
opacity = 0.5, fillOpacity = 0.10,
popup = paste("Municipio: ", Narino$MPIO_CNMBR)) %>%
addRasterImage(Narino_soc, colors ="Spectral", opacity = 0.8) %>%
addLegend(pal = orangecyan, values = valores, title = "Carbono Organico del Suelo (SOC) [g/kg]")
## Warning: sf layer has inconsistent datum (+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs).
## Need '+proj=longlat +datum=WGS84'