Python GDAL Configuration – Using `–config` Flag with Python GDAL `gdal.Translate()`

gdalpython

So I've recently learned that GDAL and OGR command line utilities can be used as functions now. See this site for more info. For example:

from osgeo import gdal
tmp_ds = gdal.Warp('temp', 'in.tif', format = 'MEM', dstSRS = 'EPSG:3857')
gdal.Translate('out.png', tmp_ds, format = 'PNG')

This works amazing! But one thing I can't figure out is how to pass in the –config flag. In my case, I am using gdal_translate to convert a US Topo GeoPDF to GeoTiff, exporting out certain PDF layers:

gdal_translate -of “GTiff” --config GDAL_PDF_LAYER “Map_Collar,Map_Frame.Terrain” ustopo.pdf ustopo_terrain.tif 

I've tried adding to the gdal.Translate in 2 ways:

gdal.Translate('out.tif', tmp_ds, format = 'GTiff',options='--config GDAL_PDF_LAYER “Map_Collar,Map_Frame.Terrain”')

and

gdal.Translate('out.tif', tmp_ds, format = 'GTiff',config='GDAL_PDF_LAYER “Map_Collar,Map_Frame.Terrain”')

Neither work. I am totally stumped.

Best Answer

I would post it in comment, but can't yet. Anyway, when I am working with GDAL i usually use this:

gdal.SetConfigOption('COMPRESS_OVERVIEW', 'LZW')

which basically is

gdal.SetConfigOption(option, settings)

or when you are creating a file driver you can set options like this:

        format = "GTiff"
        driver = gdal.GetDriverByName(format)
        dst_ds = driver.Create(out_file, xsize, ysize, 1, gdal.GDT_Int32, creationOptions=['COMPRESS=LZW', 'TFW=YES'])

another thing could be this (i am not really sure about the correct syntax for GDAL PDF LAYER:

co = ['GDAL_PDF_LAYER']
gdal.Translate('out.tif', tmp_ds, format = 'GTiff',options=co)
Related Question