Without getting too much into the context of this workflow, I'm trying to find whether a stream segment is flowing in the right direction. If the "FROM_NODE" or the "TO_NODE" values of two adjacent stream segments are equal, then one of the segments isn't digitized in the correct direction, and that record is tagged with an error code (i.e. 6).
I am trying to do this by iterating through a file geodatabase table using da.UpdateCursor, iterating two cursor rows at a time.
I'm using a function called "pairwise" to iterate through two rows at a time, which I got from the Python 2.7 itertools documentation here:
https://docs.python.org/2/library/itertools.html#recipes
When stepping through the code, everything works fine until I get to one of the cursor.updateRow(row1)
lines, then I get the following error:
StopIteration: iteration not started
Here's the code:
# iterates over two items at a time (recipe from python documentation)
def pairwise(iterable):
a, b = tee(iterable)
next(b, None)
return zip(a,b)
# Find flow direction errors
def flow_direction(tmp_network_tbl):
arcpy.AddMessage("... flow direction")
fields = ["ReachID", "UpstreamID", "FROM_NODE", "TO_NODE", "ERROR_CODE"]
with arcpy.da.UpdateCursor(tmp_network_tbl, fields) as cursor:
for row1, row2 in pairwise(cursor):
if (row1[2] == row2[2]): # upstream has same from_node as reach
row1[4] = 6
cursor.updateRow(row1)
elif (row1[3] == row2[3]): # reach has same to_node as upstream
row1[4] = 6
cursor.updateRow(row1)
elif (row1[4] != 0):
pass
I'm assuming that the fact that the cursor is being run through the pairwise
function is causing the problem, so I suppose my whole effort to try iterating two rows at a time isn't a good idea... Or is there just something simple I'm missing?
try/except
statements removed, and retested, because they can mask error messages which would otherwise be helpful. As it stands this does not qualify as a code snippet that works up to where you are stuck because it consists only of two functions with no call to yourflow_direction
function to run it.