Select to view content in your preferred language

Python Error: arcpy.SelectLayerByLocation_management

2798
4
Jump to solution
09-30-2016 04:52 AM
JerryGarcia
Frequent Contributor

See screen shot.

Any suggestions?

Thanks!!!

import arcpy

wk=r'C:\Users\Administrator\AppData\Roaming\Esri\Desktop10.3\ArcCatalog\GIS DataCreator.sde'

arcpy.env.workspace=wk

print arcpy.env.workspace

fcCounties = "GIS.DATACREATOR.counties"
fieldsCounties = ['OBJECTID_1', 'Shape', 'NAME']
print arcpy.Exists(fcCounties)

fcSections = "GIS.DATACREATOR.sections_test"
fieldsSections = ['OBJECTID', 'Shape', 'county']
print arcpy.Exists(fcSections)

arcpy.MakeFeatureLayer_management(fcSections, 'sections_lyr')
arcpy.MakeFeatureLayer_management(fcCounties, 'counties_lyr')

with arcpy.da.SearchCursor('counties_lyr', fieldsCounties) as cursor:
for row in cursor:
print row[1]
arcpy.SelectLayerByLocation_management('sections_lyr', row[1])

----------

Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
C:\Users\Administrator\AppData\Roaming\ESRI\Desktop10.3\ArcCatalog\GIS DataCreator.sde
True
True
(-9645043.375313655, 3833831.999483527)

Traceback (most recent call last):
File "E:\Python\relateCopyCounties.py", line 23, in <module>
arcpy.SelectLayerByLocation_management('sections_lyr', row[1])
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 7359, in SelectLayerByLocation
raise e
RuntimeError: Object: Error in executing tool
>>>

0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Honored Contributor

Take a look at the example I sent in my previous message. Your problem is that you need to provide the 3rd parameter as a feature class or a geometry object. By default the da cursor will return geometry as a tuple. You will want to use the Shape@ token to have it return the geometry object.

Just to provide a little more detail I've also included my logic below. 

# Example 1 
geom = next(arcpy.da.SearchCursor("fnet", "SHAPE"))[0]
arcpy.management.SelectLayerByLocation("fnet", "INTERSECT", geom)
''' In the above example "SHAPE" returns (2124.3204345703125, 3.0001017252604063) '''

# Example 2
geom = next(arcpy.da.SearchCursor("fnet", "SHAPE@XY"))[0]
arcpy.management.SelectLayerByLocation("fnet", "INTERSECT", geom)
''' In the above example "SHAPE@XY" returns (2124.3204345703125, 3.0001017252604063) '''

# Example 3 
geom = next(arcpy.da.SearchCursor("fnet", "SHAPE@"))[0]
arcpy.management.SelectLayerByLocation("fnet", "INTERSECT", geom)
''' In the above example "SHAPE@" returns <Polygon object at 0x2382fcf0[0x2382f3e0]> '''

In the above examples note what is returned from the cursor. If you were to run each of these expressions you'd not that only the third one works, whereas the first two return the following exception.

Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 7320, in SelectLayerByLocation
raise e
RuntimeError: Object: Error in executing tool

View solution in original post

4 Replies
FreddieGibson
Honored Contributor

Take a look at the parameters for the tool.

Select Layer By Location

http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/select-layer-by-location.ht... 

Note that the second parameter is "overlap_type" (e.g. INTERSECT, CONTAINS, etc.) and you're supplying row[1], which is a tuple representation of the geometry with the value (-9645043.375313655, 3833831.999483527). 

JerryGarcia
Frequent Contributor

Thanks.  I was messing around with the parameters.  This is a mistake.  I get the same error when adding {overlap_type}:

arcpy.SelectLayerByLocation_management('sections_lyr', 'INTERSECT', row[1])

0 Kudos
FreddieGibson
Honored Contributor

Take a look at the example I sent in my previous message. Your problem is that you need to provide the 3rd parameter as a feature class or a geometry object. By default the da cursor will return geometry as a tuple. You will want to use the Shape@ token to have it return the geometry object.

Just to provide a little more detail I've also included my logic below. 

# Example 1 
geom = next(arcpy.da.SearchCursor("fnet", "SHAPE"))[0]
arcpy.management.SelectLayerByLocation("fnet", "INTERSECT", geom)
''' In the above example "SHAPE" returns (2124.3204345703125, 3.0001017252604063) '''

# Example 2
geom = next(arcpy.da.SearchCursor("fnet", "SHAPE@XY"))[0]
arcpy.management.SelectLayerByLocation("fnet", "INTERSECT", geom)
''' In the above example "SHAPE@XY" returns (2124.3204345703125, 3.0001017252604063) '''

# Example 3 
geom = next(arcpy.da.SearchCursor("fnet", "SHAPE@"))[0]
arcpy.management.SelectLayerByLocation("fnet", "INTERSECT", geom)
''' In the above example "SHAPE@" returns <Polygon object at 0x2382fcf0[0x2382f3e0]> '''

In the above examples note what is returned from the cursor. If you were to run each of these expressions you'd not that only the third one works, whereas the first two return the following exception.

Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 7320, in SelectLayerByLocation
raise e
RuntimeError: Object: Error in executing tool

JerryGarcia
Frequent Contributor

THANKS!

0 Kudos