Obtencion y visualizacion de datos de elevacion:

1. Configuracion

Cargue las librerias correspondientes

library(terra)
library(leaflet)
library(sf)
library(elevatr)
library(dplyr)

2. Cargando los datos correspondientes de la zona de interes

##(list.files("C:\\Users\\suare\\OneDrive\\Documentos\\GB2\\Proyecto1\\Datos"))
munic <- st_read("C:\\Users\\suare\\OneDrive\\Documentos\\GB2\\Proyecto1\\Datos\\Municipios_Magdalena.gpkg")
## Multiple layers are present in data source C:\Users\suare\OneDrive\Documentos\GB2\Proyecto1\Datos\Municipios_Magdalena.gpkg, reading layer `municipios_'.
## Use `st_layers' to list all layer names and their type in a data source.
## Set the `layer' argument in `st_read' to read a particular layer.
## Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
## automatically selected the first layer in a data source containing more than
## one.
## Reading layer `municipios_' from data source 
##   `C:\Users\suare\OneDrive\Documentos\GB2\Proyecto1\Datos\Municipios_Magdalena.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 20 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.9569 ymin: 8.956799 xmax: -73.6021 ymax: 11.34958
## Geodetic CRS:  WGS 84
st_layers("C:\\Users\\suare\\OneDrive\\Documentos\\GB2\\Proyecto1\\Datos\\Municipios_Magdalena.gpkg")
## Driver: GPKG 
## Available layers:
##    layer_name geometry_type features fields crs_name
## 1 municipios_ Multi Polygon       20     11   WGS 84
## 2       munic Multi Polygon       30     12   WGS 84
# Cargar el archivo especificando la capa correcta de 30 municipios del Magdalena
munic <- st_read("C:\\Users\\suare\\OneDrive\\Documentos\\GB2\\Proyecto1\\Datos\\Municipios_Magdalena.gpkg", 
                layer = "munic")
## Reading layer `munic' from data source 
##   `C:\Users\suare\OneDrive\Documentos\GB2\Proyecto1\Datos\Municipios_Magdalena.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 30 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.9466 ymin: 8.936489 xmax: -73.54184 ymax: 11.34891
## Geodetic CRS:  WGS 84

3. Calculando el area del municipio del departamento

(munic$area = st_area(munic)/1000000)
## Units: [m^2]
##  [1] 2356.4614  408.0399 1747.8150 1136.3788  177.8944  538.7049 1336.3925
##  [8]  109.9128  818.2538  559.3419  263.2340  975.8236  566.8661  845.5057
## [15]  306.9748  664.2835 1647.0267 1463.1680  682.4828  597.2839 1244.0021
## [22]  171.1467  439.4513  269.9888 1125.0051  496.6960  974.7055  494.1405
## [29]  374.4043  437.6871
(centros = st_centroid(munic))
## Simple feature collection with 30 features and 13 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.83757 ymin: 9.114921 xmax: -73.87979 ymax: 11.12189
## Geodetic CRS:  WGS 84
## First 10 features:
##    dpto_ccdgo mpio_ccdgo mpio_cdpmp dpto_cnmbr           mpio_cnmbr
## 1          47        001      47001  MAGDALENA          SANTA MARTA
## 2          47        030      47030  MAGDALENA            ALGARROBO
## 3          47        053      47053  MAGDALENA            ARACATACA
## 4          47        058      47058  MAGDALENA             ARIGUANÍ
## 5          47        161      47161  MAGDALENA CERRO DE SAN ANTONIO
## 6          47        170      47170  MAGDALENA              CHIVOLO
## 7          47        189      47189  MAGDALENA              CIÉNAGA
## 8          47        205      47205  MAGDALENA            CONCORDIA
## 9          47        245      47245  MAGDALENA             EL BANCO
## 10         47        258      47258  MAGDALENA             EL PIÑÓN
##                                  mpio_crslc mpio_tipo mpio_narea mpio_nano
## 1                                      1525 MUNICIPIO  2343.9033      2025
## 2         Ordenanza 008 de Junio 24 de 1999 MUNICIPIO   405.8850      2024
## 3      Ordenanza 47 del 28 de Abril de 1915 MUNICIPIO  1738.4243      2025
## 4  Ordenanza 14 Bis de Noviembre 30 de 1966 MUNICIPIO  1130.3299      2024
## 5                    Ordenanza 3038 de 1912 MUNICIPIO   177.0656      2024
## 6            Decreto 107 de Marzo 8 de 1974 MUNICIPIO   536.0309      2024
## 7                          Ley 0339 de 1876 MUNICIPIO  1329.3560      2025
## 8     Ordenanza 007 del 24 de Junio de 1999 MUNICIPIO   109.3953      2024
## 9                          Ley 0182 de 1871 MUNICIPIO   813.8018      2025
## 10    Ordenanza 032 del 20 de Abril de 1815 MUNICIPIO   556.6568      2024
##    shape_Leng  shape_Area      AREA                       geom            area
## 1   3.1304030 0.194233447 2347.0954 POINT (-73.88522 11.12189) 2356.4614 [m^2]
## 2   1.2656799 0.033536941  406.3880 POINT (-74.10323 10.25355)  408.0399 [m^2]
## 3   2.6435461 0.143834703 1740.7962 POINT (-73.87979 10.64492) 1747.8150 [m^2]
## 4   1.9928810 0.093278409 1131.7403 POINT (-74.08459 9.834264) 1136.3788 [m^2]
## 5   0.7004114 0.014623054  177.1748 POINT (-74.81982 10.29373)  177.8944 [m^2]
## 6   1.3253970 0.044254975  536.5174 POINT (-74.52607 10.09951)  538.7049 [m^2]
## 7   2.6311586 0.110066127 1331.0537  POINT (-74.0424 10.88858) 1336.3925 [m^2]
## 8   0.5443263 0.009033178  109.4677  POINT (-74.7781 10.23285)  109.9128 [m^2]
## 9   1.6767358 0.067024887  814.8694 POINT (-73.98104 9.114921)  818.2538 [m^2]
## 10  1.1771096 0.045985558  557.0816 POINT (-74.67816 10.34311)  559.3419 [m^2]

5. Limpiando la tabla con los atributos de interes

(munic_2 <- munic %>% dplyr::select(mpio_cdpmp ,mpio_cnmbr, AREA, geom))
## Simple feature collection with 30 features and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.9466 ymin: 8.936489 xmax: -73.54184 ymax: 11.34891
## Geodetic CRS:  WGS 84
## First 10 features:
##    mpio_cdpmp           mpio_cnmbr      AREA                           geom
## 1       47001          SANTA MARTA 2347.0954 MULTIPOLYGON (((-74.21468 1...
## 2       47030            ALGARROBO  406.3880 MULTIPOLYGON (((-74.08572 1...
## 3       47053            ARACATACA 1740.7962 MULTIPOLYGON (((-73.59859 1...
## 4       47058             ARIGUANÍ 1131.7403 MULTIPOLYGON (((-74.03381 9...
## 5       47161 CERRO DE SAN ANTONIO  177.1748 MULTIPOLYGON (((-74.85084 1...
## 6       47170              CHIVOLO  536.5174 MULTIPOLYGON (((-74.50895 1...
## 7       47189              CIÉNAGA 1331.0537 MULTIPOLYGON (((-74.26505 1...
## 8       47205            CONCORDIA  109.4677 MULTIPOLYGON (((-74.81188 1...
## 9       47245             EL BANCO  814.8694 MULTIPOLYGON (((-73.79896 9...
## 10      47258             EL PIÑÓN  557.0816 MULTIPOLYGON (((-74.67476 1...

6. Calculamos los centroides correspondientes a los municipio

(centros = st_centroid(munic_2))
## Warning: st_centroid assumes attributes are constant over geometries
## Simple feature collection with 30 features and 3 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.83757 ymin: 9.114921 xmax: -73.87979 ymax: 11.12189
## Geodetic CRS:  WGS 84
## First 10 features:
##    mpio_cdpmp           mpio_cnmbr      AREA                       geom
## 1       47001          SANTA MARTA 2347.0954 POINT (-73.88522 11.12189)
## 2       47030            ALGARROBO  406.3880 POINT (-74.10323 10.25355)
## 3       47053            ARACATACA 1740.7962 POINT (-73.87979 10.64492)
## 4       47058             ARIGUANÍ 1131.7403 POINT (-74.08459 9.834264)
## 5       47161 CERRO DE SAN ANTONIO  177.1748 POINT (-74.81982 10.29373)
## 6       47170              CHIVOLO  536.5174 POINT (-74.52607 10.09951)
## 7       47189              CIÉNAGA 1331.0537  POINT (-74.0424 10.88858)
## 8       47205            CONCORDIA  109.4677  POINT (-74.7781 10.23285)
## 9       47245             EL BANCO  814.8694 POINT (-73.98104 9.114921)
## 10      47258             EL PIÑÓN  557.0816 POINT (-74.67816 10.34311)
centros$x = st_coordinates(centros)[,1]
centros$y = st_coordinates(centros)[,2]
centros
## Simple feature collection with 30 features and 5 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.83757 ymin: 9.114921 xmax: -73.87979 ymax: 11.12189
## Geodetic CRS:  WGS 84
## First 10 features:
##    mpio_cdpmp           mpio_cnmbr      AREA                       geom
## 1       47001          SANTA MARTA 2347.0954 POINT (-73.88522 11.12189)
## 2       47030            ALGARROBO  406.3880 POINT (-74.10323 10.25355)
## 3       47053            ARACATACA 1740.7962 POINT (-73.87979 10.64492)
## 4       47058             ARIGUANÍ 1131.7403 POINT (-74.08459 9.834264)
## 5       47161 CERRO DE SAN ANTONIO  177.1748 POINT (-74.81982 10.29373)
## 6       47170              CHIVOLO  536.5174 POINT (-74.52607 10.09951)
## 7       47189              CIÉNAGA 1331.0537  POINT (-74.0424 10.88858)
## 8       47205            CONCORDIA  109.4677  POINT (-74.7781 10.23285)
## 9       47245             EL BANCO  814.8694 POINT (-73.98104 9.114921)
## 10      47258             EL PIÑÓN  557.0816 POINT (-74.67816 10.34311)
##            x         y
## 1  -73.88522 11.121892
## 2  -74.10323 10.253554
## 3  -73.87979 10.644920
## 4  -74.08459  9.834264
## 5  -74.81982 10.293730
## 6  -74.52607 10.099513
## 7  -74.04240 10.888580
## 8  -74.77810 10.232849
## 9  -73.98104  9.114921
## 10 -74.67816 10.343107

7. Obtencion de datos de elevacion

##elevacion = get_elev_raster(munic_2, z=10)
##elevacion

8. Guardando el raster dentro del equipo

##writeRaster(elevacion, "C:\\Users\\suare\\OneDrive\\Documentos\\GB2\\Proyecto1\\Datos\\elev_Magdalena_z10.tif", overwrite=TRUE) 
elevacion = terra::rast("C:\\Users\\suare\\OneDrive\\Documentos\\GB2\\Proyecto1\\Datos\\elev_Magdalena_z10.tif")

9. Pasamos el raster a un objeto terra

(elevacion2 <- terra::rast(elevacion))
## class       : SpatRaster
## size        : 4078, 2589, 1  (nrow, ncol, nlyr)
## resolution  : 0.0006789017, 0.0006789017  (x, y)
## extent      : -75.23438, -73.4767, 8.754526, 11.52309  (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)

10. Visualizacion

pal <- colorNumeric(c("cyan", "forestgreen","yellow","tan","orange", "brown"), values(elevacion),
  na.color = "transparent")
(elevacion3 <- terra::aggregate(elevacion, fact = 2))
## |---------|---------|---------|---------|=========================================                                          
## class       : SpatRaster
## size        : 2039, 1295, 1  (nrow, ncol, nlyr)
## resolution  : 0.001357803, 0.001357803  (x, y)
## extent      : -75.23438, -73.47602, 8.754526, 11.52309  (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## source(s)   : memory
## name        : elev_Magdalena_z10
## min value   :            -2437.5
## max value   :            5659.25
(elevacion4  <- terra::clamp(elevacion3, lower = 0, values = TRUE))
## class       : SpatRaster
## size        : 2039, 1295, 1  (nrow, ncol, nlyr)
## resolution  : 0.001357803, 0.001357803  (x, y)
## extent      : -75.23438, -73.47602, 8.754526, 11.52309  (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## source(s)   : memory
## name        : elev_Magdalena_z10
## min value   :                  0
## max value   :            5659.25

Elevacion

elevacion5 <- terra::crop(elevacion4, munic_2, mask=TRUE)
leaflet(munic_2) %>% addTiles() %>% setView(-73.11157,7.155834, 9) %>% 
    addPolygons(color = "white", weight = 1.0, smoothFactor = 0.5,
    opacity = 0.25, fillOpacity = 0.15,
    popup = paste("Municipio: ", munic_2$mpio_cnmbr, "<br>",
                          "Km2: ", munic_2$AREA, "<br>")) %>%
  addLabelOnlyMarkers(data = centros,
                    lng = ~x, lat = ~y, label = ~mpio_cnmbr,
                    labelOptions = labelOptions(noHide = TRUE, direction = 'top', textOnly = TRUE, textsize = "10px")) %>%
  addRasterImage(elevacion5, colors = pal, opacity = 0.9)  %>%
  addLegend(pal = pal, values = values(elevacion5),
    title = "Elevation data for Magdalena (mts)")