Skip to content

Commit

Permalink
feat(cell1d): add support for 1D vertex grids (#2296)
Browse files Browse the repository at this point in the history
  • Loading branch information
langevin-usgs authored Aug 23, 2024
1 parent c42d878 commit 4ea7192
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions flopy/discretization/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def xyzvertices(self):
def cross_section_vertices(self):
return self.xyzvertices[0], self.xyzvertices[1]

def geo_dataframe(self, polys):
def geo_dataframe(self, features, featuretype="Polygon"):
"""
Method returns a geopandas GeoDataFrame of the Grid
Expand All @@ -606,7 +606,7 @@ def geo_dataframe(self, polys):
from ..utils.geospatial_utils import GeoSpatialCollection

gc = GeoSpatialCollection(
polys, shapetype=["Polygon" for _ in range(len(polys))]
features, shapetype=[featuretype for _ in range(len(features))]
)
gdf = gc.geo_dataframe
if self.crs is not None:
Expand Down
38 changes: 28 additions & 10 deletions flopy/discretization/vertexgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def iverts(self):
def cell1d(self):
if self._cell1d is not None:
return [
[ivt for ivt in t if ivt is not None] for t in self._cell2d
[ivt for ivt in t if ivt is not None] for t in self._cell1d
]

@property
Expand Down Expand Up @@ -221,15 +221,30 @@ def grid_lines(self):
xgrid = self.xvertices
ygrid = self.yvertices

# close the cell by connecting the last vertex with the first
close_cell = True
if self.cell1d is not None:
close_cell = False

# go through each cell and create a line segement for each face
lines = []
for ncell, verts in enumerate(xgrid):
for ix, vert in enumerate(verts):
ncpl = len(xgrid)
for icpl in range(ncpl):
xcoords = xgrid[icpl]
ycoords = ygrid[icpl]
npoints = len(xcoords)
for ipoint in range(npoints - 1):
lines.append(
[
(xgrid[ncell][ix - 1], ygrid[ncell][ix - 1]),
(xgrid[ncell][ix], ygrid[ncell][ix]),
(xcoords[ipoint], ycoords[ipoint]),
(xcoords[ipoint + 1], ycoords[ipoint + 1]),
]
)
if close_cell:
lines.append(
[(xcoords[-1], ycoords[-1]), (xcoords[0], ycoords[0])]
)

self._copy_cache = True
return lines

Expand Down Expand Up @@ -300,8 +315,11 @@ def geo_dataframe(self):
-------
GeoDataFrame
"""
polys = [[self.get_cell_vertices(nn)] for nn in range(self.ncpl)]
gdf = super().geo_dataframe(polys)
cells = [[self.get_cell_vertices(nn)] for nn in range(self.ncpl)]
featuretype = "Polygon"
if self._cell1d is not None:
featuretype = "multilinestring"
gdf = super().geo_dataframe(cells, featuretype)
return gdf

def convert_grid(self, factor):
Expand Down Expand Up @@ -458,12 +476,12 @@ def _build_grid_geometry_info(self):
if self._cell1d is not None:
zcenters = []
zvertices = []
vertexdict = {v[0]: [v[1], v[2], v[3]] for v in self._vertices}
vertexdict = {v[0]: [v[1], v[2]] for v in self._vertices}
for cell1d in self.cell1d:
cell1d = tuple(cell1d)
xcenters.append(cell1d[1])
ycenters.append(cell1d[2])
zcenters.append(cell1d[3])
zcenters.append(0.0)

vert_number = []
for i in cell1d[3:]:
Expand All @@ -475,7 +493,7 @@ def _build_grid_geometry_info(self):
for ix in vert_number:
xcellvert.append(vertexdict[ix][0])
ycellvert.append(vertexdict[ix][1])
zcellvert.append(vertexdict[ix][2])
zcellvert.append(0.0)
xvertices.append(xcellvert)
yvertices.append(ycellvert)
zvertices.append(zcellvert)
Expand Down

0 comments on commit 4ea7192

Please sign in to comment.