def gp2cellids(grid, gp, idomain, idomain_active=True, type="polygon", layer=0, areas=3): """ this function extract the cellids of the intersection between a geopandas object and a grid """ ix = GridIntersect(grid) if type == "polygon": result = ix.intersect_polygon(gp.geometry[0]) result = result[result.areas > ( np.max(result.areas) / 3 )] # only take into account cells that have a least 1/3 intersected by the polygon if type == "boundary": result = ix.intersect_linestring(gp.geometry[0].boundary) if type == "line": result = ix.intersect_linestring(gp.geometry[0]) lst = [] for irow, icol in result.cellids: lst.append(((layer, irow, icol))) if idomain_active: idomain[irow * grid.ncol + icol] = 1 return lst
def test_tri_grid_polygon_on_inner_boundary(): gr = get_tri_grid(triangle_exe=triangle_exe) if gr == -1: return ix = GridIntersect(gr) result = ix.intersect_polygon( Polygon([(5., 10.0), (15., 10.0), (15., 5.), (5., 5.)])) assert len(result) == 4 assert result.areas.sum() == 50. return result
def test_rect_grid_polygon_outside(): # avoid test fail when shapely not available try: import shapely except: return gr = get_rect_grid() ix = GridIntersect(gr, method="structured") result = ix.intersect_polygon(Polygon([(21., 11.), (23., 17.), (25., 11.)])) assert len(result) == 0 return result
def test_rect_grid_polygon_on_outer_boundary_shapely(): # avoid test fail when shapely not available try: import shapely except: return gr = get_rect_grid() ix = GridIntersect(gr) result = ix.intersect_polygon( Polygon([(20., 5.0), (25., 5.0), (25., 15.), (20., 15.)])) assert len(result) == 0 return result
def test_rect_grid_polygon_in_2cells_shapely(): # avoid test fail when shapely not available try: import shapely except: return gr = get_rect_grid() ix = GridIntersect(gr) result = ix.intersect_polygon( Polygon([(2.5, 5.0), (7.5, 5.0), (7.5, 15.), (2.5, 15.)])) assert len(result) == 2 assert result.areas.sum() == 50. return result
def test_rect_grid_polygon_on_inner_boundary_shapely(): # avoid test fail when shapely not available try: import shapely except: return gr = get_rect_grid() ix = GridIntersect(gr) result = ix.intersect_polygon( Polygon([(5., 10.0), (15., 10.0), (15., 5.), (5., 5.)])) assert len(result) == 2 assert result.areas.sum() == 50. return result
def gp2idomain(gp, grid, idomain, area=0, layer=0, method="none"): ''' This function attribute active values to cells given a certain geopandas object and a grid (flopy.discretization) with idomain the area is a value that determine at which level a cell will be counted as intersected by the polygon (3 for example mean that only cells that have 1/3 of their area intersected by the polygon will be accounted) ''' if method == "none": ix = GridIntersect(grid) if method == "structured": ix = GridIntersect(grid, method=method) if area == 0: result = ix.intersect_polygon(gp.geometry[0]) if area >= 1: result = ix.intersect_polygon(gp.geometry[0]) result = result[result.areas > (np.max(result.areas) / area)] lst = [] for irow, icol in result.cellids: idomain[irow * grid.ncol + icol] = 1 lst.append(((layer, irow, icol))) return lst
def test_tri_grid_polygon_on_outer_boundary(rtree=True): # avoid test fail when shapely not available try: import shapely except: return gr = get_tri_grid() if gr == -1: return ix = GridIntersect(gr, rtree=rtree) result = ix.intersect_polygon( Polygon([(20., 5.0), (25., 5.0), (25., 15.), (20., 15.)])) assert len(result) == 0 return result
def test_polygon_offset_rot_structured_grid(): # avoid test fail when shapely not available try: import shapely except: return sgr = get_rect_grid(angrot=45., xyoffset=10.) p = Polygon([(5, 10. + np.sqrt(200.)), (15, 10. + np.sqrt(200.)), (15, 10. + 1.5 * np.sqrt(200.)), (5, 10. + 1.5 * np.sqrt(200.))]) ix = GridIntersect(sgr, method="structured") result = ix.intersect_polygon(p) # assert len(result) == 3. return result
def test_rect_grid_polygon_in_edge_in_cell(rtree=True): # avoid test fail when shapely not available try: import shapely except: return gr = get_rect_grid() ix = GridIntersect(gr, method='vertex', rtree=rtree) p = Polygon([(0., 5.), (3., 0.), (7., 0.), (10., 5.), (10., -1.), (0., -1.)]) result = ix.intersect_polygon(p) assert len(result) == 1 assert result.areas.sum() == 15. return result
def test_tri_grid_polygon_outside(): # avoid test fail when shapely not available try: import shapely except: return gr = get_tri_grid(triangle_exe=triangle_exe) if gr == -1: return ix = GridIntersect(gr) result = ix.intersect_polygon(Polygon([(21., 11.), (23., 17.), (25., 11.)])) assert len(result) == 0 return result
def test_rect_grid_polygon_with_hole_shapely(): # avoid test fail when shapely not available try: import shapely except: return gr = get_rect_grid() ix = GridIntersect(gr) p = Polygon([(5., 5.), (5., 15.), (25., 15.), (25., -5.), (5., -5.)], holes=[[(9., -1), (9, 11), (21, 11), (21, -1)]]) result = ix.intersect_polygon(p) assert len(result) == 3 assert result.areas.sum() == 104. return result
def test_tri_grid_polygon_in_2cells(): # avoid test fail when shapely not available try: import shapely except: return gr = get_tri_grid(triangle_exe=triangle_exe) if gr == -1: return ix = GridIntersect(gr) result = ix.intersect_polygon( Polygon([(2.5, 5.0), (5.0, 5.0), (5.0, 15.), (2.5, 15.)])) assert len(result) == 2 assert result.areas.sum() == 25. return result
def test_rect_grid_multipolygon_in_multiple_cells_shapely(): # avoid test fail when shapely not available try: import shapely except: return gr = get_rect_grid() ix = GridIntersect(gr) p1 = Polygon([(1., 1.), (19., 1.), (19., 3.), (1., 3.)]) p2 = Polygon([(1., 9.), (19., 9.), (19., 7.), (1., 7.)]) p = MultiPolygon([p1, p2]) result = ix.intersect_polygon(p) assert len(result) == 2 assert result.areas.sum() == 72. return result
def test_rect_grid_multipolygon_in_one_cell(): # avoid test fail when shapely not available try: import shapely except: return gr = get_rect_grid() ix = GridIntersect(gr, method="structured") p1 = Polygon([(1., 1.), (8., 1.), (8., 3.), (1., 3.)]) p2 = Polygon([(1., 9.), (8., 9.), (8., 7.), (1., 7.)]) p = MultiPolygon([p1, p2]) result = ix.intersect_polygon(p) assert len(result) == 1 assert result.areas.sum() == 28. return result
def test_tri_grid_multipolygon_in_one_cell(): # avoid test fail when shapely not available try: import shapely except: return gr = get_tri_grid(triangle_exe=triangle_exe) if gr == -1: return ix = GridIntersect(gr) p1 = Polygon([(1., 1.), (8., 1.), (8., 3.), (3., 3.)]) p2 = Polygon([(5., 5.), (8., 5.), (8., 8.)]) p = MultiPolygon([p1, p2]) result = ix.intersect_polygon(p) assert len(result) == 1 assert result.areas.sum() == 16.5 return result
def gp2cellids(grid, gp, idomain, idomain_active=True, type="polygon", layer=0, areas=3): """ this function extract the cellids of the intersection between a geopandas object and a grid grid : modelgrid gp : geopandas object (polygon, linestring only) idomain : the idomain array to update it idomain_active : bool, if true the idomain is update (cells intersect by the gp will be noted as active), prevents some issues type : str, features type (polygon or line) layer : int, the layer on which is the gp areas : factor that determine if a cell is accounted intersected or not based on the total area intersected in this cell (a value of 3, for example, mean only cells which have 1/3 of their area intersected by the polygon will be taken into account) """ ix = GridIntersect(grid) if type == "polygon": result = ix.intersect_polygon(gp.geometry[0]) result = result[result.areas > ( np.max(result.areas) / 3 )] # only take into account cells that have a least 1/3 intersected result = result[result.areas != 0] # fix bug if type == "boundary": result = ix.intersect_linestring(gp.geometry[0].boundary) if type == "line": result = ix.intersect_linestring(gp.geometry[0]) lst = [] for irow, icol in result.cellids: lst.append(((layer, irow, icol))) if idomain_active: idomain[irow * grid.ncol + icol] = 1 return lst