Este cuaderno es una prueba de algunas funcionalidades de la libreria sf (simple features) de R.
Antes de todo, hay que instalar las librerias que vamos a usar. Esto se debe hacer desde la consola, no desde un cuaderno por que si no se instala n veces.
#install.packages("sf")
#install.packages("dplyr")
#install.packages("readr")
Vamos a cargar las librerias que necesitamos:
library(sf)
library(readr)
library(dplyr)
Vamos a definir el directorio de trabajo:
setwd("C:/Users/Acer/Desktop/GB2/Proyecto1")
getwd()
## [1] "C:/Users/Acer/Desktop/GB2/Proyecto1"
list.files("datos")
## [1] "Ciudades Narino.gpkg" "ciudadesCol.gpkg"
## [3] "COL_adm2.cpg" "COL_adm2.csv"
## [5] "COL_adm2.dbf" "COL_adm2.prj"
## [7] "COL_adm2.shp" "COL_adm2.shx"
## [9] "COL_roads.dbf" "COL_roads.prj"
## [11] "COL_roads.shp" "COL_roads.shx"
## [13] "Departamento narino.gpkg" "Municipios Narinox.gpkg"
## [15] "narino.gpkg" "narino_agua_area.gpkg"
## [17] "narino_agua_lineas.gpkg" "narino_alt.tif"
## [19] "narino_alt.tif.aux.xml" "narino_mun.gpkg"
## [21] "narino_roads.gpkg"
municipios <- st_read("datos/COL_adm2.shp")
## Reading layer `COL_adm2' from data source
## `C:\Users\Acer\Desktop\GB2\Proyecto1\datos\COL_adm2.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1065 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -81.84153 ymin: -4.228429 xmax: -66.87033 ymax: 15.91247
## Geodetic CRS: WGS 84
vias <- st_read("datos/COL_roads.shp")
## Reading layer `COL_roads' from data source
## `C:\Users\Acer\Desktop\GB2\Proyecto1\datos\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: WGS 84
## wkt:
## GEOGCRS["WGS 84",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["latitude",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["longitude",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4326]]
vias_4686 <- st_transform(vias, 4686)
names(municipios)
## [1] "ID_0" "ISO" "NAME_0" "ID_1" "NAME_1" "ID_2"
## [7] "NAME_2" "TYPE_2" "ENGTYPE_2" "NL_NAME_2" "VARNAME_2" "geometry"
mun_narino <- municipios %>% filter(NAME_1 == "Nariño")
mun_narino_4686 <- st_transform(mun_narino, 4686)
vias_narino <- st_intersection(vias_4686, mun_narino_4686)
mun_narino2 <- mun_narino_4686 %>%
select(NAME_1, NAME_2, TYPE_2)
head(mun_narino2)
## Simple feature collection with 6 features and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -78.4908 ymin: 0.83672 xmax: -76.965 ymax: 1.751301
## Geodetic CRS: MAGNA-SIRGAS
## NAME_1 NAME_2 TYPE_2 geometry
## 1 Nariño Albán Municipio MULTIPOLYGON (((-76.965 1.5...
## 2 Nariño Aldana Municipio MULTIPOLYGON (((-77.718 0.8...
## 3 Nariño Ancuyá Municipio MULTIPOLYGON (((-77.5396 1....
## 4 Nariño Arboleda Municipio MULTIPOLYGON (((-77.1356 1....
## 5 Nariño Barbacoas Municipio MULTIPOLYGON (((-77.847 1.5...
## 6 Nariño Belén <NA> MULTIPOLYGON (((-77.0411 1....