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.
## install.packages("rgdal")
## install.packages("raster")
## install.packages("elevatr")
## install.packages("rasterVis")
## install.packages("rgl")
## install.packages("hash")
library(rasterVis)
library(raster)
library(rgl)
library(rgdal)
library(elevatr)
## obtener raster
library(shapefiles)
library(sf)
library(rgeos)
library(hash)
list.files("C:/Users/familiar/Downloads/gb2/La guajira/44_LA_GUAJIRA/ADMINISTRATIVO")
[1] "MGN_DPTO_POLITICO.cpg" "MGN_DPTO_POLITICO.dbf" "MGN_DPTO_POLITICO.prj"
[4] "MGN_DPTO_POLITICO.sbn" "MGN_DPTO_POLITICO.sbx" "MGN_DPTO_POLITICO.shp"
[7] "MGN_DPTO_POLITICO.shp.xml" "MGN_DPTO_POLITICO.shx" "MGN_MPIO_POLITICO.cpg"
[10] "MGN_MPIO_POLITICO.dbf" "MGN_MPIO_POLITICO.prj" "MGN_MPIO_POLITICO.sbn"
[13] "MGN_MPIO_POLITICO.sbx" "MGN_MPIO_POLITICO.shp" "MGN_MPIO_POLITICO.shp.xml"
[16] "MGN_MPIO_POLITICO.shx"
muni <- shapefile("C:/Users/familiar/Downloads/gb2/La guajira/44_LA_GUAJIRA/ADMINISTRATIVO/MGN_MPIO_POLITICO.shp")
head(muni)
guaji <- muni[muni$MPIO_CNMBR=="La Guajira",]
plot(muni, main="- LA GUAJIRA -", axes=TRUE)
plot(guaji, add=TRUE)
invisible(text(coordinates(muni), labels=as.character(muni$MPIO_CNMBR), cex=0.8))
elevation <- get_elev_raster(guaji, z = 8)
Downloading DEMs [===>-----------------------] 17% eta: 8s
Downloading DEMs [======>--------------------] 25% eta: 6s
Downloading DEMs [========>------------------] 33% eta: 6s
Downloading DEMs [==========>----------------] 42% eta: 5s
Downloading DEMs [=============>-------------] 50% eta: 4s
Downloading DEMs [===============>-----------] 58% eta: 3s
Downloading DEMs [=================>---------] 67% eta: 3s
Downloading DEMs [===================>-------] 75% eta: 2s
Downloading DEMs [=====================>-----] 83% eta: 1s
Downloading DEMs [========================>--] 92% eta: 1s
Downloading DEMs [===========================] 100% eta: 0s
Merging DEMs
Reprojecting DEM to original projection
ning昼㹡n argumento finito para min; retornando Infningun argumento finito para max; retornando -InfNote: Elevation units are in meters.
Note: The coordinate reference system is:
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
elevation
class : RasterLayer
dimensions : 1546, 2055, 3177030 (nrow, ncol, ncell)
resolution : 0.00275, 0.0027 (x, y)
extent : -74.545, -68.89375, 8.393864, 12.56806 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : memory
names : layer
values : -3936.986, 5539.776 (min, max)
plot(elevation, main="This the downloaded DEM [meters]")
plot(muni, add=TRUE)
elev_crop = crop(elevation,muni)
plot(elev_crop, main="Cropped Digital elevation model")
plot(muni, add=TRUE)
elev_crop
class : RasterLayer
dimensions : 764, 928, 708992 (nrow, ncol, ncell)
resolution : 0.00275, 0.0027 (x, y)
extent : -73.665, -71.113, 10.39726, 12.46006 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : memory
names : layer
values : -2867.742, 5345.619 (min, max)
spatialref <-"+proj=tmerc +lat_0=4.596200416666666 +lon_0=-80.07750791666666 +k=1 +x_0=1000000 +y_0=1000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs "
pr3 <- projectExtent(elev_crop, spatialref)
res(pr3) <- 100
rep_elev <- projectRaster(elev_crop, pr3)
ning昼㹡n argumento finito para min; retornando Infningun argumento finito para max; retornando -Inf
rep_elev
class : RasterLayer
dimensions : 2377, 2870, 6821990 (nrow, ncol, ncell)
resolution : 100, 100 (x, y)
extent : 1698465, 1985465, 1648658, 1886358 (xmin, xmax, ymin, ymax)
crs : +proj=tmerc +lat_0=4.596200416666666 +lon_0=-80.07750791666666 +k=1 +x_0=1000000 +y_0=1000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
source : memory
names : layer
values : -2867.697, 5337.705 (min, max)
(rep_guaji = spTransform(muni,spatialref))
class : SpatialPolygonsDataFrame
features : 15
extent : 1702207, 1979765, 1649852, 1884291 (xmin, xmax, ymin, ymax)
crs : +proj=tmerc +lat_0=4.596200416666666 +lon_0=-80.07750791666666 +k=1 +x_0=1000000 +y_0=1000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +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 : 44, 44001, ALBANIA, 1888, 179.59085022, 2017, LA GUAJIRA, 0.710049632727, 0.0148255649224
max values : 44, 44874, VILLANUEVA, Ordenanza015 del 29 de Noviembre de 1989, 7886.05230923, 2017, LA GUAJIRA, 6.11271226506, 0.653549310217
plot(rep_elev, main="Reprojected Digital elevation model")
plot(rep_guaji, add=TRUE)
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))
slope = terrain(rep_elev,opt='slope', unit='degrees')
aspect = terrain(rep_elev,opt='aspect',unit='degrees')
hill = hillShade(slope,aspect,40,315)
plot(rep_elev,main="DEM for LA GUAJIRA [meters]", col=terrain.colors(25,alpha=0.7))
plot(slope,main="Slope for LA GUAJIRA [degrees]", col=topo.colors(25,alpha=0.7))
plot(aspect,main="Aspect for LA GUAJIRA [degrees]", col=rainbow(25,alpha=0.7))
plot(hill,
col=grey(1:100/100), # create a color ramp of grey colors for hillshade
legend=FALSE, # no legend, we don't care about the grey of the hillshade
main="DEM forLA GUAJIRA",
axes=FALSE) # makes for a cleaner plot, if the coordinates aren't necessary
plot(rep_elev,
axes=FALSE,
col=terrain.colors(12, alpha=0.35), add=TRUE) # color method
library(rayshader)
elmat = raster_to_matrix(rep_elev)
[1] "Dimensions of matrix are: 2870x2377."
elmat %>%
sphere_shade(texture = "imhof2") %>%
plot_map()
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color = "desert") %>%
plot_map()
elmat %>%
sphere_shade(texture = "desert") %>%
add_water(detect_water(elmat), color = "desert") %>%
add_shadow(ray_shade(elmat), 0.5) %>%
plot_map()
install.packages("jpeg")
Error in install.packages : Updating loaded packages
library(jpeg)
getv=function(i,a,s){
ct = dim(i)[1:2]/2
sx = values(s)/90 * ct[1]
sy = values(s)/90 * ct[2]
a = values(a) * 0.01745
px = floor(ct[1] + sx * -sin(a))
py = floor(ct[2] + sy * cos(a))
template = brick(s,s,s)
values(template)=NA
cellr = px + py * ct[1]*2
cellg = px + py * ct[1]*2 + (ct[1]*2*ct[2]*2)
cellb = px + py * ct[1]*2 + 2*(ct[1]*2*ct[2]*2)
template[[1]] = i[cellr]
template[[2]] = i[cellg]
template[[3]] = i[cellb]
template = template * 256
template
}
# Load an environment map image
# An example can be found at http://i.imgur.com/9pvbHjN.jpg
map=readJPEG("./dem/9pvbHjN.jpg")
Error in readJPEG("./dem/9pvbHjN.jpg") : unable to open ./dem/9pvbHjN.jpg
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.