Skip to content

Commit

Permalink
fix(shapely): handle deprecation warnings from shapely 1.8 (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Mar 19, 2021
1 parent 44ebec2 commit ff7b822
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion flopy/utils/geospatial_utils.py
Expand Up @@ -330,7 +330,7 @@ def __init__(self, obj, shapetype=None):
MultiPolygon,
),
):
for geom in list(obj):
for geom in obj.geoms:
self.__collection.append(GeoSpatialUtil(geom))

def __iter__(self):
Expand Down
57 changes: 46 additions & 11 deletions flopy/utils/gridintersect.py
Expand Up @@ -25,6 +25,33 @@
except ImportError:
shply = False

import contextlib
import shapely
import warnings
from distutils.version import LooseVersion

SHAPELY_GE_20 = str(shapely.__version__) >= LooseVersion("2.0")

try:
from shapely.errors import ShapelyDeprecationWarning as shapely_warning
except ImportError:
shapely_warning = None

if shapely_warning is not None and not SHAPELY_GE_20:

@contextlib.contextmanager
def ignore_shapely_warnings_for_object_array():
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=shapely_warning)
yield


else:

@contextlib.contextmanager
def ignore_shapely_warnings_for_object_array():
yield


def parse_shapely_ix_result(collection, ix_result, shptyps=None):
"""Recursive function for parsing shapely intersection results. Returns a
Expand Down Expand Up @@ -62,7 +89,7 @@ def parse_shapely_ix_result(collection, ix_result, shptyps=None):
return collection
# recursion for collections
elif hasattr(ix_result, "geoms"):
for ishp in ix_result:
for ishp in ix_result.geoms:
parse_shapely_ix_result(collection, ishp, shptyps=shptyps)
# if collecting all types
elif shptyps[0] is None:
Expand Down Expand Up @@ -465,7 +492,8 @@ def _intersect_point_shapely(self, shp, sort_by_cellid=True):
names=["cellids", "vertices", "ixshapes"],
formats=["O", "O", "O"],
)
rec.ixshapes = isectshp
with ignore_shapely_warnings_for_object_array():
rec.ixshapes = isectshp
rec.vertices = vertices
rec.cellids = cellids

Expand Down Expand Up @@ -534,7 +562,8 @@ def _intersect_linestring_shapely(
names=["cellids", "vertices", "lengths", "ixshapes"],
formats=["O", "O", "f8", "O"],
)
rec.ixshapes = isectshp
with ignore_shapely_warnings_for_object_array():
rec.ixshapes = isectshp
rec.vertices = vertices
rec.lengths = lengths
rec.cellids = cellids
Expand Down Expand Up @@ -597,7 +626,8 @@ def _intersect_polygon_shapely(self, shp, sort_by_cellid=True):
names=["cellids", "vertices", "areas", "ixshapes"],
formats=["O", "O", "f8", "O"],
)
rec.ixshapes = isectshp
with ignore_shapely_warnings_for_object_array():
rec.ixshapes = isectshp
rec.vertices = vertices
rec.areas = areas
rec.cellids = cellids
Expand Down Expand Up @@ -649,10 +679,12 @@ def _intersect_point_structured(self, shp):

Xe, Ye = self.mfgrid.xyedges

try:
iter(shp)
except TypeError:
if isinstance(shp, Point):
shp = [shp]
elif isinstance(shp, MultiPoint):
shp = list(shp.geoms)
else:
raise ValueError("expected Point or MultiPoint")

ixshapes = []
for p in shp:
Expand Down Expand Up @@ -710,7 +742,8 @@ def _intersect_point_structured(self, shp):
len(nodelist), names=["cellids", "ixshapes"], formats=["O", "O"]
)
rec.cellids = nodelist
rec.ixshapes = ixshapes
with ignore_shapely_warnings_for_object_array():
rec.ixshapes = ixshapes
return rec

def _intersect_linestring_structured(self, shp, keepzerolengths=False):
Expand Down Expand Up @@ -764,7 +797,7 @@ def _intersect_linestring_structured(self, shp, keepzerolengths=False):
if lineclip.geom_type == "MultiLineString": # there are multiple lines
nodelist, lengths, vertices = [], [], []
ixshapes = []
for ls in lineclip:
for ls in lineclip.geoms:
n, l, v, ixs = self._get_nodes_intersecting_linestring(ls)
nodelist += n
lengths += l
Expand Down Expand Up @@ -888,7 +921,8 @@ def _intersect_linestring_structured(self, shp, keepzerolengths=False):
rec.vertices = vertices
rec.lengths = lengths
rec.cellids = nodelist
rec.ixshapes = ixshapes
with ignore_shapely_warnings_for_object_array():
rec.ixshapes = ixshapes

return rec

Expand Down Expand Up @@ -1320,7 +1354,8 @@ def _intersect_polygon_structured(self, shp):
rec.vertices = vertices
rec.areas = areas
rec.cellids = nodelist
rec.ixshapes = ixshapes
with ignore_shapely_warnings_for_object_array():
rec.ixshapes = ixshapes

return rec

Expand Down

0 comments on commit ff7b822

Please sign in to comment.