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