library(rasterVis)
## Warning: package 'rasterVis' was built under R version 4.0.5
## Loading required package: raster
## Warning: package 'raster' was built under R version 4.0.5
## Loading required package: sp
## Loading required package: terra
## Warning: package 'terra' was built under R version 4.0.5
## terra version 1.1.4
## Loading required package: lattice
## Loading required package: latticeExtra
## Warning: package 'latticeExtra' was built under R version 4.0.5
library(raster)
library(rgl)
## Warning: package 'rgl' was built under R version 4.0.5
library(rgdal)
## Warning: package 'rgdal' was built under R version 4.0.5
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/Juan Narvaez/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/Juan Narvaez/Documents/R/win-library/4.0/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/Juan Narvaez/Documents/R/win-library/4.0/rgdal/proj
##
## Attaching package: 'rgdal'
## The following object is masked from 'package:terra':
##
## project
library(elevatr)
## Warning: package 'elevatr' was built under R version 4.0.5
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.0 v dplyr 1.0.5
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::collapse() masks terra::collapse()
## x dplyr::desc() masks terra::desc()
## x tidyr::expand() masks terra::expand()
## x tidyr::extract() masks terra::extract(), raster::extract()
## x tidyr::fill() masks terra::fill()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x ggplot2::layer() masks latticeExtra::layer()
## x dplyr::near() masks terra::near()
## x tidyr::pack() masks terra::pack()
## x dplyr::select() masks terra::select(), raster::select()
## x tidyr::separate() masks terra::separate()
## x purrr::transpose() masks terra::transpose()
library(mapview)
## Warning: package 'mapview' was built under R version 4.0.5
(munici <- sf::st_read("geomatica/94_GUAINIA/ADMINISTRATIVO/MGN_MPIO_POLITICO.shp"))
## Reading layer `MGN_MPIO_POLITICO' from data source `C:\Users\Juan Narvaez\Documents\geomatica\94_GUAINIA\ADMINISTRATIVO\MGN_MPIO_POLITICO.shp' using driver `ESRI Shapefile'
## Simple feature collection with 9 features and 9 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -70.94249 ymin: 1.165633 xmax: -66.84722 ymax: 4.045026
## geographic CRS: WGS 84
## Simple feature collection with 9 features and 9 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -70.94249 ymin: 1.165633 xmax: -66.84722 ymax: 4.045026
## geographic CRS: WGS 84
## DPTO_CCDGO MPIO_CCDGO MPIO_CNMBR
## 1 94 94001 INÍRIDA
## 2 94 94343 BARRANCO MINA
## 3 94 94663 MAPIRIPANA
## 4 94 94883 SAN FELIPE
## 5 94 94884 PUERTO COLOMBIA
## 6 94 94885 LA GUADALUPE
## 7 94 94886 CACAHUAL
## 8 94 94887 PANÁ-PANÁ (Campo Alegre)
## 9 94 94888 MORICHAL (Morichal Nuevo)
## MPIO_CRSLC MPIO_NAREA
## 1 Decreto 1593 de Agosto 5 de 1974 15970.268
## 2 Resolución 83 del 1 de Febrero de 1988 que aprueba con modi 9467.804
## 3 ACUERDO COMISARIAL 018 DEL 25 DE AGOSTO DE 1990 4927.971
## 4 Resolución 83 2926.365
## 5 Resolución 83 15700.545
## 6 Resolución 83 1178.868
## 7 Resolución 83 2335.323
## 8 Resolución 83 de 1988 10227.139
## 9 1988 8554.996
## MPIO_NANO DPTO_CNMBR Shape_Leng Shape_Area geometry
## 1 2017 GUAINÍA 9.256685 1.2869895 POLYGON ((-67.67638 3.91228...
## 2 2017 GUAINÍA 6.825914 0.7652557 POLYGON ((-68.91332 3.68215...
## 3 2017 GUAINÍA 5.010789 0.3990354 POLYGON ((-70.10453 3.38436...
## 4 2017 GUAINÍA 4.029719 0.2346116 POLYGON ((-67.34976 2.50451...
## 5 2017 GUAINÍA 9.166487 1.2632740 POLYGON ((-67.5385 3.177568...
## 6 2017 GUAINÍA 1.750810 0.0943385 POLYGON ((-66.96243 1.66858...
## 7 2017 GUAINÍA 2.364174 0.1876089 POLYGON ((-67.49529 3.75681...
## 8 2017 GUAINÍA 7.805305 0.8250362 POLYGON ((-68.77132 2.42182...
## 9 2017 GUAINÍA 6.916544 0.6917881 POLYGON ((-69.46493 2.88960...
elevacion<- get_elev_raster(munici,z=10)
## Note: Your request will download approximately 124.2Mb.
## Mosaicing & Projecting
## Note: Elevation units are in meters.
## Note: The coordinate reference system is:
## GEOGCRS["WGS 84 (with axis order normalized for visualization)",
## 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["geodetic longitude (Lon)",east,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433,
## ID["EPSG",9122]]],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433,
## ID["EPSG",9122]]]]
if (require(rgdal)){
rf <- writeRaster(elevacion, filename=file.path("C:/Users/Juan Narvaez/Documents/geomatica/Guainia_Dem_z10.tif"), format="GTiff", overwrite=TRUE)
}
munici_sp <- as(munici, "Spatial")
spatialref <- CRS(SRS_string="EPSG:32618")
elev_crop = crop(elevacion, munici_sp)
pr3 <- projectExtent(elev_crop, crs=spatialref)
res(pr3) <- 180
rep_elev <- projectRaster(elev_crop, pr3)
(rep_munic = spTransform(munici_sp,spatialref))
## class : SpatialPolygonsDataFrame
## features : 9
## extent : 951413.4, 1410054, 130083.7, 450762.9 (xmin, xmax, ymin, ymax)
## crs : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs
## variables : 9
## names : DPTO_CCDGO, MPIO_CCDGO, MPIO_CNMBR, MPIO_CRSLC, MPIO_NAREA, MPIO_NANO, DPTO_CNMBR, Shape_Leng, Shape_Area
## min values : 94, 94001, BARRANCO MINA, 1988, 1178.86832903, 2017, GUAINÍA, 1.75081007547, 0.0943384971244
## max values : 94, 94888, SAN FELIPE, Resolución 83 del 1 de Febrero de 1988 que aprueba con modi, 15970.2680471, 2017, GUAINÍA, 9.25668481051, 1.28698950156
###datos basicos estadisticos de elevacion generamos un histograma para explorar los datos del DEM
hist(rep_elev)
promedio <- cellStats(rep_elev, 'mean')
minimo <- cellStats(rep_elev, 'min')
maximo <- cellStats(rep_elev, 'max')
desviacion <- cellStats(rep_elev, 'sd')
metricas <- c('mean', 'min', 'max', 'std')
valores <- c(promedio, minimo, maximo, desviacion)
(df_estadisticas <- data.frame(metricas, valores))
## metricas valores
## 1 mean 150.94975
## 2 min -268.23602
## 3 max 960.60735
## 4 std 42.12976
slope = terrain(rep_elev,opt='slope', unit='degrees')
aspect = terrain(rep_elev,opt='aspect',unit='degrees')
hill = hillShade(slope,aspect,40,315)
obtenemos el Dem de los municipios
plot(rep_elev,main="DEM de los municios de Guainia [metros]", col=terrain.colors(25,alpha=0.8))
plot(rep_munic, add=TRUE, lwd=0.5)
text(coordinates(rep_munic), labels=as.character(rep_munic$MPIO_CNMBR), cex=0.2)
pendiente de la zona
plot(slope,main="pendiente de los municipios de Guainia [Grados]", col=topo.colors(6,alpha=0.6))
plot(rep_munic, add=TRUE, lwd=0.5)
text(coordinates(rep_munic), labels=as.character(rep_munic$MPIO_CNMBR), cex=0.2)
aspecto de la zona podremos ver donde se encuentran los picos y en que municipios
plot(aspect,main="aspectos de grados municipios [grados]", col=rainbow(10,alpha=0.7))
plot(rep_munic, add=TRUE, lwd=0.5)
text(coordinates(rep_munic), labels=as.character(rep_munic$MPIO_CNMBR), cex=0.2)
se combinan los mapas para tener uno solo
plot(hill,
col=grey(1:100/100),
legend=FALSE,
main="DEM de municipios de Guainia [m]",
axes=FALSE)
plot(rep_elev,
axes=FALSE,
col=terrain.colors(12, alpha=0.6), add=TRUE)
plot(rep_munic, add=TRUE, lwd=0.5)
text(coordinates(rep_munic), labels=as.character(rep_munic$MPIO_CNMBR), cex=0.2)
Utilizaremos la libreria rayshader para hacer un mapeo de los datos de elevacion
library(rayshader)
## Warning: package 'rayshader' was built under R version 4.0.5
#Convert the DEM into a matrix:
elmat = raster_to_matrix(rep_elev)
#And we can add a raytraced layer from that sun direction as well:
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color = "desert") %>%
add_shadow(ray_shade(elmat), 0.5) %>%
add_shadow(ambient_shade(elmat), 0) %>%
plot_map()
visualizaremos una zona mas pequeña , la zona norte y central del departamento
lista <- list("MORICHAL (Morichal Nuevo)", "INÍRIDA", "BARRANCO MINA", "MAPIRIPANA", "PANÁ-PANÁ (Campo Alegre)")
(algunos <- munici %>% filter(MPIO_CNMBR %in% unlist(lista) ))
## Simple feature collection with 5 features and 9 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -70.94249 ymin: 1.692744 xmax: -67.53842 ymax: 4.045026
## geographic CRS: WGS 84
## DPTO_CCDGO MPIO_CCDGO MPIO_CNMBR
## 1 94 94001 INÍRIDA
## 2 94 94343 BARRANCO MINA
## 3 94 94663 MAPIRIPANA
## 4 94 94887 PANÁ-PANÁ (Campo Alegre)
## 5 94 94888 MORICHAL (Morichal Nuevo)
## MPIO_CRSLC MPIO_NAREA
## 1 Decreto 1593 de Agosto 5 de 1974 15970.268
## 2 Resolución 83 del 1 de Febrero de 1988 que aprueba con modi 9467.804
## 3 ACUERDO COMISARIAL 018 DEL 25 DE AGOSTO DE 1990 4927.971
## 4 Resolución 83 de 1988 10227.139
## 5 1988 8554.996
## MPIO_NANO DPTO_CNMBR Shape_Leng Shape_Area geometry
## 1 2017 GUAINÍA 9.256685 1.2869895 POLYGON ((-67.67638 3.91228...
## 2 2017 GUAINÍA 6.825914 0.7652557 POLYGON ((-68.91332 3.68215...
## 3 2017 GUAINÍA 5.010789 0.3990354 POLYGON ((-70.10453 3.38436...
## 4 2017 GUAINÍA 7.805305 0.8250362 POLYGON ((-68.77132 2.42182...
## 5 2017 GUAINÍA 6.916544 0.6917881 POLYGON ((-69.46493 2.88960...
algunos_sp <- as(algunos, "Spatial")
elev_crop = crop(elevacion, algunos_sp)
plot(elev_crop, main=" modelo digital recortado ")
plot(algunos_sp, add=TRUE)
text(coordinates(algunos_sp), labels=as.character(algunos_sp$MPIO_CNMBR), cex=0.5)
pr3 <- projectExtent(elev_crop, crs=spatialref)
# Adjust the cell size
res(pr3) <- 120
# now project
rep_elev <- projectRaster(elev_crop, pr3)
(rep_algunos = spTransform(algunos_sp,spatialref))
## class : SpatialPolygonsDataFrame
## features : 5
## extent : 951413.4, 1330940, 187882.8, 450762.9 (xmin, xmax, ymin, ymax)
## crs : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs
## variables : 9
## names : DPTO_CCDGO, MPIO_CCDGO, MPIO_CNMBR, MPIO_CRSLC, MPIO_NAREA, MPIO_NANO, DPTO_CNMBR, Shape_Leng, Shape_Area
## min values : 94, 94001, BARRANCO MINA, 1988, 4927.97124104, 2017, GUAINÍA, 5.01078939041, 0.399035434995
## max values : 94, 94888, PANÁ-PANÁ (Campo Alegre), Resolución 83 del 1 de Febrero de 1988 que aprueba con modi, 15970.2680471, 2017, GUAINÍA, 9.25668481051, 1.28698950156
lo veremos de una zona 3D
#Convert the DEM into a matrix:
elmat = raster_to_matrix(rep_elev)
#detect_water and add_water adds a water layer to the map:
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color = "desert") %>%
add_shadow(ray_shade(elmat), 0.5) %>%
add_shadow(ambient_shade(elmat), 0) %>%
plot_map()