Generate Smooth Contour Lines from a Drone DTM in QGIS

QGIStutorialcontoursdrone mappingterrainPython

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:

  1. Gaussian smoothing — applies a Gaussian filter to the DTM raster to remove the triangular mesh artefact
  2. Contour generation — runs QGIS’s built-in contour algorithm at your chosen interval
  3. 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.py script from the Terrain Analysis Pack

Steps

  1. Load your DTM raster into QGIS. The layer name must contain “dtm”.
  2. Check (tick) the DTM in the Layers panel.
  3. Open Plugins → Python Console → Show Editor.
  4. Paste the script. At the bottom, find the configurable parameters:
    • interval — contour spacing in metres (e.g. 2 for 2 m contours)
    • major_interval — every Nth contour is drawn thick and labelled (e.g. 5 for every 5th)
    • sigma — Gaussian smoothing strength (default: 1.0; increase for very noisy DTMs)
  5. 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 typeSuggested interval
Flat to gently sloping0.5–1 m
Undulating plantation terrain1–2 m
Hilly or steep terrain5–10 m
Mountain survey10–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.