Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ArcPy: Remove Holes in Polygons
#!python
# Reference: 1) https://pro.arcgis.com/en/pro-app/arcpy/classes/spatialreference.htm
# 2) https://pro.arcgis.com/en/pro-app/arcpy/classes/array.htm
# 3) https://pro.arcgis.com/en/pro-app/arcpy/classes/polygon.htm
# 4) https://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htm
# 5) https://docs.python.org/3/library/itertools.html
import arcpy
from itertools import takewhile
# Example 1: Remove holes in single polygon
polygon = # Polygon with holes
SR = polygon.spatialReference
polygon = arcpy.Polygon(
arcpy.Array(
(pt for pt in takewhile(bool, part))
for part
in polygon.getPart() or arcpy.Array(arcpy.Array())
), SR
)
# Example 2: Remove holes from polygons in feature class
fc = # Path to feature class containing polygons with holes
with arcpy.da.UpdateCursor(fc, "SHAPE@") as cur:
for polygon, in cur:
if polygon is None: continue
SR = polygon.spatialReference
polygon = arcpy.Polygon(
arcpy.Array(
(pt for pt in takewhile(bool, part))
for part
in polygon.getPart() or arcpy.Array(arcpy.Array())
), SR
)
cur.updateRow([polygon])
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment