Select to view content in your preferred language

Select By Location has output when done manually but not when done by script?

426
7
01-27-2026 10:52 AM
AlfredBaldenweck
MVP Frequent Contributor

I'm trying to script a workflow that involves selecting a polygon and then selecting by location a different polygon.

When I do this manually in the map, it works great. If I do it in the script (same record), there isn't any output.

What's going on here, and how do I get around it?

0 Kudos
7 Replies
DanPatterson
MVP Esteemed Contributor

Select Layer By Location (Data Management)—ArcGIS Pro | Documentation

does it follow the code examples in the help topic?  The output should be a layer if a selection was matched


... sort of retired...
MErikReedAugusta
MVP Regular Contributor

I just ran into a very similar (possibly exact opposite?) issue about 10 minutes ago, and it was because I was feeding in a Feature Class, instead of a Feature Layer.  Feature Classes can't hold selections, so it was ignoring my selection and trying to work from the entire Feature Class.

Is that maybe what's going on in your case?  Instead of selecting nothing, maybe it's selecting everything, and the results are just skewing in a direction that looks the same?

------------------------------
M Reed
"The pessimist may be right oftener than the optimist, but the optimist has more fun, and neither can stop the march of events anyhow." — Lazarus Long, in Time Enough for Love, by Robert A. Heinlein
0 Kudos
AlfredBaldenweck
MVP Frequent Contributor

contains.gif

Well, to update:

I went through again. Somehow, Pro thinks that this orange hatched square does not contain the blue rectangle (even though it very much does???).

However, setting the search distance to -1 meters seems to work just fine with Contains (This interaction isn't documented, but if it works...)

I've had this issue on Select By Location and Add/Spatial Join. I'm gonna give this another shot in the script with the search distance and contains and see what's up.

0 Kudos
DavidSolari
MVP Regular Contributor

For the two points where the blue vertices hit the edge of the orange square, are there matching vertices on the orange square? I've had issues with spatial selections where minute imprecisions along edges cause the inner feature to bleed out of the outer feature, even if it seems mathematically sound. If there are matching vertices then yeah this is a bug.

0 Kudos
AlfredBaldenweck
MVP Frequent Contributor

It's not my personal data, but yeah I think you're right. The two tables, as far as I know, are supposed to participate in a topology (This is PLSS data). The blue rectangle very slightly overhangs the border and I think that's what's causing the problem after all.

AlfredBaldenweck_0-1769548861816.png

I wonder if the search distance acts as like, a negative buffer or something and feeds in a smaller shape, and that's why it works? That would kind of make sense to me, and on a scale of 20 acres, 1 meter all around is an acceptable loss.

0 Kudos
DanPatterson
MVP Esteemed Contributor

It is useful to see the applicable selectors for polygon selecting polygon in the visual guide

Select By Location graphic examples—ArcGIS Pro | Documentation

under Select polygon using polygon

The other examples are useful as well


... sort of retired...
JonathanDelura
Emerging Contributor

You need to first create a layer from the first selected polygon and then use this layer to select the second polygon:

#Create a layer from selected features.
select_layer = arcpy.management.SelectLayerByLocation(infeat1,
                                                    {overlap_type},
                                                    selfeat1)

#Use layer created from first selection for second selection.
arcpy.management.SelectLayerByLocation(infeat2, 
                                       {overlap_type}, 
                                       select_layer)

 

0 Kudos