0

I am currently trying to calculate the flow path length in a DEM towards a streamnetwork. My desired outcome would be a raster with values of how long the flowpath to the nearest streamcell would be for every cell.

The streams are generated by flow accumulation and are also available as binary raster files.

I am working in R but am unable to find a function that provides this functionality.

I stumbled upon a similar function that might work in GRASS however I would like to forego setting up rgrass. For my Watersehd deliniation and flowpath prerpocessing im using whiteboxtools for R which works like a charm.

2
  • We do this kind of task with Taudem integrated in Qgis : hydrology.usu.edu/taudem/taudem5 but nothing to do with R ... Commented Jun 30, 2023 at 9:36
  • If WhiteBoxTools doesn't do it and you don't want to set up GRASS then maybe there's a tool in SAGA saga-gis.sourceforge.io/en/index.html that can do it and there's R to SAGA integration, but you'll have to set that up, but if you don't want to do that for the same reason as not wanting to set GRASS up then I don't think that leaves much...
    – Spacedman
    Commented Jun 30, 2023 at 10:04

1 Answer 1

1

You can use the R package {whitebox} to bring whitebox tools to your R session. The tool you might be looking for is called wbt_downslope_distance_to_stream. Below you will find an example how to extract the distance to the nearest slope end-to-end. Note, that you will have to chose sensible settings for your area of interest.

library(whitebox)
library(terra)
#> terra 1.7.40
library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.6.2, PROJ 9.1.1; sf_use_s2() is TRUE


loc <- tempfile()
dir.create(loc)

dem_file <- system.file("ex/elev.tif", package="terra")


install_whitebox()
#> Performing one-time download of WhiteboxTools binary from
#>   https://www.whiteboxgeo.com/WBT_Linux/WhiteboxTools_linux_amd64.zip 
#> (This could take a few minutes, please be patient...)
#> WhiteboxTools binary is located here:  /home/darius/.local/share/R/whitebox/WBT/whitebox_tools 
#> You can now start using whitebox
#>     library(whitebox)
#>     wbt_version()
wbt_version()


wbt_fill_depressions(
  dem = dem_file,
  output = file.path(loc, "dem_filled.tif")
)


wbt_flow_accumulation_full_workflow(
  dem = file.path(loc, "dem_filled.tif"), 
  out_dem = file.path(loc, "out_dem.tif"),
  out_pntr = file.path(loc, "out_pntr.tif"),
  out_accum = file.path(loc, "out_accum.tif"),
  out_type = "sca"
)

wbt_extract_streams(
  flow = file.path(loc, "out_accum.tif"),
  output = file.path(loc, "streams.tif"),
  threshold = 1
)


wbt_downslope_distance_to_stream(
  dem = file.path(loc, "dem_filled.tif"),
  streams = file.path(loc, "streams.tif"),
  output = file.path(loc, "flow_path_length.tif") 
)

flow_path <- rast(file.path(loc, "flow_path_length.tif"))

plot(flow_path)

Created on 2023-06-30 with reprex v2.0.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.