Skip to content

GridIntersect fails with specific shapely Polygon due to change of nrow, ncol in the discretization object #1216

@javgs-bd

Description

@javgs-bd

The code below works if ncol = 50 and nrow = 80, but fails if ncol = 25 and nrow = 40. Am still trying to figure out why, but in any case I though it should be reported.

This is the result of the version printing:

3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]
numpy version: 1.20.1
flopy version: 3.3.5
matplotlib version: 3.3.4
shapely version: 1.7.1

Result with nrow=50, ncol=80
image

Error with nrow=25, ncol=40

KeyError Traceback (most recent call last)
C:\Users\XXXXX~1\AppData\Local\Temp/ipykernel_20404/762291030.py in
74
75
---> 76 intersection = gridint.intersect(shply_polygon)
77
78

~.conda\envs\flopy-develop\lib\site-packages\flopy\utils\gridintersect.py in intersect(self, shp, **kwargs)
231 and self.mfgrid.grid_type == "structured"
232 ):
--> 233 rec = self._intersect_polygon_structured(shp)
234 else:
235 rec = self._intersect_polygon_shapely(shp, sort_by_cellid)

~.conda\envs\flopy-develop\lib\site-packages\flopy\utils\gridintersect.py in _intersect_polygon_structured(self, shp)
1345 )
1346 else:
-> 1347 v_realworld = intersect.geo_interface[
1348 "coordinates"
1349 ]

KeyError: 'coordinates'

Code

import sys
import os
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors as clt
import flopy

from flopy.utils.gridintersect import GridIntersect
from flopy.utils.geometry import shape
import flopy.discretization as fgrid

import shapely
from shapely.geometry import Point, MultiPoint, LineString, Polygon


print(sys.version)
print('numpy version: {}'.format(np.__version__))
print('flopy version: {}'.format(flopy.__version__))
print('matplotlib version: {}'.format(mpl.__version__))
print('shapely version: {}'.format(shapely.__version__))


points  = [
(-18.261405672010028,493.7361282367448),
(20.0,490.0),
(80.0,490.0),
(120.0,450.0),
(150.0,380.0),
(160.0,350.0),
(160.0,310.0),
(160.0,290.0),
(150.0,280.0),
(140.0,250.0),
(130.0,240.0),
(120.0,230.0),
(90.0,220.0),
(90.0,220.0),
(70.0,240.0),
(60.0,290.0),
(30.0,280.0),
(20.0,250.0),
(20.0,250.0),
(20.0,210.0),
(20.0,170.0),
(10.0,160.0),
(10.0,160.0),
(0.0,150.0),
(-22.404438964241834,169.54377311960536),
(-70.04932182490768,284.51294697903825),
(-79.37114673242925,394.30332922318127),
(-83.51418002466107,444.019728729963)
]

shply_polygon = Polygon(shell=points)

shply_polygon


Lx      = 500.              
Ly      = 800.              

nlay    = 1                
ncol    = 50
nrow    = 80           
             
delr    = np.ones(ncol)*Lx / ncol        
delc    = np.ones(nrow)*Ly / nrow    

sgr = fgrid.StructuredGrid(delc, delr, top=None, botm=None, xoff=0, yoff=0, angrot=0)  


gridint  = GridIntersect(sgr, method="structured", rtree=False)


intersection  = gridint.intersect(shply_polygon)



fig, ax = plt.subplots(1, 1, figsize=(8, 8))

sgr.plot(ax=ax)

gridint.plot_polygon(intersection,ax = ax, lw=2) 

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions