5

In ArcGIS, I want to use a Python script (and a standard ArcGIS script tool UI) to call functionality from FWTools.

How would I make the call to FWTools? I tried using os.system(C:\WINDOWS\system32\cmd.exe /K "C:\Program Files\FWTools2.4.7\setfw.bat") which is what the windows shortcut calls, but that is not it. It seems I am probably missing something rather simple, but I can't figure out how to make the right call through python to get FWTools & ogr2ogr operating on the string I pass.

My UI looks like this:

enter image description here

and the pseudo-code behind it is something like:

'ogr2ogr -f "' + outformat + '" "' + outfile + '" "' + infile + '" ' + ogrvars
1
  • 1
    You might consider installing the gdal python module (pypi.python.org/pypi/GDAL/1.7.1), and then calling the ogr library direct from python. It will be a little lower level, but more direct. Commented Feb 8, 2011 at 17:56

2 Answers 2

4

I did this a while ago, I'm not sure it is the best way to go, but it worked at the time.

g.fwtools = r"D:\FWTools2.2.8"
# name of translate command
g.cmd = os.path.join(g.fwtools, "bin", "gdal_translate.exe")


def setupfwtools(g):
    """
    Sets the environment variables to access the GDAL tools.
    This allows us to run this script without depending on the
    use running the FW Tools seup batch file first.
    """
    os.environ['FWTOOLS_DIR']=g.fwtools
    os.environ['PATH']=g.fwtools+'\\bin;'+g.fwtools+'\\python;'+os.environ['PATH']
    os.environ['PYTHONPATH']=g.fwtools+'\\pymod'
    os.environ['PROJ_LIB']=g.fwtools+'\\proj_lib'
    os.environ['GEOTIFF_CSV']=g.fwtools+'\\data'
    os.environ['GDAL_DATA']=g.fwtools+'\\data'
    os.environ['GDAL_DRIVER_PATH']=g.fwtools+'\\gdal_plugins'

args = [g.cmd] + [option1 option2]
args.append(infile)
args.append(outfile)
os.spawnve(os.P_WAIT, args[0], args, os.environ)
4

It turns out it was just a silly mistake on my part and forgetting that paths in Python need to be either "\" or "/". The code itself was very simple:

'"C:\\WINDOWS\\system32\\cmd.exe /C" "C:\\Program Files\\FWTools2.4.7\\bin\\ogr2ogr.exe" ' + ogrstring

I have posted the toolbox and python code at:

http://resources.arcgis.com/Resources2010/gallery/file/geoprocessing/details?entryID=068D81A7-1422-2418-A092-A96C07DB6E25

1
  • It's okay to mark your own answer accepted. I'm glad you figured it out, I have far too many of those "Doh!" over a single misplaced character myself. :) Commented Feb 8, 2011 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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