Select to view content in your preferred language

Passing Python Variables into the Geospatial Modeling Environment?

953
6
07-01-2011 07:59 AM
ShawnONeil
Emerging Contributor
Hello,

I am trying to process a batch of rasters using the GME command isectpolyrst.  I am running ArcMap 10 and the newest standalone version of GME.  Does anyone know if it is possible to pass a list of .tif files using the command arcpy.ListRasters() from Python into the GME environment?  If so, how is that done?  The script that I started is below.  It runs without error but doesn't do anything.  I am a beginner when it comes to Python and all other programming so please take that into consideration.

Thank you! 

Shawn O.
U.S. Fish and Wildlife Service (HAPET)
Great Falls, MT 59404

# import standard system modules
import arcpy, os, sys
from arcpy import env

# set workspace directory

Dir = "C:\\Users\\GISuser\\Desktop\\ONeil_2011\\Scaup Folders\\Habitat Attributes\\2007"
env.workspace = Dir

# List all rasters in the workspace

rasters = arcpy.ListRasters()
grid = "C:\\Users\\GISuser\\Desktop\\ONeil_2011\\Scaup Folders\\all_grids.shp"

# Run each raster through the GME command "isectpolyrst"

for raster in rasters:
    os.system("C:\\Program Files (x86)\\SpatialEcology\\GME\\SEGME.exe isectpolyrst(in=\\\"grid\\\", raster = \\\"raster\\\", prefix = \\\"raster[:3])\\\", where \\\"Year=2007\\\";")
Tags (2)
0 Kudos
6 Replies
AlisonMontgomery
Deactivated User
Were you ever able to resolve this? I am dealing with a similar situation where it won't read in the inputs I am putting in. Well it reads them but can't find the file.

My code is a little different, as I write all my variables into a text file like he suggests so I essentially have a text file that looks like what his interface would have as code, and then I have the text file read in so it looks like his example of using Python with GME of reading in textfiles.

Been trying to figure this out. (Also, shout out to Montana!)
0 Kudos
MattSayler
Frequent Contributor
I think it's primarily that last line. Your variable names are just being read as part of the string. Use the '+' operator to concatenate the variables with the string. Something like:
gmePath = r"C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe"
for raster in rasters:
  os.system(gmePath + " isectpolyrst(in=\"" + grid + "\", raster = \"" + raster + "\", prefix = \"" + raster[:3] + "\", where \"Year=2007\");")


Other changes:
*I made the path to the program a variable, primarily to reduce the number of escape backslashes and enhance the readability.
*The closing ')' for isectpolyrst() looked like it needed to be moved to the end.

Also make sure that ListRasters() is returning what you expect. Run something like
Dir = r"C:\Users\GISuser\Desktop\ONeil_2011\Scaup Folders\Habitat Attributes\2007"
env.workspace = Dir
rasters = arcpy.ListRasters()
for raster in rasters:
  print raster

in IDLE to test if it's returning what you want in the format you want.

One final note: I'm not familiar with that tool, so I don't have a way to test this. Additional tweaks may be needed.
AlisonMontgomery
Deactivated User
I finally got it! Though I did it a different way, and instead of reading each thing into the subprocess call, I have all the output written to a text file, and that text file is then read in (I'm sure either way will work, but I found all the slashes a bit hard to follow when reading them in). I have posted it below:

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

#Supply the following arguments prior to running:
#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, by getting through this, I just end up hitting another wall...It opens GME, but alas, it doesn't actually do anything. The isectpolyrst 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....I have seen this error before in other posts, so I might have to look there.
0 Kudos
ZacharyHart
Honored Contributor
Alison,

I know this thread is 2 months old now, but I'm scouring the forums in a desperate attempt to get to the bottom of an error that myself and (at least one other) user have experienced

Can you describe the exact behavior that occurs in GME when you execute the python script? Would it be possible for you to drop the '-c' switch to have a look?

Also, nice work. I had also thought about just taking all of my GME commands and stuffing them down into a text file to be run as a script for GME, but i'm not sure it will help me at this point given the error we're experiencing.

Thanks,
Z
0 Kudos
ZacharyHart
Honored Contributor
Allison,

I know this thread is 2 months old now, but I'm scouring the forums in a desperate attempt to get to the bottom of an error that myself and (at least one other) user have experienced

Can you describe the exact behavior that occurs in GME when you execute the python script? Would it be possible for you to drop the '-c' switch to have a look?

Also, nice work. I had also thought about just taking all of my GME commands and stuffing them down into a text file to be run as a script for GME, but i'm not sure it will help me at this point given the error we're experiencing.

Thanks,
Z
0 Kudos
AlisonMontgomery
Deactivated User
Hi Z,

Regarding that pop-up, I will also post in that forum...

As an update to my most recent post here, the script did run and it worked, sometimes it just takes a couple tries (annoyingly). Typically, with the scripts I wrote that integrate GME and Arc via Python, Arc and GME don't always play nice and I have a feeling the Arc software runs something that GME has a tough time handling. However, when I have had that pop-up appear, when I click ok, typically my scripts run, albeit on the 2nd or 3rd try.

I know the GME creator is switching everything to R so that he won't have to rely on the libraries. Already GME doesn't work with 10.2, or least I had an issue with it.

I created the scripts and use them as in a ArcToolbox, but they definitely run best when run directly via a Python IDE (I use PythonWin), and then are ok through ArcCatalog, and all the inputs have to be out of the map document if I run them in ArcMap.

The writing everything to a text file, then reading that into the run is definitely a lot cleaner and neater than trying to read in each variable. Pretty new to Python too, so thanks for the thumbs up!

Alison
0 Kudos