Class 18
Then:
slope <- terrain(dem, v = "slope", unit = "radians", neighbors = 8)
aspect <- terrain(dem, v = "aspect", neighbors = 8)
hillshade <- shade(slope, aspect / 180 * pi, angle = 40, direction = 270)
# Plot
l_figs <- lapply(list(slope, aspect, hillshade), function(lyr){
ggplot() + geom_spatraster(data = lyr) +
scale_fill_terrain_c(na.value = "transparent") +
theme_void()
})
ggpubr::ggarrange(plotlist = l_figs, nrow = 1)Topographic variations
# variations
tpi <- terrain(dem, v = "TPI", neighbors = 8)
tri <- terrain(dem, v = "TRI", neighbors = 8)
roughness <- terrain(dem, v = "roughness", neighbors = 8)
# Plot
l_figs <- lapply(list(tpi, tri, roughness), function(lyr){
ggplot() + geom_spatraster(data = lyr) +
scale_fill_terrain_c(na.value = "transparent") +
theme_void()
})
ggpubr::ggarrange(plotlist = l_figs, nrow = 1)Identify suitable areas for a hiking trail using terrain data:
Tobler’s First Law of Geography: Everything is related to everything else, but near things are more related than distant things.
Different approaches:
NOTE: we could do interpolation for any numeric variables, but it does not mean the interpolation always makes sense.
# Get the annual rainfall
rainfall <- worldclim_country(country = "COL", var = "prec", res = 2.5, path = tempdir())
rainfall <- sum(rainfall); names(rainfall) <- "mean_rainfall"
plot(rainfall)
# Get some pseudo samples
set.seed(10)
pts_rainfall <- spatSample(
rainfall, size = 200, na.rm = TRUE, xy = TRUE) %>%
rename(rainfall = sum)
# Visually check
plot(rainfall, breaks = seq(0, 8000, 1000),
col = terrain.colors(length(seq(0, 8000, 1000))))
plot(pts_rainfall %>% st_as_sf(coords = c("x", "y"), crs = 4326) %>% st_geometry(),
add = TRUE, pch = 20, col = 'black', size = 1)gstat package
gstat object (like a model)library(gstat)
library(dplyr)
# step 1 - Define a raster template for the result
rst_template <- rainfall
# Step 2 - Define gstat object that hold all necessary info for interpolation
nn_gs <- gstat(formula = rainfall ~ 1,
locations = ~x + y, data = pts_rainfall,
nmax = 5, set = list(idp = 0))
# Step 3 - Do the prediction
rainfall_nn <- interpolate(rst_template, nn_gs)
rainfall_nn <- mask(rainfall_nn, rst_template)
# Step 4 - Evaluation
plot(c(rainfall_nn$var1.pred, rainfall))Nearest neighbor interpolation is a special case of IDW, which not use weights at all. In function gstat, we set set = list(idp = 0).
Now, it is your turn to create a map using IDW. And compare it with the real mean rainfall and nearest neighbor interpolation.
gstat# Revise the step 2 and step 3 to make rainfall_nn
# and save the result to rainfall_idw
# Put together to compare
imgs <- c(rainfall,
rainfall_nn$var1.pred,
rainfall_idw$var1.pred)
names(imgs) <- c('Real mean rainfall',
'NN interpolation',
'IDW interpolation')
# Introduce a new way to plot
library(tmap)
tm_shape(imgs) +
tm_raster(title = 'Rainfall',
breaks = seq(0, 8000, 1000),
palette = "-RdYlGn",
n = length(seq(0, 8000, 1000)))