diff --git a/adcircpy/mesh/base.py b/adcircpy/mesh/base.py index 21ddb0da..f9e0510d 100644 --- a/adcircpy/mesh/base.py +++ b/adcircpy/mesh/base.py @@ -7,8 +7,7 @@ import pathlib from typing import Hashable, Mapping, Union -import geopandas as gpd -from geopandas import GeoDataFrame +from geopandas import tools, GeoDataFrame from matplotlib.collections import PolyCollection from matplotlib.path import Path import matplotlib.pyplot as plt @@ -187,7 +186,9 @@ def gdf(self): } for id, element in self.elements.iterrows() ] - self._gdf = gpd.GeoDataFrame(data, crs=self.crs) + self._gdf = GeoDataFrame() + if len(data) > 0: + self._gdf = GeoDataFrame(data, crs=self.crs) return self._gdf @@ -209,6 +210,8 @@ def edges(self) -> GeoDataFrame: 'type': ring.type, } ) + if len(data) == 0: + return GeoDataFrame() return GeoDataFrame(data, crs=self._grd.crs) @@ -217,7 +220,7 @@ def __init__(self, grd: 'Grd'): self._grd = grd @lru_cache(maxsize=1) - def __call__(self) -> gpd.GeoDataFrame: + def __call__(self) -> GeoDataFrame: data = [] for bnd_id, rings in self.sorted().items(): geometry = LinearRing(self._grd.coords.iloc[rings['exterior'][:, 0], :].values) @@ -225,6 +228,8 @@ def __call__(self) -> gpd.GeoDataFrame: for interior in rings['interiors']: geometry = LinearRing(self._grd.coords.iloc[interior[:, 0], :].values) data.append({'geometry': geometry, 'bnd_id': bnd_id, 'type': 'interior'}) + if len(data) == 0: + return GeoDataFrame() return GeoDataFrame(data, crs=self._grd.crs) def exterior(self): @@ -258,6 +263,8 @@ def geodataframe(self) -> GeoDataFrame: 'bnd_id': bnd_id, } ) + if len(data) == 0: + return GeoDataFrame() return GeoDataFrame(data, crs=self._grd.crs) @property @@ -265,6 +272,8 @@ def exterior(self) -> GeoDataFrame: data = [] for exterior in self.rings().loc[self.rings()['type'] == 'exterior'].itertuples(): data.append({'geometry': Polygon(exterior.geometry.coords)}) + if len(data) == 0: + return GeoDataFrame() return GeoDataFrame(data, crs=self._grd.crs) @property @@ -272,18 +281,13 @@ def interior(self) -> GeoDataFrame: data = [] for interior in self.rings().loc[self.rings()['type'] == 'interior'].itertuples(): data.append({'geometry': Polygon(interior.geometry.coords)}) + if len(data) == 0: + return GeoDataFrame() return GeoDataFrame(data, crs=self._grd.crs) @property def implode(self) -> GeoDataFrame: - return GeoDataFrame( - { - 'geometry': MultiPolygon( - [polygon.geometry for polygon in self.geodataframe.itertuples()] - ) - }, - crs=self._grd.crs, - ) + return tools.collect(self.geodataframe) @property @lru_cache(maxsize=1) diff --git a/adcircpy/mesh/fort14.py b/adcircpy/mesh/fort14.py index 74c95397..be1d8be0 100644 --- a/adcircpy/mesh/fort14.py +++ b/adcircpy/mesh/fort14.py @@ -1,6 +1,6 @@ from typing import Union -import geopandas as gpd +from geopandas import GeoDataFrame from matplotlib.cm import ScalarMappable import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable @@ -55,7 +55,9 @@ def gdf(self): **boundary, } ) - self._gdf = gpd.GeoDataFrame(data, crs=self._mesh.crs) + self._gdf = GeoDataFrame() + if len(data) > 0: + self._gdf = GeoDataFrame(data, crs=self._mesh.crs) return self._gdf def __eq__(self, other: 'BaseBoundaries') -> bool: @@ -97,7 +99,9 @@ def gdf(self): **boundary, } ) - self._gdf = gpd.GeoDataFrame(data, crs=self._mesh.crs) + self._gdf = GeoDataFrame() + if len(data) > 0: + self._gdf = GeoDataFrame(data, crs=self._mesh.crs) return self._gdf diff --git a/adcircpy/plotting.py b/adcircpy/plotting.py index d00fa36c..1860be9d 100644 --- a/adcircpy/plotting.py +++ b/adcircpy/plotting.py @@ -8,7 +8,7 @@ import geopandas from matplotlib import pyplot from matplotlib.axes import Axes -from matplotlib.cm import get_cmap +from matplotlib import colormaps import numpy import requests from shapely.geometry import MultiPoint, MultiPolygon, Polygon @@ -92,7 +92,7 @@ def plot_polygons( colors = [kwargs['c'] for _ in range(len(geometries))] elif colors is None: colors = [ - get_cmap('gist_rainbow')(color_index / len(geometries)) + colormaps['gist_rainbow'](color_index / len(geometries)) for color_index in range(len(geometries)) ] diff --git a/pyproject.toml b/pyproject.toml index 72944c7b..3987ee74 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,9 +26,9 @@ enable = true python = '^3.8, <3.12' appdirs = '*' dunamai = { version = '*', optional = true } -geopandas = '<0.11' +geopandas = '*' haversine = '*' -matplotlib = '<3.9' +matplotlib = '*' netCDF4 = '*' numpy = '*' pandas = '*'