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

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Nov 28, 2020 at 5:13

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.

answered Nov 28, 2020 at 9:05
1
  • Thank you for the answer and tip. I will be checking my work next time. 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"])
answered May 14, 2024 at 12:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.