Python script to find all layers intersecting a polygon

2245
9
Jump to solution
03-10-2021 12:39 PM
JoseSanchez
Occasional Contributor III

Hello everyone,

 

I am looking for a Python script to find all layers intersecting a polygon.

 

Thanks

0 Kudos
1 Solution

Accepted Solutions
JeffK
by MVP Regular Contributor
MVP Regular Contributor

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')

 

View solution in original post

9 Replies
DavidPike
MVP Frequent Contributor

what do you mean by layer?

JoeBorgione
MVP Esteemed Contributor

I'm wondering what is meant by 'looking for'....

That should just about do it....
0 Kudos
DanPatterson
MVP Esteemed Contributor

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.


... sort of retired...
JoseSanchez
Occasional Contributor III

 

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

 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

JeffK
by MVP Regular Contributor
MVP Regular Contributor

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')

 

JoseSanchez
Occasional Contributor III

JeffK thanks for sharing.

0 Kudos
JohnMorgan
New Contributor III

Nice work, Jeff!

0 Kudos
JohnMorgan
New Contributor III

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

0 Kudos