Select to view content in your preferred language

Aggregating graffiti incidents with python script

728
2
11-04-2011 08:31 AM
Stephaniejohnson2
Emerging Contributor
Hi 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 would appreciate any help!!
Tags (2)
0 Kudos
2 Replies
HemingZhu
Frequent Contributor
Hi 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 would appreciate any help!!


Use SpatialJoin_analysis (target_features, join_features, out_feature_class, {join_operation}, {join_type}, {field_mapping}, {match_option}, {search_radius}, {distance_field_name}).  target_features will by your polygon feature class. join_features will be your point feature class. {match_option} will be CONTAINS, in your case, means patrolZone contains graffiti incidents. The result feature classs will have a field Join_Count which indicates how many join features match each target feature, in your case, means how many graffiti incidents in each patrolZone. Then you can use CalculateField_management: count/SHAPE.area...
0 Kudos
Stephaniejohnson2
Emerging Contributor
Thank you. I'm not sure how to do the write the script to calculate the "SHAPE_Area/2589988.11"
and then take that value and divide the # of incidents by it?
0 Kudos