Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
share|improve this question
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. –  Regan Sarwas Feb 8 '11 at 17:56
add comment

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
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. :) –  matt wilkie Feb 8 '11 at 21:40
add comment

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)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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