Generates contour lines or filled polygons from a digital elevation model (DEM) raster band.
Syntax
set of record ST_Contour(
raster rast,
integer band,
cstring contourOptions);Parameters
| Parameter | Description |
|---|---|
rast | The raster object. Must be a DEM with exactly one band. |
band | The band number. Starts from 0. |
contourOptions | A JSON string that controls contour generation. See the fields below. |
contourOptions fields
| Field | Type | Default | Description |
|---|---|---|---|
level_base | float | 0.0 | The starting offset for contour values. For example, with level_base=5 and interval=10, contours are generated at 5, 15, 25, and so on. |
fixed_level | float array | None | An explicit list of contour values (e.g., [100, 200, 300]). When specified, interval is ignored. |
interval | float | None | The spacing between contours. Used only when fixed_level is not specified. |
polygonize | bool | false | The output type. false returns each contour as a closed curve. true fills the area between adjacent contours with DEM values and returns polygons. |
nodata | float | -1.0 | The nodata value. |
Description
ST_Contour processes the specified raster band and returns a set of records — one per contour line or filled polygon.
Choosing between `fixed_level` and `interval`: Use fixed_level to generate contours at exact elevations (e.g., [100, 200, 500]). Use interval for evenly spaced contours across the full elevation range. If both are specified, fixed_level takes precedence and interval is ignored.
Each returned record contains the following fields:
| Field | Type | Description |
|---|---|---|
id | integer | The sequence number of the contour line or polygon. |
min_value | float | The minimum DEM value in the contour line or polygon. For a DEM input, this is the lower elevation bound of that contour band. |
max_value | float | The maximum DEM value in the contour line or polygon. For a DEM input, this is the upper elevation bound of that contour band. |
geom | geometry | The geometry of the contour line or polygon. |
Examples
Generate contour lines at a fixed interval
SELECT (ST_Contour(rast, 0, '{"interval": "2.0"}')).*
FROM raster_table
WHERE id = 1;Generate filled polygons at specific elevations
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
SELECT (ST_Contour(rast, 0, '{
"interval": "5",
"level_base": "1.0",
"nodata": "0.0",
"polygonize": "true"
}')).*
FROM raster_table
WHERE id = 1;