Skip to content

Commit

Permalink
Warning for Polygon wiht z dimension (scverse#493)
Browse files Browse the repository at this point in the history
added warning in ShapesModel for Polygon wiht z dimension
  • Loading branch information
LucaMarconato authored Mar 19, 2024
1 parent 421c315 commit 7f90f0f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/spatialdata/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ def validate(cls, data: GeoDataFrame) -> None:
raise ValueError("Radii of circles must be positive.")
if cls.TRANSFORM_KEY not in data.attrs:
raise ValueError(f":class:`geopandas.GeoDataFrame` does not contain `{TRANSFORM_KEY}`.")
if len(data) > 0:
n = data.geometry.iloc[0]._ndim
if n != 2:
warnings.warn(
f"The geometry column of the GeoDataFrame has {n} dimensions, while 2 is expected. Please consider "
"discarding the third dimension as it could led to unexpected behaviors.",
UserWarning,
stacklevel=2,
)

@singledispatchmethod
@classmethod
Expand Down
7 changes: 4 additions & 3 deletions tests/core/test_data_extent.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ def test_get_extent_shapes(shape_type):
)


def test_get_extent_points():
@pytest.mark.parametrize("exact", [True, False])
def test_get_extent_points(exact: bool):
# 2d case
extent = get_extent(sdata["blobs_points"])
extent = get_extent(sdata["blobs_points"], exact=exact)
check_test_results0(
extent,
min_coordinates=np.array([3.0, 4.0]),
Expand All @@ -68,7 +69,7 @@ def test_get_extent_points():
data = np.array([[1, 2, 3], [4, 5, 6]])
df = pd.DataFrame(data, columns=["zeta", "x", "y"])
points_3d = PointsModel.parse(df, coordinates={"x": "x", "y": "y", "z": "zeta"})
extent_3d = get_extent(points_3d)
extent_3d = get_extent(points_3d, exact=exact)
check_test_results0(
extent_3d,
min_coordinates=np.array([2, 3, 1]),
Expand Down
13 changes: 13 additions & 0 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,16 @@ def test_points_and_shapes_conversions(shapes, points):
t2 = get_transformation(points2, get_all=True)
assert t0 == t1
assert t0 == t2


def test_model_polygon_z():
import geopandas as gpd
from shapely.geometry import Polygon

polygon = Polygon([(0, 0, 0), (1, 1, 0), (2, 0, 0)])

with pytest.warns(
UserWarning,
match="The geometry column of the GeoDataFrame has 3 dimensions, while 2 is expected. Please consider",
):
_ = ShapesModel.parse(gpd.GeoDataFrame(geometry=[polygon]))

0 comments on commit 7f90f0f

Please sign in to comment.