Skip to content

Commit

Permalink
feat: Add to_geodataframe()
Browse files Browse the repository at this point in the history
  • Loading branch information
pmav99 committed Aug 22, 2024
1 parent 9859d25 commit ed740af
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
7 changes: 4 additions & 3 deletions pyposeidon/boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import shapely
from tqdm.auto import tqdm
from pyposeidon.tools import to_geodataframe
from pyposeidon.utils.coastfix import simplify
import sys

Expand Down Expand Up @@ -56,7 +57,7 @@ def __init__(self, **kwargs):

elif isinstance(coastlines, str):
logger.info("reading {}".format(coastlines))
coasts = gp.GeoDataFrame.from_file(coastlines)
coasts = to_geodataframe(coastlines)
# check coastlines
if coasts.buffer(0).is_valid.all() and (coasts.buffer(0).boundary.geom_type == "LineString").all():
self.coasts = gp.GeoDataFrame(geometry=coasts.buffer(0))
Expand Down Expand Up @@ -96,7 +97,7 @@ def __init__(self, **kwargs):

else:
try:
self.geometry = gp.read_file(geometry)
self.geometry = to_geodataframe(geometry)
except:
logger.warning("geometry is not a file, trying with geopandas Dataset")
if isinstance(geometry, gp.GeoDataFrame):
Expand All @@ -107,7 +108,7 @@ def __init__(self, **kwargs):

else:
try:
self.geometry = gp.read_file(geometry)
self.geometry = to_geodataframe(geometry)
except:
logger.warning("geometry is not a file, trying with geopandas Dataset")
if isinstance(geometry, gp.GeoDataFrame):
Expand Down
11 changes: 11 additions & 0 deletions pyposeidon/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,14 @@ def get_netcdf_encoding(
)
update_or_add(encoding, var, params)
return encoding


def to_geodataframe(
path: str | os.PathLike[str],
**kwargs: T.Any,
) -> gpd.GeoDataFrame:
if str(path).endswith("parquet") or str(path).endswith("pq"):
gdf = gpd.read_parquet(path=path, **kwargs)
else:
gdf = gpd.read_file(filename=path, **kwargs)
return gdf
7 changes: 4 additions & 3 deletions tests/test_boundary.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import pyposeidon.boundary as pb
import pytest
import geopandas as gp
import pytest

import pyposeidon.boundary as pb
from . import DATA_DIR
from pyposeidon.tools import to_geodataframe

noaa = DATA_DIR / "bl.zip"

COAST_FILE = (DATA_DIR / "ocean.parquet").as_posix()

land = gp.read_file(COAST_FILE)
land = to_geodataframe(COAST_FILE)
coast = gp.GeoDataFrame(geometry=land.boundary)

INPUTS = pytest.mark.parametrize("input", [land, coast])
Expand Down
9 changes: 4 additions & 5 deletions tests/test_dem_fix.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pyposeidon.dem as pdem
import pyposeidon.mesh as pmesh
import pytest
import numpy as np
import geopandas as gp

import pyposeidon.dem as pdem
import pyposeidon.mesh as pmesh
from . import DATA_DIR
from pyposeidon.tools import to_geodataframe

COAST_FILE = (DATA_DIR / "ocean.parquet").as_posix()

Expand All @@ -29,7 +28,7 @@

@pytest.fixture(scope="session")
def coasts():
coast = gp.read_file(COAST_FILE)
coast = to_geodataframe(COAST_FILE)
return coast


Expand Down

0 comments on commit ed740af

Please sign in to comment.