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?
3 Answers 3
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)
-
1I 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!Renee Cammarere– Renee Cammarere2015年06月18日 15:03:57 +00:00Commented Jun 18, 2015 at 15:03
-
1I 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.Renee Cammarere– Renee Cammarere2015年06月22日 12:56:50 +00:00Commented Jun 22, 2015 at 12:56
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.
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