I have a fairly large project and I would like to automate some tasks using Python. Problem is, I don’t have any experience with Python for ArcGIS. And I rarely use ArcGIS until something big like this comes up, and usually I am under some time constraint (smh) I am looking for some guidance (such as python syntax to use, methodology, etc.) to help me achieve the following, using the screenshots as a reference…

  1. The red dots denote multiple events that overlap each other (contain multiple records) that fall within the polygon buffer points (labeled A and B, respectively): Screenshot 1
  2. The events are part of a shapefile specific to a road corridor, so that each corridor shapefile may contain several event points, representing motor vehicle crashes at an intersection. Each event contains a field “YEAR” that has either 2007, 2008, 2009, or 2010 in the field.: enter image description here
  3. The polygon buffer shapefiles have a separate field for each year listed above. Most corridors have about 6-10 rows, representing checkpoints along the corridor. enter image description here

What I wish to accomplish is to:

  1. Count all the events that fall within in the polygon points.
  2. Populate the polygon fields for each year with the corresponding counts of the “YEAR” field in the event shapefiles to the “CNT_20XX” fields in the polygon shapefiles
  3. Example: Polygon A has a total of 10 events within it. The counts for each year are 2007= 3, 2008= 1, 2009= 2, 2010= 4; so the Polygon for point A would have its corresponding “CNT_20XX” fields populated with the sum of the counts for each year in the events shapefile.

So...can this be accomplished in python? Is there a non-python way to accomplish the same thing? I have 16 corridors.

1 Answer 1


You could possibly do this with nested cursors and selection statements in Python. Something like the following, perhaps?

    polygons = arcpy.UpdateCursor(yourIntersectionsLayer)
    for row in polygons:
        arcpy.management.SelectLayerByAttribute(yourIntersectionsLayer,"NEW_SELECTION","\"ObjectID\" = "+str(row.ObjectID))    
        #you now have one polygon at a time selected
        #this has selected the points within the polygon on the cursor
        for x in range(0,2):
            if x==0:
                eventCursor = arcpy.SearchCursor(eventsLayer,"\"Year\" = 2005")
                for event in eventCursor:
                del event,eventCursor
            if x==1:
                eventCursor = arcpy.SearchCursor(eventsLayer,"\"Year\" = 2006")
                for event in eventCursor:
                del event,eventCursor
            if x==2:
                eventCursor = arcpy.SearchCursor(eventsLayer,"\"Year\" = 2007")
                for event in eventCursor:
                del event,eventCursor

del row,polygons

I haven't had a chance to test this in an environment with data yet, but it might be plausible. Here's some documentation on the cursors in ArcPy: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001q000000

  • Thanks a lot @AHigh. I will give this a try. This sounds like a good cookbook project I can experiment with and perhaps learn from it, so I am going to give you a +1. I am going to hang on for a day or two and let others respond, if any before accepting the solution though. Many thanks.
    – myClone
    Jul 3, 2012 at 20:51
  • 1
    Sure. Hopefully it helps, there's definitely room for optimization if it works. It may be a framework at any rate.
    – AHigh
    Jul 3, 2012 at 20:52
  • absolutely, no harm in trying it. Its a good start.
    – myClone
    Jul 3, 2012 at 20:57

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.