LinuxCommandLibrary

gdal_contour

Generate contour lines from raster data

TLDR

Create a vector dataset with contour lines spread over an 100-meter [i]nterval while [a]ttributing the elevation property as "ele"

$ gdal_contour -a [ele] -i [100.0] [path/to/input.tif] [path/to/output.gpkg]
copy

Create a vector dataset with [p]olygons spread over an 100-meter [i]nterval
$ gdal_contour -i [100.0] -p [path/to/input.tif] [path/to/output.gpkg]
copy

SYNOPSIS

gdal_contour [-band <band>] [-a <attrname>] [-3d] [-inhibit_attr_settings] [-f <formatname>] [-i <interval>] [-off <offset>] [-fl <level_1>,<level_2>,...] [-flevels <filename>] [-nln <layername>] [-q] <input_dem> <output_contour>

PARAMETERS

-band <n>
    Select input raster band number <n> (default 1)

-a <attrname>
    Name of attribute field to store contour elevation (default 'elevation')

-3d
    Generate 3D contours by appending Z (elevation) to vertices

-inhibit_attr_settings
    Skip writing source dataset attribute settings to output

-f <formatname>
    Output format (e.g., 'ESRI Shapefile', default inferred from extension)

-i <interval>
    Contour interval (default calculated from data range)

-off <offset>
    Contour base offset (default data minimum)

-fl <list>
    Comma-separated list of fixed contour levels

-flevels <filename>
    File containing list of contour levels (one per line)

-nln <layername>
    Short name for output layer(s)

-q
    Quiet mode: suppress progress output

DESCRIPTION

gdal_contour is a command-line utility from the GDAL (Geospatial Data Abstraction Library) suite designed to generate vector contour lines from raster datasets, particularly digital elevation models (DEMs). It interpolates contour polylines at user-specified intervals or levels, attributing each with elevation values for analysis or visualization.

The tool processes input rasters in various formats (e.g., GeoTIFF, ASC), handling floating-point or integer data, nodata regions, and projections. Output is a vector file (e.g., shapefile, GeoPackage) with optional 3D geometries. Key features include customizable intervals, offsets, fixed contour levels, and quiet mode for scripting.

Common applications include topographic mapping, flood modeling, terrain analysis, and preparing data for GIS software like QGIS or ArcGIS. It leverages GDAL's raster engine for efficiency on large datasets, though very high-resolution rasters may require significant memory and time. Supports band selection for multi-band inputs and layer naming for multi-layer outputs.

Integration with OGR ensures broad output format support, making it versatile for geospatial workflows.

CAVEATS

Computationally intensive for large or high-resolution rasters; may require substantial RAM. Handles nodata but results depend on raster quality. Not suitable for non-elevation rasters without scaling.

EXAMPLE

gdal_contour -a ELEV -i 10.0 input_dem.tif contours.shp
Generates 10m interval contours with elevation in 'ELEV' attribute.

COMMON OUTPUT FLAGS

Use -f "GPKG" -co LAYER_CREATION=contours dem.tif contours.gpkg for GeoPackage with creation options.

HISTORY

Introduced in early GDAL releases (circa 2001) as part of core raster utilities. Evolved with GDAL versions, adding 3D support (GDAL 1.10+), fixed levels, and OGR integration for modern vector formats. Maintained by the open-source GDAL/OGR project.

SEE ALSO

gdal_grid(1), gdal_rasterize(1), ogr2ogr(1), gdal_translate(1)

Copied to clipboard