Wednesday, October 14, 2009

Long Running Scripts

Well, today I've hit a major road block, but I have a work around that should help many of you out there. Have you ever had a process that performs the same operation multiple times, but sometimes that operation fails. It's almost random. Take my situation, I have a clipping function that clips 100s of data sets, but it will randomly fail with a TopoEngine error. The help suggests that you check the geometries of your features, but it turns out, they are fine.

After much search, and questioning, I discovered that I need to delete the Geoprocessing object, then re-create it as such:


import arcgisscripting
from time import strftime
print str(strftime("%H:%M:%S"))
gp = arcgisscripting.create(9.3)
gp.overwriteoutput = True
print str(strftime("%H:%M:%S"))
del gp
print str(strftime("%H:%M:%S"))
gp = arcgisscripting.create(9.3)
gp.overwriteoutput = True
print str(strftime("%H:%M:%S"))


I did it twice, and measured the time. The first time you create the object, it will take the longest. On my machine, it takes 3 seconds to initialize, but after that, the process takes less than a second. Since the processes I am running already take 2-3 hours to complete a 1-2 minute increase is not too hard to live with.

I found that if I delete the geoprocessing object after I perform work on about 3 processes, I eliminate the error.

Enjoy

Monday, October 12, 2009

Listing Only Directories Using Python

Sometimes it's necessary to list directories, but when the os.listdir() method is called, the return list contains both folders and files. To fix this do the following:



dirs = [ name for name in os.listdir(--Directory--)) if os.path.isdir(os.path.join(str(--Directory--), name))]
for d in dirs:
d = str(d)
d = d.upper()
if d.find(".GDB") == -1:
outString = outString + str(d) + "; "



Methods like this are very helpful when you need to use tool validation. It allows you to quickly list all data in a directory, so users can select what they need.

You'll notice that the script has a find(). This is used because file geodatabases are essentially folders and therefore need to be excluded from the generated list.

Enjoy