Skip to content

Instantly share code, notes, and snippets.

@bixb0012
Last active July 31, 2019 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bixb0012/5cdc276d6e590fe14f2183fd2041c50c to your computer and use it in GitHub Desktop.
Save bixb0012/5cdc276d6e590fe14f2183fd2041c50c to your computer and use it in GitHub Desktop.
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