Este cuaderno es para aprender a usar las funciones basicas de extraccion d einformacion y mapeo. Primero,Cargar los paquetes tidyverse y sf que previamente han sido instalados en “Mi primer cuaderno”
library (tidyverse)
Registered S3 method overwritten by 'dplyr':
method from
print.rowwise_df
[30m-- [1mAttaching packages[22m --------------------------------------- tidyverse 1.2.1 --[39m
[30m[32mv[30m [34mggplot2[30m 3.2.1 [32mv[30m [34mpurrr [30m 0.3.2
[32mv[30m [34mtibble [30m 2.1.3 [32mv[30m [34mdplyr [30m 0.8.3
[32mv[30m [34mtidyr [30m 1.0.0 [32mv[30m [34mstringr[30m 1.4.0
[32mv[30m [34mreadr [30m 1.3.1 [32mv[30m [34mforcats[30m 0.4.0[39m
[30m-- [1mConflicts[22m ------------------------------------------ tidyverse_conflicts() --
[31mx[30m [34mdplyr[30m::[32mfilter()[30m masks [34mstats[30m::filter()
[31mx[30m [34mdplyr[30m::[32mlag()[30m masks [34mstats[30m::lag()[39m
library(sf)
Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
Plotar las Coordenadas geograficas de Carolina del Norte separada por condados
system.file("gpkg/nc.gpkg", package = "sf") %>%
read_sf() %>%
st_transform(32119) %>%
select (BIR74) %>%
plot(graticule = TRUE, axes = TRUE)
Leer los sf del ploter de Carolina del Norte
read_sf(system.file("gpkg/nc.gpkg", package = "sf"))
Simple feature collection with 100 features and 14 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID): 4267
proj4string: +proj=longlat +datum=NAD27 +no_defs
#Cargar el programa GADMTools
library(GADMTools)
Obtener los datos administrativos del pais Colombia por medio de la funcion gadm_sf_loadCountries, teniendo encuenta el nivel para pais es 0
(COL<-gadm_sf_loadCountries(c( "COL" ), level = 0,
basefile = "./"))
trying URL 'https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_COL_0_sf.rds'
Content type '' length 533640 bytes (521 KB)
downloaded 521 KB
$basename
[1] "./"
$sf
Simple feature collection with 1 feature and 2 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -81.84153 ymin: -4.228429 xmax: -66.87033 ymax: 15.91247
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
ISO NAME_0 geometry
1 COL Colombia MULTIPOLYGON (((-78.97958 1...
$level
[1] 0
$hasBGND
[1] FALSE
attr(,"class")
[1] "gadm_sf"
Plotear a Colombia haciendo uso de gadm_plot
gadm_plot (COL)
Obtener datos administrativos sobre los departamentos de Colombia usando la funcion gadm_sf_loadCountries y teniendo en cuenta que el nivel para departamentos es 1
(DEPTOS <- gadm_sf_loadCountries(c("COL"), level=1,
basefile= "./"))
$basename
[1] "./"
$sf
Simple feature collection with 32 features and 5 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -81.84153 ymin: -4.228429 xmax: -66.87033 ymax: 15.91247
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 10 features:
ISO NAME_0 NAME_1 TYPE_1 ENGTYPE_1 geometry
1 COL Colombia Amazonas Comisaría Commissiary MULTIPOLYGON (((-69.43138 -...
12 COL Colombia Antioquia Departamento Department MULTIPOLYGON (((-76.99986 8...
23 COL Colombia Arauca Intendencia Intendancy MULTIPOLYGON (((-69.92316 6...
27 COL Colombia Atlántico Departamento Department MULTIPOLYGON (((-74.8816 10...
28 COL Colombia Bolívar Departamento Department MULTIPOLYGON (((-75.79681 1...
29 COL Colombia Boyacá Departamento Department MULTIPOLYGON (((-74.2158 5....
30 COL Colombia Caldas Departamento Department MULTIPOLYGON (((-75.3569 4....
31 COL Colombia Caquetá Intendencia Intendancy MULTIPOLYGON (((-73.88365 -...
32 COL Colombia Casanare Intendencia Intendancy MULTIPOLYGON (((-72.6363 4....
2 COL Colombia Cauca Departamento Department MULTIPOLYGON (((-77.97681 2...
$level
[1] 1
$hasBGND
[1] FALSE
attr(,"class")
[1] "gadm_sf"
Plotear Departamentos de Colombia usando gadm_plot
gadm_plot(DEPTOS)
Obtener datos administrativos sobre los municipios de Colombia usando la funcion gadm_sf_loadCountries y teniendo en cuenta que el nivel para municipios es 2
(MUNI <- gadm_sf_loadCountries(c("COL"), level=2,
basefile="./"))
$basename
[1] "./"
$sf
Simple feature collection with 1065 features and 6 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -81.84153 ymin: -4.228429 xmax: -66.87033 ymax: 15.91247
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 10 features:
ISO NAME_0 NAME_1 NAME_2 TYPE_2 ENGTYPE_2
15629 COL Colombia Amazonas El Encanto Corregimiento Departamento Corregimiento Departamento
15634 COL Colombia Amazonas La Chorrera Corregimiento Departamento Corregimiento Departamento
15699 COL Colombia Amazonas La Pedrera Corregimiento Departamento Corregimiento Departamento
16578 COL Colombia Amazonas Leticia Municipio Municipality
15505 COL Colombia Amazonas Mirití-Paraná Corregimiento Departamento Corregimiento Departamento
16577 COL Colombia Amazonas Puerto Nariño Municipio Municipality
15494 COL Colombia Amazonas Puerto Santander Corregimiento Departamento Corregimiento Departamento
16055 COL Colombia Amazonas Tarapacá Corregimiento Departamento Corregimiento Departamento
14570 COL Colombia Antioquia Abejorral Municipio Municipality
14343 COL Colombia Antioquia Abriaquí Municipio Municipality
geometry
15629 MULTIPOLYGON (((-73.43552 -...
15634 MULTIPOLYGON (((-71.9802 -1...
15699 MULTIPOLYGON (((-72.61586 -...
16578 MULTIPOLYGON (((-70.0579 -4...
15505 MULTIPOLYGON (((-70.2188 -0...
16577 MULTIPOLYGON (((-70.28952 -...
15494 MULTIPOLYGON (((-71.4002 -0...
16055 MULTIPOLYGON (((-70.1296 -3...
14570 MULTIPOLYGON (((-75.5753 5....
14343 MULTIPOLYGON (((-76.0697 6....
$level
[1] 2
$hasBGND
[1] FALSE
attr(,"class")
[1] "gadm_sf"
Plotear Municipios de Colombia usando gadm_plot
gadm_plot(MUNI)
Plotear mi Departamento, primero usando gadm_subset para extraer solo el departamento que se quiere trabajar, no olvide nombrar esta nueva información recolectada como usted quiera, y despues usando la funcion de gadm_plot
VICH <- gadm_subset(DEPTOS, level=1, regions = "Vichada")
gadm_plot(VICH) %>% gadm_showNorth("tl") %>% gadm_showScale('bl')
Crear la caja envolvente de mi Departamento con la función gadm_getBbox y nombrar estos nuevos datos como sbox
(sbox <- gadm_getBbox(VICH))
xmin ymin xmax ymax
-71.097801 2.708100 -67.421112 6.301989
Estructura de nuestra caja con la funcion str
str(sbox)
'bbox' Named num [1:4] -71.1 2.71 -67.42 6.3
- attr(*, "names")= chr [1:4] "xmin" "ymin" "xmax" "ymax"
- attr(*, "crs")=List of 2
..$ epsg : int 4326
..$ proj4string: chr "+proj=longlat +datum=WGS84 +no_defs"
..- attr(*, "class")= chr "crs"
Recortar mi Depratamento, para esto se usa gadm_crop y al reportar las coordenadas se usan valores un poco mas grandes a los de la caja, estos datos se deben guardar bajo el nombre de STUDY_AREA
(STUDY_AREA <- gadm_crop(DEPTOS, xmin=-72.1, ymin=2.71, xmax =-67.42, ymax=6.3))
although coordinates are longitude/latitude, st_intersection assumes that they are planar
attribute variables are assumed to be spatially constant throughout all geometries
$basename
[1] "./"
$sf
Simple feature collection with 6 features and 5 fields
geometry type: GEOMETRY
dimension: XY
bbox: xmin: -72.1 ymin: 2.71 xmax: -67.42 ymax: 6.3
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
ISO NAME_0 NAME_1 TYPE_1 ENGTYPE_1 geometry
23 COL Colombia Arauca Intendencia Intendancy POLYGON ((-69.92316 6.05792...
32 COL Colombia Casanare Intendencia Intendancy POLYGON ((-72.1 6.075137, -...
7 COL Colombia Guainía Comisaría Commissiary POLYGON ((-70.56591 2.71, -...
8 COL Colombia Guaviare Comisaría Commissiary MULTIPOLYGON (((-72.1 2.804...
13 COL Colombia Meta Departamento Department POLYGON ((-72.1 4.397398, -...
26 COL Colombia Vichada Comisaría Commissiary POLYGON ((-71.0978 3.3434, ...
$level
[1] 1
$hasBGND
[1] FALSE
attr(,"class")
[1] "gadm_sf"
Plotear el area de estudio con la funcion gadm_plot
gadm_plot(STUDY_AREA)
Seleccionar todas las caracterisiticas simples de cada departamento
DEPTOS$sf
Simple feature collection with 32 features and 5 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -81.84153 ymin: -4.228429 xmax: -66.87033 ymax: 15.91247
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 10 features:
ISO NAME_0 NAME_1 TYPE_1 ENGTYPE_1 geometry
1 COL Colombia Amazonas Comisaría Commissiary MULTIPOLYGON (((-69.43138 -...
12 COL Colombia Antioquia Departamento Department MULTIPOLYGON (((-76.99986 8...
23 COL Colombia Arauca Intendencia Intendancy MULTIPOLYGON (((-69.92316 6...
27 COL Colombia Atlántico Departamento Department MULTIPOLYGON (((-74.8816 10...
28 COL Colombia Bolívar Departamento Department MULTIPOLYGON (((-75.79681 1...
29 COL Colombia Boyacá Departamento Department MULTIPOLYGON (((-74.2158 5....
30 COL Colombia Caldas Departamento Department MULTIPOLYGON (((-75.3569 4....
31 COL Colombia Caquetá Intendencia Intendancy MULTIPOLYGON (((-73.88365 -...
32 COL Colombia Casanare Intendencia Intendancy MULTIPOLYGON (((-72.6363 4....
2 COL Colombia Cauca Departamento Department MULTIPOLYGON (((-77.97681 2...
Instalar mapview
install.packages("mapview")
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/
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/mapview_2.7.0.zip'
Content type 'application/zip' length 2947933 bytes (2.8 MB)
downloaded 2.8 MB
package ‘mapview’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\usuagro\AppData\Local\Temp\RtmpMzZ3mT\downloaded_packages
Cargar mapview y plotear el pais que se ha venido trabajando con todos sus departamentos, para evitar que aparezcan toda la información que sale al cargar un paquete se usa suppressPackageStartupMessages(), y luego si se usa la funcion mapview. No olvide que el nivel para departamentos es 1
suppressPackageStartupMessages(library(mapview))
DEPTOS$sf %>% mapview(zcol="NAME_1", legend=TRUE, col.regions=sf.colors)
Plotear los municipios de mi Departamento, primero usando gadm_subset para extraer los minicipios del departamento que se quiere trabajar, no olvide nombrar esta nueva información recolectada como usted quiera, y despues usando la funcion de mapview. No olvide que para el nivel para municipio es 2
(VICH_MUNI <- gadm_subset(MUNI, 1, "Vichada", usevar = NULL))
$basename
[1] "./"
$sf
Simple feature collection with 6 features and 6 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -71.0978 ymin: 2.7081 xmax: -67.42111 ymax: 6.301989
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
ISO NAME_0 NAME_1 NAME_2 TYPE_2 ENGTYPE_2 geometry
14744 COL Colombia Vichada Cumaribo Municipio Municipality MULTIPOLYGON (((-68.7135 5....
14481 COL Colombia Vichada La Primavera Municipio Municipality MULTIPOLYGON (((-68.5412 5....
14453 COL Colombia Vichada Puerto Carreño Municipio Municipality MULTIPOLYGON (((-67.49085 6...
14970 COL Colombia Vichada San Jose de Ocune Municipio Municipality MULTIPOLYGON (((-71.0978 3....
14725 COL Colombia Vichada Santa Rita Municipio Municipality MULTIPOLYGON (((-68.8793 3....
14706 COL Colombia Vichada Santa Rosalía Municipio Municipality MULTIPOLYGON (((-70.3883 4....
$level
[1] 2
$hasBGND
[1] FALSE
attr(,"class")
[1] "gadm_sf"
VICH_MUNI$sf %>% mapview(zcol="NAME_2", legend=TRUE, col.regions=sf.colors)
methods(class="sf")
[1] $<- [ [[<- aggregate
[5] anti_join arrange as.data.frame cbind
[9] coerce dbDataType dbWriteTable distinct
[13] filter full_join gather group_by
[17] group_map group_split identify initialize
[21] inner_join left_join mapView merge
[25] mutate nest plot print
[29] rbind rename right_join sample_frac
[33] sample_n select semi_join separate
[37] separate_rows show slice slotsFromS3
[41] spread st_agr st_agr<- st_area
[45] st_as_sf st_bbox st_boundary st_buffer
[49] st_cast st_centroid st_collection_extract st_convex_hull
[53] st_coordinates st_crop st_crs st_crs<-
[57] st_difference st_geometry st_geometry<- st_interpolate_aw
[61] st_intersection st_intersects st_is st_join
[65] st_line_merge st_nearest_points st_node st_normalize
[69] st_point_on_surface st_polygonize st_precision st_segmentize
[73] st_set_precision st_simplify st_snap st_sym_difference
[77] st_transform st_triangulate st_union st_voronoi
[81] st_wrap_dateline st_write st_zm summarise
[85] transmute ungroup unite unnest
see '?methods' for accessing help and source code
st_write(VICH_MUNI$sf, "VICH_mun.shp")
Dataset VICH_mun.shp already exists: remove first, use update=TRUE to append,
delete_layer=TRUE to delete layer, or delete_dsn=TRUE to remove the entire data source before writing.
Error in CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options), :
Dataset already exists.
(Vichada_mun <- st_read("./VICH_mun.shp"))
Reading layer `VICH_mun' from data source `G:\Cursos\Geomatica grupo 2\VICH_mun.shp' using driver `ESRI Shapefile'
Simple feature collection with 6 features and 6 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: -71.0978 ymin: 2.7081 xmax: -67.42111 ymax: 6.301989
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
Simple feature collection with 6 features and 6 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: -71.0978 ymin: 2.7081 xmax: -67.42111 ymax: 6.301989
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
ISO NAME_0 NAME_1 NAME_2 TYPE_2 ENGTYPE_2 geometry
1 COL Colombia Vichada Cumaribo Municipio Municipality POLYGON ((-68.7135 5.3568, ...
2 COL Colombia Vichada La Primavera Municipio Municipality POLYGON ((-68.5412 5.437, -...
3 COL Colombia Vichada Puerto Carreño Municipio Municipality POLYGON ((-67.49085 6.14925...
4 COL Colombia Vichada San Jose de Ocune Municipio Municipality POLYGON ((-71.0978 3.3434, ...
5 COL Colombia Vichada Santa Rita Municipio Municipality POLYGON ((-68.8793 3.929101...
6 COL Colombia Vichada Santa Rosalía Municipio Municipality POLYGON ((-70.3883 4.673415...