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.

  • We do this kind of task with Taudem integrated in Qgis : hydrology.usu.edu/taudem/taudem5 but nothing to do with R ... 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
    Jun 30, 2023 at 10:04

1 Answer 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.

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

loc <- tempfile()

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

#> 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()

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

  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"

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

  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"))


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.