From 0ecd2fc7c2e440c6c7a968067fc7c7a33cd6a01b Mon Sep 17 00:00:00 2001 From: Noelle Cheng Date: Wed, 8 Oct 2025 11:28:27 +0800 Subject: [PATCH] add geometry validation to Sampler --- map2loop/sampler.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/map2loop/sampler.py b/map2loop/sampler.py index b4c7835c..baeefff0 100644 --- a/map2loop/sampler.py +++ b/map2loop/sampler.py @@ -90,6 +90,15 @@ def sample( Returns: pandas.DataFrame: the sampled data points """ + valid_geometry = ( + shapely.geometry.point.Point, + shapely.geometry.multipoint.MultiPoint + ) + + all_valid_geometries = all(isinstance(geom, valid_geometry) for geom in spatial_data.geometry) + if not all_valid_geometries: + raise ValueError("Invalid geometry types found in spatial_data") + data = spatial_data.copy() data["X"] = data.geometry.x data["Y"] = data.geometry.y @@ -149,6 +158,16 @@ def sample( Returns: pandas.DataFrame: the sampled data points """ + valid_geometry = ( + shapely.geometry.multipolygon.MultiPolygon, + shapely.geometry.polygon.Polygon, + shapely.geometry.multilinestring.MultiLineString, + shapely.geometry.linestring.LineString + ) + all_valid_geometries = all(isinstance(geom, valid_geometry) for geom in spatial_data.geometry) + if not all_valid_geometries: + raise ValueError("Invalid geometry types found in spatial_data") + schema = {"ID": str, "X": float, "Y": float, "featureId": str} df = pandas.DataFrame(columns=schema.keys()).astype(schema) for _, row in spatial_data.iterrows():