0

I need help with writing a script:

The task

I have a point feature class of graffiti incidents and a polygon feature class of patrol zones with some empty attributes already created for you. I must write a script that updates the attributes of the patrol zones with:

  • The number of graffiti incidents falling within the patrol zone. This is an integer that goes in the INCIDENTS field.
  • The priority ranking for the patrol zone. This is a string that goes in the PRIORITY field. You will derive this string using some simple math that compares the number of incidents in the zone with the area of the zone.

Patrol zone priority rankings

I then will calculate a priority ranking for each zone by dividing the number of graffiti incidents in the zone by the area of the zone. My script should then examine the result and assign the appropriate priority ranking (PRIORITY). These are the priority rankings:

  • TOP CONCERN—15 or more incidents per square mile
  • HIGH CONCERN— At least 12 but less than 15 incidents per square mile
  • SOME CONCERN— At least 6 but less than 12 incidents per square mile
  • LOW CONCERN—Fewer than 6 incidents per square mile

Here's what I have so far:

import arcpy
arcpy.env.overwriteOutput= True

# layer that we are changing
patrolZone= "C:\\...\\PoliceData.gdb\\PatrolZones"
# the layer we are selecting from
graffiti= "C:\\...\\PoliceData.gdb\\GraffitiIncidents"
nameField= "NAME"
graffitiField= "OBJECTID"
incidentsField= "INCIDENTS"


# sits above the first row, starts the cursor
patrolRows= arcpy.UpdateCursor(patrolZone)

# tells it to go to the first row
patrol= patrolRows.next()
# now were in the first row doing looping

while patrol:
arcpy.MakeFeatureLayer_management(graffiti, "GraffitiLayer")

zones= patrol.getValue(nameField)
#print incidents
queryString = '"' + str(nameField) + '" = ' + "'" + str(zones) + "'"
print str(queryString)

arcpy.MakeFeatureLayer_management(patrolZone, "PatrolLayer", queryString)
arcpy.SelectLayerByLocation_management("GraffitiLayer", "CONTAINED_BY", "PatrolLayer")

numGraffiti= arcpy.GetCount_management("GraffitiLayer")
print numGraffiti

patrol= patrolRows.next()

I'm stuck on updating the INCIDENTS field

2
  • Can you update the thread title to reflect what problem you are trying to solve? "PythonWin question" is neither specific nor accurate (PythonWin is a set of Windows extensions for Python as well as an IDE).
    – blah238
    Commented Nov 5, 2011 at 20:18
  • Did you ever end up getting this code to work? I'm working through this same exercise and am having problems. Also, I ran the code you posted and the output was a count for North Central over and over again.
    – Jyler
    Commented Nov 24, 2013 at 23:31

1 Answer 1

3

You could perform a spatial join, joining points to polygons, which would result in a new feature class with all of the original polygon attributes, plus a field containing the count of points falling within each one. This could then be joined back to the original polygons (using a regular join), and you could use the Calculate Field tool to grab the counts from the joined data and put them in the INCIDENTS field. Then, optionally, remove the join and delete the spatially-joined featured class . This can all be done with Python.

5
  • Thank you! Now I'm working on the next step TOP CONCERN—15 or more incidents per square mile HIGH CONCERN— At least 12 but less than 15 incidents per square mile SOME CONCERN— At least 6 but less than 12 incidents per square mile LOW CONCERN—Fewer than 6 incidents per square mile" but first I have to divide the SHAPE_AREA by 2589988.11, then divide the number of incidents by that??
    – caligrl
    Commented Nov 5, 2011 at 20:21
  • 1
    Most if not all of the tasks that you need to perform for this analysis could be modeled in model builder, than exported out as a python script.
    – artwork21
    Commented Nov 5, 2011 at 21:28
  • Tried that... didn't get anywhere
    – caligrl
    Commented Nov 5, 2011 at 21:59
  • 1
    @caligrl to populate the PRIORITY field, use the Calculate Field tool. You can adapt esri's "Calculate ranges" example on this page to suit your needs: help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//…
    – nmpeterson
    Commented Nov 6, 2011 at 19:34
  • @caligrl if you don't need anymore help with this, would you mind marking my answer as correct, so that people know your question has been answered? Thanks.
    – nmpeterson
    Commented Nov 7, 2011 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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