Last active
July 31, 2019 17:55
-
-
Save bixb0012/5cdc276d6e590fe14f2183fd2041c50c to your computer and use it in GitHub Desktop.
ArcPy: Remove Holes in Polygons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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