-1

I am brand new to running python in ArcMap. I have New York Metro Stop locations and the route polylines. I'm trying to automatically find what routes go through which stops (to be put into a corresponding field in the Metro Stops table), but my code just returns every route anyway.

Code Below:

import arcpy

#First check to see if the covered_routes field exists yet
if len(arcpy.ListFields(Metro_Stops_Buffer,"covered_routes")) > 0: 
    arcpy.DeleteField_management(Metro_Stops_Buffer, "covered_routes")
    arcpy.AddField_management(Metro_Stops_Buffer, "covered_routes", "string")
else:
    arcpy.AddField_management(Metro_Stops_Buffer, "covered_routes", "string") 

#Delete Extra Layers
arcpy.Delete_management('Metro_Routes_lyr')
arcpy.Delete_management('Metro_Stops_Buffer_lyr')

#A Cursor that runs through each Stop.
with arcpy.da.UpdateCursor(Metro_Stops_Buffer, "covered_routes") as cursor:
    for row in cursor:
        routes = []

        #Create the layers to work with from the GDB.
        arcpy.MakeFeatureLayer_management(Metro_Stops_Buffer, 'Metro_Stops_Buffer_lyr')
        arcpy.MakeFeatureLayer_management(Metro_Routes, 'Metro_Routes_lyr')

        #Select the routes that intersect the stop (row)
        arcpy.SelectLayerByLocation_management('Metro_Routes_lyr', "INTERSECT", 'Metro_Stops_Buffer_lyr')

        #Given the selected routes, create a new cursor that will return the route name (route_long) for each.
        with arcpy.da.SearchCursor('Metro_Routes_lyr', "route_long") as cursor2:
            for j in cursor2:
                print(j)
                routes.append(j)
                print(routes)

                #Insert the routes into the "covered_routes" field. 
                row[0] = str(routes)
                cursor.updateRow(row)
        del(routes)
        #Remove the unnecessary layers
        arcpy.Delete_management('Metro_Routes_lyr')
        arcpy.Delete_management('Metro_Stops_Buffer_lyr')

I think I've narrowed my error down to how it is doing the select by location, but I am not seeing whats wrong.

1 Answers1

0

I figured out that it was an issue with what data is in the cursor. Adding SHAPE@ into both cursor allowed the selection to happen.

Below is the working code:

import arcpy

#First check to see if the covered_routes field exists yet
if len(arcpy.ListFields(Metro_Stops,"covered_routes")) > 0: 
    arcpy.DeleteField_management(Metro_Stops_Buffer, "covered_routes")
    arcpy.AddField_management(Metro_Stops_Buffer, "covered_routes", "string")
else:
    arcpy.AddField_management(Metro_Stops_Buffer, "covered_routes", "string") 

#Delete Extra Layers
arcpy.Delete_management('Metro_Routes_lyr')
arcpy.Delete_management('Metro_Stops_Buffer_lyr')

#A Cursor that runs through each Stop.
with arcpy.da.UpdateCursor(Metro_Stops_Buffer, ("covered_routes","SHAPE@")) as cursor:
    for row in cursor:
        routes = []
        clean_row = '{}'.format(row[0])

        #Create the layers to work with from the GDB.
        arcpy.MakeFeatureLayer_management(Metro_Stops_Buffer, 'Metro_Stops_Buffer_lyr')
        arcpy.MakeFeatureLayer_management(Metro_Routes, 'Metro_Routes_lyr')

        #Select the routes that intersect the stop (row)
        arcpy.SelectLayerByLocation_management('Metro_Routes_lyr', "INTERSECT", row[1])

        #Given the selected routes, create a new cursor that will return the route name (route_long) for each.
        with arcpy.da.SearchCursor('Metro_Routes_lyr', "route_long") as cursor2:
            for j in cursor2:
                clean_j = '{}'.format(j[0])
                print(clean_j)
                routes.append(clean_j)
                print(routes)

                #Insert the routes into the "covered_routes" field. 
                row[0] = str(routes)
                cursor.updateRow(row)
        del(routes)
        #Remove the unnecessary layers
        arcpy.Delete_management('Metro_Routes_lyr')
        arcpy.Delete_management('Metro_Stops_Buffer_lyr')