import arcgisscripting gp = arcgisscripting.create() inputFC = r'path to a feature class' inRows = gp.searchcursor(inputFC) inRow = inRows.next() while inRow: ...do something inRow = inRows.next()This is quiet a verbose way to loop through the rows. Fortunately there is a way to turn the cursor into an iterator that can be used in a for loop. This can be done by adding the following short class to your code.
class CursorIterator(object): def __init__(self, cursor): self.cursor = cursor self.cursor.reset() def next(self): n = self.cursor.next() if n is None: self.cursor.reset() raise StopIteration return n def __iter__(self): return selfAs it is nicely explained in Expert Python Programming by Tarek Ziadé we just created an iterator class. This is a container class with two methods namely next and __iter__. Usage of the CursorIterator class looks like this.
rows = gp.searchcursor(inputFC) rowsIterator = CursorIterator(rows) for row in rowsIterator: ... do somethingor shorter
for row in CursorIterator(gp.searchcursor(inputFC)): ... do somethingI hope you enjoyed my first post. Have fun.
Update : As Jason Schreider points out in his comment the CursorIterator class can be easily replaced by the following :
rows = gp.searchcursor(inputFC) for row in iter(rows.next, None): ... do something
8 comments:
I am looking for assistance in either your script or another that I have that creates polygons from the extents of rasters. What we have are subfolders of imagery. All of which are captured in a unmanaged raster catalog which I would like to use to build a cache from on my GIS Server. How do you get the listrasters to do a recursive drill down into subfolders (upwards of 60 subfolders)? Other recommendations are welcome.
travlerfx@gmail.com
Hello,
What you can do is use the os.walk function.
for root, directories, files in os.walk(startdir):
gp.workspace = root
...
Succes
Or easier, using Python's second iter() behavior:
for row in iter(cursor.next, None):
...
Thanks for the tip !!! I must have overlooked this. Still got a lot to learn.
Does it make it faster?
I'm running a search cursor on a 300000 features in a FC.
the searchRow = search.Next() every iteration takes a long time...
It will not make it faster because it calls the search.Next() under the hood. It only makes it more convenient to use in Python.
So how do I decide which to use when writing a Python script - For Loop or a While Loop?
There doesn't seem to be any discussion on when to use what and the "help" doesn't address this at all.
see> http://resources.arcgis.com/en/help/main/10.1/index.html#/UpdateCursor/018v00000064000000/
I would use a for loop for any new code that you write because the for loop is more compact to write and you can't forget to update the row (row = cursor.next()) and in that way create an infinite while loop.
Post a Comment