fix(imports): fix shapely and geojson imports (#1083)

develop
jdhughes-usgs 2021-03-20 08:58:50 -04:00 committed by GitHub
parent ff7b8223a9
commit 36426534fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 8 deletions

View File

@ -24,3 +24,4 @@ dependencies:
- pyproj
- shapely
- geos=3.8.1 # required until 3.9.2 is available
- geojson

View File

@ -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

View File

@ -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:

View File

@ -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)