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)
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. #cargue de librerias
#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)
(read_sf(system.file ("gpkg/nc.gpkg", package = "sf")))
Error in read_sf(system.file("gpkg/nc.gpkg", package = "sf")) :
could not find function "read_sf"
library(GADMTools)
COL <- gadm_sf_loadCountries(c("COL"), level =0, basefile = "./")
COL
$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"
gadm_plot(COL)
library(GADMTools)
Departamentos <- gadm_sf_loadCountries(c("COL"), level =1, basefile = "./")
gadm_plot(Departamentos)
library(GADMTools)
Municipios <- gadm_sf_loadCountries(c("COL"), level =2, basefile = "./")
gadm_plot(Municipios)
choco <- gadm_subset(Departamentos, level=1, regions="Chocó")
gadm_plot(choco) %>% gadm_showNorth("tl") %>% gadm_showScale("bl")
listNames(Departamentos, 1)
[1] "Amazonas" "Antioquia"
[3] "Arauca" "Atlántico"
[5] "Bolívar" "Boyacá"
[7] "Caldas" "Caquetá"
[9] "Casanare" "Cauca"
[11] "Cesar" "Chocó"
[13] "Córdoba" "Cundinamarca"
[15] "Guainía" "Guaviare"
[17] "Huila" "La Guajira"
[19] "Magdalena" "Meta"
[21] "Nariño" "Norte de Santander"
[23] "Putumayo" "Quindío"
[25] "Risaralda" "San Andrés y Providencia"
[27] "Santander" "Sucre"
[29] "Tolima" "Valle del Cauca"
[31] "Vaupés" "Vichada"
(sbox <- gadm_getBbox(choco))
xmin ymin xmax ymax
-78.313751 3.992801 -76.017303 8.658194
str(sbox)
'bbox' Named num [1:4] -78.31 3.99 -76.02 8.66
- 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"
(STUDY_AREA <- gadm_crop(Departamentos, xmin = -78.31, ymin = 3.99, xmax = -76.02, ymax = 8.))
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 5 features and 5 fields
geometry type: GEOMETRY
dimension: XY
bbox: xmin: -77.88348 ymin: 3.99 xmax: -76.02 ymax: 8
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
ISO NAME_0 NAME_1 TYPE_1 ENGTYPE_1
12 COL Colombia Antioquia Departamento Department
4 COL Colombia Chocó Departamento Department
5 COL Colombia Córdoba Departamento Department
18 COL Colombia Risaralda Departamento Department
24 COL Colombia Valle del Cauca Departamento Department
geometry
12 POLYGON ((-76.02 5.473456, ...
4 MULTIPOLYGON (((-77.38069 4...
5 POLYGON ((-76.02 7.354475, ...
18 POLYGON ((-76.02 4.89694, -...
24 MULTIPOLYGON (((-77.24208 3...
$level
[1] 1
$hasBGND
[1] FALSE
attr(,"class")
[1] "gadm_sf"
gadm_plot(STUDY_AREA)
install.packages("mapview")
Error in install.packages : Updating loaded packages
suppressPackageStartupMessages(library(mapview))
Departamentos$sf %>% mapview(zcol = "NAME_1", legend=TRUE, col.regions=sf.colors)
Error in Departamentos$sf %>% mapview(zcol = "NAME_1", legend = TRUE, :
could not find function "%>%"
Municipios
$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
15629 COL Colombia Amazonas El Encanto Corregimiento Departamento
15634 COL Colombia Amazonas La Chorrera Corregimiento Departamento
15699 COL Colombia Amazonas La Pedrera Corregimiento Departamento
16578 COL Colombia Amazonas Leticia Municipio
15505 COL Colombia Amazonas Mirití-Paraná Corregimiento Departamento
16577 COL Colombia Amazonas Puerto Nariño Municipio
15494 COL Colombia Amazonas Puerto Santander Corregimiento Departamento
16055 COL Colombia Amazonas Tarapacá Corregimiento Departamento
14570 COL Colombia Antioquia Abejorral Municipio
14343 COL Colombia Antioquia Abriaquí Municipio
ENGTYPE_2 geometry
15629 Corregimiento Departamento MULTIPOLYGON (((-73.43552 -...
15634 Corregimiento Departamento MULTIPOLYGON (((-71.9802 -1...
15699 Corregimiento Departamento MULTIPOLYGON (((-72.61586 -...
16578 Municipality MULTIPOLYGON (((-70.0579 -4...
15505 Corregimiento Departamento MULTIPOLYGON (((-70.2188 -0...
16577 Municipality MULTIPOLYGON (((-70.28952 -...
15494 Corregimiento Departamento MULTIPOLYGON (((-71.4002 -0...
16055 Corregimiento Departamento MULTIPOLYGON (((-70.1296 -3...
14570 Municipality MULTIPOLYGON (((-75.5753 5....
14343 Municipality MULTIPOLYGON (((-76.0697 6....
$level
[1] 2
$hasBGND
[1] FALSE
attr(,"class")
[1] "gadm_sf"
###lirary(GADMTools)(extraer municipios del departamento a partir de los municipios de colombia)
library(GADMTools)
Loading required package: sp
Loading required package: tidyverse
[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
Loading required package: classInt
Loading required package: sf
Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
Loading required package: rgdal
rgdal: version: 1.4-6, (SVN revision 841)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
Path to GDAL shared files: C:/Users/usuagro/Documents/R/win-library/3.6/rgdal/gdal
GDAL binary built with GEOS: TRUE
Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
Path to PROJ.4 shared files: C:/Users/usuagro/Documents/R/win-library/3.6/rgdal/proj
Linking to sp version: 1.3-1
Attaching package: 㤼㸱GADMTools㤼㸲
The following object is masked from 㤼㸱package:utils㤼㸲:
vignette
(cho_mun <- gadm_subset(Municipios, level=1, regions="Chocó"))
$basename
[1] "./"
$sf
Simple feature collection with 21 features and 6 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -78.31375 ymin: 3.992801 xmax: -76.0173 ymax: 8.658194
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 10 features:
ISO NAME_0 NAME_1 NAME_2 TYPE_2 ENGTYPE_2
13704 COL Colombia Chocó Acandí Municipio Municipality
14525 COL Colombia Chocó Alto Baudó Municipio Municipality
14669 COL Colombia Chocó Bagadó Municipio Municipality
14404 COL Colombia Chocó Bahía Solano <NA> <NA>
14772 COL Colombia Chocó Bajo Baudó <NA> <NA>
14296 COL Colombia Chocó Bojayá Municipio Municipality
14788 COL Colombia Chocó Condoto Municipio Municipality
14737 COL Colombia Chocó El Cantón del San Pablo Municipio Municipality
14542 COL Colombia Chocó El Carmen de Atrato Municipio Municipality
15023 COL Colombia Chocó El Litoral del San Juan Municipio Municipality
geometry
13704 MULTIPOLYGON (((-77.15708 8...
14525 MULTIPOLYGON (((-76.8387 5....
14669 MULTIPOLYGON (((-76.0173 5....
14404 MULTIPOLYGON (((-77.43291 6...
14772 MULTIPOLYGON (((-77.33264 4...
14296 MULTIPOLYGON (((-76.8514 6....
14788 MULTIPOLYGON (((-76.703 4.9...
14737 MULTIPOLYGON (((-76.6074 5....
14542 MULTIPOLYGON (((-76.1327 5....
15023 MULTIPOLYGON (((-77.49958 4...
$level
[1] 2
$hasBGND
[1] FALSE
attr(,"class")
[1] "gadm_sf"
gadm_plot(cho_mun)%>% gadm_showNorth("tl") %>% gadm_showScale("bl")