flopy.utils.gridintersect module

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

Bases: object

Class for intersecting shapely geometries (Point, Linestring, Polygon, or their Multi variants) with MODFLOW grids. Contains optimized search routines for structured 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.

  • The optimized routines for structured grids can outperform the shapely routines for point and linestring intersections because of the reduced overhead of building and parsing the STR-tree. However, for polygons the STR-tree implementation is often faster than the optimized structured routines, especially for larger grids.

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, shapely2=True)[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”

  • shapely2 (bool, optional) – temporary flag to determine whether to use methods optimized for shapely 2.0. Useful for comparison performance between the old (shapely 1.8) and new (shapely 2.0) implementations.

Returns:

a record array containing information about the intersection

Return type:

numpy.recarray

intersects(shp, shapetype=None)[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

Returns:

a record array containing cell IDs of the gridcells the shape intersects with

Return type:

numpy.recarray

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

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

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

Parameters:
  • rec (numpy.recarray) – record array containing intersection results (the resulting shapes)

  • 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(rec, ax=None, **kwargs)[source]

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

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

Parameters:
  • rec (numpy.recarray) – record array 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(rec, ax=None, **kwargs)[source]

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

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

Parameters:
  • rec (numpy.recarray) – record array containing intersection results (the resulting shapes)

  • 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

static sort_gridshapes(geoms, cellids)[source]

Sort geometries (from i.e. query result) by cell id.

Deprecated since version 3.3.6: sorting is now performed on cellids.

Parameters:

geoms (iterable) – list or iterable of geometries

Returns:

sorted list of gridcells

Return type:

list

class ModflowGridIndices[source]

Bases: object

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

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.

ignore_shapely2_strtree_warning()[source]
ignore_shapely_warnings_for_object_array()[source]
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