|
60 | 60 | print(sys.version) |
61 | 61 | print(f"numpy version: {np.__version__}") |
62 | 62 | print(f"matplotlib version: {mpl.__version__}") |
63 | | -print(f"flopy version: {flopy.__version__}") |
64 | 63 | print(f"shapely version: {shapely.__version__}") |
| 64 | +print(f"flopy version: {flopy.__version__}") |
65 | 65 | # - |
66 | 66 |
|
67 | 67 | # ## <a id="gridclass"></a>[GridIntersect Class](#top) |
|
70 | 70 | # the constructor. There are options users can select to change how the |
71 | 71 | # intersection is calculated. |
72 | 72 | # |
73 | | -# - `method`: derived from model grid type or defined by the user: can be either `"vertex"` or |
74 | | -# `"structured"`. If `"structured"` is passed, the intersections are performed |
75 | | -# using structured methods. These methods use information about the regular grid |
76 | | -# to limit the search space for intersection calculations. Note that `method="vertex"` |
77 | | -# also works for structured grids. |
78 | | -# - `rtree`: either `True` (default) or `False`, only read when |
79 | | -# `method="vertex"`. When True, an STR-tree is built, which allows for fast |
80 | | -# spatial queries. Building the STR-tree does take some time however. Setting the |
81 | | -# option to False avoids building the STR-tree but requires the intersection |
82 | | -# calculation to loop through all grid cells. |
83 | | -# |
84 | | -# In general the "vertex" option is robust and fast and is therefore recommended |
85 | | -# in most situations. In some rare cases building the STR-tree might not be worth |
86 | | -# the time, in which case it can be avoided by passing `rtree=False`. If you are |
87 | | -# working with a structured grid, then the `method="structured"` can speed up |
88 | | -# intersection operations in some situations (e.g. for (multi)points) with the added |
89 | | -# advantage of not having to build an STR-tree. |
| 73 | +# - `rtree`: either `True` (default) or `False`. When True, an STR-tree is built, |
| 74 | +# which allows for fast spatial queries. Building the STR-tree takes some |
| 75 | +# time however. Setting the option to False avoids building the STR-tree but requires |
| 76 | +# the intersection calculation to loop through all grid cells. It is generally |
| 77 | +# recommended to set this option to True. |
| 78 | +# - `local`: either `False` (default) or `True`. When True the local model coordinates |
| 79 | +# are used. When False, real-world coordinates are used. Can be useful if shapes are |
| 80 | +# defined in local coordinates. |
90 | 81 | # |
91 | 82 | # The important methods in the GridIntersect object are: |
92 | 83 | # |
|
96 | 87 | # - `intersect()`: for intersecting the modelgrid with point, linestrings, and |
97 | 88 | # polygon geometries (accepts shapely geometry objects, flopy geometry object, |
98 | 89 | # shapefile.Shape objects, and geojson objects) |
99 | | -# - `plot_point()`: for plotting point intersection results |
100 | | -# - `plot_linestring()`: for plotting linestring intersection results |
101 | | -# - `plot_polygon()`: for plotting polygon intersection results |
| 90 | +# - `ix.plot_intersection_result()`: for plotting intersection results |
102 | 91 | # |
103 | 92 | # In the following sections examples of intersections are shown for structured |
104 | 93 | # and vertex grids for different types of shapes (Polygon, LineString and Point). |
|
133 | 122 | holes=[[(25, 25), (25, 45), (45, 45), (45, 25)]], |
134 | 123 | ) |
135 | 124 |
|
136 | | -# Create the GridIntersect class for our modelgrid. The `method` kwarg is passed to force GridIntersect to use the `"vertex"` intersection methods. |
137 | | - |
| 125 | +# Create the GridIntersect class for our modelgrid. |
| 126 | +# TODO: remove method kwarg in v3.9.0 |
138 | 127 | ix = GridIntersect(sgr, method="vertex") |
139 | 128 |
|
140 | 129 | # Do the intersect operation for a polygon |
|
151 | 140 | # Looking at the first few entries of the results of the polygon intersection (convert to pandas.DataFrame for prettier formatting) |
152 | 141 |
|
153 | 142 | result[:5] |
154 | | -# pd.DataFrame(result) # recommended for prettier formatting and working with result |
| 143 | +# pd.DataFrame(result).head() |
155 | 144 |
|
156 | 145 | # The cellids can be easily obtained |
157 | 146 |
|
|
165 | 154 |
|
166 | 155 | ix.intersects(p) |
167 | 156 |
|
168 | | -# The results of an intersection can be visualized with the plotting methods in the `GridIntersect` object: |
169 | | -# - `plot_polygon` |
170 | | -# - `plot_linestring` |
171 | | -# - `plot_point` |
| 157 | +# The results of an intersection can be visualized with the `GridIntersect.plot_intersection_result()` method. |
172 | 158 |
|
173 | 159 | # + |
174 | 160 | # create a figure and plot the grid |
175 | 161 | fig, ax = plt.subplots(1, 1, figsize=(8, 8)) |
176 | | -sgr.plot(ax=ax) |
177 | 162 |
|
178 | 163 | # the intersection object contains some helpful plotting commands |
179 | | -ix.plot_polygon(result, ax=ax) |
| 164 | +ix.plot_intersection_result(result, ax=ax) |
180 | 165 |
|
181 | 166 | # add black x at cell centers |
182 | 167 | for irow, icol in result.cellids: |
|
205 | 190 |
|
206 | 191 | result2 = ix.intersect(p, contains_centroid=True) |
207 | 192 |
|
208 | | -# create a figure and plot the grid |
209 | 193 | fig, ax = plt.subplots(1, 1, figsize=(8, 8)) |
210 | | -sgr.plot(ax=ax) |
211 | | - |
212 | | -# the intersection object contains some helpful plotting commands |
213 | | -ix.plot_polygon(result2, ax=ax) |
| 194 | +ix.plot_intersection_result(result2, ax=ax) |
214 | 195 |
|
215 | 196 | # add black x at cell centers |
216 | 197 | for irow, icol in result2.cellids: |
|
232 | 213 |
|
233 | 214 | result3 = ix.intersect(p, min_area_fraction=0.35) |
234 | 215 |
|
235 | | -# create a figure and plot the grid |
236 | 216 | fig, ax = plt.subplots(1, 1, figsize=(8, 8)) |
237 | | -sgr.plot(ax=ax) |
238 | | - |
239 | | -# the intersection object contains some helpful plotting commands |
240 | | -ix.plot_polygon(result3, ax=ax) |
| 217 | +ix.plot_intersection_result(result3, ax=ax) |
241 | 218 |
|
242 | 219 | # add black x at cell centers |
243 | 220 | for irow, icol in result3.cellids: |
|
247 | 224 | "kx", |
248 | 225 | label="centroids of intersected gridcells", |
249 | 226 | ) |
250 | | - |
251 | | -# add legend |
252 | | -ax.legend([h2], [i.get_label() for i in [h2]], loc="best") |
253 | | -# - |
254 | | - |
255 | | -# Alternatively, the intersection can be calculated using special methods optimized for structured grids. Access these methods by instantiating the GridIntersect class with the `method="structured"` keyword argument. |
256 | | - |
257 | | -ixs = GridIntersect(sgr, method="structured") |
258 | | -result4 = ixs.intersect(p) |
259 | | - |
260 | | -# The result is the same as before: |
261 | | - |
262 | | -# + |
263 | | -# create a figure and plot the grid |
264 | | -fig, ax = plt.subplots(1, 1, figsize=(8, 8)) |
265 | | -sgr.plot(ax=ax) |
266 | | - |
267 | | -# the intersection object contains some helpful plotting commands |
268 | | -ix.plot_polygon(result4, ax=ax) |
269 | | - |
270 | | -# add black x at cell centers |
271 | | -for irow, icol in result4.cellids: |
272 | | - (h2,) = ax.plot( |
273 | | - sgr.xcellcenters[0, icol], |
274 | | - sgr.ycellcenters[irow, 0], |
275 | | - "kx", |
276 | | - label="centroids of intersected gridcells", |
277 | | - ) |
278 | | - |
279 | 227 | # add legend |
280 | 228 | ax.legend([h2], [i.get_label() for i in [h2]], loc="best") |
281 | 229 | # - |
|
295 | 243 | # + |
296 | 244 | fig, ax = plt.subplots(1, 1, figsize=(8, 8)) |
297 | 245 | sgr.plot(ax=ax) |
298 | | -ix.plot_linestring(result, ax=ax, cmap="viridis") |
| 246 | +ix.plot_intersection_result(result, ax=ax, cmap="viridis") |
299 | 247 |
|
300 | 248 | for irow, icol in result.cellids: |
301 | 249 | (h2,) = ax.plot( |
|
308 | 256 | ax.legend([h2], [i.get_label() for i in [h2]], loc="best") |
309 | 257 | # - |
310 | 258 |
|
311 | | -# Same as before, the intersect for structured grids can also be performed with a different method optimized for structured grids |
312 | | - |
313 | | -ixs = GridIntersect(sgr, method="structured") |
314 | | - |
315 | | -# + |
316 | | -result2 = ixs.intersect(mls) |
317 | | - |
318 | | -# ordering is different so compare sets to check equality |
319 | | -check = len(set(result2.cellids) - set(result.cellids)) == 0 |
320 | | -print( |
321 | | - "Intersection result with method='structured' and " |
322 | | - f"method='vertex' are equal: {check}" |
323 | | -) |
324 | | -# - |
325 | | - |
326 | 259 | # ### [MultiPoint with regular grid](#top)<a id="rectgrid.3"></a> |
327 | 260 | # |
328 | 261 | # MultiPoint to intersect with |
|
368 | 301 | ax.legend([h2, h3], [i.get_label() for i in [h2, h3]], loc="best") |
369 | 302 | # - |
370 | 303 |
|
371 | | -# Same as before, the intersect for structured grids can also be performed with a different method written specifically for structured grids. |
372 | | - |
373 | | -ixs = GridIntersect(sgr, method="structured") |
374 | | - |
375 | | -# + |
376 | | -result2 = ixs.intersect(mp, return_all_intersections=False) |
377 | | - |
378 | | -# ordering is different so compare sets to check equality |
379 | | -check = len(set(result2.cellids) - set(result.cellids)) == 0 |
380 | | -print( |
381 | | - "Intersection result with method='structured' and " |
382 | | - f"method='vertex' are equal: {check}" |
383 | | -) |
384 | | -# - |
385 | | - |
386 | 304 | # ## <a id="trigrid"></a>[Vertex Grid](#top) |
387 | 305 |
|
388 | 306 | cell2d = [ |
|
420 | 338 |
|
421 | 339 | # + |
422 | 340 | fig, ax = plt.subplots(1, 1, figsize=(8, 8)) |
423 | | -pmv = fplot.PlotMapView(ax=ax, modelgrid=tgr) |
424 | | -pmv.plot_grid() |
425 | | -ix.plot_polygon(result, ax=ax) |
| 341 | +ix.plot_intersection_result(result, ax=ax) |
426 | 342 |
|
427 | 343 | # only cells that intersect with shape |
428 | 344 | for cellid in result.cellids: |
|
442 | 358 |
|
443 | 359 | # + |
444 | 360 | fig, ax = plt.subplots(1, 1, figsize=(8, 8)) |
445 | | -pmv = fplot.PlotMapView(ax=ax, modelgrid=tgr) |
446 | | -pmv.plot_grid() |
447 | | -ix2.plot_linestring(result, ax=ax, lw=3) |
| 361 | +ix2.plot_intersection_result(result, ax=ax, lw=3) |
448 | 362 |
|
449 | 363 | for cellid in result.cellids: |
450 | 364 | (h2,) = ax.plot( |
|
464 | 378 |
|
465 | 379 | # + |
466 | 380 | fig, ax = plt.subplots(1, 1, figsize=(8, 8)) |
467 | | -pmv = fplot.PlotMapView(ax=ax, modelgrid=tgr) |
468 | | -pmv.plot_grid() |
469 | | -ix2.plot_point(result, ax=ax, color="k", zorder=5, s=80) |
| 381 | +ix2.plot_intersection_result(result, ax=ax, color="k", zorder=5, s=80) |
470 | 382 |
|
471 | 383 | for cellid in result.cellids: |
472 | 384 | (h2,) = ax.plot( |
|
0 commit comments