Cursor and Select by Location

2479
2
Jump to solution
02-06-2016 12:11 PM
MichaelKowalczyk
New Contributor III

Hello,

I have a cursor cycling through several polygon features.  Would like to pass the current cursor entry to the Select by Location tool, to select a series of lines contained by the polygons.  I am getting an error stating the Select by Location tool is not working.

Is my syntax correct?  In the for loop, I defined for "Cup_row in C_update"; do I just pass "Cup_row" as the input to the "SelectLayerByLocation_management" tool

The "A2" exception is displaying "Object: Error in executing tool"

for Cup_row in C_update:

    C_count+=1
            
    Cup_totalest = Cup_row[0] 
    Cup_totalmoe = Cup_row[1]
    Cup_pairid   = Cup_row[2]
    Cup_subpairs = Cup_row[3]
    Cup_OX       = Cup_row[4]
    Cup_OY       = Cup_row[5]
    Cup_DX       = Cup_row[6]
    Cup_DY       = Cup_row[7]

    #initial values
    Cup_totalest = 0.0
    Cup_totalmoe = 0.0
    Cup_pairid = ""
    C_pairlist = []        
    if(p_er):print "A1"
    #---Select Subpairs containted by corridor---##
    try:
        arcpy.SelectLayerByLocation_management(Cup_row, "COMPLETELY_CONTAINS", P_paths, "", "NEW_SELECTION")
    except Exception as A2:
        arcpy.AddError(A2)
        print "A2 "+ str(A2)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

No, you can't pass a tuple (the "row") to the Select by Location tool. You should be able to pass a geometry though.

And if I understand what you're trying to do correctly, you have your select from and select by features in the wrong order.

Try:

with arcpy.da.SearchCursor(fc, ["SHAPE@", "totalest", ..., "DY"]) as C_update:

    for Cup_row in C_update:  

        C_count+=1  

        Cup_shape = Cup_row[0]  
        Cup_totalest = Cup_row[1]  
        ...
        Cup_DY      = Cup_row[8]  

        ...
        arcpy.SelectLayerByLocation_management(P_paths, "COMPLETELY_CONTAINS", Cup_shape, "", "NEW_SELECTION") 

View solution in original post

2 Replies
Luke_Pinner
MVP Regular Contributor

No, you can't pass a tuple (the "row") to the Select by Location tool. You should be able to pass a geometry though.

And if I understand what you're trying to do correctly, you have your select from and select by features in the wrong order.

Try:

with arcpy.da.SearchCursor(fc, ["SHAPE@", "totalest", ..., "DY"]) as C_update:

    for Cup_row in C_update:  

        C_count+=1  

        Cup_shape = Cup_row[0]  
        Cup_totalest = Cup_row[1]  
        ...
        Cup_DY      = Cup_row[8]  

        ...
        arcpy.SelectLayerByLocation_management(P_paths, "COMPLETELY_CONTAINS", Cup_shape, "", "NEW_SELECTION") 
MichaelKowalczyk
New Contributor III

Thank you so much! switching the positions and passing the geometry fixed this immediate error!

<< arcpy.SelectLayerByLocation_management(P_paths, "COMPLETELY_CONTAINS", Cup_shape, "", "NEW_SELECTION") >>

0 Kudos