avatarAbdishakur

Summary

This context provides a tutorial on how to read, visualize, and analyze Digital Elevation Models (DEM) in Python using RasterIO and RichDEM packages.

Abstract

The context begins by explaining what Digital Elevation Models (DEM) are and their applications, such as relief maps, modeling water flow and hydrology, surface analysis, and precision farming. It then proceeds to demonstrate how to read and visualize DEM files using RasterIO, a Python library for handling geospatial raster data. The tutorial uses the Copernicus DEM dataset with a 30-meter resolution. The visualization includes both the DEM and its contours. The context then introduces the RichDEM package, which is specifically designed for handling elevation data. It shows how to convert the data to RichDEM arrays and create DEM-derived visualizations, including slope, aspect, and curvature. The context concludes by mentioning other functionalities of the RichDEM package, such as filling depressions in the DEM.

Bullet points

  • Digital Elevation Models (DEM) are 3D representations of the earth’s terrain used in various applications.
  • RasterIO is a Python library used for reading and visualizing DEM files.
  • The tutorial uses the Copernicus DEM dataset with a 30-meter resolution.
  • Visualization includes both the DEM and its contours.
  • RichDEM is a package specifically designed for handling elevation data.
  • It allows for the creation of DEM-derived visualizations, including slope, aspect, and curvature.
  • RichDEM also allows for the modification of DEMs, such as filling depressions.

Digital Elevation Model (DEM) in Python

Reading, Visualizing and Analysing DEM in Python

Photo by Pierpaolo Lanfrancotti on Unsplash

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 plt
dem = 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.

DEM Visualization with RasterIO

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.

DEM with Contour Visualization.

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
DEM visualization with RichDEM Package.

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));
Slope Visualization from DEM.

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))
Depression filled DEM

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.

Earth Observation
Python
Remote Sensing
Rasterio
Tutorial
Recommended from ReadMedium