Raw contour lines generated from a drone DTM usually look jagged and triangulated — a consequence of the mesh structure in photogrammetry outputs. This script smooths the raster first, then smooths the lines, to produce clean cartographic-quality contours ready for client maps.
What it does
The generate_contours.py script runs a three-stage process:
- Gaussian smoothing — applies a Gaussian filter to the DTM raster to remove the triangular mesh artefact
- Contour generation — runs QGIS’s built-in contour algorithm at your chosen interval
- Chaikin’s algorithm — smooths the resulting contour lines by iterative corner-cutting, removing the stepped appearance
Major contours (every Nth line) are styled thicker and labelled with their elevation in metres. The output is a single GeoJSON file added to your project.
What you need
- QGIS 3.28 or newer
numpy,scipy,osgeo.gdal(bundled with QGIS)- A DTM raster with “dtm” in the layer name, checked in the Layers panel
- The
generate_contours.pyscript from the Terrain Analysis Pack
Steps
- Load your DTM raster into QGIS. The layer name must contain “dtm”.
- Check (tick) the DTM in the Layers panel.
- Open Plugins → Python Console → Show Editor.
- Paste the script. At the bottom, find the configurable parameters:
interval— contour spacing in metres (e.g.2for 2 m contours)major_interval— every Nth contour is drawn thick and labelled (e.g.5for every 5th)sigma— Gaussian smoothing strength (default:1.0; increase for very noisy DTMs)
- Click Run.
The output {dtm_name}_contours_{interval}m.geojson is saved in the DTM directory and added to your project with major/minor styling applied.
Choosing the right interval
| Terrain type | Suggested interval |
|---|---|
| Flat to gently sloping | 0.5–1 m |
| Undulating plantation terrain | 1–2 m |
| Hilly or steep terrain | 5–10 m |
| Mountain survey | 10–25 m |
Too small an interval on flat terrain produces dense, overlapping contours. Too large an interval on steep terrain misses detail. Start with 2 m and adjust.
Get the script
generate_contours.py is part of the Terrain Analysis Pack — five terrain analysis scripts for QGIS at $35 USD.