Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 51 additions & 24 deletions flopy/utils/gridintersect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"])
Expand All @@ -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:
Expand Down Expand Up @@ -512,6 +511,13 @@ def _intersect_linestring_structured(self, shp, keepzerolengths=False):
a record array containing information about the intersection

"""
try:
from shapely.geometry import box
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.):
Expand Down Expand Up @@ -654,6 +660,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 = []
Expand Down Expand Up @@ -744,8 +755,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 = []
Expand Down Expand Up @@ -886,6 +902,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 = []

Expand Down Expand Up @@ -960,6 +981,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 = []
Expand Down