Extract river/stream centerlines from a DEM file and output as shapefile. Two approaches available:
| QGIS/GRASS (recommended) | WhiteboxTools | |
|---|---|---|
| Script | extract_river_qgis.bat |
extract_river_whitebox.bat |
| Algorithm | MFD (Multi-Flow Direction) | D8 (Single-Flow Direction) |
| Depression handling | A* priority-flood | Simple fill |
| Stream quality | Better (more complete network) | Good (may miss small branches) |
| Dependency | QGIS installed | WhiteboxTools + Python rasterio |
| Threshold default | 100 (watershed basin size) | auto (1% of max accumulation) |
# Requires: QGIS installed (winget install OSGeo.QGIS_LTR)
extract_river_qgis.bat D:\data\dem.tif D:\data\river.shp
extract_river_qgis.bat D:\data\dem.tif D:\data\river.shp 100Results are identical to running GRASS r.watershed + r.to.vect in QGIS GUI.
# Requires: Python 3 + rasterio, WhiteboxTools binary
extract_river_whitebox.bat D:\data\dem.tif D:\data\river.shp
extract_river_whitebox.bat D:\data\dem.tif D:\data\river.shp 500| Parameter | Required | Description |
|---|---|---|
dem.tif |
Yes | Input DEM raster file |
output.shp |
Yes | Output shapefile path |
threshold |
No | Stream extraction threshold (see below) |
- QGIS method: min watershed basin size in cells. Default: 100
- WhiteboxTools method: flow accumulation cell count. Default: auto (1% of max)
Higher threshold = fewer streams (main rivers only). Lower = more tributaries.
For QGIS method:
winget install OSGeo.QGIS_LTRFor WhiteboxTools method:
pip install rasterio
# Download WhiteboxTools from https://www.whiteboxgeo.com/download-whiteboxtools/
# Extract so that whitebox_tools.exe is at:
# WhiteboxTools_win_amd64/WBT/whitebox_tools.exeCopy config.example.txt to config.txt and set your paths:
# Path to qgis_process
QGIS_PROCESS=D:\QGIS 3.44.9\bin\qgis_process-qgis-ltr.bat
# Path to whitebox_tools.exe
WHITEBOX_TOOLS=D:\tools\WhiteboxTools\whitebox_tools.exe
# Default threshold (overridden by command-line argument)
# DEFAULT_THRESHOLD=100If config.txt is not set, scripts will auto-detect installed tools.
- GRASS r.watershed — A* priority-flood + MFD flow accumulation + stream extraction
- GRASS r.to.vect — Convert stream raster to polyline shapefile with smoothing
- Convert — Re-encode DEM to compatible GeoTIFF (removes PREDICTOR=3)
- Fill Depressions — Remove spurious sinks
- D8 Flow Direction — Compute flow direction per pixel
- D8 Flow Accumulation — Count upstream contributing cells
- Extract Streams + Vectorize — Threshold and convert to polyline shapefile
- GRASS GIS via QGIS
qgis_process - WhiteboxTools by Prof. John Lindsay (MIT License)
- rasterio for GeoTIFF I/O