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)<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Here is the code:
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token">## Get references to ArcGIS Pro objects</SPAN>
pro_proj<SPAN class="operator token">=</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN>
r_map <SPAN class="operator token">=</SPAN> pro_proj<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Map"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
routelyr <SPAN class="operator token">=</SPAN> r_map<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"routes"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">generateNewRoute</SPAN><SPAN class="punctuation token">(</SPAN>rstart<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'ROUTENUM'</SPAN><SPAN class="punctuation token">]</SPAN>
wc<SPAN class="operator token">=</SPAN><SPAN class="string token">"ROUTENUM = "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>rstart<SPAN class="punctuation token">)</SPAN>
rnums<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
cnums<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
nnums<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># correctly prints "ROUTENUM = 211" so SQL seems fine</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>wc<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Clear any existing selection</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>routelyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"CLEAR_SELECTION"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Check that no records are selected: (This should now return a </SPAN>
<SPAN class="comment token"># list of all records (the behaviour of SearchCursor if no records</SPAN>
<SPAN class="comment token"># are selected) but instead returns a list of records that </SPAN>
<SPAN class="comment token"># were selected prior to running the tool</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>routelyr<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> selectioncleared<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> n <SPAN class="keyword token">in</SPAN> selectioncleared<SPAN class="punctuation token">:</SPAN>
nnums<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Should be all recs:"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>nnums<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># select the feature we want to use in SelectLayerByLocation</SPAN>
<SPAN class="comment token"># In this case the feature with ROUTENUM = 211:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>routelyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN>wc<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Check that the record was selected: This correctly prints the message</SPAN>
<SPAN class="comment token"># "Selected records: [211]"</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>routelyr<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> selectedrecs<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> r <SPAN class="keyword token">in</SPAN> selectedrecs<SPAN class="punctuation token">:</SPAN>
rnums<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Selected records: "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>rnums<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Select records touching the boundary of the selected record.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation_management<SPAN class="punctuation token">(</SPAN>routelyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"boundary_touches"</SPAN><SPAN class="punctuation token">,</SPAN> select_features<SPAN class="operator token">=</SPAN>routelyr<SPAN class="punctuation token">,</SPAN> selection_type<SPAN class="operator token">=</SPAN><SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Check the result: This should return a list of records touching </SPAN>
<SPAN class="comment token"># the endpoints of record 211. But, instead it returns a list of </SPAN>
<SPAN class="comment token"># records touching the endpoints of records selected prior to </SPAN>
<SPAN class="comment token"># running the tool (or, if no records were previously selected, a </SPAN>
<SPAN class="comment token"># list of all records that have any endpoints touching an endpoint </SPAN>
<SPAN class="comment token"># of another feature)</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>routelyr<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> connectedrecs<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> c <SPAN class="keyword token">in</SPAN> connectedrecs<SPAN class="punctuation token">:</SPAN>
cnums<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Connected records: "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>rnums<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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.