This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
#Cargar los paquetes tidyverse y sf que previamente han sido instalados en "Mi primer cuaderno"
library (tidyverse)
library(sf)
#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
(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
gadm_plot (COL)
#Obtener datos administrativos sobre los departamentos de Colombia
(DEPTOS <- gadm_sf_loadCountries(c("COL"), level=1,
basefile= "./"))
trying URL 'https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_COL_1_sf.rds'
Content type '' length 782954 bytes (764 KB)
downloaded 764 KB
$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
gadm_plot(DEPTOS)
#Obtener datos administrativos sobre los municipios de Colombia
(MUNI <- gadm_sf_loadCountries(c("COL"), level=2,
basefile="./"))
trying URL 'https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_COL_2_sf.rds'
Content type '' length 1092557 bytes (1.0 MB)
downloaded 1.0 MB
$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
gadm_plot(MUNI)
#Plotear mi Departamento
VICH <- gadm_subset(DEPTOS, level=1, regions = "Vichada")
gadm_plot(VICH) %>% gadm_showNorth("tl") %>% gadm_showScale('bl')
#Crear la caja envolvente de mi Departamento
(sbox <- gadm_getBbox(VICH))
xmin ymin xmax ymax
-71.097801 2.708100 -67.421112 6.301989
#Estructura de nuestra caja
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, se usan valores > a los de la caja
(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
gadm_plot(STUDY_AREA)
#Seleccionar de 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 2947906 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\Rtmp2x1zHp\downloaded_packages
#Cargar mapview y plotear el pais con todos sus departamentos
suppressPackageStartupMessages(library(mapview))
DEPTOS$sf %>% mapview(zcol="NAME_1", legend=TRUE, col.regions=sf.colors)
#Plotear los municipios de mi Departamento
(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)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.