Terrain Calculations from DEM

library(terra)
library(tmap)

dem <- rast("data/AnalysisReady/elevation.tif")

# Calculate slope in degrees from the DEM and save output
slp <- terrain(dem, v = "slope", unit = "degrees")
writeRaster(slp, "data/AnalysisReady/slope.tif")

## Derive a hillshade image

# First calculate slope and aspect in radians
slp <- terrain(dem, v = "slope", unit = "radians")
asp <- terrain(dem, v = "aspect", unit = "radians")
# Create hillshade layer
hill <- shade(slp, asp)

# View results
tmap_mode("view")

tm_shape(hill) +
  tm_raster(style = "cont", palette = "-Greys", legend.show = FALSE)+
  tm_scale_bar()+
  tm_shape(dem)+
  tm_raster(style = "cont", alpha = 0.6, palette = hcl.colors(10, "Terrain"))