Python Object Error on Zonal Statistics For Loop

1149
6
Jump to solution
09-01-2016 11:33 AM
JohnWall
Occasional Contributor

I have a feature class containing multiple buffers. I would like to iterate over those buffers and perform Zonal Statistics identifying the minimum. But, I keep getting an RuntimeError: Object: Error in executing tool and I can't for the life of me figure out what I'm doing wrong.

#Inputs & Outputs
useMultiBuffs = "C:/gis/data/xy/hydropath.gdb/useErased"
gdbName = "scratch.gdb"
filepath = "C:/gis/data/xy"
DEM = "C:/gis/data/raster/dem1m"
allMinPnts = "C:/gis/data/scratch.gdb/allminpnts"
buffIntersects = "C:/gis/data/xy/hydropath.gdb/buffIntersect15m"

#Workspace, Extensions & Environment Variables
arcpy.CheckOutExtension("Spatial") #Checkout Spatial Extention

arcpy.CreateFileGDB_management(filepath,gdbName)


field = "OBJECTID_1"
callField = '"%s"' % field
cursor = arcpy.SearchCursor(useMultiBuffs)
row = cursor.next()
while row:
outZonalStats = ZonalStatistics(row, callField, DEM, "MINIMUM", "NODATA")
buildOutName = filepath + "/" + gdbName + "/" + "z" + row.getValue(field) + "r"
outZonalStats.save(buildOutName)
row = cursor.next()

print "DONE!"

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

The faster method that comes to mind is to use a data access module search cursor to get at the geometry, and use that in zonal statistics.

View solution in original post

6 Replies
DarrenWiens2
MVP Honored Contributor

I know it's probably a quirk in the interface, but can you please format your code block into multiple rows?

JohnWall
Occasional Contributor

Oh gosh, how embarrassing! Thanks for telling me.

0 Kudos
DarrenWiens2
MVP Honored Contributor

I think what's happening is that you're trying to jam a row object where a feature layer should be (first parameter of ZonalStatistics). I'm not quite sure how to convert a row object to a layer with the old-style cursors, but it is definitely possible...

JohnWall
Occasional Contributor

Hi Darren,

Thanks for letting me know. I guess I'd have to use the row object to Select Layer by Attribute then pass that to Zonal Statistics? I keep thinking there used to be a faster way, but I cannot remember what it was and I haven't been able to locate any other method online in my searches. If you have any recommendations they would be much appreciated.

Thanks,

John

0 Kudos
DarrenWiens2
MVP Honored Contributor

The faster method that comes to mind is to use a data access module search cursor to get at the geometry, and use that in zonal statistics.

JohnWall
Occasional Contributor

Oh gosh! I was using the wrong SearchCursor! Thanks for the recommendation. I've implemented that now and everything seems to be running smoothly.

0 Kudos