SelectLayerbyLocation nested in SearchCursor

674
5
Jump to solution
01-21-2020 11:40 AM
QuangTruong
New Contributor II

Hi, I am getting an error when I try and run this code, but can't figure out why. Is there something I am missing? 

Thanks,

resultCoT = 'A'
with arcpy.da.SearchCursor(inputpoints, '*') as pointList:
    for point in pointList:
        countTest = arcpy.SelectLayerByLocation_management(inputlines, "INTERSECT", point, "", "NEW_SELECTION")
        with arcpy.da.SearchCursor(countTest, '*') as lineList:
             for line in linelist:
                 if line[0] == 'PROPERTY':
                     count = count + 1
        if count == 2:
            resultCoT = 'B'
print(resultCoT)

The error I get at the SelectLayerByLocation tool line is:

RuntimeError: Object: Error in executing tool

Shows you how little I know. . . 

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

You can pass a single geometry, like a point, to Select Layer By Location.  Looking at your code, I am guessing the issue is more with the fact that you are passing a Python tuple instead of an ArcPy geometry object.  Additionally, you are using "*" to define the fields, which means all fields and not just geometry.

Try:

resultCoT = 'A'
with arcpy.da.SearchCursor(inputpoints, 'SHAPE@') as pointList:
    for point, in pointList:  # important to have comma after point variable for tuple unpacking
        countTest = arcpy.SelectLayerByLocation_management(inputlines, "INTERSECT", point, "", "NEW_SELECTION")
        with arcpy.da.SearchCursor(countTest, '*') as lineList:
             for line in linelist:
                 if line[0] == 'PROPERTY':
                     count = count + 1
        if count == 2:
            resultCoT = 'B'
print(resultCoT)

View solution in original post

5 Replies
DanPatterson_Retired
MVP Emeritus

'point' isn't a feature layer it looks to be a row in a cursor

Select Layer By Location—Data Management toolbox | ArcGIS Desktop 

0 Kudos
QuangTruong
New Contributor II

Yes, I am starting to see. 

So a row is not a feature layer, and therefore can't be passed into the SelectLayerByLocation too. What can I do to test each point in a feature layer like I want to?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You can pass a single geometry, like a point, to Select Layer By Location.  Looking at your code, I am guessing the issue is more with the fact that you are passing a Python tuple instead of an ArcPy geometry object.  Additionally, you are using "*" to define the fields, which means all fields and not just geometry.

Try:

resultCoT = 'A'
with arcpy.da.SearchCursor(inputpoints, 'SHAPE@') as pointList:
    for point, in pointList:  # important to have comma after point variable for tuple unpacking
        countTest = arcpy.SelectLayerByLocation_management(inputlines, "INTERSECT", point, "", "NEW_SELECTION")
        with arcpy.da.SearchCursor(countTest, '*') as lineList:
             for line in linelist:
                 if line[0] == 'PROPERTY':
                     count = count + 1
        if count == 2:
            resultCoT = 'B'
print(resultCoT)
QuangTruong
New Contributor II

Yes, that appears to be the issue. I assumed that by specifying the fields as '*', all fields and geometry would be passed in.  

I was able to make the code snippet work by specifically passing through the 'SHAPE@' field. The revised code as you wrote it works. 

Thank you both so much. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The asterisk/wildcard for fields will return geometry information but not a complete ArcPy Geometry object; instead, a tuple of the geometry's centroid is returned.  If you want a full-blown ArcPy Geometry object, you need to specify SHAPE@