Skip to content

Commit

Permalink
Fix issues with potentially empty datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
SorooshMani-NOAA committed Aug 1, 2024
1 parent 383b4d1 commit df4ca8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
29 changes: 17 additions & 12 deletions adcircpy/mesh/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand All @@ -209,6 +210,8 @@ def edges(self) -> GeoDataFrame:
'type': ring.type,
}
)
if len(data) == 0:
return GeoDataFrame()
return GeoDataFrame(data, crs=self._grd.crs)


Expand All @@ -217,14 +220,16 @@ 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)
data.append({'geometry': geometry, 'bnd_id': bnd_id, 'type': 'exterior'})
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):
Expand Down Expand Up @@ -258,32 +263,32 @@ def geodataframe(self) -> GeoDataFrame:
'bnd_id': bnd_id,
}
)
if len(data) == 0:
return GeoDataFrame()
return GeoDataFrame(data, crs=self._grd.crs)

@property
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
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)
Expand Down
8 changes: 6 additions & 2 deletions adcircpy/mesh/fort14.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = gpd.GeoDataFrame(data, crs=self._mesh.crs)
return self._gdf

def __eq__(self, other: 'BaseBoundaries') -> bool:
Expand Down Expand Up @@ -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 = gpd.GeoDataFrame(data, crs=self._mesh.crs)
return self._gdf


Expand Down

0 comments on commit df4ca8e

Please sign in to comment.