From 36426534fae226570884f82a93a42374bd0019da Mon Sep 17 00:00:00 2001 From: jdhughes-usgs Date: Sat, 20 Mar 2021 08:58:50 -0400 Subject: [PATCH] fix(imports): fix shapely and geojson imports (#1083) --- etc/environment.yml | 1 + flopy/utils/geospatial_utils.py | 16 ++++++++++++++-- flopy/utils/gridintersect.py | 15 ++++++++++----- flopy/utils/voronoi.py | 5 ++++- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/etc/environment.yml b/etc/environment.yml index ccf5605f..688324b9 100644 --- a/etc/environment.yml +++ b/etc/environment.yml @@ -24,3 +24,4 @@ dependencies: - pyproj - shapely - geos=3.8.1 # required until 3.9.2 is available + - geojson diff --git a/flopy/utils/geospatial_utils.py b/flopy/utils/geospatial_utils.py index 6d609716..5a1fb322 100644 --- a/flopy/utils/geospatial_utils.py +++ b/flopy/utils/geospatial_utils.py @@ -8,12 +8,12 @@ try: LineString, MultiLineString, ) -except ImportError: +except: shapely = None try: import geojson -except ImportError: +except: geojson = None import numpy as np @@ -135,6 +135,8 @@ class GeoSpatialUtil(object): ), ): self.__geo_interface = obj.__geo_interface__ + else: + raise ModuleNotFoundError("shapely is not installed") @property def __geo_interface__(self): @@ -186,6 +188,8 @@ class GeoSpatialUtil(object): if self._shapely is None: self._shapely = shapely.geometry.shape(self.__geo_interface) return self._shapely + else: + raise ModuleNotFoundError("shapely is not installed") @property def geojson(self): @@ -201,6 +205,8 @@ class GeoSpatialUtil(object): cls = geojson_classes[self.__geo_interface["type"].lower()] self._geojson = cls(self.__geo_interface["coordinates"]) return self._geojson + else: + raise ModuleNotFoundError("geojson is not installed") @property def shape(self): @@ -332,6 +338,8 @@ class GeoSpatialCollection(object): ): for geom in obj.geoms: self.__collection.append(GeoSpatialUtil(geom)) + else: + raise ModuleNotFoundError("shapely is no installed") def __iter__(self): """ @@ -385,6 +393,8 @@ class GeoSpatialCollection(object): self._shapely = shapely.geometry.collection.GeometryCollection( [i.shapely for i in self.__collection] ) + else: + raise ModuleNotFoundError("shapely is not installed") return self._shapely @@ -402,6 +412,8 @@ class GeoSpatialCollection(object): self._geojson = geojson.GeometryCollection( [i.geojson for i in self.__collection] ) + else: + raise ModuleNotFoundError("geojson is not installed") return self._geojson @property diff --git a/flopy/utils/gridintersect.py b/flopy/utils/gridintersect.py index 843dd54c..9eaddc12 100644 --- a/flopy/utils/gridintersect.py +++ b/flopy/utils/gridintersect.py @@ -22,19 +22,24 @@ try: from shapely.prepared import prep shply = True -except ImportError: +except: shply = False import contextlib -import shapely import warnings from distutils.version import LooseVersion -SHAPELY_GE_20 = str(shapely.__version__) >= LooseVersion("2.0") +try: + import shapely + + SHAPELY_GE_20 = str(shapely.__version__) >= LooseVersion("2.0") +except: + shapely = None + SHAPELY_GE_20 = False try: from shapely.errors import ShapelyDeprecationWarning as shapely_warning -except ImportError: +except: shapely_warning = None if shapely_warning is not None and not SHAPELY_GE_20: @@ -145,7 +150,7 @@ class GridIntersect: "Please install shapely if you need to use grid intersect " "functionality." ) - raise ImportError(msg) + raise ModuleNotFoundError(msg) self.mfgrid = mfgrid if method is None: diff --git a/flopy/utils/voronoi.py b/flopy/utils/voronoi.py index ada552aa..d313876b 100644 --- a/flopy/utils/voronoi.py +++ b/flopy/utils/voronoi.py @@ -29,7 +29,10 @@ def get_valid_faces(vor): # todo: send this to point in polygon method defined in Rasters def point_in_cell(point, vertices): - from shapely.geometry import Point, Polygon + try: + from shapely.geometry import Point, Polygon + except: + raise ModuleNotFoundError("shapely is not installed") p = Point(point) poly = Polygon(vertices)