Skip to content

Commit

Permalink
Allow Polygon geometry for Basin / Area. (#1875)
Browse files Browse the repository at this point in the history
Fixes #1850
  • Loading branch information
evetion authored Oct 7, 2024
1 parent e9339b8 commit 7e3cd9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion python/ribasim/ribasim/geometry/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pandera.dtypes import Int32
from pandera.typing import Index, Series
from pandera.typing.geopandas import GeoSeries
from shapely.geometry import MultiPolygon
from shapely.geometry import MultiPolygon, Polygon

from .base import _GeoBaseSchema

Expand All @@ -11,3 +11,9 @@ class BasinAreaSchema(_GeoBaseSchema):
fid: Index[Int32] = pa.Field(default=0, check_name=True)
node_id: Series[Int32] = pa.Field(nullable=False, default=0)
geometry: GeoSeries[MultiPolygon] = pa.Field(default=None, nullable=True)

@pa.parser("geometry")
def convert_to_multi(cls, series):
return series.apply(
lambda geom: MultiPolygon([geom]) if isinstance(geom, Polygon) else geom
)
17 changes: 16 additions & 1 deletion python/ribasim/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
pid_control,
pump,
)
from shapely import Point
from shapely import MultiPolygon, Point, Polygon


def test_duplicate_edge(basic):
Expand Down Expand Up @@ -171,3 +171,18 @@ def test_minimum_control_neighbor():
],
)
model.write("test.toml")


def test_geometry_validation():
point = Point(0.0, 0.0)
poly = point.buffer(1.0)

assert isinstance(poly, Polygon)
basinarea = basin.Area(geometry=[poly])
assert isinstance(basinarea.df.geometry[0], MultiPolygon)

basinarea = basin.Area(geometry=[basinarea.df.geometry[0]])
assert isinstance(basinarea.df.geometry[0], MultiPolygon)

with pytest.raises(ValueError):
basin.Area(geometry=[point])

0 comments on commit 7e3cd9d

Please sign in to comment.