QGIS Script · Python
Tree Height Extractor
Adds a height field (DSM − DTM in metres) to every feature in a point or polygon layer. Works on both geometry types, handles CRS mismatches automatically, and saves the result as a new GeoJSON loaded into the QGIS project.
What you get
tree_heights_from_dsm_dtm.pyWhat it does
Point and polygon support
For point features: samples DSM and DTM at the exact point location. For polygon features: max DSM − min DTM in the bounding box.
height field added
Writes a new "height" field (DSM − DTM in metres) to every feature in the output GeoJSON.
Automatic CRS alignment
If the vector layer and rasters are in different coordinate reference systems, the vector is reprojected automatically before sampling.
No point cloud required
Extracts height directly from DSM and DTM rasters — no need to re-process the original point cloud.
How to use it
- 1
Load your DSM and DTM rasters. Layer names must contain
_dsmand_dtm. - 2
Load your tree point or polygon layer and click it to make it the active layer.
- 3
Check (tick) the DSM and DTM rasters in the Layers panel.
- 4
Open Plugins → Python Console → Show Editor.
- 5
Paste the script and click Run.
Requirements
- QGIS 3.28 or newer
- numpy, rasterio — bundled with QGIS
- Active point or polygon layer (tree annotations)
- DSM — layer name contains "_dsm"
- DTM — layer name contains "_dtm"
Also in the Vegetation & Field Analysis Pack
Get all 6 vegetation scripts for $40 — save $20 vs buying individually.