flopy.utils.gridintersect module

class GridIntersect(mfgrid, method=None, rtree=True, local=False)[source]

Bases: object

Class for intersecting shapely geometries with MODFLOW grids.

Notes

  • The STR-tree query is based on the bounding box of the shape or collection, if the bounding box of the shape covers nearly the entire grid, the query won’t be able to limit the search space much, resulting in slower performance. Therefore, it can sometimes be faster to intersect each individual shape in a collection than it is to intersect with the whole collection at once.

  • Building the STR-tree can take a while for large grids. Once built the intersect routines (for individual shapes) should be pretty fast.

filter_query_result(cellids, shp)[source]

Filter array of geometries to obtain grid cells that intersect with shape.

Used to (further) reduce query result to cells that intersect with shape.

Parameters:
  • cellids (iterable) – iterable of cellids, query result

  • shp (shapely.geometry) – shapely geometry that is prepared and used to filter query result

Returns:

filter or generator containing polygons that intersect with shape

Return type:

array_like

intersect(shp, shapetype=None, sort_by_cellid=True, keepzerolengths=False, return_all_intersections=False, contains_centroid=False, min_area_fraction=None, geo_dataframe=False, shapely2=None)[source]

Method to intersect a shape with a model grid.

Parameters:
  • shp (shapely.geometry, geojson object, shapefile.Shape,) – or flopy geometry object

  • shapetype (str, optional) – type of shape (i.e. “point”, “linestring”, “polygon” or their multi-variants), used by GeoSpatialUtil if shp is passed as a list of vertices, default is None

  • sort_by_cellid (bool) – sort results by cellid, ensures cell with lowest cellid is returned for boundary cases when using vertex methods, default is True

  • keepzerolengths (bool) – boolean method to keep zero length intersections for linestring intersection, only used if shape type is “linestring”

  • return_all_intersections (bool, optional) – if True, return multiple intersection results for points or linestrings on grid cell boundaries (e.g. returns 2 intersection results if a point lies on the boundary between two grid cells). The default is False. Only used if shape type is “point” or “linestring”.

  • contains_centroid (bool, optional) – if True, only store intersection result if cell centroid is contained within intersection shape, only used if shape type is “polygon”

  • min_area_fraction (float, optional) – float defining minimum intersection area threshold, if intersection area is smaller than min_frac_area * cell_area, do not store intersection result, only used if shape type is “polygon”

  • geo_dataframe (bool, optional) – if True, return a geopandas GeoDataFrame, default is False

Returns:

a record array containing information about the intersection or a geopandas.GeoDataFrame if geo_dataframe=True

Return type:

numpy.recarray or gepandas.GeoDataFrame

intersects(shp, shapetype=None, dataframe=False)[source]

Return cellids for grid cells that intersect with shape.

Parameters:
  • shp (shapely.geometry, geojson geometry, shapefile.shape,) – or flopy geometry object shape to intersect with the grid

  • shapetype (str, optional) – type of shape (i.e. “point”, “linestring”, “polygon” or their multi-variants), used by GeoSpatialUtil if shp is passed as a list of vertices, default is None

  • dataframe (bool, optional) – if True, return a pandas.DataFrame, default is False

Returns:

a record array or pandas.DataFrame containing cell IDs of the gridcells the shape intersects with.

Return type:

numpy.recarray or pandas.DataFrame

plot_intersection_result(result, plot_grid=True, ax=None, **kwargs)[source]

Plot intersection result.

Parameters:
  • result (numpy.rec.recarray or geopandas.GeoDataFrame) – result of intersect()

  • plot_grid (bool, optional) – plot model grid, by default True

  • ax (matplotlib.Axes, optional) – axes to plot on, by default None which creates a new axis

Returns:

ax – returns axes handle

Return type:

matplotlib.Axes

static plot_linestring(result, ax=None, cmap=None, **kwargs)[source]

method to plot the linestring intersection results from the resulting numpy.recarray.

Note: only works when recarray has ‘ixshapes’ column!

Parameters:
  • result (numpy.recarray or geopandas.GeoDataFrame) – record array or GeoDataFrame containing intersection results

  • ax (matplotlib.pyplot.axes, optional) – axes to plot onto, if not provided, creates a new figure

  • cmap (str) – matplotlib colormap

  • **kwargs – passed to the plot function

Returns:

returns the axes handle

Return type:

matplotlib.pyplot.axes

static plot_point(result, ax=None, **kwargs)[source]

method to plot the point intersection results from the resulting numpy.recarray.

Note: only works when recarray has ‘ixshapes’ column!

Parameters:
  • result (numpy.recarray or geopandas.GeoDataFrame) – record array or GeoDataFrame containing intersection results

  • ax (matplotlib.pyplot.axes, optional) – axes to plot onto, if not provided, creates a new figure

  • **kwargs – passed to the scatter function

Returns:

returns the axes handle

Return type:

matplotlib.pyplot.axes

static plot_polygon(result, ax=None, **kwargs)[source]

method to plot the polygon intersection results from the resulting numpy.recarray.

Note: only works when recarray has ‘ixshapes’ column!

Parameters:
  • result (numpy.recarray or geopandas.GeoDataFrame) – record array or GeoDataFrame containing intersection results

  • ax (matplotlib.pyplot.axes, optional) – axes to plot onto, if not provided, creates a new figure

  • **kwargs – passed to the plot function

Returns:

returns the axes handle

Return type:

matplotlib.pyplot.axes

query_grid(shp)[source]

Perform spatial query on grid with shapely geometry. If no spatial query is possible returns all grid cells.

Parameters:

shp (shapely.geometry) – shapely geometry

Returns:

array containing cellids of grid cells in query result

Return type:

array_like

class ModflowGridIndices[source]

Bases: object

Collection of methods that can be used to find cell indices for a structured, but irregularly spaced MODFLOW grid.

Deprecated since version 3.9.0: This class is deprecated and will be removed in version 3.10.0.

static find_position_in_array(arr, x)[source]

If arr has x positions for the left edge of a cell, then return the cell index containing x.

Parameters:
  • arr (A one dimensional array (such as Xe) that contains) – coordinates for the left cell edge.

  • x (float) – The x position to find in arr.

static kij_from_nn0(n, nlay, nrow, ncol)[source]

Convert the node number to a zero-based layer, row and column format. Return (k0, i0, j0).

Parameters:
  • nodenumber (int) – The cell nodenumber, ranging from 0 to number of nodes - 1.

  • nlay (int) – The number of layers.

  • nrow (int) – The number of rows.

  • ncol (int) – The number of columns.

static kij_from_nodenumber(nodenumber, nlay, nrow, ncol)[source]

Convert the modflow node number to a zero-based layer, row and column format. Return (k0, i0, j0).

Parameters:
  • nodenumber (int) – The cell nodenumber, ranging from 1 to number of nodes.

  • nlay (int) – The number of layers.

  • nrow (int) – The number of rows.

  • ncol (int) – The number of columns.

static nn0_from_kij(k, i, j, nrow, ncol)[source]

Calculate the zero-based nodenumber using the zero-based layer, row, and column values. The first node has a value of 0.

Parameters:
  • k (int) – The model layer number as a zero-based value.

  • i (int) – The model row number as a zero-based value.

  • j (int) – The model column number as a zero-based value.

  • nrow (int) – The number of model rows.

  • ncol (int) – The number of model columns.

static nodenumber_from_kij(k, i, j, nrow, ncol)[source]

Calculate the nodenumber using the zero-based layer, row, and column values. The first node has a value of 1.

Parameters:
  • k (int) – The model layer number as a zero-based value.

  • i (int) – The model row number as a zero-based value.

  • j (int) – The model column number as a zero-based value.

  • nrow (int) – The number of model rows.

  • ncol (int) – The number of model columns.

parse_shapely_ix_result(collection, ix_result, shptyps=None)[source]

Recursive function for parsing shapely intersection results. Returns a list of shapely shapes matching shptyps.

Parameters:
  • collection (list) – state variable for storing result, generally an empty list

  • ix_result (shapely.geometry type) – any shapely intersection result

  • shptyps (str, list of str, or None, optional) – if None (default), return all types of shapes. if str, return shapes of that type, if list of str, return all types in list

Returns:

list containing shapely geometries of type shptyps

Return type:

list