rm(list=ls())
library(elevatr)
## elevatr v0.99.0 NOTE: Version 0.99.0 of 'elevatr' uses 'sf' and 'terra'. Use
## of the 'sp', 'raster', and underlying 'rgdal' packages by 'elevatr' is being
## deprecated; however, get_elev_raster continues to return a RasterLayer. This
## will be dropped in future versions, so please plan accordingly.
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(leaflet)
library(terra)
## terra 1.8.29
library(MultiscaleDTM)
library(exactextractr)
(dem=terra::rast("datos/elev_cauca_z8.tif"))
## class : SpatRaster
## dimensions : 1535, 1537, 1 (nrow, ncol, nlyr)
## resolution : 0.002745343, 0.002745343 (x, y)
## extent : -78.75, -74.53041, 0.0008414842, 4.214943 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
## source : elev_cauca_z8.tif
## name : elev_cauca_z8
## min value : -4109
## max value : 5730
dem2=terra::aggregate(dem,2,"mean")
(munic<- sf::st_read("datos/cauca_munic.gpkg"))
## Reading layer `cauca_munic' from data source
## `E:\Acer\Universidad Nacional de Colombia sede Bogota\sexto\geomatica\GB2 juan\r proyect notebook\geomorfometria\datos\cauca_munic.gpkg'
## using driver `GPKG'
## Simple feature collection with 42 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -77.92834 ymin: 0.9580285 xmax: -75.74782 ymax: 3.328941
## Geodetic CRS: MAGNA-SIRGAS
dem3=terra::crop(dem2,munic,mask=TRUE)
(dem_plane=project(dem3,"EPSG:9377"))
## class : SpatRaster
## dimensions : 432, 399, 1 (nrow, ncol, nlyr)
## resolution : 609.7177, 609.7177 (x, y)
## extent : 4451430, 4694708, 1664048, 1927446 (xmin, xmax, ymin, ymax)
## coord. ref. : MAGNA-SIRGAS 2018 / Origen-Nacional (EPSG:9377)
## source(s) : memory
## name : elev_cauca_z8
## min value : -89.38462
## max value : 5240.40283
(munic_plane=sf::st_transform(munic,"EPSG:9377"))
(slp_asp = MultiscaleDTM::SlpAsp(
dem_plane,
w = c(3, 3),
unit = "degrees",
method = "queen",
metrics = c("slope", "aspect"),
na.rm = TRUE,
include_scale = FALSE,
mask_aspect = TRUE
))
## class : SpatRaster
## dimensions : 432, 399, 2 (nrow, ncol, nlyr)
## resolution : 609.7177, 609.7177 (x, y)
## extent : 4451430, 4694708, 1664048, 1927446 (xmin, xmax, ymin, ymax)
## coord. ref. : MAGNA-SIRGAS 2018 / Origen-Nacional (EPSG:9377)
## source(s) : memory
## names : slope, aspect
## min values : 8.164541e-04, 1.995955e-03
## max values : 3.657462e+01, 3.599998e+02
(slope = subset(slp_asp, 1))
## class : SpatRaster
## dimensions : 432, 399, 1 (nrow, ncol, nlyr)
## resolution : 609.7177, 609.7177 (x, y)
## extent : 4451430, 4694708, 1664048, 1927446 (xmin, xmax, ymin, ymax)
## coord. ref. : MAGNA-SIRGAS 2018 / Origen-Nacional (EPSG:9377)
## source(s) : memory
## name : slope
## min value : 8.164541e-04
## max value : 3.657462e+01
terra::hist(slope,
main = "Pendiente de Cauca",
xlab = "Pendiente (in degrees)")
(aspect =subset(slp_asp, 2))
## class : SpatRaster
## dimensions : 432, 399, 1 (nrow, ncol, nlyr)
## resolution : 609.7177, 609.7177 (x, y)
## extent : 4451430, 4694708, 1664048, 1927446 (xmin, xmax, ymin, ymax)
## coord. ref. : MAGNA-SIRGAS 2018 / Origen-Nacional (EPSG:9377)
## source(s) : memory
## name : aspect
## min value : 1.995955e-03
## max value : 3.599998e+02
terra::hist(aspect,
main = "Cauca's aspect",
xlab = "Aspect (in degrees)")
(slope_perc = tan(slope*(pi/180))*100)
## class : SpatRaster
## dimensions : 432, 399, 1 (nrow, ncol, nlyr)
## resolution : 609.7177, 609.7177 (x, y)
## extent : 4451430, 4694708, 1664048, 1927446 (xmin, xmax, ymin, ymax)
## coord. ref. : MAGNA-SIRGAS 2018 / Origen-Nacional (EPSG:9377)
## source(s) : memory
## name : slope
## min value : 0.001424981
## max value : 74.197847671
terra::hist(slope_perc,
main = "Cauca's slope",
xlab = "Slope (in percentage)")
m <- c(0, 3, 1,
3, 7, 2,
7, 12, 3,
12, 25, 4,
25, 50, 5,
50, 75, 6,
75, 80, 7)
m <- matrix(m, ncol=3, byrow = TRUE)
rc <- classify(slope_perc, m, right=TRUE)
(munic$mean_slope <- exactextractr::exact_extract(slope_perc, munic, 'mean'))
## Warning in .local(x, y, ...): Polygons transformed to raster CRS (EPSG:9377)
##
|
| | 0%
|
|== | 2%
|
|=== | 5%
|
|===== | 7%
|
|======= | 10%
|
|======== | 12%
|
|========== | 14%
|
|============ | 17%
|
|============= | 19%
|
|=============== | 21%
|
|================= | 24%
|
|================== | 26%
|
|==================== | 29%
|
|====================== | 31%
|
|======================= | 33%
|
|========================= | 36%
|
|=========================== | 38%
|
|============================ | 40%
|
|============================== | 43%
|
|================================ | 45%
|
|================================= | 48%
|
|=================================== | 50%
|
|===================================== | 52%
|
|====================================== | 55%
|
|======================================== | 57%
|
|========================================== | 60%
|
|=========================================== | 62%
|
|============================================= | 64%
|
|=============================================== | 67%
|
|================================================ | 69%
|
|================================================== | 71%
|
|==================================================== | 74%
|
|===================================================== | 76%
|
|======================================================= | 79%
|
|========================================================= | 81%
|
|========================================================== | 83%
|
|============================================================ | 86%
|
|============================================================== | 88%
|
|=============================================================== | 90%
|
|================================================================= | 93%
|
|=================================================================== | 95%
|
|==================================================================== | 98%
|
|======================================================================| 100%
## [1] 6.0591545 21.2668552 25.5946407 23.8386974 16.3837967 11.3771334
## [7] 8.0072994 10.7128363 12.0224895 16.9185810 17.1392117 14.1960907
## [13] 0.5801414 6.3647037 15.0563478 20.2112751 12.2242479 17.6430302
## [19] 10.1799469 10.2113247 14.3064795 13.4449120 0.4071290 20.6346149
## [25] 10.3958302 9.3028030 5.8970127 0.2290211 11.6826315 13.3244066
## [31] 13.2739315 7.3543401 18.1282501 11.7993879 13.3046751 15.0261841
## [37] 18.0949020 5.2900014 6.7969737 21.5576363 9.3154936 0.2428199
hist(munic$mean_slope,
main = "Cauca's mean slope",
xlab = "Slope (in percentage)")
(munic$class <- exactextractr::exact_extract(rc, munic, 'mode'))
## Warning in .local(x, y, ...): Polygons transformed to raster CRS (EPSG:9377)
##
|
| | 0%
|
|== | 2%
|
|=== | 5%
|
|===== | 7%
|
|======= | 10%
|
|======== | 12%
|
|========== | 14%
|
|============ | 17%
|
|============= | 19%
|
|=============== | 21%
|
|================= | 24%
|
|================== | 26%
|
|==================== | 29%
|
|====================== | 31%
|
|======================= | 33%
|
|========================= | 36%
|
|=========================== | 38%
|
|============================ | 40%
|
|============================== | 43%
|
|================================ | 45%
|
|================================= | 48%
|
|=================================== | 50%
|
|===================================== | 52%
|
|====================================== | 55%
|
|======================================== | 57%
|
|========================================== | 60%
|
|=========================================== | 62%
|
|============================================= | 64%
|
|=============================================== | 67%
|
|================================================ | 69%
|
|================================================== | 71%
|
|==================================================== | 74%
|
|===================================================== | 76%
|
|======================================================= | 79%
|
|========================================================= | 81%
|
|========================================================== | 83%
|
|============================================================ | 86%
|
|============================================================== | 88%
|
|=============================================================== | 90%
|
|================================================================= | 93%
|
|=================================================================== | 95%
|
|==================================================================== | 98%
|
|======================================================================| 100%
## [1] 1 4 5 5 4 4 1 4 4 4 4 4 1 1 4 4 4 4 1 4 1 4 1 4 2 1 1 1 4 4 4 1 4 4 4 4 4 1
## [39] 1 4 3 1
hist(munic$class,
main = "Recalsificación de pendiente",
xlab = "Pendiente (por categoria)")
(rc.geo = project(rc, "EPSG:4326"))
## class : SpatRaster
## dimensions : 434, 399, 1 (nrow, ncol, nlyr)
## resolution : 0.005490595, 0.005490595 (x, y)
## extent : -77.93401, -75.74326, 0.9565678, 3.339486 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## source(s) : memory
## name : slope
## min value : 1
## max value : 6
(slope.geo = project(slope_perc, "EPSG:4326"))
## class : SpatRaster
## dimensions : 434, 399, 1 (nrow, ncol, nlyr)
## resolution : 0.005490595, 0.005490595 (x, y)
## extent : -77.93401, -75.74326, 0.9565678, 3.339486 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## source(s) : memory
## name : slope
## min value : 0.004573855
## max value : 71.354812622
palredgreen <- colorNumeric(c("darkseagreen3","yellow2", "orange", "brown2", "darkred"), values(slope.geo),
na.color = "transparent")
munic_wgs84 <- st_transform(munic, crs = 4326)
leaflet(munic_wgs84) %>% addTiles() %>% setView(-75.9, 2.0, 8) %>%
addPolygons(color = "gray", weight = 1.0, smoothFactor = 0.5,
opacity = 0.4, fillOpacity = 0.10,
popup = paste("Municipio: ", munic$mpio_cnmbr, "<br>",
"Slope class: ", munic$class, "<br>")) %>%
addRasterImage(slope.geo, colors = palredgreen, opacity = 0.8) %>%
addLegend(pal = palredgreen, values = values(slope.geo),
title = "Cauca pendiente porcentaje (%)")
library(sf)
library(terra)
library(leaflet)
print(st_crs(munic))
## Coordinate Reference System:
## User input: MAGNA-SIRGAS
## wkt:
## GEOGCRS["MAGNA-SIRGAS",
## DATUM["Marco Geocentrico Nacional de Referencia",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## USAGE[
## SCOPE["Horizontal component of 3D system."],
## AREA["Colombia - onshore and offshore. Includes San Andres y Providencia, Malpelo Islands, Roncador Bank, Serrana Bank and Serranilla Bank."],
## BBOX[-4.23,-84.77,15.51,-66.87]],
## ID["EPSG",4686]]
if (st_crs(munic)$epsg != 4326) {
munic <- st_transform(munic, crs = 4326)
}
print(terra::crs(slope.geo))
## [1] "GEOGCRS[\"WGS 84\",\n ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n MEMBER[\"World Geodetic System 1984 (Transit)\"],\n MEMBER[\"World Geodetic System 1984 (G730)\"],\n MEMBER[\"World Geodetic System 1984 (G873)\"],\n MEMBER[\"World Geodetic System 1984 (G1150)\"],\n MEMBER[\"World Geodetic System 1984 (G1674)\"],\n MEMBER[\"World Geodetic System 1984 (G1762)\"],\n MEMBER[\"World Geodetic System 1984 (G2139)\"],\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ENSEMBLEACCURACY[2.0]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n USAGE[\n SCOPE[\"Horizontal component of 3D system.\"],\n AREA[\"World.\"],\n BBOX[-90,-180,90,180]],\n ID[\"EPSG\",4326]]"
if (terra::crs(slope.geo, describe = TRUE)$code != 4326) { names(slope.geo) <- "slope"
slope.geo <- terra::project(slope.geo, "EPSG:4326")
}
centers <- st_centroid(munic)
palredgreen <- colorNumeric(palette = c("#EEDFCC","#BF3EFF","darkseagreen3","#C1CDCD", "#0000CD", "brown2","#8B4513", "darkred"),
domain = values(slope.geo, na.rm = TRUE))
leaflet(munic) %>%
addTiles() %>%
setView(lng = -75.9, lat = 2.0, zoom = 8) %>%
addPolygons(color = "black", weight = 1.0, smoothFactor = 0.5,
opacity = 0.30, fillOpacity = 0.05,
popup = paste("Municipio: ", munic$mpio_cnmbr, "<br>",
"Km2: ", munic$area, "<br>",
"Pendiente media Cauca (grados): ", round(munic$class, 2), "<br>")) %>%
addLabelOnlyMarkers(
data = centers,
lng = ~st_coordinates(centers)[,1],
lat = ~st_coordinates(centers)[,2],
label = ~paste(mpio_cnmbr, " ", round(munic$class, 2), "°"),
labelOptions = labelOptions(
noHide = TRUE,
direction = 'top',
textOnly = TRUE,
textsize = "10px",
style = list(
"color" = "black",
"font-family" = "Georgia",
"font-weight" = "bold",
"background-color" = "rgba(255,255,255,0.6)",
"padding" = "4px",
"border-radius" = "4px"
)
)
) %>%
addRasterImage(slope.geo, colors = palredgreen, opacity = 0.9, project = FALSE) %>%
addLegend(pal = palredgreen, values = values(slope.geo),
title = "Pendiente de Cauca (grados)")
library(raster)
## Warning: package 'raster' was built under R version 4.3.3
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.3.3
elev_cauca <- raster("datos/elev_cauca_z8.tif")
exten_cauca <- extent(-78, -75, 1, 2)
elev_cauca_fin <- crop(elev_cauca, exten_cauca)
elev_cauca_fin_reducida <- aggregate(elev_cauca, fact=8)
aspecto_final <- terrain(elev_cauca_fin_reducida, opt="aspect", unit= "degrees", neighbors=8)
#Paleta de colores
colors <- colorRampPalette(c("magenta","orange","red", "yellow", "green","blue"))(255)
leaflet() %>%
addTiles() %>%
setView(lng = -75.9, lat = 2.0, zoom = 7.2) %>%
addRasterImage(aspecto_final, colors = colors, opacity = 0.8) %>%
addLegend(pal = colorNumeric(c("magenta", "red", "yellow", "orange", "green", "blue"), values(aspecto_final)), values = values(aspecto_final), title = "Aspecto del terreno")
sessionInfo()
## R version 4.3.2 (2023-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 11 x64 (build 26100)
##
## Matrix products: default
##
##
## locale:
## [1] LC_COLLATE=Spanish_Colombia.utf8 LC_CTYPE=Spanish_Colombia.utf8
## [3] LC_MONETARY=Spanish_Colombia.utf8 LC_NUMERIC=C
## [5] LC_TIME=Spanish_Colombia.utf8
##
## time zone: America/Bogota
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] raster_3.6-32 sp_2.2-0 exactextractr_0.10.0
## [4] MultiscaleDTM_0.9.1 terra_1.8-29 leaflet_2.2.2
## [7] sf_1.0-20 elevatr_0.99.0
##
## loaded via a namespace (and not attached):
## [1] s2_1.1.7 utf8_1.2.4 sass_0.4.8 generics_0.1.3
## [5] class_7.3-22 KernSmooth_2.23-22 lattice_0.21-9 digest_0.6.34
## [9] magrittr_2.0.3 rgl_1.3.1 evaluate_0.23 grid_4.3.2
## [13] fastmap_1.2.0 jsonlite_1.8.8 e1071_1.7-16 DBI_1.2.3
## [17] promises_1.3.2 purrr_1.0.2 fansi_1.0.6 scales_1.3.0
## [21] crosstalk_1.2.1 codetools_0.2-19 jquerylib_0.1.4 cli_3.6.2
## [25] shiny_1.10.0 rlang_1.1.3 units_0.8-7 ellipsis_0.3.2
## [29] munsell_0.5.1 base64enc_0.1-3 cachem_1.1.0 yaml_2.3.8
## [33] tools_4.3.2 dplyr_1.1.4 colorspace_2.1-0 httpuv_1.6.14
## [37] png_0.1-8 vctrs_0.6.5 R6_2.5.1 mime_0.12
## [41] proxy_0.4-27 lifecycle_1.0.4 classInt_0.4-11 htmlwidgets_1.6.4
## [45] pkgconfig_2.0.3 progressr_0.15.1 bslib_0.6.1 pillar_1.9.0
## [49] later_1.3.2 glue_1.7.0 Rcpp_1.0.12 highr_0.10
## [53] tidyselect_1.2.1 tibble_3.2.1 xfun_0.42 rstudioapi_0.17.1
## [57] knitr_1.45 farver_2.1.1 xtable_1.8-4 htmltools_0.5.7
## [61] rmarkdown_2.25 wk_0.9.4 compiler_4.3.2