Segundo cuaderno de R notebook. representara como hacer un mapa con poligonos.
library(rgeos)
## Warning: package 'rgeos' was built under R version 4.0.4
## Loading required package: sp
## rgeos version: 0.5-5, (SVN revision 640)
## GEOS runtime version: 3.8.0-CAPI-1.13.1
## Linking to sp version: 1.4-5
## Polygon checking: TRUE
library(sf)
## Warning: package 'sf' was built under R version 4.0.4
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.0.3
# usando la tabla anterior de ciudades
ciudades= read.csv(file = 'colombi.txt', header = F, sep= ";" )
ciudades
names(ciudades)= c("id", "pais", "ciudad", "latitud","longitud", "altitud")
ciudades
map= st_as_sf(ciudades, coords = c(5,4,6))
map
#podemos extraer unicamente las coordenadas
coords= st_coordinates(map); coords
## X Y Z
## 1 -74.08334 4.6000000 2620
## 2 -76.52250 3.4372222 758
## 3 -75.53611 6.2913889 2076
## 4 -74.79639 10.9638889 33
## 5 -75.51444 10.3997222 36
## 6 -72.50528 7.8833333 314
## 7 -73.12583 7.1297222 967
## 8 -75.69611 4.8133333 1445
## 9 -74.20167 11.2472222 9
## 10 -75.23222 4.4388889 1082
## 11 -75.56223 6.3388889 1489
## 12 -77.28111 1.2136111 2569
## 13 -75.52055 5.0700000 1935
## 14 -75.33028 2.9305556 507
## 15 -74.76667 10.9172222 10
## 16 -73.63500 4.1533333 529
## 17 -75.68111 4.5338889 1349
## 18 -74.22139 4.5872222 2413
## 19 -73.25056 10.4769444 163
## 20 -75.61139 6.1719444 1637
## 21 -75.89000 8.7575000 25
## 22 -75.39778 9.3047222 219
## 23 -73.08972 7.0647222 1026
## 24 -76.30361 3.5394444 863
## 25 -77.06973 3.8933333 1
## 26 -73.85472 7.0652778 45
## 27 -75.67250 4.8347222 1500
## 28 -76.20000 4.0866667 766
## 29 -75.56389 6.1730556 1784
## 30 -75.91167 4.7463889 722
## 31 -72.24416 11.3841667 44
## 32 -75.61750 1.6175000 579
## 33 -74.80083 4.3030556 328
## 34 -72.92973 5.7205556 2419
## 35 -76.30278 3.9022222 722
## 36 -73.36777 5.5352778 2717
## 37 -73.17306 7.0708333 712
## 38 -74.77306 10.8588889 5
## 39 -74.75333 9.2413889 18
## 40 -74.36667 4.8166667 2816
## 41 -72.90722 11.5444444 5
## 42 -73.02028 5.8269444 2501
## 43 -74.00584 5.0283333 2656
## 44 -74.36777 4.3438889 1745
## 45 -74.25417 11.0094444 12
## 46 -78.81556 1.7986111 1
## 47 -76.63472 7.8855556 57
## 48 -73.05361 6.9894444 1080
## 49 -73.35778 8.2363889 1272
## 50 -74.66917 5.4522222 152
## 51 -77.64445 0.8302778 2954
## 52 -76.66111 5.6947222 67
## 53 -73.62695 8.3125000 151
## 54 -76.49583 3.5850000 991
## 55 -70.76167 7.0902778 119
## 56 -74.92278 10.6377778 88
## 57 -75.60361 4.9825000 1488
## 58 -75.63750 6.0900000 1929
## 59 -72.47417 7.8338889 381
## 60 -74.06667 4.8666667 2325
## 61 -75.38889 6.1552778 2058
## 62 -75.64361 4.5325000 1459
## 63 -72.39417 5.3394444 426
## 64 -75.44778 8.9494444 72
## 65 -74.18667 10.5213889 55
## 66 -72.51334 7.8383333 385
## 67 -81.70055 12.5847222 -9999
## 68 -75.19722 7.9869444 65
## 69 -75.62139 4.8680556 1827
## 70 -74.88861 4.1527778 329
## 71 -75.40833 10.3294444 129
## 72 -75.79667 8.8855556 15
## 73 -73.97806 9.0047222 21
## 74 -74.21667 4.7166667 2276
## 75 -76.05639 1.8675000 1319
## 76 -76.41944 3.2336111 847
## 77 -72.65250 7.3780556 2516
## 78 -73.23889 10.0358333 183
## 79 -76.73167 8.0980556 7
## 80 -74.26833 4.7344444 2400
## 81 -75.34389 10.2544444 58
## 82 -75.64694 6.1605556 1887
## 83 -75.51305 6.3488889 1498
## 84 -74.78722 9.7919444 18
## 85 -76.68639 7.6769444 109
## 86 -75.13306 9.7222222 154
## 87 -76.23861 3.3275000 894
## 88 -73.82000 5.6188889 2713
## 89 -76.54444 3.2638889 890
## 90 -76.24472 3.4211111 912
## 91 -74.91833 10.7997222 124
## 92 -75.93611 4.2688889 1394
## 93 -76.48666 3.0130556 1061
## 94 -74.81194 7.5941667 51
## 95 -73.75667 3.9875000 579
## 96 -75.81973 9.2316667 7
## 97 -74.70361 7.0833333 638
## 98 -75.29583 9.3177778 154
## 99 -76.31667 3.6880556 851
## 100 -75.58833 8.4147222 113
#crear un vector de cada coordenada
lat= coords[,2]
long= coords[,1]
alt=coords[,3]
list.files("departamentos1")
## [1] "MGN_DPTO_POLITICO.CPG" "MGN_DPTO_POLITICO.dbf"
## [3] "MGN_DPTO_POLITICO.prj" "MGN_DPTO_POLITICO.sbn"
## [5] "MGN_DPTO_POLITICO.sbx" "MGN_DPTO_POLITICO.shp"
## [7] "MGN_DPTO_POLITICO.shp.xml" "MGN_DPTO_POLITICO.shx"
## [9] "MGN2020_DPTO_POLITICO.rar"
deptos= read_sf("../geomatica/departamentos1/MGN_DPTO_POLITICO.shp"); deptos
class(deptos) #Conocer el tipo de archivos
## [1] "sf" "tbl_df" "tbl" "data.frame"
st_crs(deptos)# conocer que tipo de coordenadas y parametros de elipsoide.
## 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]]
head(deptos)
deptos$DPTO_CNMBR
## [1] "CAQUETÁ"
## [2] "CAUCA"
## [3] "PUTUMAYO"
## [4] "VALLE DEL CAUCA"
## [5] "GUAINÍA"
## [6] "VICHADA"
## [7] "CASANARE"
## [8] "AMAZONAS"
## [9] "VAUPÉS"
## [10] "GUAVIARE"
## [11] "CALDAS"
## [12] "QUINDIO"
## [13] "RISARALDA"
## [14] "ANTIOQUIA"
## [15] "CHOCÓ"
## [16] "NARIÑO"
## [17] "CÓRDOBA"
## [18] "BOLÍVAR"
## [19] "CESAR"
## [20] "LA GUAJIRA"
## [21] "MAGDALENA"
## [22] "SUCRE"
## [23] "ARCHIPIÉLAGO DE SAN ANDRÉS, PROVIDENCIA Y SANTA CATALINA"
## [24] "ARAUCA"
## [25] "BOYACÁ"
## [26] "CUNDINAMARCA"
## [27] "NORTE DE SANTANDER"
## [28] "BOGOTÁ, D.C."
## [29] "META"
## [30] "HUILA"
## [31] "SANTANDER"
## [32] "TOLIMA"
## [33] "ATLÁNTICO"
# Para reducir el trabajo de memoria. se puede generalizar las lineas que conforman los limites de los departamentos. solo se puede haecr con coordenadas planas. por lo que es necesario proyectar los departamentos ( comvertir coordenadas geograficas a planas)
deptos2= deptos %>% st_transform(3116)
deptos3= st_simplify(deptos2, preserveTopology = T, dTolerance = 50) #el parametro preservar topologia es importante para mantener a los poligonos sin superponerse, la tolerancia tambien se puede ampliar o reducir
# Leafleat trabaja con datos geograficos en sistema de coordenadas CRS WGS84 por lo que necesiamos transformar el objeto simplificado a coordenadas geograficas.
deptos4= deptos3 %>% st_transform(4326)
map= leaflet(deptos4) %>% addTiles()
labels= sprintf("<strong>%s</strong><br/>%g unknow units</sup>", deptos4$DPTO_CNMBR, deptos4$SHAPE_AREA) %>% lapply(htmltools::HTML) #lista que se puede representra en un archivo HTML
map= addPolygons(map, color = "#444444", weight = 1, smoothFactor = 0.5, opacity = 1.0, fillOpacity = 0.5, fillColor = ~colorQuantile("BrBG", SHAPE_AREA)(SHAPE_AREA), highlightOptions = highlightOptions(color = "orange", weight = 2, bringToFront = T), label = labels,labelOptions = labelOptions(style = list("font-weight"= "normal", padding= "3px 8px"), textsize = "15px", direction = "auto"))
#se añaden marcadores
map= addMarkers(map, lng = long,lat=lat, popup = map$ciudad)
map
### 5. Informacion de la sesion
sessionInfo()
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19041)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Spanish_Colombia.1252 LC_CTYPE=Spanish_Colombia.1252
## [3] LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C
## [5] LC_TIME=Spanish_Colombia.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] leaflet_2.0.4.1 sf_0.9-7 rgeos_0.5-5 sp_1.4-2
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.5 RColorBrewer_1.1-2 compiler_4.0.2 pillar_1.4.6
## [5] class_7.3-17 tools_4.0.2 digest_0.6.25 jsonlite_1.7.0
## [9] evaluate_0.14 lifecycle_0.2.0 tibble_3.0.3 lattice_0.20-41
## [13] pkgconfig_2.0.3 rlang_0.4.7 DBI_1.1.1 crosstalk_1.1.0.1
## [17] yaml_2.2.1 xfun_0.16 e1071_1.7-4 dplyr_1.0.2
## [21] stringr_1.4.0 knitr_1.29 generics_0.0.2 vctrs_0.3.3
## [25] htmlwidgets_1.5.2 classInt_0.4-3 grid_4.0.2 tidyselect_1.1.0
## [29] glue_1.4.1 R6_2.4.1 rmarkdown_2.3 farver_2.0.3
## [33] purrr_0.3.4 magrittr_1.5 scales_1.1.1 htmltools_0.5.0
## [37] ellipsis_0.3.1 units_0.7-0 colorspace_1.4-1 KernSmooth_2.23-17
## [41] stringi_1.4.6 munsell_0.5.0 crayon_1.3.4