flopy.utils.gridintersect module

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

Bases: object

Class for intersecting shapely shapes (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. It is possible to perform intersects without building the STR-tree by setting rtree=False.

  • The optimized routines for structured grids will often outperform the shapely routines 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.

static filter_query_result(qresult, shp)[source]

Filter query result to obtain grid cells that intersect with shape. Used to (further) reduce query result to cells that definitely intersect with shape.

Parameters
  • qresult (iterable) – query result, iterable of polygons

  • 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

qfiltered

intersect(shp, shapetype=None, sort_by_cellid=True, keepzerolengths=False)[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, used when shp is of type “linestring”

Returns

a record array containing information about the intersection

Return type

numpy.recarray

intersects(shp, shapetype=None)[source]

Return cellIDs for shapes 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

rec – 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

ax – 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

ax – 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

ax – 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

list or generator containing grid cells in query result

Return type

list or generator expression

static sort_gridshapes(shape_iter)[source]

Sort query result by node id.

Parameters

shape_iter (iterable) – list or iterable of gridcells

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_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 shptyp.

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

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

  • shptyp (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

collection – list containing shapely geometries of type shptyp

Return type

list