## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = FALSE # spatial chunks need terra/tmap; not run at build time ) ## ----install------------------------------------------------------------------ # install.packages(c("terra", "tmap")) ## ----dem---------------------------------------------------------------------- # library(terra) # library(floodflow) # # # Bundled DEM (elevation of a small area, in metres). No download needed. # dem <- rast(system.file("ex/elev.tif", package = "terra")) # dem # # # Slope in radians, then as a simple gradient (rise/run) for routing # slope <- terrain(dem, v = "slope", unit = "radians") # plot(dem, main = "Bundled elevation model (terra ex/elev.tif)") ## ----roughness---------------------------------------------------------------- # # A stand-in NDVI raster on the same grid as the DEM (values 0-1) # set.seed(1) # ndvi <- setValues(dem, runif(ncell(dem), 0, 1)) # # # Per-cell Manning's n from vegetation greenness # rough <- roughness(ndvi, method = "ndvi") # rough$n # this is now a SpatRaster of roughness values # plot(rough$n, main = "Manning's n from vegetation (per cell)") ## ----vulnerability------------------------------------------------------------ # # Three layers on the DEM grid (replace with your real rasters) # hazard <- dem / global(dem, "max", na.rm = TRUE)[1, 1] # deeper where higher-relief, illustrative # exposure <- setValues(dem, rpois(ncell(dem), 50)) # population per cell # vuln <- setValues(dem, runif(ncell(dem))) # deprivation index # # risk <- flood_vulnerability(hazard, # exposure = exposure, # vulnerability = vuln) # risk$risk # a SpatRaster: relative risk, 0 to 1 ## ----map---------------------------------------------------------------------- # fp <- flood_project("example basin", crs = crs(dem)) # fp$vulnerability <- risk # # m <- flood_map(fp, layer = "risk") # m$engine # "tmap" when tmap is installed # m$map # the interactive map object; print it to view ## ----plot-direct-------------------------------------------------------------- # plot(risk$risk, main = "Flood risk index (Hazard x Exposure x Vulnerability)") ## ----depth-map---------------------------------------------------------------- # # A HAND raster on the DEM grid. For a quick look you can pass the DEM itself # # (floodflow derives a crude proxy); for accuracy use a true HAND surface, e.g. # # from whitebox::wbt_elevation_above_stream(). # hand <- dem - global(dem, "min", na.rm = TRUE)[1, 1] # # fp <- flood_project("example basin", crs = crs(dem)) # fp$rainfall <- data.frame( # date = seq(as.Date("2020-01-01"), by = "day", length.out = 365), # precip_mm = round(runif(365, 0, 20), 1)) # fp <- flood_runoff(fp, engine = "simple") # fp <- flood_route(fp, area_km2 = 300, hand = hand) # <- pass the HAND raster # # fp$route$depth_raster # a SpatRaster of inundation depth # flood_map(fp, layer = "depth") # now draws a real inundation map # plot(fp$route$depth_raster, main = "Inundation depth (m)") ## ----substitute--------------------------------------------------------------- # # Instead of the bundled DEM: # # dem <- rast(system.file("ex/elev.tif", package = "terra")) # # # use your own: # dem <- rast("path/to/your_dem.tif") # ndvi <- rast("path/to/your_ndvi.tif") # # ... the rest of the workflow is unchanged.