Digital Elevation Model (DEM) in Python
Reading, Visualizing and Analysing DEM in Python
Digital Elevation Models (DEM) is a 3D representation of the earth’s terrain. We use DEM most often for relief maps but are also helpful in many applications, including modeling the water flow and hydrology, surface analysis, and precision farming.
This blog post teaches us how to effectively read, modify, and analyze DEMs in Python using RasterIO and RichDEM packages.
Reading and Visualizing DEM with RasterIO
Let us read a DEM file with RasterIO. This tutorial uses the Copernicus DEM dataset with a 30-meter resolution. Feel free to use any other DEM dataset of your choice.
import numpy as np
import rasterio as rio
from rasterio.plot import show
import matplotlib.pyplot as pltdem = rio.open("data/DEM.tiff")
dem_array = dem.read(1).astype('float64')We can directly visualize the DEM in Rasterio using the show() method.
fig, ax = plt.subplots(1, figsize=(12, 12))
show(dem_array, cmap=’Greys_r’, ax=ax)
plt.axis(“off”)
plt.show()The output is the DEM shown below.

We can also visualize the contours from the DEM and overlay the original DEM using the same show() method from rasterio.
fig, ax = plt.subplots(1, figsize=(12, 12))
show(dem_array, cmap='Greys_r', ax=ax)
show(dem_array, contour=True, ax=ax, linewidths=0.7)
plt.axis("off")
plt.show()So now the output is the following DEM with contours.

Rasterio is an excellent package for many tasks, including reading and writing raster data. Since we are dealing with DEMs, we use the RichDEM package that deals explicitly with Elevation data.
Modifying & Analysing DEM with RichDEM
To use the many functionalities in the RichDEM package, we need to convert the data to RichDEM arrays.
import richdem as rd
dem_richdem = rd.rdarray(dem_array, no_data=-9999)Now that we have RichDEM arrays, we can analyze and create other DEM-derived visualizations, including Slope, aspect, and curvature. Before we do that, we can also visualize the DEM with RichDEM package like this.
fig = rd.rdShow(dem_richdem, axes=False, cmap=’bone’, figsize=(16, 10));
fig
With TerrainAttribute, we can create DEM slope, aspect, and curvature attributes. Let us see how to develop slopes from DEM with RichDEM.
dem_slope = rd.TerrainAttribute(dem_richdem, attrib=’slope_degrees’)
rd.rdShow(dem_slope, axes=False, cmap=’YlOrBr’, figsize=(16, 10));
You can also create curvature, aspect, and some other available methods passing the attrib parameter.
You can also modify DEMs using the RichDEM package. For example, you can fill depressions to remove the flat areas and ensure continuous flow in flow-path analysis.
dem_filled = rd.FillDepressions(dem_richdem, in_place=False)
dem_filled_fig = rd.rdShow(dem_filled, ignore_colours=[0], axes=False, cmap=’jet’, vmin=fig[‘vmin’], vmax=fig[‘vmax’], figsize=(16,10))
Conclusion
Digital Elevation Models are an integral part of remote sensing applications such as 3D terrain visualization. In this blog post, we have seen how to read, analyze and modify DEM data in Python using RasterIO and RichDEM Packages.






