bagardne

selecting features from a shapefile

Discussion created by bagardne on Nov 19, 2013
I have two shapefiles (CountyCenterLine and GBMPO_Ped_07_11). I am attempting to return the number of points found within 60 feet of each road segment. I then want to access a field I have created within GBMPO that gives a score to each point based on its injury value that I have assigned.

Ultimately, I want to assign a score to each road segment. That score will be determined by adding all of the injury values that occur within 60 feet of that road segment.

The rest of it I can handle from my own knowledge I think, but I am struggling with accessing the values from the point class based on their location to the road segments. I am thinking maybe I need to run a loop that looks at each individual road segment and finds the score associated with each, if any, points with 60 feet. Here is what I have so far.

'''This program will find all the pedestrian/bike accidents with vehicles along
all the corridors. It will count how many happen on each corridor and assign
a number from 1-5 depending on the type of injury sustained from the accident.
Finally, the program will list the top ten most dangerous corridors in
******** and export that list to a new shapefile'''

#let the user know the program is running
print 'Crash Corridor Analysis Study running...'

#import arcpy, sys, traceback
import arcpy, sys, traceback

arcpy.env.workspace = 'E:\\school\\GEO614\\project\\'
arcpy.env.overwriteOutput = True

#variables
feature_class = 'CountyCenterLine.shp'
feature_layer = 'centerline'

point_class = 'GBMPO_Ped_07_11.shp'
point_layer = 'pedestrian crash'

distance = '60 FEET'
count = 1

try:
    #1. select by location
    #print the number of records in the feature class
    result = arcpy.GetCount_management(feature_class)
    print 'Number of features in the feature class ' + feature_class + ' : ' + str(result)
    result = arcpy.GetCount_management(point_class)
    print 'Number of features in the point class ' + point_class + ' : ' + str(result)
    #make a feature layer for streets
    arcpy.MakeFeatureLayer_management(feature_class, feature_layer)
    #select points that are 60 feet from roads
        #a. make a feature layer for pedestrian crashes
    arcpy.MakeFeatureLayer_management(point_class, point_layer)
        #b. select by location
    arcpy.SelectLayerByLocation_management(point_layer, 'INTERSECT', feature_layer, distance, 'NEW_SELECTION')
    result = arcpy.GetCount_management(point_layer)
    print 'Number of selected features in the feature layer ' + point_layer + ' within a distance of ' + distance + ' : ' + str(result)

    #2. assign severity score to each accident
    arcpy.DeleteField_management(point_class, 'severity')
    arcpy.AddField_management(point_class, 'severity', 'TEXT')
    #get a collection of rows from the point class
    srows = arcpy.SearchCursor(point_layer)
    #irows = arcpy.InsertCursor(point_class)
    #create a for loop to iterate over the rows of the cursor
    for srow in srows:
        injury = srow.Ped_Injury
        if injury == 'Unknown Injury':
            srows.severity = '1'
        elif injury == 'No Injury':
            srows.severity = '1'
        elif injury == 'Possible Injury':
            srows.severity = '2'
        elif injury == 'Evident Injury':
            srows.severity = '3'
        elif injury == 'Disabling Injury':
            srows.severity = '4'
        elif injury == 'Fatality':
            srows.severity = '5'
        else:
            srows.severity = '??'
        sev = srows.severity
        print injury, sev, count
        count += 1

    #3. tally 

    #4. calculate the score for each corridor

    #5. sort the corridors decendingly based on score

    #6. rank them starting at one

    #7. export the top ten most dangerous corridors to their own shapefile

    #finish statement
    print '...done'

#exception phrase
except:
    # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000q000000
    
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     " +        str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
    msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"

    arcpy.AddError(msgs)
    arcpy.AddError(pymsg)

    print msgs
    print pymsg
    
    arcpy.AddMessage(arcpy.GetMessages(1))
    print arcpy.GetMessages(1)


Thanks,

Bryce

Outcomes