Search code examples
pythongisarcgis

Success in integration Hawth's Tools and ArcGIS 10.1 via Python?


So I have been working on integrating the Geospatial Modeling Environment tools (formerly Hawth's) with ArcGIS 10.1 via Python. Below is the code I am using, which works great, to create a text file of code and then call up GME via Python to process the shapefiles I am using. As far as I can tell, I have been able to mimic verbatim what the creator states will work in Python (see his documentation here: http://www.spatialecology.com/gme/images/SpatialEcologyGME.pdf)

Code:

import arcpy, sys, os, subprocess
from arcpy import env

#Supply the following arguments:
#Workspace (full path)
#Catchment Polygons (full path)
#Raster Data (full path)
#Prefix for the output: 6 characters to denote the raster dataset.
#Thematic value: TRUE or FALSE
#An output txt file (full path -> eg. C:/Users/Alison/Desktop/file.txt)
########
#Each argument must be in double quotes, and they must be separated by a space.
#The polygon and raster datasets must be in same coordinate system.

env.workspace = sys.argv[1]
print env.workspace

inputPoly = sys.argv[2]
inputRast = sys.argv[3]
prefix = sys.argv[4]
thematic = sys.argv[5]

code = 'isectpolyrst(in="' + inputPoly + '", raster="' + inputRast + '", prefix="' +   prefix + '", thematic="' + thematic +'");'
print code

newFile = sys.argv[6]
print newFile
newFileObj = open(newFile, 'w')
newFileObj.write(code)
newFileObj.close()

print newFile

os.system(r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe')

print "subprocess.call(r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe -c   run(in=\\\"" + newFile + "\\\");');"

subprocess.call(r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe -c run(in=\\\"" + newFile + "\\\");');

However, while this process works fine, I just end up hitting another wall...It opens GME, but alas, it doesn't actually do anything. It ultimately doesn't seem to run the created text file. The isectpolyrst tool works like Tabulate Area, so in theory, the values should all be appended to the polygon data, but through Python it doesn't seem to do it....(and I am using GME because Tabulate Area cannot handle the size of my datafiles and crashes both in Arc but also as a Python script).

I am wondering if anyone has successfully been able to run GME through Python for use in what will be an ArcPy script, so that the task can be automated, rather than having to go through GME and then into Arc. My searching suggests that this is a common problem for those trying to automate the process, but for all I know I am just missing a colon somewhere, or some other piece of code.

Thanks for the feedback!


Solution

  • Figured it out!

    GME can use a text file to read the desired code, so I wrote the inputs in Python in as they should appear in GME, and wrote these to a text file. These were then read into a subprocess call that brings up GME and runs the text file. Works like a charm.

    Took some tinkering, but worth it!