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. . .
Solved! Go to Solution.
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)
'point' isn't a feature layer it looks to be a row in a cursor
Select Layer By Location—Data Management toolbox | ArcGIS Desktop
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?
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)
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.
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@