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?
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
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?
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.
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.
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.
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.
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
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)