3

I am working through an open courseware project from PSU found at https://www.e-education.psu.edu/geog485/node/145

I am trying to use an update cursor in a while loop to perform an analysis on individual polygons within a multipart feature. However, the output is for the first row ad infinitum.

Here is what i have so far:

import arcpy
arcpy.env.workspace = "c:/users/documents/arcgis/arcgis tutorials/psu tutorials/project3/policedata.gdb"
arcpy.env.overwriteOutput = True

# Declare variables for files and fields used in analysis

incidents = "c:/users/documents/arcgis/arcgis tutorials/psu tutorials/project3/policedata.gdb/GraffitiIncidents"
patrol_zones = "c:/users/documents/arcgis/arcgis tutorials/psu tutorials/project3/policedata.gdb/PatrolZones"
incidents_layer = "IncidentsLayer"
patrol_field = "NAME"

cursor = arcpy.UpdateCursor(patrol_zones)
zone = cursor.next()

# Loop through each patrol zone

while zone:
    try:
        arcpy.MakeFeatureLayer_management(incidents,"IncidentsLayer" )
        zone_name = zone.getValue(patrol_field)
        print zone_name

        sql = patrol_field + " = " + "'" + zone_name + "'"

    # Make a feature layer for each zone
        arcpy.MakeFeatureLayer_management(patrol_zones, "CurrentZone", sql)


    # Make selection
        arcpy.SelectLayerByLocation_management(incidents_layer, "CONTAINED_BY", "CurrentZone")
        num_incidents = arcpy.GetCount_management(incidents_layer)


        print "Graffitti incidents = " + str(num_incidents)
##        arcpy.Delete_management("CurrentZone")
##        zone
    except:
        print "Could not perform analysis."


del cursor

What in my code is causing the script to produce results only for the first row?

  • 3
    Don't do while zone:, do for zone in iter(cursor.next, None):. Missing the .next() call or not reaching it in a certain if condition is a common pitfall with old-style arcpy cursors. Better yet, use arcpy.da.UpdateCursor. – Jason Scheirer Nov 25 '13 at 21:08
  • @JasonScheirer Thanks, your edit worked. I have not had exposure to the iter() function before. If you move your comment to an answer I will accept it so the question can be closed. – Jyler Nov 25 '13 at 21:36
6

It looks like you are missing another

zone = cursor.next()

within the While loop

You need to increment the cursor to the next feature after you're done with it or the loop starts over on the same feature.

incidents = r"c:\users\documents\arcgis\arcgis tutorials\psu tutorials\project3\policedata.gdb\GraffitiIncidents"
patrol_zones = r"c:\users\documents\arcgis\arcgis tutorials\psu tutorials\project3\policedata.gdb\PatrolZones"
incidents_layer = "IncidentsLayer"
patrol_field = "NAME"

cursor = arcpy.UpdateCursor(patrol_zones)
zone = cursor.next()

# Loop through each patrol zone

while zone:
    try:
        arcpy.MakeFeatureLayer_management(incidents,"IncidentsLayer" )
        zone_name = zone.getValue(patrol_field)
        print zone_name

        sql = patrol_field + " = " + "'" + zone_name + "'"

        # Make a feature layer for each zone
        arcpy.MakeFeatureLayer_management(patrol_zones, "CurrentZone", sql)

        # Make selection
        arcpy.SelectLayerByLocation_management(incidents_layer, "CONTAINED_BY", "CurrentZone")
        num_incidents = arcpy.GetCount_management(incidents_layer)

        print "Graffitti incidents = " + str(num_incidents)
    except:
        print "Could not perform analysis."

    zone = cursor.next()

del cursor

An alternative to having to add a .next() call (and avoid the potential for misplacing it) is to use the iter builtin function:

cursor = arcpy.UpdateCursor(patrol_zones)
for zone in iter(cursor.next, None):
    # (Code)
  • Added cursor.next() to end of loop and now the result hangs on the second entry. – Jyler Nov 25 '13 at 21:31
  • 2
    @Jyler did you put it in the right place? – Jason Scheirer Nov 25 '13 at 21:35
  • @JasonScheirer No, actually I had it dedented from the while: loop. It works, now, thank you. – Jyler Nov 25 '13 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.