Select Current Feature in a Search Cursor Loop

7233
6
04-30-2015 08:40 AM
ClintonCooper1
New Contributor III

I am looping through a shapefile using a Search Cursor loop, and need to create a specific operation.  I need to select the current feature that the loop is on, and then run a select by location operation on that selected feature.  Once my operation is done, I then need to move onto the next feature in the loop.  Here is my current code:

import arcpy

fc = r"V:/Metro/STOP_CONSOLIDATION/STOPS.gdb/Stop_Database_Points"
fields = ["BEGINSTOPI", "METRO", "GpsLongitu", "GpsLatitud"]

with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:

      
arcpy.SelectLayerByLocation_management(point_lyr, overlap_type="WITHIN_A_DISTANCE", select_features=point_lyr, search_distance="1320 Feet", selection_type="NEW_SELECTION", invert_spatial_relationship="#")

ps, as a side note, how do I add code tags to the new board?

0 Kudos
6 Replies
DarrenWiens2
MVP Honored Contributor

See here for Posting Code Blocks.

AllenDailey1
Occasional Contributor III

Hi!  I am hoping to achieve this exact thing.  Did you ever figure out how to do it, @ClintonCooper1 ?

Thanks!

0 Kudos
by Anonymous User
Not applicable

You need to get the row ID or OBJECTID from the cursor, then use it in a SQL query in the selectlayerbyattribute to select the feature.  Then use the selected feature in the select by location.  Something like:

 

fc = r"V:/Metro/STOP_CONSOLIDATION/STOPS.gdb/Stop_Database_Points"
fields = ["OBJECTID", "BEGINSTOPI", "METRO", "GpsLongitu", "GpsLatitud"]

with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        whereclause = """OBJECTID = {}""".format(row[0])
        selectLyr = arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", whereclause)
        intersectLyr = arcpy.SelectLayerByLocation_management(point_lyr, overlap_type="WITHIN_A_DISTANCE", select_features=selectLyr,
                                       search_distance="1320 Feet", selection_type="NEW_SELECTION",
                                       invert_spatial_relationship="#")

 

 then do something with the selected by location layer.  You'll have to update the code to match your field names.

JoshuaBixby
MVP Esteemed Contributor

Although this approach will definitely work, I think a cleaner approach would be to retrieve the geometry in question and pass that to Select Layer By Location since that tool can accept ArcPy geometry objects as the selecting layer.

fc = r"V:/Metro/STOP_CONSOLIDATION/STOPS.gdb/Stop_Database_Points"
fields = ["SHAPE@", "OBJECTID", "BEGINSTOPI", "METRO", "GpsLongitu", "GpsLatitud"]

with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        shp = row[0]
        intersectLyr = arcpy.SelectLayerByLocation_management(point_lyr, overlap_type="WITHIN_A_DISTANCE", select_features=shp,
                                       search_distance="1320 Feet", selection_type="NEW_SELECTION",
                                       invert_spatial_relationship="#")
by Anonymous User
Not applicable

That is pretty legit. Thanks!

0 Kudos
AllenDailey1
Occasional Contributor III

Thank you very much, JeffK and JoshuaBixby, for your helpful replies!!

0 Kudos