-
Notifications
You must be signed in to change notification settings - Fork 4
/
close_hole.py
34 lines (29 loc) · 1.07 KB
/
close_hole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import glob
import geopandas as gpd
from shapely.geometry import Polygon
def close_holes(poly: Polygon) -> Polygon:
"""
Close polygon holes by limitation to the exterior ring.
Args:
poly: Input shapely Polygon
Example:
df.geometry.apply(lambda p: close_holes(p))
"""
if poly.interiors:
return Polygon(list(poly.exterior.coords))
else:
return poly
if __name__ == '__main__':
files = glob.glob('./*.shp')
for f_in in files:
f_out = os.path.splitext(os.path.basename(f_in))[0] + '_closed.shp'
gdf = gpd.read_file(f_in, encoding='gbk')
gdf.geometry = gdf.geometry.apply(lambda p: close_holes(p))
gdf.to_file(f_out, encoding='gbk')
# uncomment to export as .kml format
# from osgeo import gdal
# f_out_kml = os.path.splitext(os.path.basename(f_in))[0] + '_closed.kml'
# ds_kml = gdal.VectorTranslate(destNameOrDestDS=f_out_kml,
# srcDS=f_out, format='KML')
# ds_kml = None