Skip to content

Commit 05a7380

Browse files
committed
Add support for simplifying polygons
1 parent a820dcd commit 05a7380

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

samgeo/common.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,6 +2879,35 @@ def regularize(source, output=None, crs="EPSG:4326", **kwargs):
28792879
return result
28802880

28812881

2882+
def simplify(source, tolerance=0.01, output=None, **kwargs):
2883+
"""Simplify a polygon GeoDataFrame.
2884+
2885+
Args:
2886+
source (str | gpd.GeoDataFrame): The input file path or a GeoDataFrame.
2887+
tolerance (float, optional): The tolerance value for simplification. Defaults to 0.01.
2888+
output (str, optional): The output file path. Defaults to None.
2889+
2890+
Returns:
2891+
gpd.GeoDataFrame: The output GeoDataFrame.
2892+
"""
2893+
if isinstance(source, str):
2894+
gdf = gpd.read_file(source)
2895+
elif isinstance(source, gpd.GeoDataFrame):
2896+
gdf = source
2897+
else:
2898+
raise ValueError("The input source must be a GeoDataFrame or a file path.")
2899+
2900+
polygons = gdf.geometry.apply(
2901+
lambda geom: geom.simplify(tolerance, preserve_topology=True, **kwargs)
2902+
)
2903+
result = gpd.GeoDataFrame(geometry=polygons, data=gdf.drop("geometry", axis=1))
2904+
2905+
if output is not None:
2906+
result.to_file(output)
2907+
else:
2908+
return result
2909+
2910+
28822911
def split_raster(filename, out_dir, tile_size=256, overlap=0):
28832912
"""Split a raster into tiles.
28842913

0 commit comments

Comments
 (0)