Hello everyone,
I am looking for a Python script to find all layers intersecting a polygon.
Thanks
Solved! Go to Solution.
I did this not too long ago and thought I'd share the code. Had to compare two featureclasses of polygons for intersecting features so I looped through one to select the other/ count the other like @JoshuaBixby described.
def intersectLayers():
bldgFp = os.path.join(wrkingdb, r'bldg_footprints')
bldgFP_Lyr = arcpy.MakeFeatureLayer_management(bldgFp, "bldgFP_lyr")
with arcpy.da.SearchCursor(pictBldgFP, ['OBJECTID', 'SHAPE@']) as cur:
for row in cur:
curIntersect = arcpy.SelectLayerByLocation_management(bldgFP_Lyr, "INTERSECT", row[1], '', 'NEW_SELECTION')
intersectCnt = int(arcpy.GetCount_management(curIntersect).getOutput(0))
if intersectCnt > 0:
print(f'{row[0]} intersects with {intersectCnt} features')
what do you mean by layer?
I'm wondering what is meant by 'looking for'....
Intersection is an intensive process. There is no simple check, the intersection has to be made and to complicate matters, you need to have an advanced license to perform more than a pairwise intersection.
Polygon—ArcGIS Pro | Documentation
in arcpy the polygon class has an intersects method which checks all shapes and performs the intersection.
there is also an "overlaps" property that does a similar thing, but returns a boolean but not the actual intersection (see clarification in that section of the help).
Loading all the layers, setting them selectable, then selecting your polygon in the map would quickly allow you to see whether there is any intersection and the geometry type of the selection.
I won't even address projection differences... a geometry may "intersect" another geometry after projection differences are taken into account.
From a specific polygon shape in a polygon feature class find all feature classes, points, lines, and polygons, that intersect with the polygon.
The goal is to find all the assets located in a project (polygon): devices (points), pipes (lines), other polygons (municipality, commission district, etc).
Thanks
The Select Layer By Location tool allows for an ArcPy Geometry object to be passed as the selecting layer. I would extract the polygon you want, store it in an ArcPy Geometry, then loop through all your feature layers in the map using Select Layer By Location on each one (with mixed geometry types, I would use basic intersection). After running Select Layer By Location, you run Get Count to see how many features were selected.
I did this not too long ago and thought I'd share the code. Had to compare two featureclasses of polygons for intersecting features so I looped through one to select the other/ count the other like @JoshuaBixby described.
def intersectLayers():
bldgFp = os.path.join(wrkingdb, r'bldg_footprints')
bldgFP_Lyr = arcpy.MakeFeatureLayer_management(bldgFp, "bldgFP_lyr")
with arcpy.da.SearchCursor(pictBldgFP, ['OBJECTID', 'SHAPE@']) as cur:
for row in cur:
curIntersect = arcpy.SelectLayerByLocation_management(bldgFP_Lyr, "INTERSECT", row[1], '', 'NEW_SELECTION')
intersectCnt = int(arcpy.GetCount_management(curIntersect).getOutput(0))
if intersectCnt > 0:
print(f'{row[0]} intersects with {intersectCnt} features')
JeffK thanks for sharing.
Nice work, Jeff!
I will just add that if you are working with a lot of features it may help to divide and conquer with a subsection first such as
intersect_lyr = arcpy.SelectLayerByLocation_management(one_fc, 'INTERSECT', two_fc)
Derek