Introducción

Este cuaderno es una prueba de algunas funcionalidades de la libreria sf (simple features) de R.

Configuración

Antes de todo, hay que instalar las librerias que vamos a usar. Esto se debe hacer desde la consola, no desde un cuaderno porque si no se instilarán n veces.

Vamos a cargar las librerias que necesitamos:

library(sf)
library(dplyr)
library(readr)

Vamos a definir el directorio de trabajo:

setwd("C:\\Users\\isabe\\OneDrive\\Documentos\\GB2\\proyecto 2")
getwd()
## [1] "C:/Users/isabe/OneDrive/Documentos/GB2/proyecto 2"
list.files("datos")
##  [1] "Aguasantander.gpkg"             "ciudades.gpkg"                 
##  [3] "ciudadescol.gpkg"               "ciudadesdesantander.gpkg"      
##  [5] "ciudadessantander.gpkg"         "COL_adm"                       
##  [7] "COL_msk_alt_tif"                "COL_msk_cov_tif"               
##  [9] "COL_rds"                        "COL_rrd"                       
## [11] "COL_rrd.zip"                    "COL_wat"                       
## [13] "COL_wat.zip"                    "curvasantander.gpkg"           
## [15] "Departamentosdisueltos.gpkg"    "deptos.gpkg"                   
## [17] "elevacion__9377.tif"            "elevacion__9377.tif.aux.xml"   
## [19] "elevacion_9377.tif"             "elevacion_9377.tif.aux.xml"    
## [21] "elevacion_santandr.tif"         "elevacion_santandr.tif.aux.xml"
## [23] "elevacion9377.tif"              "elevacion9377.tif.aux.xml"     
## [25] "elevacion9377_.gpkg"            "elevacioncol9377.tif"          
## [27] "elevacioncol9377.tif.aux.xml"   "ElevacionSantander.tif"        
## [29] "ElevacionSantander.tif.aux.xml" "mapaborrador.pdf"              
## [31] "MGN2018_DPTO_POLITICO"          "MGN2024_DPTO_POLITICO.zip"     
## [33] "mun_stder9377.gpkg"             "muncipios__9377.gpkg"          
## [35] "muncipios_9377.gpkg"            "Municipios__9377.gpkg"         
## [37] "municipios9377.gpkg"            "proyecto2.qgz"                 
## [39] "santander.9377.tif"             "Santander.tif"                 
## [41] "Santander.tif.aux.xml"          "santander_9377.tif"            
## [43] "santander_9377.tif.aux.xml"     "SHP_MGN2018_INTGRD_MPIO"       
## [45] "SHP_MGN2018_INTGRD_MPIO.zip"    "worldcities.csv"

Lectura de datos

municipios <-  st_read("C:\\Users\\isabe\\OneDrive\\Documentos\\GB2\\proyecto 2\\Datos\\SHP_MGN2018_INTGRD_MPIO")
## Reading layer `MGN_ANM_MPIOS' from data source 
##   `C:\Users\isabe\OneDrive\Documentos\GB2\proyecto 2\Datos\SHP_MGN2018_INTGRD_MPIO' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1122 features and 90 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -81.73562 ymin: -4.229406 xmax: -66.84722 ymax: 13.39473
## Geodetic CRS:  MAGNA-SIRGAS
vias <- st_read("C:\\Users\\isabe\\OneDrive\\Documentos\\GB2\\proyecto 2\\Datos\\COL_rds\\COL_roads.shp")
## Reading layer `COL_roads' from data source 
##   `C:\Users\isabe\OneDrive\Documentos\GB2\proyecto 2\Datos\COL_rds\COL_roads.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2593 features and 5 fields
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: -78.80151 ymin: -3.15505 xmax: -67.48318 ymax: 12.45071
## Geodetic CRS:  WGS 84
(nuevo_crs <- st_crs(municipios))
## 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]]
vias_4686 <- st_transform(vias, 4686)

Seleccionar municipios de un departamento, en este caso Santander

mun_santander <- municipios %>% filter(DPTO_CCDGO == "68")

Recortar las vias del Santander

vias_santander <- st_intersection(vias_4686, mun_santander)
mun_santander2 <- mun_santander %>% select(DPTO_CCDGO, MPIO_CCDGO, MPIO_CNMBR, AREA)
head(mun_santander2)