From 6281c30c430fc0e5f65ec4243d6868f2ff837b4e Mon Sep 17 00:00:00 2001 From: Huite Bootsma Date: Wed, 13 Mar 2024 10:32:03 +0100 Subject: [PATCH 1/2] Initialize spatial table as GeoDataFrame --- python/ribasim/ribasim/input_base.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/ribasim/ribasim/input_base.py b/python/ribasim/ribasim/input_base.py index c76ca6cbd..df39ccdd7 100644 --- a/python/ribasim/ribasim/input_base.py +++ b/python/ribasim/ribasim/input_base.py @@ -17,6 +17,7 @@ import pandas as pd import pandera as pa from pandera.typing import DataFrame +from pandera.typing.geopandas import GeoDataFrame from pydantic import BaseModel as PydanticBaseModel from pydantic import ( ConfigDict, @@ -339,6 +340,8 @@ def _repr_html_(self): class SpatialTableModel(TableModel[TableT], Generic[TableT]): + df: GeoDataFrame[TableT] | None = Field(default=None, exclude=True, repr=False) + @classmethod def _from_db(cls, path: FilePath, table: str): with connect(path) as connection: From 5d5bd8bd84e599b9ce5a986a95b9dfeafeea0e5b Mon Sep 17 00:00:00 2001 From: Hofer-Julian Date: Wed, 13 Mar 2024 11:13:50 +0100 Subject: [PATCH 2/2] Fix mypy warnings --- python/ribasim/ribasim/config.py | 2 +- python/ribasim/ribasim/geometry/edge.py | 2 +- python/ribasim/ribasim/input_base.py | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/python/ribasim/ribasim/config.py b/python/ribasim/ribasim/config.py index e28c73ed2..b34710bb0 100644 --- a/python/ribasim/ribasim/config.py +++ b/python/ribasim/ribasim/config.py @@ -132,7 +132,7 @@ def add(self, node: Node, tables: Sequence[TableModel[Any]] | None = None) -> No node_type=self.__class__.__name__, ) self.node.df = ( - node_table # type: ignore + node_table if self.node.df is None else pd.concat([self.node.df, node_table]) ) diff --git a/python/ribasim/ribasim/geometry/edge.py b/python/ribasim/ribasim/geometry/edge.py index 7bb69e944..c95e5d96a 100644 --- a/python/ribasim/ribasim/geometry/edge.py +++ b/python/ribasim/ribasim/geometry/edge.py @@ -76,7 +76,7 @@ def add( if self.df is None: self.df = table_to_append else: - self.df = pd.concat([self.df, table_to_append]) # type: ignore + self.df = pd.concat([self.df, table_to_append]) def get_where_edge_type(self, edge_type: str) -> NDArray[np.bool_]: assert self.df is not None diff --git a/python/ribasim/ribasim/input_base.py b/python/ribasim/ribasim/input_base.py index df39ccdd7..b1cd8d6cc 100644 --- a/python/ribasim/ribasim/input_base.py +++ b/python/ribasim/ribasim/input_base.py @@ -361,10 +361,9 @@ def _write_table(self, path: FilePath) -> None: ---------- path : FilePath """ - - gdf = gpd.GeoDataFrame(data=self.df) - gdf = gdf.set_geometry("geometry") - gdf.to_file(path, layer=self.tablename(), driver="GPKG", mode="a") + if self.df is None: + return + self.df.to_file(path, layer=self.tablename(), driver="GPKG", mode="a") def sort(self): if self.df is not None: