Alesinar

Python won't continue to the third loop/update cursor

Discussion created by Alesinar on Apr 2, 2014
Latest reply on Apr 3, 2014 by mdenil
Hi,

Can someone look at my code and tell me why it is not working:

try:      
    with arcpy.da.SearchCursor("selectingBufferLayer", isSelectorField) as cursor:
        for row in cursor:
            oid = int(row[1])
            if (row[0] >= 1.5):
                arcpy.SelectLayerByAttribute_management("selectingBufferLayer", "NEW_SELECTION", '"ID" = {}'.format(oid))
                print "Selected row No. " + str(oid) + " by attributes."
                arcpy.SelectLayerByLocation_management("depressionsLayer", "INTERSECT", "selectingBufferLayer")
                print "Selected row by No. " + str(oid) + " by location."
                arcpy.Append_management("depressionsLayer", depressionsLocation + "firstResults.shp", "NO_TEST", "", "")
                print "Appended selected rows to new shapefile."
        
                with arcpy.da.SearchCursor("depressionsFIDLayer", wasSelectedField) as selcursor:
                    for selrow in selcursor:
                        arcpy.SelectLayerByAttribute_management("depressionsFIDLayer", "NEW_SELECTION", '"ID" = {}'.format(oid))
                        arcpy.Append_management("depressionsFIDLayer", depressionsLocation + "firstResults.shp", "NO_TEST", "", "")
                        print "Exporting depressions that were selected by depression No. " + str(oid)
                        

                        with arcpy.da.UpdateCursor(caveFID, caveFIDField) as cavecursor:
                            for caverow in cavecursor:
                                if row[0] == 999999999:
                                    row[0] = oid
                                    cursor.updateRow(caverow)
                                    print "Updating CaveFid."
                                    break

        del cursor, row
finally: 
    if arcpy.Exists("depressionsLayer"): arcpy.Delete_management("depressionsLayer")
    if arcpy.Exists("selectingBufferLayer"): arcpy.Delete_management("selectingBufferLayer")
    if arcpy.Exists("depressionsFIDLayer"): arcpy.Delete_management("depressionsFIDLayer")


What I would like achieve: First search cursor will select single row, that has row[0]>=1.5. Then with this selection a select by location will be performed and the selected elements will be exported/appended to the new shapefile.

The second loop will select an element that has the same ID as selecting feature from select by location from previous loop and append it to the same shapefile as before.

Lastly the third loop will update field in the shapefile that contains appended records with the ID of the selecting feature from the first loop if the current value in that field is 999999999.

The problem is that process passes through the first loop succesfully but then iterates through second loop for many times every time appending the same polygon. I would like it to go through first cursor once, continue to the second cursor and execute it once and then the same for last cursor. Afted that it is supposed to return back to the first cursor repeating the process.

Outcomes