QGIS Script · Python
Canopy Height Model Generator
A single Python script for QGIS that subtracts your DTM from your DSM to produce a Canopy Height Model — showing the height of every tree, structure, and surface feature above bare ground.
Handles resolution mismatches automatically and clamps negative artefacts to zero. The output is loaded straight into your QGIS project.
Note: The CHM output is a continuous height raster — each pixel is a raw height value in metres. To classify it into named bands (Ground, Low Vegetation, Tall Trees, etc.) as shown in the cover image, pair this with the CHM Height Classifier script.
What you get
generate_chm.py What it does
DSM − DTM subtraction
Computes height above ground at every pixel by subtracting bare-earth DTM from surface DSM.
Auto resolution matching
If DSM and DTM have different pixel sizes, the DTM is automatically resampled to match the DSM grid before subtraction.
Negative artefact clamping
Pixels where DTM > DSM (noise, edge effects) are clamped to zero so the CHM never shows negative heights.
Auto-loads in QGIS
The output CHM raster is added to your QGIS project immediately — no manual drag-and-drop.
How to use it
- 1
Load your DSM and DTM rasters into QGIS. Layer names must contain
dsmanddtm. - 2
Check (tick) both layers in the Layers panel.
- 3
Open Plugins → Python Console → Show Editor.
- 4
Paste the script and click Run.
Requirements
- QGIS 3.28 or newer (LTR recommended)
- numpy, rasterio — bundled with QGIS
- DSM raster — layer name contains "dsm"
- DTM raster — layer name contains "dtm"
Also available in the Terrain Analysis Pack
Get all 6 terrain scripts for $35 — save $15 vs buying individually.