New R-package for flow routing on digital elevation models

Digital elevation models (DEMs) are very convenient for modeling water flow. Some of the applications include delineation of watersheds, flowlines, or deriving useful other useful measures such as the ‘height above nearest drainage’ (HAND, link to another post on this). As a consequence of climate change, the frequency of extreme precipitation events is expected to increase in the future. Therefore, knowing the whereabouts of water is highly relevant and an important tool for the management of surface water in the landscape.

New R package

Flow routing on DEMs are not new and is accessible in most GIS software. Developments in data collection, e.g. LIDAR, have made it possible to create large scale very high-resolution DEMs. These are often publicly available from national authorities. However, this does also call for highly efficient tools to process large amounts of available data. Luckily, great open-source alternatives exists, e.g. GRASS GIS, TauDEM or RichDEM. As a supplement to these tools, I have created the flowdem R-package which provides access to algorithms from RichDEM and other functionality in R. The algorithms have been implemented using Rcpp for high performance. Note that command-line tools are still the best option for very demanding tasks.

What can be done?

As of now, the package is functional but still in a developmental state. It includes algorithms for:

  • preprocessing DEMs by filling or breaching
  • routing water using D8 system
  • flow accumulation
  • watershed delineation

Watersheds, i.e. the area contributing with water to a target area, can be delineated using point, lines and polygons as target areas. The workflow for delineating the watershed for Lake Esrum would be something like:

#Read in data
#'dem' is a national DEM for Denmark (10 m resolution)
#'lake' is the polygon for Lake Esrum 
dem <- raster("dk_dem.tif")
lake <- st_read("esrum.sqlite") %>% 
  st_zm()

#Preprocess DEM by breaching and filling (epsilon=TRUE to resolve flats)
dem_proc <- dem |>
  breach() |>
  fill(epsilon = TRUE)

#Calculate flowdirections
dem_dirs <- dirs(dem_proc, mode="d8")

#Delineate watershed
lake_watershed <- watershed(dem_dirs, lake)

#Convert watershed raster to vector
#trim and aggregate speeds up the polygonization
lake_watershed_vec <- lake_watershed |>
  trim() |>
  aggregate(fact=5) |>
  rasterToPolygons(dissolve = TRUE) |>
  st_as_sf()

The result of this workflow is a watershed that we can visualize using Leaflet:

#Re-project lake and watershed polygons
lake <- lake |>
  st_transform(4326)

lake_watershed_vec <- lake_watershed_vec |>
  st_transform(4326)

#Create leaflet map
leaflet() |>
  addTiles() |>
  addPolygons(data = lake_watershed_vec, fillOpacity = 0, color = "coral") |>
  addPolygons(data = lake, fillOpacity = 0, color = "purple")

Future development

The goal of this R-package is to provide convenient access to the most efficient algorithms for preprocessing and routing water on DEMs. The package provides easy-to-use, R native tools for workflows related to flow routing on DEMs which alleviates the need for calling other GIS software through the command-line in R scripts. Some of the high priority tasks are further improve the functionality of flowdem::watershed, adding support for the dInf flow system, implementing the latest priority-flood algorithms, improving flow accumulation to support weights, and adding general flat resolution as done in RichDEM. Development status is updated on the Github package repository.

Avatar
Kenneth Thorø Martinsen
Biologist (PhD)

Research interests in data science and carbon cycling.

Related