I am trying to use Select Layers By Location tool in Python.
I have a polygon feature class that I would like to iterate though each feature and do a select layers by location in order to get a list of layers that each polygon intersects with.
Can I use a search cursor to iterate through the polygons and pass the geometry to Select Layers By Location as the select_features parameter?
extents = sys.argv[1]
#polygon shapefile containing 4 polygons
try: arcpy.env.workspace = "C:\Dataset.gdb" #get extents spatial reference ex_sp = arcpy.Describe(extents).spatialReference fcs = arcpy.ListFeatureClasses() with da.SearchCursor(extents,["SHAPE@"]) as scursor: for row in scursor: boundary = row[0] Output_Layer_Names, Count = arcpy.management.SelectLayerByLocation(in_layer=fcs, overlap_type="INTERSECT", select_features=boundary, search_distance="", selection_type="NEW_SELECTION", invert_spatial_relationship="NOT_INVERT") print(Output_Layer_Names,Count) except:
Any help is appreciated
Thanks,
Arthur
Solved! Go to Solution.
Your issue is with this:
Output_Layer_Names, Count =
You are trying to unpack the Result object's outputs, but you haven't given the correct number of variables to unpack into. The following should work:
_, Output_Layer_Names, Count =
what trackback error did you get when you ran it?
too many values to unpack (expected 2)
I also get the same error if I try creating a new polygon object and passing that in
this
fcs = arcpy.ListFeatureClasses()
then this
.... SelectLayerByLocation(in_layer=fcs....,
how many featureclasses are there? perhaps you want the first? slice with ....ListFeatureClasses()[0] perhaps.
It would be easier to reference with code formatting
/blogs/dan_patterson/2016/08/14/script-formatting
and if you dump the try-except block, you will still get real error messages
Also the help topic still says layers are required. You can do geometry intersections using arcpy directly
Your issue is with this:
Output_Layer_Names, Count =
You are trying to unpack the Result object's outputs, but you haven't given the correct number of variables to unpack into. The following should work:
_, Output_Layer_Names, Count =
Yup, that did it. Thanks.