2

I'm trying to update a python script using gdal, moving from a subprocess to the actual python bindings but finding the documentation a bit lacking.

My original sub process -

cmd = f"RASTER_PROJ=$(gdalsrsinfo -o wkt_esri --single-line {source_raster}) &&" \
      f"gdal_translate -of {gdal_output} -co COMPRESS=LERC -co QUALITY=75 -co BLOCKSIZE=512 -a_srs \"$RASTER_PROJ\" {source_raster} {destination_raster} && " \
      f"gdaladdo -r average --config COMPRESS_OVERVIEW LERC {destination_raster} 2"
subprocess.call(cmd, shell=True)

My python excerpt -

#translate
translate_options = gdal.TranslateOptions(gdal.ParseCommandLine(f"-of {gdal_output} -co COMPRESS={compression} -co QUALITY=75 -co BLOCKSIZE=512"))
gdal.Translate(destination_raster, source_raster, options=translate_options)

#addo
image = gdal.Open(destination_raster, 1)
gdal.SetConfigOption('COMPRESS_OVERVIEW LERC', 'DEFLATE')
image.BuildOverviews('AVERAGE', [2], gdal.TermProgress_nocb)
del image 

This works but ideally I would be creating the overviews the same way as the translate or alternatively setting the image variable first and inputting into the translate function, however I haven't been able to do either without errors.

I've found no documentation on the bindings for BuildOverviews or Addo so am just cobbling things together from SE currently. Is there a better way?

1

1 Answer 1

4

There is no Pythonized version of the gdaladdo utility. From the source code of gdaladdo https://github.com/OSGeo/gdal/blob/master/apps/gdaladdo.cpp it can be seen that the code mainly reads the config options and executes GDALBuildOverviews.

if( nLevelCount > 0 &&
GDALBuildOverviews(hDataset,pszResampling, nLevelCount, anLevels,
                   nBandCount, panBandList, pfnProgress,
                   nullptr) != CE_None)

{

BuildOverviews is documented in the GDAL Python API documentation within gdal.Dataset https://gdal.org/python/osgeo.gdal.Dataset-class.html as https://gdal.org/python/osgeo.gdal.Dataset-class.html#BuildOverviews

Some Python examples can be found from the GDAL autotests. The best test to look is probably the one that is testing the GeoTIFF overviews https://github.com/OSGeo/gdal/blob/master/autotest/gcore/tiff_ovr.py.

For me it seems that you are doing the things right. One option to avoid the separate buildOverviews step could be to use the COG driver https://gdal.org/drivers/raster/cog.html instead of GeoTIFF because COG generates the overviews by the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.