Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize spatial table as GeoDataFrame #1242

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/ribasim/ribasim/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
)
Expand Down
2 changes: 1 addition & 1 deletion python/ribasim/ribasim/geometry/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions python/ribasim/ribasim/input_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -339,6 +340,8 @@


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:
Expand All @@ -358,10 +361,9 @@
----------
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

Check warning on line 365 in python/ribasim/ribasim/input_base.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/input_base.py#L365

Added line #L365 was not covered by tests
self.df.to_file(path, layer=self.tablename(), driver="GPKG", mode="a")

def sort(self):
if self.df is not None:
Expand Down
Loading