[GIS] Using gdal functions from OSGeo4W out of their shell

arcpygdalmodelbuilderosgeo4w

I would like to use some gdal functions from OSGeo4W (gdal2xyz, and gdal_merge) for my work. I can use them in the OsGeo4W shell provided after installing the package. But I want to use them in Pythonout of the OSGeo4W shell. I have Python2.7, gdal, gdal-python binding installed (all for 64 bit). However, I don't have a clue on how to use those gdal functions out of their shell.

The reason I want to do this is that I have a module created from ArcGIS ModelBuilder which I exported as a Python script. With this script, I want to use gdal2xyz, gdal_merge, etc. to continue data process.

Best Answer

I rarely use the OSGEO4W shell, although I have it because I used it to download some utilities. If you have QGIS you have most of the GDAL tools in the QGIS install directory already. If you prefer you can download 32 and 64 bit GDAL/OGR from http://www.gisinternals.com/sdk/ I did because I wanted the lib and include files but find that version works well with the utilities as well.

In order to use GDAL/OGR on the command line without invoking the shell you must set some environment variables so that required files can be found; I use windows so the environments are windows flavored but I assume that Mac and Linux are similar.

// GDAL folders from http://www.gisinternals.com/sdk/
GDAL_DATA        C:\Program Files\GDAL\gdal-data   // csv's and stuff
GDAL_DRIVER_PATH C:\Program Files\GDAL\gdalplugins // gdal_ECW_JP2ECW.dll, gdal_HDF6.dll...
GEOTIFF_CSV      C:\Program Files\GDAL\gdal-data   // csv's and stuff
PROJ_LIB         C:\Program Files\GDAL\projlib     // nad.lst...
PATH             %PATH%;C:\Program Files\GDAL      // GDAL_TRANSLATE.exe ...

// QGIS folders
GDAL_DATA        C:\Program Files\QGIS Dufour\share\gdal      // csv's and stuff
GDAL_DRIVER_PATH C:\Program Files\QGIS Dufour\bin\gdalplugins // gdal_ECW_JP2ECW.dll, gdal_HDF6.dll...
GEOTIFF_CSV      C:\Program Files\QGIS Dufour\share\epsg_csv  // csv's and stuff
PROJ_LIB         C:\Program Files\QGIS Dufour\share\proj      // nad.lst...
PATH             %PATH%;C:\Program Files\QGIS Dufour\bin      // GDAL_TRANSLATE.exe ...

After you have set these environment variables GDAL should work just fine in a normal command (cmd) window.

Interrogate your PATH environment before setting, you might find the path already contains the right folder. Be aware that when windows searches for programs it looks in the current folder and then the paths (separaed by semi-colons ';') from first to last, earlier instances in the path will be executed as soon as there's a match! The order of extensions, unless implicitly supplied (like do_this.py is calling specifically for a python file), is governed by the pathext environment (usually .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS; .JSE; .WSF; .WSH; .MSC)

For example: when you call gdal_translate the system looks in the current folder for a file called gdal_translate (not case sensitive) and then looks in the first path, the next and all the paths and then if it's not found returns 'is not recognized as an internal or external command operable program or batch file.' so it is not really needed to modify the path if you are in the folder that the program is.