I am working on generating contour polygons with gdal in python. I have a working script, which utilizes the osgeo.gdal.ContourGenerateEx
function from the gdal
library. However, I am having trouble finding documentation on this function. So far, I've been referencing the following:
- GDAL Python API (https://gdal.org/api/python/osgeo.gdal.html#osgeo.gdal.ContourGenerateEx)
- GDAL Autotest (https://github.com/OSGeo/gdal/blob/master/autotest/alg/contour.py)
On the GDAL Autotest script, lines 233 through 259 are:
def test_contour_3(input_tif, tmp_path):
output_shp = str(tmp_path / "contour.shp")
ogr_ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(output_shp)
ogr_lyr = ogr_ds.CreateLayer("contour", geom_type=ogr.wkbMultiPolygon)
field_defn = ogr.FieldDefn("ID", ogr.OFTInteger)
ogr_lyr.CreateField(field_defn)
field_defn = ogr.FieldDefn("elevMin", ogr.OFTReal)
ogr_lyr.CreateField(field_defn)
field_defn = ogr.FieldDefn("elevMax", ogr.OFTReal)
ogr_lyr.CreateField(field_defn)
ds = gdal.Open(input_tif)
# gdal.ContourGenerateEx(ds.GetRasterBand(1), 0, 0, 0, [10, 20, 25], 0, 0, ogr_lyr, 0, 1, 1)
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=[
"FIXED_LEVELS=10,20,25",
"ID_FIELD=0",
"ELEV_FIELD_MIN=1",
"ELEV_FIELD_MAX=2",
"POLYGONIZE=TRUE",
],
)
ds = None
Specifically, lines 248 through 257 show:
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=[
"FIXED_LEVELS=10,20,25",
"ID_FIELD=0",
"ELEV_FIELD_MIN=1",
"ELEV_FIELD_MAX=2",
"POLYGONIZE=TRUE",
],
I am interested in the "options" parameter of the gdal.ContourGenerateEx
function. For instance, could I somehow specify a regular interval with an initial offset? In general, what options are available in this function? Is there a helper function to describe this?
Obviously the GDAL Autotest script describes options for fixed levels, ID field, elevation fields, and polygonize, but I am wondering what other options are available.
Here is my script for reference:
# import libraries
from osgeo import ogr
from osgeo import gdal
from osgeo import osr
# define input files
input_dem = "input path"
# define output files
output_gpkg = "output path"
# define output geopackage layer name
output_gpkg_layer = 'contour_polygons2'
# create raster data source
ds_dem = gdal.Open(input_dem)
# create spatial reference system
srs_26910 = osr.SpatialReference()
# define spatial reference system
srs_26910.ImportFromEPSG(26910)
# create geopackage driver
driver_gpkg = ogr.GetDriverByName('GPKG')
# create geopackage data source
ds_gpkg = driver_gpkg.CreateDataSource(output_gpkg)
# create geopackage layer
layer_contour1 = ds_gpkg.CreateLayer(output_gpkg_layer, geom_type=ogr.wkbMultiPolygon, srs=srs_26910)
# create field definitions
field_definition_elevMin = ogr.FieldDefn("elevMin", ogr.OFTReal)
field_definition_elevMax = ogr.FieldDefn("elevMax", ogr.OFTReal)
# create fields in layer
layer_contour1.CreateField(field_definition_elevMin)
layer_contour1.CreateField(field_definition_elevMax)
gdal.ContourGenerateEx(
ds_dem.GetRasterBand(1),
layer_contour1,
options=[
"FIXED_LEVELS=10,20,25",
"ID_FIELD=-1",
"ELEV_FIELD_MIN=0",
"ELEV_FIELD_MAX=1",
"POLYGONIZE=TRUE",
],
)
# close data sources and layers
layer_contour1 = None
ds_gpkg = None
ds_dem = None