#Configuración
rm(list = ls())
## Instalar y llamar librerías
install.packages("XML")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
G3;Installing package into ‘C:/Users/RAUL TORRES MOYA/AppData/Local/R/win-library/4.5’
(as ‘lib’ is unspecified)
gprobando la URL 'https://cran.rstudio.com/bin/windows/contrib/4.5/XML_3.99-0.18.zip'
Content type 'application/zip' length 3122466 bytes (3.0 MB)
downloaded 3.0 MB
package ‘XML’ successfully unpacked and MD5 sums checked
G1;H1;Errorh in install.packages : ERROR: failed to lock directory ‘C:\Users\RAUL TORRES MOYA\AppData\Local\R\win-library\4.5’ for modifying
Try removing ‘C:\Users\RAUL TORRES MOYA\AppData\Local\R\win-library\4.5/00LOCK’
g
install.packages("sf")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
G3;Installing package into ‘C:/Users/RAUL TORRES MOYA/AppData/Local/R/win-library/4.5’
(as ‘lib’ is unspecified)
gprobando la URL 'https://cran.rstudio.com/bin/windows/contrib/4.5/sf_1.0-21.zip'
Content type 'application/zip' length 44893966 bytes (42.8 MB)
downloaded 42.8 MB
package ‘sf’ successfully unpacked and MD5 sums checked
G1;H1;Errorh in install.packages : ERROR: failed to lock directory ‘C:\Users\RAUL TORRES MOYA\AppData\Local\R\win-library\4.5’ for modifying
Try removing ‘C:\Users\RAUL TORRES MOYA\AppData\Local\R\win-library\4.5/00LOCK’
g
install.packages("gdalUltilities")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
G3;Installing package into ‘C:/Users/RAUL TORRES MOYA/AppData/Local/R/win-library/4.5’
(as ‘lib’ is unspecified)
g
Warning in install.packages :
package ‘gdalUltilities’ is not available for this version of R
A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
install.packages("terra")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
G3;Installing package into ‘C:/Users/RAUL TORRES MOYA/AppData/Local/R/win-library/4.5’
(as ‘lib’ is unspecified)
gprobando la URL 'https://cran.rstudio.com/bin/windows/contrib/4.5/terra_1.8-54.zip'
Content type 'application/zip' length 45403529 bytes (43.3 MB)
downloaded 43.3 MB
package ‘terra’ successfully unpacked and MD5 sums checked
G1;H1;Errorh in install.packages : ERROR: failed to lock directory ‘C:\Users\RAUL TORRES MOYA\AppData\Local\R\win-library\4.5’ for modifying
Try removing ‘C:\Users\RAUL TORRES MOYA\AppData\Local\R\win-library\4.5/00LOCK’
g
install.packages("dplyr")
G1;H1;Errorh in install.packages : Updating loaded packages
g
install.packages("leaflet")
G1;H1;Errorh in install.packages : Updating loaded packages
g
install.packages("RColorBrewer")
G1;H1;Errorh in install.packages : Updating loaded packages
g
library(XML)
library(sf)
library(terra)
G3;terra 1.8.54
g
library(dplyr)
G3;
Adjuntando el paquete: ‘dplyr’
gG3;The following objects are masked from ‘package:terra’:
intersect, union
gG3;The following objects are masked from ‘package:stats’:
filter, lag
gG3;The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
g
library(leaflet)
G3;Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
g
library(RColorBrewer)
url = "https://files.isric.org/soilgrids/latest/data/"
voi= "soc" #carbono orgánico del suelo
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"
Descargar Tiff por la región de interés ROI
(stder = st_read("Choco1/choco.gpkg"))
Reading layer `choco' from data source
`C:\Users\RAUL TORRES MOYA\Documents\UNAL Valentina\GB2\Choco1\choco.gpkg' using driver `GPKG'
Simple feature collection with 30 features and 11 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -77.88378 ymin: 3.964883 xmax: -76.00185 ymax: 8.67773
Geodetic CRS: MAGNA-SIRGAS
Simple feature collection with 30 features and 11 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -77.88378 ymin: 3.964883 xmax: -76.00185 ymax: 8.67773
Geodetic CRS: MAGNA-SIRGAS
First 10 features:
dpto_ccdgo mpio_ccdgo mpio_cdpmp dpto_cnmbr mpio_cnmbr
1 27 001 27001 CHOCÓ QUIBDÓ
2 27 025 27025 CHOCÓ ALTO BAUDÓ
3 27 050 27050 CHOCÓ ATRATO
4 27 073 27073 CHOCÓ BAGADÓ
5 27 099 27099 CHOCÓ BOJAYÁ
6 27 245 27245 CHOCÓ EL CARMEN DE ATRATO
7 27 413 27413 CHOCÓ LLORÓ
8 27 425 27425 CHOCÓ MEDIO ATRATO
9 27 495 27495 CHOCÓ NUQUÍ
10 27 600 27600 CHOCÓ RÍO QUITO
mpio_crslc mpio_tipo mpio_narea mpio_nano
1 Decreto ejecutivo de 30 de Marzo de 1825 MUNICIPIO 3507.1228 2023
2 Ordenanza 16 del 21 Noviembre de 1958 MUNICIPIO 2050.3982 2023
3 ORD 10 DE MAYO 09 DE 1997 MUNICIPIO 421.5667 2023
4 1857 MUNICIPIO 807.4542 2023
5 Ordenanza 13 de Diciembre 12 de 1960 MUNICIPIO 3630.5716 2023
6 Ordenanza 1 del 11 de Enero de 1883 MUNICIPIO 830.1809 2023
7 Ordenanza 15 de Diciembre 13 de 1955. Ordenanza 16 Noviemb MUNICIPIO 842.8958 2023
8 Ordenanza 008 del 23 de Junio de 1999 MUNICIPIO 1812.9329 2023
9 1917 MUNICIPIO 704.0984 2023
10 Ordenanza 4 del 25 de Abril de 1999 MUNICIPIO 698.9568 2023
shape_Leng shape_Area geom
1 5.361054 0.28583233 MULTIPOLYGON (((-76.79793 6...
2 3.348864 0.16690542 MULTIPOLYGON (((-77.16987 6...
3 1.351263 0.03433926 MULTIPOLYGON (((-76.51249 5...
4 1.470993 0.06580383 MULTIPOLYGON (((-76.08567 5...
5 3.933908 0.29597381 MULTIPOLYGON (((-76.91531 6...
6 1.430906 0.06769170 MULTIPOLYGON (((-76.11068 6...
7 1.775616 0.06868411 MULTIPOLYGON (((-76.39108 5...
8 2.649834 0.14777937 MULTIPOLYGON (((-76.24923 6...
9 2.440645 0.05729703 MULTIPOLYGON (((-77.28584 5...
10 1.494709 0.05691633 MULTIPOLYGON (((-76.76326 5...
igh='+proj=igh +lat_0=0 +lon_0=0 +datum=WGS84 +units=m +no_defs'
stder_igh <- st_transform(stder, igh)
(bbox <- st_bbox(stder_igh))
xmin ymin xmax ymax
-8689487.2 441368.8 -8473239.5 966000.4
## ul means upper left
## lr means lower right
ulx = bbox$xmin
uly = bbox$ymax
lrx= bbox$xmax
lry = bbox$ymin
(bb <- c(ulx, uly, lrx, lry))
xmin ymax xmax ymin
-8689487.2 966000.4 -8473239.5 441368.8
sg_url="/vsicurl/https://files.isric.org/soilgrids/latest/data/"
datos = 'soc/soc_15-30cm_mean.vrt'
file = "soc_igh_15_30.tif"
DESDE AQUÍ EL CÓDIGO NO FUNCIONA :C
# UNCOMMENT FOR FIRST TIME USE
# THEN, COMMENT IT BACK
gdal_translate(paste0(sg_url,datos), file , tr=c(250,250), projwin=bb,
projwin_srs =igh)
(stder_soc <- terra::rast(file)/10)
terra::hist(stder_soc)
summary(stder_soc)
(names(stder_soc) <- "soc")
valores <- values(stder_soc, na.rm=TRUE)
# change as needed
orangecyan <- colorNumeric(c("orange","yellow2", "darkseagreen", "cyan" ), valores,
na.color = "transparent")
# plot an interactive map
leaflet::leaflet(stder) %>%
addTiles() %>%
setView(-74, 6, 9) %>%
addPolygons(color = "gray", weight = 1.0, smoothFactor = 0.5,
opacity = 0.5, fillOpacity = 0.10,
popup = paste("Municipio: ", stder$MPIO_CNMBR)) %>%
addRasterImage(stder_soc, colors ="Spectral", opacity = 0.8) %>%
addLegend(pal = orangecyan, values = valores, title = "Soil Organic Carbon (SOC) [g/kg]")
# change as needed
set.seed(123456)
# Conversion of SOC coordinate reference system
geog ="+proj=longlat +datum=WGS84"
(geog.soc = project(stder_soc, geog))
# Random sampling of 2000 points
(samples <- terra::spatSample(geog.soc, 2000, "random", as.points=TRUE))
(muestras <- sf::st_as_sf(samples))
nmuestras <- na.omit(muestras)
longit <- st_coordinates(nmuestras)[,1]
latit <- st_coordinates(nmuestras)[,2]
soc <- nmuestras$soc
summary(soc)
length(soc)
id <- seq(1,1850,1)
(sitios <- data.frame(id, longit, latit, soc))
m <- leaflet() %>%
addTiles() %>%
addMarkers(lng=sitios$longit,lat=sitios$latit, popup=sitios$soc, clusterOptions = markerClusterOptions())
m # Print the map
# change path and filename to match your directories
# as well as your department name
sf::st_write(nmuestras, "data/soc_stder.gpkg", driver = "GPKG",
append=FALSE)
sessionInfo()