tri_grid

class gridkit.tri_grid.TriGrid(*args, size, offset=(0, 0), rotation=0, **kwargs)[source]

Bases: BaseGrid

property dx: float

The spacing between cell centers in x-direction

property dy: float

The spacing between cell centers in y-direction

property r: float

The radius of the cell. The radius is defined to be the distance from the cell center to a cell corner.

property size: float

The size of the cell as supplied when initiating the class. The size is equivalent to dx, which is half a cell edge length.

property offset: float

The offset off the grid in dx and dy. The offset is never larger than the (dx, dy) of a single grid cell. The offset represents the shift from the origin (0,0).

centroid(index)[source]

Coordinates at the center of the cell(s) specified by index.

If this method is called on a ‘Bounded’ class, the index argument is optional. In such a case the cell IDs of the cells contained in the Bounded product are returned.

Parameters:

index (GridIndex) – The index of the cell(s) of which the centroid is to be obtained.

Returns:

Multidimensional array containing the longitude and latitude of the center of each cell respectively, in (width, height, lonlat)

Return type:

numpy.ndarray

Raises:

ValueError – No index parameter was supplied to a grid that does not contain data.

cell_corners(index=None)[source]

Coordinates of the cell corners as specified by index.

Parameters:

index (GridIndex) – The indices of the cells of interest. Each id contains an x and y value.

Returns:

An array of coordinates in (x,y) specifying each of the corners. The returned array will be of the same shape as the input index, but with an extra axis containing the corners. The last axis is always of size 2 (x,y). The second to last axis is the length of the corners. The other axis are in the shape of the supplied index.

Return type:

numpy.ndarray

cell_at_point(point)[source]

Determine the ID of the cell in which point falls.

Parameters:

point (tuple) – The coordinates of the point to which to match the cell

Returns:

The ID of the cell in (x,y)

Return type:

tuple

cells_in_bounds(bounds, return_cell_count=False)[source]

Cells contained within a bounding box.

Parameters:
  • bounds (tuple) – The bounding box in which to find the cells in (min_x, min_y, max_x, max_y)

  • return_cell_count (bool) – Return a tuple containing the nr of cells in x and y direction inside the provided bounds

Returns:

The indices of the cells contained in the bounds

Return type:

GridIndex

cells_near_point(point)[source]

The cells nearest to a point, often used in interpolation at the location of the point. For a TriGrid there are 6 nearby points. For a HexGrid there are 3 nearby points. For a RectGrid there are 4 nearby points.

is_cell_upright(index)[source]

Whether the selected cell points up or down. True if the cell points up, False if the cell points down.

Parameters:

index (GridIndex) – The index of the cell(s) of interest

Returns:

A boolean value reflecting whether the cell is upright or not. Or a 1d array containing the boolean values for each cell.

Return type:

numpy.ndarray or bool

property parent_grid_class
relative_neighbours(index=None, depth=1, connect_corners=False, include_selected=False)[source]

The relative indices of the neighbouring cells.

Parameters:
  • depth (int Default: 1) – Determines the number of neighbours that are returned. If depth=1 the direct neighbours are returned. If depth=2 the direct neighbours are returned, as well as the neighbours of these neighbours. depth=3 returns yet another layer of neighbours, and so forth.

  • index (numpy.ndarray) – The index of the cell of which the relative neighbours are desired. This is mostly relevant because in hexagonal grids the neighbouring indices differ when dealing with odd or even indices.

  • include_selected (bool Default: False) – Whether to include the specified cell in the return array. Even though the specified cell can never be a neighbour of itself, this can be useful when for example a weighted average of the neighbours is desired in which case the cell itself often should also be included.

  • connect_corners (bool Default: False) – Whether to consider cells that touch corners but not sides as neighbours. Each cell has 3 neighbours if connect_corners is False, and 9 neighbours if connect_corners is True.

neighbours(index=None, depth=1, connect_corners=False, include_selected=False)[source]

The indices of the neighbouring cells. The argument ‘depth’ can be used to include cells from further away.

Parameters:
  • index (numpy.ndarray) – The index of the cell(s) of which to get the neighbours.

  • depth (int Default: 1) – Determines the number of neighbours that are returned. If depth=1 the direct neighbours are returned. If depth=2 the direct neighbours are returned, as well as the neighbours of these neighbours. depth=3 returns yet another layer of neighbours, and so forth.

  • connect_corners (bool Default: False) – Whether to consider cells that touch corners but not sides as neighbours. This does not apply to HexGrid.relative_neighbours(). For further information on this argument, refer to RectGrid.relative_neighbours().

  • include_selected (bool Default: False) – Whether to include the specified cell in the return array. Even though the specified cell can never be a neighbour of itself, this can be useful when for example a weighted average of the neighbours is desired in which case the cell itself often should also be included.

Examples

The direct neighbours of a cell can be returned by using depth=1, which is the default.

>>> from gridkit.rect_grid import RectGrid
>>> grid = RectGrid(dx=2, dy=3)
>>> grid.neighbours([1,2]).index
array([[1, 3],
       [0, 2],
       [2, 2],
       [1, 1]])

For more detailed examples:

to_bounded(bounds, fill_value=nan)[source]

Create a bounded version of this grid where the data in the bounds is filled with the supplied fill_value

Parameters:
  • bounds (tuple) – The bounds of the area of interest in (minx, miny, maxx, maxy). The bounds need to be aligned to the grid. See BaseGrid.align_bounds()

  • fill_value (numpy.dtype (optional)) – The value to assign to the newly created array that fills the supplied bounds. Default: numpy.nan

Returns:

A bounded version of the current grid where the data is filled with fill_value.

Return type:

BoundedHexGrid or BoundedRectGrid

to_crs(crs)[source]

Transforms the Coordinate Reference System (CRS) from the current CRS to the desired CRS. This will update the cell size and the origin offset.

The crs attribute on the current grid must be set.

Parameters:

crs (Union[int, str, pyproj.CRS]) – The value can be anything accepted by pyproj.CRS.from_user_input(), such as an epsg integer (eg 4326), an authority string (eg “EPSG:4326”) or a WKT string.

Returns:

A copy of the grid with modified cell spacing to match the specified CRS

Return type:

HexGrid

class gridkit.tri_grid.BoundedTriGrid(data, *args, bounds, **kwargs)[source]

Bases: BoundedGrid, TriGrid

centroid(index=None)[source]

Coordinates at the center of the cell(s) specified by index.

If this method is called on a ‘Bounded’ class, the index argument is optional. In such a case the cell IDs of the cells contained in the Bounded product are returned.

Parameters:

index (GridIndex) – The index of the cell(s) of which the centroid is to be obtained.

Returns:

Multidimensional array containing the longitude and latitude of the center of each cell respectively, in (width, height, lonlat)

Return type:

numpy.ndarray

Raises:

ValueError – No index parameter was supplied to a grid that does not contain data.

intersecting_cells(other)[source]
crop(new_bounds, bounds_crs=None, buffer_cells=0)[source]

Cut out a slice of data contained within the supplied bounds.

Parameters:
  • new_bounds (Tuple(minx, miny, maxx, maxy)) – The bounds defining the area to crop, in (minx, miny, maxx, maxy).

  • bounds_crs (pyproj.CRS (optional)) – The bounds defining the extent of the cropped data. The value can be anything accepted by pyproj.CRS.from_user_input().

Returns:

A BoundedGrid containing the data included in the cropped area contained within the bounds.

Return type:

class: BoundedGrid

cell_corners(index: ndarray = None) ndarray[source]

Coordinates of the cell corners as specified by index.

Parameters:

index (GridIndex) – The indices of the cells of interest. Each id contains an x and y value.

Returns:

An array of coordinates in (x,y) specifying each of the corners. The returned array will be of the same shape as the input index, but with an extra axis containing the corners. The last axis is always of size 2 (x,y). The second to last axis is the length of the corners. The other axis are in the shape of the supplied index.

Return type:

numpy.ndarray

to_shapely(index=None, as_multipolygon: bool = False)[source]

Refer to parent method BaseGrid.to_shapely()

Difference with parent method:

index is optional. If index is None (default) the cells containing data are used as the index argument.

to_crs(crs, resample_method='nearest')[source]

Transforms the Coordinate Reference System (CRS) from the current CRS to the desired CRS. This will modify the cell size and the bounds accordingly.

The crs attribute on the current grid must be set.

Parameters:
  • crs (Union[int, str, pyproj.CRS]) – The value can be anything accepted by pyproj.CRS.from_user_input(), such as an epsg integer (eg 4326), an authority string (eg “EPSG:4326”) or a WKT string.

  • resample_method (str) – The resampling method to be used for BoundedGrid.resample().

Returns:

A copy of the grid with modified cell spacing and bounds to match the specified CRS

Return type:

BoundedTriGrid

numpy_id_to_grid_id(np_index)[source]
argmax(*args, **kwargs)
argmin(*args, **kwargs)
grid_id_to_numpy_id(index)[source]
max(*args, **kwargs)
mean(*args, **kwargs)
median(*args, **kwargs)
min(*args, **kwargs)
std(*args, **kwargs)
sum(*args, **kwargs)