RichDEM Python for Beginners: Fast DEM Correction and Hydrology Tools
- Anvita Shrivastava
- 3 days ago
- 4 min read
Updated: 2 days ago
Digital Elevation Models (DEMs) are a vital component for performing terrain analyses, predicting river flows through watershed models, simulating floods, and helping scientists study hydrology. However, raw DEM data usually contain various types of errors—i.e., depressions, excavated areas, and flat areas—that interfere with the ongoing hydrology workflow.
RichDEM is a solution to these problems. It is a robust library that is written in Python and provides GIS professionals, researchers, and even beginners access to all necessary functionalities to perform efficient terrain processing, DEM correction, and hydrologic analysis using major files with high-resolution data.

What Is RichDEM?
The RichDEM library is an open-source terrain analysis software developed to utilize raster-based elevation datasets. The RichDEM library provides optimized algorithms for:
Filling depressions in DEMs
Removing sinks
Resolving flat areas
Calculating flow direction
Analyzing flow accumulation
Generating terrain attributes
RichDEM differs from many traditional GIS software packages in that it allows users to automate terrain analysis workflows through the use of Python scripts. This makes it ideal for processing large volumes of geospatial data.
Key Benefits of RichDEM
Very fast processing speed
Simple integration with Python code
Open-source, works on many different operating systems.
Supports commonly used raster data formats
Optimized hydrology algorithms
Can be used with large DEMs
Installing RichDEM
Before getting started, install RichDEM using pip:
pip install richdemYou may also need supporting geospatial libraries:
pip install rasterio numpy matplotlibVerify the installation:
import richdem as rd
print(rd.__version__)If no errors appear, RichDEM is ready to use.
Loading a DEM in Python
The first step is loading your elevation raster.
import richdem as rd
dem = rd.LoadGDAL("dem.tif")
print(dem)RichDEM automatically reads GeoTIFF files and stores elevation values in an efficient raster structure.
Understanding DEM Errors
Sinks/Depressions
Clumps of low-elevation cells surrounded by high elevations can trap flow.
Flats
Where the elevation of multiple cells matches, the flow direction may be unclear.
Data Artifacts
Defects introduced during the collection, interpolation, or rasterization processes.
If the above-mentioned issues are not corrected, they can create unrealistic results from hydrologic models.
Filling Depressions in DEMs
Depression filling is one of the most common preprocessing tasks.
RichDEM provides a simple function:
import richdem as rd
dem = rd.LoadGDAL("dem.tif")
rd.FillDepressions(dem, in_place=True)
rd.SaveGDAL("filled_dem.tif", dem)Why Fill Depressions?
Depression filling:
Creates continuous drainage networks
Improves flow routing accuracy
Prevents artificial water trapping
Enhances watershed delineation
This step is usually performed before any hydrological analysis.
Breaching Depressions
In some cases, breaching produces more realistic drainage patterns than filling.
rd.BreachDepressions(dem, in_place=True)Breaching cuts channels through barriers rather than raising terrain elevations.
Advantages include:
Preserving terrain characteristics
Maintaining realistic stream paths
Reducing elevation distortion
Generating Flow Directions
Flow direction determines how water moves across the landscape.
RichDEM supports the D8 flow direction algorithm:
flow_dir = rd.FlowDirD8(dem)The resulting raster indicates the direction water flows from each cell to its steepest downslope neighbor.
Applications include:
Watershed mapping
Flood modeling
Stream extraction
Erosion studies
Calculating Flow Accumulation
Flow accumulation measures how much upstream area contributes flow to a given cell.
flow_acc = rd.FlowAccumulation(flow_dir, method='D8')Cells with high accumulation values often represent:
Rivers
Streams
Drainage channels
Visualizing flow accumulation can reveal drainage networks hidden within DEM data.
Creating a Stream Network
A basic stream extraction workflow:
import numpy as np
streams = flow_acc > 1000The threshold value depends on:
DEM resolution
Watershed size
Project objectives
Higher thresholds create fewer major streams, while lower thresholds generate denser networks.
Calculating Terrain Attributes
RichDEM can also derive important terrain metrics.
Slope
slope = rd.TerrainAttribute(dem, attrib='slope_riserun')Aspect
aspect = rd.TerrainAttribute(dem, attrib='aspect')Curvature
curvature = rd.TerrainAttribute(dem, attrib='curvature')These attributes support:
Land suitability analysis
Erosion assessment
Environmental modeling
Hydrological studies
Visualizing DEM Results
Use Matplotlib to visualize outputs.
import matplotlib.pyplot as plt
plt.imshow(flow_acc, cmap='viridis')
plt.colorbar()
plt.title("Flow Accumulation")
plt.show()Visualization helps verify DEM corrections and hydrological outputs before further analysis.
Complete Hydrology Workflow Example
A typical RichDEM workflow looks like this:
import richdem as rd
# Load DEM
dem = rd.LoadGDAL("dem.tif")
# Fill depressions
rd.FillDepressions(dem, in_place=True)
# Generate flow direction
flow_dir = rd.FlowDirD8(dem)
# Calculate flow accumulation
flow_acc = rd.FlowAccumulation(flow_dir)
# Save results
rd.SaveGDAL("flow_accumulation.tif", flow_acc)This workflow prepares a DEM for watershed analysis, stream extraction, and flood modeling.
Performance Advantages of RichDEM
RichDEM is known for its efficient implementation of terrain algorithms.
Compared to many desktop GIS workflows, RichDEM offers:
Faster processing times
Python automation capabilities
Reduced manual effort
Better reproducibility
For large DEM datasets, these advantages can significantly reduce processing time.
RichDEM has proven to be one of the most efficient Python libraries that can be used for DEM corrections as well as performing hydrological analyses. Due to its ability to provide quick algorithms, easy-to-use API, and robust terrain-processing features, it is a great tool for GIS practitioners of all levels.
Whether you’re preparing elevation data to model a watershed, extract streams, or simulate flooding, RichDEM contains all of the tools necessary to transform raw DEMs into hydrologically accurate datasets.
By learning how to perform the four key processes of filling depressions, creating a flow direction, calculating flow accumulation, and creating terrain attributes, you will have developed a strong basis for performing more sophisticated geospatial and hydrological analyses using Python.
For more information or any questions regarding the LizardTech suite of products, please don't hesitate to contact us at:
Email: info@geowgs84.com
USA (HQ): (720) 702–4849
(A GeoWGS84 Corp Company)
