3

I want to port a gdal_translate into Python. It is about translating a GeoTIFF into a Cloud Optimized GeoTIFF (COG). I do not know how to insert the option -of and its input.

Here's is the equivalent CLI for that:

gdal_translate input.tif output_cog.tif -of COG -co COMPRESS=LZW

My current Python Code:

from osgeo import gdal

ds = gdal.Open('input.tif')
ds = gdal.Translate('output.tif', ds, options=["COMPRESS=LZW"])

How can I insert the input/argument -of COG into the Python code?

I got the code here: https://www.cogeo.org/developers-guide.html

2 Answers 2

6

You can simply add all your parameters as a string (like in the command line version of gdal_translate):

from osgeo import gdal
ds = gdal.Translate('output.tif', 'input.tif', options="-of GTiff -co COMPRESS=LZW")

I used GTiff format because there is no COG driver in my gdal 3.0.4 version. I used the name of the input instead loading into ds and overwriting with the output of gdal.Translate.

1
  • Thank you for the answer and tip. I will be checking my work next time.
    – Nikko
    Commented Nov 28, 2020 at 9:26
1
""" Create a TranslateOptions() object that can be passed to gdal.Translate()
    Keyword arguments are :
      options --- can be be an array of strings, a string or let empty and filled from other keywords.
      format --- output format ("GTiff", etc...)
      ...

So this is simply:

ds = gdal.Translate('output.tif', ds, format='COG', options=["COMPRESS=LZW"])

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.