Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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, and so the idea of having to do these without some assistance from a python script seems inane and insane. At this point I would just be happy with some advice, let alone a actual solution.

Many thanks in advance!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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
        arcpy.management.SelectLayerByLocation(eventsLayer,"WITHIN",yourIntersectionsLayer)
        #this has selected the points within the polygon on the cursor
        year1=0
        year2=0
        year3=0
        for x in range(0,2):
            if x==0:
                eventCursor = arcpy.SearchCursor(eventsLayer,"\"Year\" = 2005")
                for event in eventCursor:
                    year1+=1
                del event,eventCursor
            if x==1:
                eventCursor = arcpy.SearchCursor(eventsLayer,"\"Year\" = 2006")
                for event in eventCursor:
                    year2+=1
                del event,eventCursor
            if x==2:
                eventCursor = arcpy.SearchCursor(eventsLayer,"\"Year\" = 2007")
                for event in eventCursor:
                    year3+=1
                del event,eventCursor

        row.Year2005=year1
        row.Year2006=year2
        row.Year2007=year3
        polygon.updateRow(row)
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

share|improve this answer
    
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 '12 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 '12 at 20:52
    
absolutely, no harm in trying it. Its a good start. –  myClone Jul 3 '12 at 20:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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