Saturday, 21 September 2019

cursor - Counting and storing events contained within polygon using ArcPy?


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.



Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...