From 1a7c057efed89ea909b035aa7b97882e19fed85d Mon Sep 17 00:00:00 2001 From: Christian Langevin Date: Sat, 26 Oct 2019 07:13:48 -0500 Subject: [PATCH 1/2] refactor(gridintersect): wrap shapely imports in try/except This should allow flopy to be loaded even if shapely isn't installed. Closes #697 --- flopy/utils/gridintersect.py | 74 ++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/flopy/utils/gridintersect.py b/flopy/utils/gridintersect.py index a9dcb47de0..e2307346e2 100644 --- a/flopy/utils/gridintersect.py +++ b/flopy/utils/gridintersect.py @@ -3,13 +3,6 @@ if sys.version_info[0] == 2: ModuleNotFoundError = ImportError from .geometry import transform -try: - from shapely.geometry import MultiPoint, Point, Polygon, box - from shapely.strtree import STRtree - from shapely.affinity import translate, rotate -except ModuleNotFoundError: - print("Shapely is needed for grid intersect operations! " - "Please install shapely.") class GridIntersect: @@ -49,6 +42,11 @@ def __init__(self, mfgrid, method="strtree"): for structured grids, by default "strtree" """ + try: + from shapely.strtree import STRtree + except ModuleNotFoundError: + print("Shapely is needed for grid intersect operations! " + "Please install pyshp.") self.mfgrid = mfgrid @@ -86,6 +84,12 @@ def _rect_grid_to_shape_list(self): list of shapely Polygons """ + try: + from shapely.geometry import Polygon + except ModuleNotFoundError: + print("Shapely is needed for grid intersect operations! " + "Please install pyshp.") + shplist = [] for i in range(self.mfgrid.nrow): for j in range(self.mfgrid.ncol): @@ -115,7 +119,11 @@ def _vtx_grid_to_shape_list(self): list of shapely Polygons """ - + try: + from shapely.geometry import Polygon + except ModuleNotFoundError: + print("Shapely is needed for grid intersect operations! " + "Please install pyshp.") shplist = [] if isinstance(self.mfgrid._cell2d, np.recarray): for icell in self.mfgrid._cell2d.icell2d: @@ -398,20 +406,6 @@ def _intersect_polygon_shapely(self, shp, sort_by_cellid=True): vertices.append(np.nan) cellids.append(r.name) - # else: # Point or LineString - # if keep_all_ix: - # isectshp.append(intersect) - # if "Polygon" in intersect.geom_type: - # areas.append(intersect.area) - # else: - # areas.append(np.nan) - # if "coordinates" in intersect.__geo_interface__.keys(): - # vertices.append( - # intersect.__geo_interface__["coordinates"]) - # else: - # vertices.append(np.nan) - # cellids.append(r.name) - rec = np.recarray(len(isectshp), names=["cellids", "vertices", "areas", "ixshapes"], formats=["O", "O", "f8", "O"]) @@ -436,8 +430,13 @@ def _intersect_point_structured(self, shp): a record array containing information about the intersection """ - nodelist = [] + try: + from shapely.geometry import MultiPoint + except ModuleNotFoundError: + print("Shapely is needed for grid intersect operations! " + "Please install pyshp.") + nodelist = [] Xe, Ye = self.mfgrid.xyedges try: @@ -512,6 +511,12 @@ def _intersect_linestring_structured(self, shp, keepzerolengths=False): a record array containing information about the intersection """ + try: + from shapely.affinity import translate, rotate + except ModuleNotFoundError: + print("Shapely is needed for grid intersect operations! " + "Please install pyshp.") + # get local extent of grid if (self.mfgrid.angrot != 0. or self.mfgrid.xoffset != 0. or self.mfgrid.yoffset != 0.): @@ -654,6 +659,11 @@ def _get_nodes_intersecting_linestring(self, linestring): start and end points of the intersects """ + try: + from shapely.geometry import Point, box + except ModuleNotFoundError: + print("Shapely is needed for grid intersect operations! " + "Please install pyshp.") nodelist = [] lengths = [] vertices = [] @@ -744,8 +754,13 @@ def _check_adjacent_cells_intersecting_line(self, linestring, i_j, current cell (i, j) """ - i, j = i_j + try: + from shapely.geometry import box + except ModuleNotFoundError: + print("Shapely is needed for grid intersect operations! " + "Please install pyshp.") + i, j = i_j Xe, Ye = self.mfgrid.xyedges node = [] @@ -886,6 +901,11 @@ def _intersect_rectangle_structured(self, rectangle): the rectangle intersects """ + try: + from shapely.geometry import box + except ModuleNotFoundError: + print("Shapely is needed for grid intersect operations! " + "Please install pyshp.") nodelist = [] @@ -960,6 +980,12 @@ def _intersect_polygon_structured(self, shp): a record array containing information about the intersection """ + try: + from shapely.geometry import Polygon + from shapely.affinity import translate, rotate + except ModuleNotFoundError: + print("Shapely is needed for grid intersect operations! " + "Please install pyshp.") # initialize the result lists nodelist = [] From 97e6d49386eba1022ba381c008c447159ba9e22a Mon Sep 17 00:00:00 2001 From: Christian Langevin Date: Sat, 26 Oct 2019 07:47:24 -0500 Subject: [PATCH 2/2] small import fix --- flopy/utils/gridintersect.py | 1 + 1 file changed, 1 insertion(+) diff --git a/flopy/utils/gridintersect.py b/flopy/utils/gridintersect.py index e2307346e2..f436db4f51 100644 --- a/flopy/utils/gridintersect.py +++ b/flopy/utils/gridintersect.py @@ -512,6 +512,7 @@ def _intersect_linestring_structured(self, shp, keepzerolengths=False): """ try: + from shapely.geometry import box from shapely.affinity import translate, rotate except ModuleNotFoundError: print("Shapely is needed for grid intersect operations! "