diff --git a/flopy/utils/gridintersect.py b/flopy/utils/gridintersect.py index 8b3c5cda2..8bc0c7e7f 100644 --- a/flopy/utils/gridintersect.py +++ b/flopy/utils/gridintersect.py @@ -429,57 +429,28 @@ def _vtx_grid_to_geoms_cellids(self): cellids : array_like array of cellids """ - shapely_geo = import_optional_dependency("shapely.geometry") - - # for cell2d rec-arrays - geoms = [] - cellids = [] - if isinstance(self.mfgrid._cell2d, np.recarray): - for icell in self.mfgrid._cell2d.icell2d: - points = [] - icverts = [ - f"icvert_{i}" - for i in range(self.mfgrid._cell2d["ncvert"][icell]) - ] - for iv in self.mfgrid._cell2d[icverts][icell]: - if self.local: - xy = ( - self.mfgrid._vertices.xv[iv], - self.mfgrid._vertices.yv[iv], - ) - else: - xy = ( - self.mfgrid.verts[iv, 0], - self.mfgrid.verts[iv, 1], - ) - points.append(xy) - # close the polygon, if necessary - if points[0] != points[-1]: - points.append(points[0]) - geoms.append(shapely_geo.Polygon(points)) - cellids.append(icell) - # for cell2d lists - elif isinstance(self.mfgrid._cell2d, list): - for icell in range(len(self.mfgrid._cell2d)): - points = [] - for iv in self.mfgrid._cell2d[icell][4:]: - if self.local: - xy = ( - self.mfgrid._vertices[iv][1], - self.mfgrid._vertices[iv][2], - ) - else: - xy = ( - self.mfgrid.verts[iv, 0], - self.mfgrid.verts[iv, 1], + shapely = import_optional_dependency("shapely") + if self.local: + geoms = [ + shapely.polygons( + list( + zip( + *self.mfgrid.get_local_coords( + *np.array( + self.mfgrid.get_cell_vertices(node) + ).T + ) ) - points.append(xy) - # close the polygon, if necessary - if points[0] != points[-1]: - points.append(points[0]) - geoms.append(shapely_geo.Polygon(points)) - cellids.append(icell) - return np.array(geoms), np.array(cellids) + ) + ) + for node in range(self.mfgrid.ncpl) + ] + else: + geoms = [ + shapely.polygons(self.mfgrid.get_cell_vertices(node)) + for node in range(self.mfgrid.ncpl) + ] + return np.array(geoms), np.arange(self.mfgrid.ncpl) def _rect_grid_to_shape_list(self): """internal method, list of shapely polygons for structured grid cells.