GDALRaster Processing

gdaldem

What is gdaldem?

gdaldem is the GDAL command-line utility for terrain analysis on digital elevation models. It has several modes — hillshade, slope, aspect, color-relief, TRI, TPI, roughness — each generating a derived raster from a single-band elevation input. Under the hood each mode applies a 3×3 kernel and a mode-specific formula (Horn or Zevenbergen–Thorne for slope/aspect, Lambertian illumination for hillshade), making it the workhorse for cartographic and quantitative terrain products.

Shell
gdaldem <mode> [options] <input_dem> <output_raster>

Common modes and key options:

  • hillshade-z <factor> vertical exaggeration, -az <azimuth> sun azimuth (deg, default 315), -alt <altitude> sun altitude (deg, default 45), -multidirectional, -combined, -igor
  • slope-p percent slope instead of degrees, -s <scale> vertical-to-horizontal unit ratio
  • aspect-trigonometric (0 east counter-clockwise), -zero_for_flat
  • color-relief — takes a colour ramp text file, -alpha to add an alpha band, -nearest_color_entry or -exact_color_entry to control interpolation
  • TRI / TPI / roughness — no extra parameters; compute Terrain Ruggedness Index, Topographic Position Index, or roughness
  • Global: -of <format>, -co <NAME>=<VALUE>, -compute_edges, -b <band>, -alg Horn|ZevenbergenThorne

When would you use gdaldem?

Reach for gdaldem on any DEM-driven cartography or analysis. Typical jobs: producing a cartographic hillshade for a base map (gdaldem hillshade -z 1.5 -multidirectional dem.tif hs.tif), computing slope in degrees for avalanche risk or landslide modelling (gdaldem slope dem.tif slope.tif), producing an aspect raster for solar exposure analysis, or colourising a DEM with a hypsometric ramp for quick visualisation (gdaldem color-relief dem.tif ramp.txt colored.tif).

-multidirectional and -combined hillshade modes are worth the extra compute on terrain with complex features; they reduce the "dark side" artefact of single-azimuth shading and produce more legible relief maps. For mixed-unit DEMs (vertical units in metres but horizontal units in degrees), pass -s 111120 to scale correctly.

FAQs

Why does my slope raster look all zeros or all maximum?

Usually a unit mismatch. If the DEM has metres vertically but degrees horizontally (common with raw WGS84 DEMs), you must set -s 111120 to convert degrees of latitude to metres. Alternatively, reproject the DEM to a metric CRS with gdalwarp -t_srs EPSG:3857 before running gdaldem slope.

What format does the color-relief ramp file use?

A plain-text file with one line per break: <elevation> <R> <G> <B> [<A>]. Use nv instead of a number for the NoData colour. Add % to express elevations as percentages of the data range. GDAL linearly interpolates between entries unless you pass -nearest_color_entry or -exact_color_entry.

Horn vs Zevenbergen–Thorne — which algorithm?

Horn (default) is the de facto standard and is well-behaved on noisy DEMs. Zevenbergen–Thorne is mathematically more precise on smooth surfaces but amplifies DEM noise. For LiDAR-derived DEMs with real micro-topography use Horn; for synthetic or heavily smoothed surfaces, Zevenbergen–Thorne can be slightly sharper. Most cartographic workflows use Horn and never look back.

How do I handle edges without black borders?

Pass -compute_edges. Without it, border pixels cannot fill their 3×3 kernel and are set to NoData, producing a visible dark border one pixel thick. -compute_edges uses a reflected kernel at the edges and eliminates the artefact.