Generates contour lines or filled polygons from a single-band digital elevation model (DEM) raster.
Syntax
set of record ST_Contour(
raster rast,
integer band,
cstring contourOptions);Parameters
| Parameter | Description |
|---|---|
rast | The raster object. Must be a single-band DEM. |
band | The band index. Starts from 0. |
contourOptions | A JSON string that controls contour generation. See the following table for supported fields. |
contourOptions fields
| Field | Type | Default | Description |
|---|---|---|---|
level_base | float | 0.0 | The starting elevation for contour generation. |
fixed_level | float array | None | An array of specific elevation values at which to generate contours. When specified, interval is ignored. |
interval | float | None | The elevation difference between consecutive contour lines. |
polygonize | boolean | false | The output type. false: returns each contour as a closed curve. true: fills the area between consecutive contours with DEM values and returns a polygon. |
nodata | float | -1.0 | The nodata value. |
Description
ST_Contour generates contours from a raster object based on the specified band. The input raster must be a single-band DEM.
Parameter priority: If fixed_level is specified, interval is ignored. Use fixed_level when you need contours at specific elevation values; use interval for evenly spaced contours.
The function returns a set of records. Each record contains the following fields:
| Field | Description |
|---|---|
id | The sequence number of the contour line or polygon. |
min_value | The minimum value in the contour line or polygon. |
max_value | The maximum value in 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 closed contour lines, one for every 2.0 units of elevation.
select (ST_Contour(rast,0,'{"interval":"2.0"}')).* from raster_table where id =1;Generate filled polygons at specific elevation levels
Returns polygons that fill the areas between the specified elevation thresholds: 1, 5, 10, 15, 20, and 30. Each record includes the min_value and max_value of its elevation band.
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 handling
Returns polygons at 5-unit intervals starting from elevation 1.0, excluding pixels with a nodata value of 0.0.
select (ST_Contour(rast,0,'{"interval":"5","nodata":"0.0","level_base":"1.0","polygonize":"true"}')).* from raster_table where id =1;