Thank you in advance for any assistance.
My problem is that arcpy functions do not seem to recognize each others' results.
Specifically: arcpy.da.SearchCursor does not seem to recognize SelectLayerByAttribute having cleared the selection; and SelectLayerByLocation does not recognize either that SelectLayerByAtttribute cleared the selection or that it was then used to make a new selection.
The code sample below is from a longer script which iterates through features selected by the user then uses one of those features, given by the user as a parameter to the tool, to do further analysis. But I have limited the code sample below to the part that is a problem.
I am running the script as a tool in an ArcGIS Pro project. (2.5.0) & the data is a Shapefile of lines.
Note that in the full code, the argument rstart would be the parameter to the tool and would be selected by the user from a list created by the tool validation script (from ROUTENUM values of selected features).
However, for the example, assume that at least one record was selected before the tool was run and the function in the code was run with the call:
generateNewRoute(211)
Here is the code:
import arcpy
pro_proj=arcpy.mp.ArcGISProject("CURRENT")
r_map = pro_proj.listMaps("Map")[0]
routelyr = r_map.listLayers("routes")[0]
def generateNewRoute(rstart):
fields = ['ROUTENUM']
wc="ROUTENUM = " + str(rstart)
rnums=[]
cnums=[]
nnums=[]
arcpy.AddMessage(wc)
arcpy.SelectLayerByAttribute_management(routelyr, "CLEAR_SELECTION")
with arcpy.da.SearchCursor(routelyr, fields) as selectioncleared:
for n in selectioncleared:
nnums.append(n[0])
arcpy.AddMessage("Should be all recs:" + str(nnums))
arcpy.SelectLayerByAttribute_management(routelyr, "NEW_SELECTION", where_clause=wc)
with arcpy.da.SearchCursor(routelyr, fields) as selectedrecs:
for r in selectedrecs:
rnums.append(r[0])
arcpy.AddMessage("Selected records: " + str(rnums))
arcpy.SelectLayerByLocation_management(routelyr, "boundary_touches", select_features=routelyr, selection_type="NEW_SELECTION")
with arcpy.da.SearchCursor(routelyr, fields) as connectedrecs:
for c in connectedrecs:
cnums.append(c[0])
arcpy.AddMessage("Connected records: " + str(rnums))
What I need is a list of the features touching the boundary of a feature selected by SelectLayerByAttribute_management (in the example, record with ROUTENUM = 211 but ultimately with values that would be generated in the code, not from a preexisting selection.)
If someone could tell me what I am doing wrong here, I would be very appreciative.
Thanks,
M.J.