Hi!
I'm trying to add the polygons of lines that applied certain criteria to the last row and delete all the row beside the last one.
The problem is that it is very slow.
When I run debugger it turns out that "for row in cursor:" is stuck for a long time after finishing the last item.
I mean, the line "cursor.updateRow(row)" is executed only in the last item. after that the debugger goes back
to the line "for row in cursor:" and stays there for a few minutes.
Why is this happens?
with arcpy.da.UpdateCursor(my_layer, field_names, '"SETTLEMENT" = ' + str(x) ) as cursor:
numberOfLinesLeft = sum(1 for row in cursor)
cursor.reset()
flag=0
for row in cursor:
if flag==0:
temp_shape = row[0]
flag=1
else:
temp_shape = temp_shape.union(row[0])
if numberOfLinesLeft > 1:
cursor.deleteRow()
numberOfLinesLeft -=1
else: # We reached the last row, In this row we need to update it's shape
row[0]=temp_shape
cursor.updateRow(row)
Can you repost with it inserted in the Code window so we can see the indents?
I don't know if deleting the cursor on your last line will help?
Yes.
I also want to mention that I'm using Python 2.7.8
with arcpy.da.UpdateCursor(my_layer, field_names, '"SETTLEMENT" = ' + str(x) ) as cursor:
counter = sum(1 for row in cursor)
cursor.reset()
flag=0
for row in cursor:
if flag==0:
temp_shape = row[0]
flag=1
else:
temp_shape = temp_shape.union(row[0])
if counter > 1:
cursor.deleteRow()
counter -=1
else: # We reached the last row, In this row we need to update it's shape
row[0]=temp_shape
cursor.updateRow(row)
This looks like you are trying to do a dissolve operation?
Maybe revising the process to use the dissolve.htm tool will work better?