2

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.

  • Gdal_merge itself is a python script. Perhaps you can study the code from your computer and get some inspiration. – user30184 Jun 19 '14 at 20:56
  • @user30184 Thank you! Yes, I saw that is a py file. I was thinking whether there is a way to import it and directly give input and specify output to it. – shirleydd Jun 19 '14 at 21:01
7

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.

0

I have used gdal outside of the shell. One way is to use QGIS. Many of the gdal functions are tools that can be accessed from within QGIS. Secondly you can use any commandline tool to access gdal via a Python script. It seems that you may not have gdal listed in your Environment Variables in Windows. When gdal is in the Environment Variables you have access to the functions outside of the OSGeo4W shell (ie: other commandline tools, etc.)

  • Hi Ryan, I actually set the environment variables. Here are them: C:\Program Files\GDAL;C:\Python27\;C:\Python27\Scripts;C:\OSGeo4W\bin. But still I cannot access gdal functions from OSGeo4W. – shirleydd Jun 19 '14 at 21:06
  • So for example, in OSGeo4W if you type gdal_merge nothing comes up? You should see something like this: Usage: gdal_merge.py [-o out_filename] [-of out_format] [-co NAME=VALUE]* [-ps pixelsize_x pixelsize_y] [-tap] [-separate] [-q] [-v] [-pct] [-ul_lr ulx uly lrx lry] [-init "value [value...]"] [-n nodata_value] [-a_nodata output_nodata_value] [-ot datatype] [-createonly] input_files [--help-general] – Ryan Garnett Jun 19 '14 at 21:07
  • As for the first solution, like I said, I have a py file created through modelbuilder and I want to add those gdal function to it. So is QGIS a way to do this? – shirleydd Jun 19 '14 at 21:12
  • In OSGeo4W shell if I type gdal_merge, I can see those you mentioned. I actually run this command successfully to get a multi-band tif file. I also run gdal2xyz successfully to convert a multi-band raster to csv/txt file. – shirleydd Jun 19 '14 at 21:17
  • Then I am not sure what you mean. If you can access gdal commands from OSGeo4W then what is the issue? Do you want to bundle things from your ESRI python? That may not be possible, but I will let someone else tackle it. I have python scripts that call gdal. What I have done is to evoke it from a commandline tool, but I have to type python first, then the python script file (ie: python c:\temp\cool_script.py) If you want to use an ESRI script and a gdal script for processing, you may have to batch that. Create a bat file that calls the scripts in order, or even a python script to do that – Ryan Garnett Jun 19 '14 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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