Generates contour lines or filled contour polygons from a single-band digital elevation model (DEM) raster.
Syntax
set of record ST_Contour(
raster rast,
integer band,
cstring contourOptions);Parameters
| Parameter | Type | Description |
|---|---|---|
rast | raster | The raster object. Must be a single-band DEM. |
band | integer | The band number. Bands are zero-indexed, starting at 0. |
contourOptions | cstring | A JSON string that controls how contours are generated. See the fields below. |
contourOptions fields
| Field | Type | Default | Description |
|---|---|---|---|
level_base | float | 0.0 | The base elevation from which contour intervals are calculated. |
fixed_level | float array | None | An explicit list of contour elevation values. When set to a non-empty array, interval is ignored. |
interval | float | None | The elevation difference between consecutive contour lines. Ignored when fixed_level is set. |
polygonize | bool | false | The output geometry type. false returns each contour as a closed curve. true fills the area between each pair of contours with DEM values and returns polygons. |
nodata | float | -1.0 | The nodata value. |
Return value
The function returns a set of records with the following fields:
| Field | Description |
|---|---|
id | The sequence number of the contour line or polygon. |
max_value | The maximum DEM elevation within the contour line or polygon. |
min_value | The minimum DEM elevation within the contour line or polygon. |
geom | The geometry object representing the contour line or polygon. |
Examples
Generate contour lines at a fixed interval
Returns a set of contour lines spaced 2 elevation units apart.
SELECT (ST_Contour(rast, 0, '{"interval":"2.0"}')).*
FROM raster_table
WHERE id = 1;Generate filled polygons at specific elevation levels
Returns polygons for the areas between the specified elevation thresholds.
SELECT (ST_Contour(rast, 0, '{"fixed_level":[1,5,10,15,20,30],"polygonize":"true"}')).*
FROM raster_table
WHERE id = 1;Generate filled polygons with a custom base and nodata value
Returns polygons starting from elevation 1.0, spaced 5 units apart, treating 0.0 as nodata.
SELECT (ST_Contour(rast, 0, '{"interval":"5","nodata":"0.0","level_base":"1.0","polygonize":"true"}')).*
FROM raster_table
WHERE id = 1;