Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is it possible to use some gdal API to call gdal_translate from Python code? I do not mean simply executing the gdal_translate.py from the file system, but rather call it somehow in code so I do not need to know the exact directory the gdal_translate.py is in?

share|improve this question

3 Answers 3

See the GDAL API Tutorial.

#Import gdal
from osgeo import gdal

#Open existing dataset
src_ds = gdal.Open( src_filename )

#Open output format driver, see gdal_translate --formats for list
format = "GTiff"
driver = gdal.GetDriverByName( format )

#Output to new format
dst_ds = driver.CreateCopy( dst_filename, src_ds, 0 )

#Properly close the datasets to flush to disk
dst_ds = None
src_ds = None

If you want more output control, such as resizing, subsetting, etc... use a VRT as input, this is how gdal_translate does it internally.

share|improve this answer
    
unfortunately this doesn't include reporjection, does it? –  butcher Jul 2 at 5:46
    
@butcher - no. Because the question did not mention reprojection. Of course you can reproject rasters with the gdal python API. If you want to know how, ask a new question. –  Luke Jul 2 at 7:00
    
I have done it already here: gis.stackexchange.com/questions/103874/… but thius was marked as a duplicate :-( –  butcher Jul 2 at 7:30
1  
@butcher - that was closed as a duplicate of this question. Your question also asks about gdal_translate. Are you aware that gdal_translate does not reproject? If you want to reproject, use gdalwarp or the gdal python API method - gdal.ReprojectImage –  Luke Jul 2 at 9:11

Yes you can call the GDAL Utilities from within Python. There are very minor differences in the approach depending on whether the utility is an exe in its own right or also a piece of python code. Either way though you need to use the subprocess module:

import subprocess

# constants
gdalTranslate = r'C:\Program Files\GDAL\gdal_translate.exe'
src = r"C:\somefolder\somefile.tif"
dst = r"C:\someotherfolder\myresul.tif"
cmd = "-ot float32 -outsize 25 25"  # just for example!

# see note below
def youCanQuoteMe(item):
    return "\"" + item + "\""

fullCmd = ' '.join([gdalTranslate, cmd, youCanQuoteMe(src), youCanQuoteMe(dst)])
subprocess.popen(fullCmd)

You will notice that I add escaped quotation marks around my paths. This is because, on Windows, I have had trouble with paths, especially ones with spaces or where one of the '\' characters makes another accidental escaped character. So, I just preserve the proper path in aspec as it were.

If you are using one of the python utilities, just do the same thing except your exe at the start of the subprocess command string is now "C:\python32\python.exe" (or whichever version you have) and your second element is the python utility you want to use.

Obviously you can also iterate over your file system rather than using hard-coded constants, but this is just an example.

EDIT - Generalizing for QGIS plugins
QGIS creates/modifies a number of environment variables at start up. So, you can build generalised path variables to the GDAL libraries/utilities using these (see Settings->Options->System) instead of the hard-coded paths in the example above.

share|improve this answer
    
So I can't do this? import gdal_translate and then call the .main() ? –  Katie E. Nov 29 '12 at 17:06
    
No - that won't work. gdal_translate is not a Python package, so python won't know anything about it. You will get an error saying "ImportError No Module named gdal_translate". Use the subprocess module to call it instead. –  MappaGnosis Nov 29 '12 at 17:37
    
ok one similiar questions with using gdal_retile.py.. I tried to do the following: import gdal_retile gdal_retile.main("-v -r bilinear -levels 4 -ps 2048 2048 -co \"tiled=YES\" -targetDir cablepyramid --optfile files.txt") but I get the error: Unrecognised command option: - Any idea why? –  Katie E. Nov 29 '12 at 20:01
    
Offhand I can't see the issue except my guess is that it might not like the '--optfile' switch. The latter is not documented. –  MappaGnosis Nov 30 '12 at 9:25
    
@MappaGnosis Is there an alternative of gdal_translate within Python gdal library? –  Ardit Sulce Sep 23 '13 at 12:43

I do this with various gdal commands using os.system which you can use to call functions just as from command line:

os.system("gdal_translate -of GTiff " + sourcefile + " " +  destinationfile)

It's also described in lecture 7 here: http://www.gis.usu.edu/~chrisg/python/2009/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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