Skip to content

Commit fa5a5d6

Browse files
authored
performance(to_geodataframe()): update GeoDataFrame construction routine (#2691)
1 parent fea6fe6 commit fa5a5d6

4 files changed

Lines changed: 61 additions & 9 deletions

File tree

flopy/discretization/grid.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,37 @@ def to_geodataframe(self, features, featuretype="Polygon"):
601601
-------
602602
GeoDataFrame
603603
"""
604-
from ..utils.geospatial_utils import GeoSpatialCollection
604+
from ..utils.utl_import import import_optional_dependency
605+
606+
gpd = import_optional_dependency("geopandas")
607+
shp_geom = import_optional_dependency("shapely.geometry")
608+
609+
if featuretype.lower() == "polygon":
610+
cache_index = "grid_polygons"
611+
if (
612+
cache_index not in self._cache_dict
613+
or self._cache_dict[cache_index].out_of_date
614+
):
615+
geoms = [shp_geom.Polygon(i[0]) for i in features]
616+
self._cache_dict[cache_index] = CachedData(geoms)
617+
else:
618+
geoms = self._cache_dict[cache_index].data_nocopy
619+
gdf = gpd.GeoDataFrame(data=None, geometry=geoms)
620+
621+
elif featuretype.lower() == "linestring":
622+
cache_index = "grid_linestrings"
623+
if (
624+
cache_index not in self._cache_dict
625+
or self._cache_dict[cache_index].out_of_date
626+
):
627+
geoms = [shp_geom.LineString(i) for i in features]
628+
self._cache_dict[cache_index] = CachedData(geoms)
629+
else:
630+
geoms = self._cache_dict[cache_index].data_nocopy
631+
gdf = gpd.GeoDataFrame(data=None, geometry=geoms)
632+
else:
633+
raise NotImplementedError(f"{featuretype} is not currently supported")
605634

606-
gc = GeoSpatialCollection(
607-
features, shapetype=[featuretype for _ in range(len(features))]
608-
)
609-
gdf = gc.geodataframe
610635
gdf["node"] = gdf.index + 1
611636
if self.crs is not None:
612637
gdf = gdf.set_crs(crs=self.crs)

flopy/discretization/structuredgrid.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,16 @@ def to_geodataframe(self):
767767
-------
768768
GeoDataFrame
769769
"""
770-
polys = [[list(zip(*i))] for i in zip(*self.cross_section_vertices)]
770+
cache_index = "gdf_polys"
771+
if (
772+
cache_index not in self._cache_dict
773+
or self._cache_dict[cache_index].out_of_date
774+
):
775+
polys = [[list(zip(*i))] for i in zip(*self.cross_section_vertices)]
776+
self._cache_dict[cache_index] = CachedData(polys)
777+
else:
778+
polys = self._cache_dict[cache_index].data_nocopy
779+
771780
gdf = super().to_geodataframe(polys)
772781
gdf["row"] = sorted(list(range(1, self.nrow + 1)) * self.ncol)
773782
gdf["col"] = list(range(1, self.ncol + 1)) * self.nrow

flopy/discretization/unstructuredgrid.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,16 @@ def to_geodataframe(self):
599599
-------
600600
GeoDataFrame
601601
"""
602-
polys = [[self.get_cell_vertices(nn)] for nn in range(self.nnodes)]
602+
cache_index = "gdf_polys"
603+
if (
604+
cache_index not in self._cache_dict
605+
or self._cache_dict[cache_index].out_of_date
606+
):
607+
polys = [[self.get_cell_vertices(nn)] for nn in range(self.nnodes)]
608+
self._cache_dict[cache_index] = CachedData(polys)
609+
else:
610+
polys = self._cache_dict[cache_index].data_nocopy
611+
603612
gdf = super().to_geodataframe(polys)
604613
if self.nlay > 1:
605614
lays = []

flopy/discretization/vertexgrid.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,20 @@ def to_geodataframe(self):
308308
-------
309309
GeoDataFrame
310310
"""
311-
cells = [[self.get_cell_vertices(nn)] for nn in range(self.ncpl)]
311+
cache_index = "gdf_polys"
312+
if (
313+
cache_index not in self._cache_dict
314+
or self._cache_dict[cache_index].out_of_date
315+
):
316+
polys = [[self.get_cell_vertices(nn)] for nn in range(self.ncpl)]
317+
self._cache_dict[cache_index] = CachedData(polys)
318+
else:
319+
polys = self._cache_dict[cache_index].data_nocopy
320+
312321
featuretype = "Polygon"
313322
if self._cell1d is not None:
314323
featuretype = "multilinestring"
315-
gdf = super().to_geodataframe(cells, featuretype)
324+
gdf = super().to_geodataframe(polys, featuretype)
316325
if self.idomain is not None:
317326
active = np.sum(
318327
self.idomain.reshape(

0 commit comments

Comments
 (0)