Skip to content

Commit 57cf82e

Browse files
authored
fix(#2152): improve gridintersect geometry creation for vertex grids (#2154)
1 parent e023235 commit 57cf82e

File tree

1 file changed

+21
-50
lines changed

1 file changed

+21
-50
lines changed

flopy/utils/gridintersect.py

+21-50
Original file line numberDiff line numberDiff line change
@@ -429,57 +429,28 @@ def _vtx_grid_to_geoms_cellids(self):
429429
cellids : array_like
430430
array of cellids
431431
"""
432-
shapely_geo = import_optional_dependency("shapely.geometry")
433-
434-
# for cell2d rec-arrays
435-
geoms = []
436-
cellids = []
437-
if isinstance(self.mfgrid._cell2d, np.recarray):
438-
for icell in self.mfgrid._cell2d.icell2d:
439-
points = []
440-
icverts = [
441-
f"icvert_{i}"
442-
for i in range(self.mfgrid._cell2d["ncvert"][icell])
443-
]
444-
for iv in self.mfgrid._cell2d[icverts][icell]:
445-
if self.local:
446-
xy = (
447-
self.mfgrid._vertices.xv[iv],
448-
self.mfgrid._vertices.yv[iv],
449-
)
450-
else:
451-
xy = (
452-
self.mfgrid.verts[iv, 0],
453-
self.mfgrid.verts[iv, 1],
454-
)
455-
points.append(xy)
456-
# close the polygon, if necessary
457-
if points[0] != points[-1]:
458-
points.append(points[0])
459-
geoms.append(shapely_geo.Polygon(points))
460-
cellids.append(icell)
461-
# for cell2d lists
462-
elif isinstance(self.mfgrid._cell2d, list):
463-
for icell in range(len(self.mfgrid._cell2d)):
464-
points = []
465-
for iv in self.mfgrid._cell2d[icell][4:]:
466-
if self.local:
467-
xy = (
468-
self.mfgrid._vertices[iv][1],
469-
self.mfgrid._vertices[iv][2],
470-
)
471-
else:
472-
xy = (
473-
self.mfgrid.verts[iv, 0],
474-
self.mfgrid.verts[iv, 1],
432+
shapely = import_optional_dependency("shapely")
433+
if self.local:
434+
geoms = [
435+
shapely.polygons(
436+
list(
437+
zip(
438+
*self.mfgrid.get_local_coords(
439+
*np.array(
440+
self.mfgrid.get_cell_vertices(node)
441+
).T
442+
)
475443
)
476-
points.append(xy)
477-
# close the polygon, if necessary
478-
if points[0] != points[-1]:
479-
points.append(points[0])
480-
geoms.append(shapely_geo.Polygon(points))
481-
cellids.append(icell)
482-
return np.array(geoms), np.array(cellids)
444+
)
445+
)
446+
for node in range(self.mfgrid.ncpl)
447+
]
448+
else:
449+
geoms = [
450+
shapely.polygons(self.mfgrid.get_cell_vertices(node))
451+
for node in range(self.mfgrid.ncpl)
452+
]
453+
return np.array(geoms), np.arange(self.mfgrid.ncpl)
483454

484455
def _rect_grid_to_shape_list(self):
485456
"""internal method, list of shapely polygons for structured grid cells.

0 commit comments

Comments
 (0)