Skip to content

Commit ed740af

Browse files
committed
feat: Add to_geodataframe()
1 parent 9859d25 commit ed740af

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

pyposeidon/boundary.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import logging
1717
import shapely
1818
from tqdm.auto import tqdm
19+
from pyposeidon.tools import to_geodataframe
1920
from pyposeidon.utils.coastfix import simplify
2021
import sys
2122

@@ -56,7 +57,7 @@ def __init__(self, **kwargs):
5657

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

9798
else:
9899
try:
99-
self.geometry = gp.read_file(geometry)
100+
self.geometry = to_geodataframe(geometry)
100101
except:
101102
logger.warning("geometry is not a file, trying with geopandas Dataset")
102103
if isinstance(geometry, gp.GeoDataFrame):
@@ -107,7 +108,7 @@ def __init__(self, **kwargs):
107108

108109
else:
109110
try:
110-
self.geometry = gp.read_file(geometry)
111+
self.geometry = to_geodataframe(geometry)
111112
except:
112113
logger.warning("geometry is not a file, trying with geopandas Dataset")
113114
if isinstance(geometry, gp.GeoDataFrame):

pyposeidon/tools.py

+11
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,14 @@ def get_netcdf_encoding(
583583
)
584584
update_or_add(encoding, var, params)
585585
return encoding
586+
587+
588+
def to_geodataframe(
589+
path: str | os.PathLike[str],
590+
**kwargs: T.Any,
591+
) -> gpd.GeoDataFrame:
592+
if str(path).endswith("parquet") or str(path).endswith("pq"):
593+
gdf = gpd.read_parquet(path=path, **kwargs)
594+
else:
595+
gdf = gpd.read_file(filename=path, **kwargs)
596+
return gdf

tests/test_boundary.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import pyposeidon.boundary as pb
2-
import pytest
31
import geopandas as gp
2+
import pytest
43

4+
import pyposeidon.boundary as pb
55
from . import DATA_DIR
6+
from pyposeidon.tools import to_geodataframe
67

78
noaa = DATA_DIR / "bl.zip"
89

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

11-
land = gp.read_file(COAST_FILE)
12+
land = to_geodataframe(COAST_FILE)
1213
coast = gp.GeoDataFrame(geometry=land.boundary)
1314

1415
INPUTS = pytest.mark.parametrize("input", [land, coast])

tests/test_dem_fix.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import pyposeidon.dem as pdem
2-
import pyposeidon.mesh as pmesh
31
import pytest
4-
import numpy as np
5-
import geopandas as gp
62

3+
import pyposeidon.dem as pdem
4+
import pyposeidon.mesh as pmesh
75
from . import DATA_DIR
6+
from pyposeidon.tools import to_geodataframe
87

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

@@ -29,7 +28,7 @@
2928

3029
@pytest.fixture(scope="session")
3130
def coasts():
32-
coast = gp.read_file(COAST_FILE)
31+
coast = to_geodataframe(COAST_FILE)
3332
return coast
3433

3534

0 commit comments

Comments
 (0)