can a geometry object be used with select layers by location?

1332
5
Jump to solution
10-02-2020 12:23 PM
ArthurAbbott1
New Contributor

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

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

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 = 

View solution in original post

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

what trackback error did you get when you ran it?


... sort of retired...
0 Kudos
ArthurAbbott1
New Contributor

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

0 Kudos
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
JoshuaBixby
MVP Esteemed Contributor

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 = 
0 Kudos
ArthurAbbott1
New Contributor

Yup, that did it.  Thanks.

0 Kudos