Sunday 19 May 2019

arcgis 10.1 - Is it possible to sort an arcpy.da.UpdateCursor()?


I have an ArcPy Data Access update cursor that I would like to sort before making updates to it.


The cursor makes updates if I don't sort, but if I add a sort to the cursor I get an error, "iteration not started."


The sort is working correctly but it must be busting the cursor for some reason.


Is it not possible to sort an update cursor like a search cursor? Does it change the type or something?



    idList = ['100100', '100200', '100300', '200100']
count = 0
with arcpy.da.UpdateCursor(newFC, ("SHAPE@X", "SHAPE@Y", "Label")) as addLabelCursor:
for row in sorted(addLabelCursor, key=itemgetter(0), reverse = True):
print idList[count]
row[2] = str(idList[count])
addLabelCursor.updateRow(row)
count += 1

Answer



The sort completely exhausts the cursor before you go into updateRow. The way that cursors work is that the updateRow call doesn't work on arbitrary rows out-of-seqence, but on the current one being iterated over. That is, you can only operate on one row at a time.



You may want to look into doing this on the geodatabase side instead. You can specify an ORDER BY clause and sort it before you get to it instead.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...