<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic selecting features from a shapefile in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/selecting-features-from-a-shapefile/m-p/346967#M27203</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;'''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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #1. select by location
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print the number of records in the feature class
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = arcpy.GetCount_management(feature_class)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Number of features in the feature class ' + feature_class + ' : ' + str(result)
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = arcpy.GetCount_management(point_class)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Number of features in the point class ' + point_class + ' : ' + str(result)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #make a feature layer for streets
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(feature_class, feature_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #select points that are 60 feet from roads
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #a. make a feature layer for pedestrian crashes
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(point_class, point_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #b. select by location
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(point_layer, 'INTERSECT', feature_layer, distance, 'NEW_SELECTION')
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = arcpy.GetCount_management(point_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Number of selected features in the feature layer ' + point_layer + ' within a distance of ' + distance + ' : ' + str(result)

&amp;nbsp;&amp;nbsp;&amp;nbsp; #2. assign severity score to each accident
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.DeleteField_management(point_class, 'severity')
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(point_class, 'severity', 'TEXT')
&amp;nbsp;&amp;nbsp;&amp;nbsp; #get a collection of rows from the point class
&amp;nbsp;&amp;nbsp;&amp;nbsp; srows = arcpy.SearchCursor(point_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #irows = arcpy.InsertCursor(point_class)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #create a for loop to iterate over the rows of the cursor
&amp;nbsp;&amp;nbsp;&amp;nbsp; for srow in srows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; injury = srow.Ped_Injury
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if injury == 'Unknown Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '1'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'No Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '1'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'Possible Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '2'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'Evident Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '3'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'Disabling Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '4'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'Fatality':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '5'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '??'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sev = srows.severity
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print injury, sev, count
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count += 1

&amp;nbsp;&amp;nbsp;&amp;nbsp; #3. tally 

&amp;nbsp;&amp;nbsp;&amp;nbsp; #4. calculate the score for each corridor

&amp;nbsp;&amp;nbsp;&amp;nbsp; #5. sort the corridors decendingly based on score

&amp;nbsp;&amp;nbsp;&amp;nbsp; #6. rank them starting at one

&amp;nbsp;&amp;nbsp;&amp;nbsp; #7. export the top ten most dangerous corridors to their own shapefile

&amp;nbsp;&amp;nbsp;&amp;nbsp; #finish statement
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '...done'

#exception phrase
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000q000000
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; tb = sys.exc_info()[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(tb)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; " +&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(msgs)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(pymsg)

&amp;nbsp;&amp;nbsp;&amp;nbsp; print msgs
&amp;nbsp;&amp;nbsp;&amp;nbsp; print pymsg
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(arcpy.GetMessages(1))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages(1)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Bryce&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 19 Nov 2013 17:42:37 GMT</pubDate>
    <dc:creator>BryceGardner1</dc:creator>
    <dc:date>2013-11-19T17:42:37Z</dc:date>
    <item>
      <title>selecting features from a shapefile</title>
      <link>https://community.esri.com/t5/python-questions/selecting-features-from-a-shapefile/m-p/346967#M27203</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;'''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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #1. select by location
&amp;nbsp;&amp;nbsp;&amp;nbsp; #print the number of records in the feature class
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = arcpy.GetCount_management(feature_class)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Number of features in the feature class ' + feature_class + ' : ' + str(result)
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = arcpy.GetCount_management(point_class)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Number of features in the point class ' + point_class + ' : ' + str(result)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #make a feature layer for streets
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(feature_class, feature_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #select points that are 60 feet from roads
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #a. make a feature layer for pedestrian crashes
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(point_class, point_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #b. select by location
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(point_layer, 'INTERSECT', feature_layer, distance, 'NEW_SELECTION')
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = arcpy.GetCount_management(point_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Number of selected features in the feature layer ' + point_layer + ' within a distance of ' + distance + ' : ' + str(result)

&amp;nbsp;&amp;nbsp;&amp;nbsp; #2. assign severity score to each accident
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.DeleteField_management(point_class, 'severity')
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(point_class, 'severity', 'TEXT')
&amp;nbsp;&amp;nbsp;&amp;nbsp; #get a collection of rows from the point class
&amp;nbsp;&amp;nbsp;&amp;nbsp; srows = arcpy.SearchCursor(point_layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #irows = arcpy.InsertCursor(point_class)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #create a for loop to iterate over the rows of the cursor
&amp;nbsp;&amp;nbsp;&amp;nbsp; for srow in srows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; injury = srow.Ped_Injury
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if injury == 'Unknown Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '1'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'No Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '1'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'Possible Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '2'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'Evident Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '3'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'Disabling Injury':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '4'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif injury == 'Fatality':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '5'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; srows.severity = '??'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sev = srows.severity
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print injury, sev, count
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count += 1

&amp;nbsp;&amp;nbsp;&amp;nbsp; #3. tally 

&amp;nbsp;&amp;nbsp;&amp;nbsp; #4. calculate the score for each corridor

&amp;nbsp;&amp;nbsp;&amp;nbsp; #5. sort the corridors decendingly based on score

&amp;nbsp;&amp;nbsp;&amp;nbsp; #6. rank them starting at one

&amp;nbsp;&amp;nbsp;&amp;nbsp; #7. export the top ten most dangerous corridors to their own shapefile

&amp;nbsp;&amp;nbsp;&amp;nbsp; #finish statement
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '...done'

#exception phrase
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000q000000
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; tb = sys.exc_info()[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(tb)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; " +&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(msgs)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError(pymsg)

&amp;nbsp;&amp;nbsp;&amp;nbsp; print msgs
&amp;nbsp;&amp;nbsp;&amp;nbsp; print pymsg
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(arcpy.GetMessages(1))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages(1)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Bryce&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Nov 2013 17:42:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selecting-features-from-a-shapefile/m-p/346967#M27203</guid>
      <dc:creator>BryceGardner1</dc:creator>
      <dc:date>2013-11-19T17:42:37Z</dc:date>
    </item>
  </channel>
</rss>

