嵌套搜索光标出现问题。并按位置选择

时间:2018-08-20 18:06:52

标签: python arcpy arcmap

对于在ArcMap中运行python我是全新的。我有纽约地铁站的位置和路线折线。我正在尝试自动查找经过哪个停靠点的路线(将其放入“地铁停靠点”表中的相应字段中),但是我的代码无论如何都会返回每条路线。

下面的代码:

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')

我认为我已经将错误的范围缩小到了按位置进行选择的方式,但是我看不出有什么问题。

1 个答案:

答案 0 :(得分:0)

我发现这与游标中的数据有关。在两个光标中都添加SHAPE @可以进行选择。

下面是工作代码:

txtRent