Skip to content

Commit

Permalink
fix(imports): fix shapely and geojson imports (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhughes-usgs committed Mar 20, 2021
1 parent ff7b822 commit 3642653
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions etc/environment.yml
Expand Up @@ -24,3 +24,4 @@ dependencies:
- pyproj
- shapely
- geos=3.8.1 # required until 3.9.2 is available
- geojson
16 changes: 14 additions & 2 deletions flopy/utils/geospatial_utils.py
Expand Up @@ -8,12 +8,12 @@
LineString,
MultiLineString,
)
except ImportError:
except:
shapely = None

try:
import geojson
except ImportError:
except:
geojson = None

import numpy as np
Expand Down Expand Up @@ -135,6 +135,8 @@ def __init__(self, obj, shapetype=None):
),
):
self.__geo_interface = obj.__geo_interface__
else:
raise ModuleNotFoundError("shapely is not installed")

@property
def __geo_interface__(self):
Expand Down Expand Up @@ -186,6 +188,8 @@ def shapely(self):
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):
Expand All @@ -201,6 +205,8 @@ def geojson(self):
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):
Expand Down Expand Up @@ -332,6 +338,8 @@ def __init__(self, obj, shapetype=None):
):
for geom in obj.geoms:
self.__collection.append(GeoSpatialUtil(geom))
else:
raise ModuleNotFoundError("shapely is no installed")

def __iter__(self):
"""
Expand Down Expand Up @@ -385,6 +393,8 @@ def shapely(self):
self._shapely = shapely.geometry.collection.GeometryCollection(
[i.shapely for i in self.__collection]
)
else:
raise ModuleNotFoundError("shapely is not installed")

return self._shapely

Expand All @@ -402,6 +412,8 @@ def geojson(self):
self._geojson = geojson.GeometryCollection(
[i.geojson for i in self.__collection]
)
else:
raise ModuleNotFoundError("geojson is not installed")
return self._geojson

@property
Expand Down
15 changes: 10 additions & 5 deletions flopy/utils/gridintersect.py
Expand Up @@ -22,19 +22,24 @@
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:
Expand Down Expand Up @@ -145,7 +150,7 @@ def __init__(self, mfgrid, method=None, rtree=True):
"Please install shapely if you need to use grid intersect "
"functionality."
)
raise ImportError(msg)
raise ModuleNotFoundError(msg)

self.mfgrid = mfgrid
if method is None:
Expand Down
5 changes: 4 additions & 1 deletion flopy/utils/voronoi.py
Expand Up @@ -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)
Expand Down

0 comments on commit 3642653

Please sign in to comment.