Get inner-most poly

Nice spatial relationship question today… a co-worker asked ‘how can i get only the inner-most poly from these clusters of polygons’. Looking at the data, none of the polygons overlap, these were clearly generated from contour lines.

SelectPeaksIn

There are likely a few ways to accomplish this, but our approach was as follows: recreate all polygons but fill in their inner holes, then count how many times each filled feature intersects an input feature… those that only intersect once are the inner-most.

The function we created returned the original dataset’s ObjectIDs

Code is as follows

import arcpy

#arcpy.env.overwriteOutput = True

def listOidOfPeaks(fc):
    oids = []

    # Eliminate ALL internal parts (basically fill in all the holes)
    arcpy.management.EliminatePolygonPart(fc , "in_memory\\EPP_tmp",
                        "AREA", "1000000 SquareKilometers", 0, "true")

    arcpy.management.MakeFeatureLayer("in_memory\\EPP_tmp", "EPP_lyr")


    with arcpy.da.SearchCursor(fc, ("OID@", "SHAPE@",)) as c:
        # for each of the original poly feature's labelPoint (which
        #  is a point INSIDE the polygon) get the list of filled
        #  features being overlapped
        for row in c:
            arcpy.management.SelectLayerByLocation("EPP_lyr", "INTERSECT",
                                        arcpy.PointGeometry(row[1].labelPoint))

            # put all the selected filled feature's ORIG_FID into a list
            oids += [i[0] for i in da.SearchCursor("EPP_lyr", ("ORIG_FID"))]

    # every OID which occurs only once are ones we want
    for i in sorted(oids):
        if oids.count(i) == 1:
            yield(i)


# what's returned is a generator
oids = listOidOfPeaks("contour_polys.shp")

# bonus : turn a list of ids into a where_clause (I'm assuming FID is field name)
print("as a query :  FID in ({})".format(",".join(map(str, oids))))


The resulting where_clause which we can use with a number of GP tools such as Select Layer By Attribute
SelectPeaksOut

Advertisements