3

I downloaded and installed GDAL. I am using Python 27. I had to use the following commands at the IDLE Python GUI:

from osgeo import gdal,ogr
sys.path.append('C:\Program Files\GDAL')
import gdal_merge

I actually have C:\Program Files\GDAL listed first in my Path environmental system variables, so I don't know why I need to do the above. Anyway, when I try to use this utility, I don't get an error, but it only opens the Python script for editing, and doesn't run the script. I know that the documentation for gdal_merge states "NOTE: gdal_merge.py is a Python script, and will only work if GDAL was built with Python support." I did install GDAL with Python support, and can get some utilities to run correctly (like gdalwarp.exe) but have trouble running the python scripts.

How can I find out for sure if my installation of GDAL has Python support?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 18, 2015 at 14:29

3 Answers 3

5

If you can do from osgeo import gdal,ogr your GDAL was built with the Python bindings otherwise you wouldn't be able to import it.

If you want to start a GDAL utility (gdalwarp, gdal_translate, gdal_merge) from within Python your best bet is to use Pythons subprocess module as these utilities are spawned from the command line.

An example:

import subprocess
merge_command = ["python", "gdal_merge.py", "-o", "output.tif", "input1.tif", "input2.tif", "input3.tif", "inputN.tif"]
subprocess.call(merge_command)
answered Jun 18, 2015 at 14:52
2
  • 1
    I didn't get an error message inside the Python Shell, but it doesn't appear to have done anything when I typed in: merge_command = ["python", "gdal_merge.py", "-o", "C:\output.tif", "C:\N31.DT1", "C:\N30.DT1"] A command window flashed when I typed in subprocess.call(merge_command) and then displayed a number 2, but didn't create the output file. Did I do something wrong? Thanks! Commented Jun 18, 2015 at 15:03
  • 1
    I know that any number other than a 0 returned from calling the merge command means that there was an error, but I don't know what it's having trouble with. Commented Jun 22, 2015 at 12:56
2

I think your problem is that the Python path and/or the GDAL paths are not set. If you do not need to run gdal_merge.py from a custom python script, you can run it from Windows Command line (cmd.exe):

Open the cmd.exe and insert the following line and you should get the usage of gdal_merge.py:

C:\Python27\python.exe C:\Python27\Scripts\gdal_merge.py

Maybe you have to adopt the paths to your Python Installation.

answered Oct 13, 2017 at 14:26
0
1

Or you can use %%bash. Something like that:

%%bash
gdal_merge.py -separate -of HFA -co COMPRESSED=YES -o output.img input1.TIF input2.TIF 
answered Aug 31, 2017 at 15:43

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.