Raster Layers¶
Raster Layers¶
- class mesa_geo.raster_layers.Cell(model, pos=None, indices=None)[source]¶
Cells are containers of raster attributes, and are building blocks of RasterLayer.
- advance() None¶
- indices: tuple[int, int] | None¶
- pos: tuple[int, int] | None¶
- property random: Random¶
- remove() None¶
Remove and delete the agent from the model.
- class mesa_geo.raster_layers.ImageLayer(values, crs, total_bounds)[source]¶
- property crs: CRS | None¶
Return the coordinate reference system of the object.
- classmethod from_file(image_file) ImageLayer[source]¶
Creates an ImageLayer from an image file.
- Parameters:
image_file – The path to the image file.
- Returns:
The ImageLayer.
- Return type:
- property height: int¶
Return the height of the raster base layer.
- Returns:
Height of the raster base layer.
- Return type:
int
- out_of_bounds(pos: tuple[int, int]) bool¶
Determines whether position is off the grid.
- Parameters:
pos (Coordinate) – Position to check.
- Returns:
True if position is off the grid, False otherwise.
- Return type:
bool
- property resolution: tuple[float, float]¶
Returns the (width, height) of a cell in the units of CRS.
- Returns:
Width and height of a cell in the units of CRS.
- Return type:
Tuple[float, float]
- to_crs(crs, inplace=False) ImageLayer | None[source]¶
Transform the object to a new coordinate reference system.
- Parameters:
crs – The coordinate reference system to transform to.
inplace – Whether to transform the object in place or return a new object. Defaults to False.
- Returns:
The transformed object if not inplace.
- Return type:
GeoBase | None
- property total_bounds: ndarray | None¶
Return the bounds of the object in [min_x, min_y, max_x, max_y] format.
- Returns:
The bounds of the object in [min_x, min_y, max_x, max_y] format.
- Return type:
np.ndarray | None
- property transform: Affine¶
Return the affine transformation of the raster base layer.
- Returns:
Affine transformation of the raster base layer.
- Return type:
Affine
- property values: ndarray¶
Returns the values of the image layer.
- Returns:
The values of the image layer.
- Return type:
np.ndarray
- property width: int¶
Return the width of the raster base layer.
- Returns:
Width of the raster base layer.
- Return type:
int
- class mesa_geo.raster_layers.RasterBase(width, height, crs, total_bounds)[source]¶
Base class for raster layers.
- property crs: CRS | None¶
Return the coordinate reference system of the object.
- property height: int¶
Return the height of the raster base layer.
- Returns:
Height of the raster base layer.
- Return type:
int
- out_of_bounds(pos: tuple[int, int]) bool[source]¶
Determines whether position is off the grid.
- Parameters:
pos (Coordinate) – Position to check.
- Returns:
True if position is off the grid, False otherwise.
- Return type:
bool
- property resolution: tuple[float, float]¶
Returns the (width, height) of a cell in the units of CRS.
- Returns:
Width and height of a cell in the units of CRS.
- Return type:
Tuple[float, float]
- to_crs(crs, inplace=False) RasterBase | None[source]¶
Transform the object to a new coordinate reference system.
- Parameters:
crs – The coordinate reference system to transform to.
inplace – Whether to transform the object in place or return a new object. Defaults to False.
- Returns:
The transformed object if not inplace.
- Return type:
GeoBase | None
- property total_bounds: ndarray | None¶
Return the bounds of the object in [min_x, min_y, max_x, max_y] format.
- Returns:
The bounds of the object in [min_x, min_y, max_x, max_y] format.
- Return type:
np.ndarray | None
- property transform: Affine¶
Return the affine transformation of the raster base layer.
- Returns:
Affine transformation of the raster base layer.
- Return type:
Affine
- property width: int¶
Return the width of the raster base layer.
- Returns:
Width of the raster base layer.
- Return type:
int
- class mesa_geo.raster_layers.RasterLayer(width, height, crs, total_bounds, model, cell_cls: type[~mesa_geo.raster_layers.Cell] = <class 'mesa_geo.raster_layers.Cell'>)[source]¶
Some methods in RasterLayer are copied from mesa.space.Grid, including:
__getitem__ __iter__ coord_iter iter_neighborhood get_neighborhood iter_neighbors get_neighbors # copied and renamed to get_neighboring_cells out_of_bounds # copied into RasterBase iter_cell_list_contents get_cell_list_contents
Methods from mesa.space.Grid that are not copied over:
torus_adj neighbor_iter move_agent place_agent _place_agent remove_agent is_cell_empty move_to_empty find_empty exists_empty_cells
Another difference is that mesa.space.Grid has self.grid: List[List[Agent | None]], whereas it is self.cells: List[List[Cell]] here in RasterLayer.
- apply_raster(data: ndarray, attr_name: str | None = None) None[source]¶
Apply raster data to the cells.
- Parameters:
data (np.ndarray) – 2D numpy array with shape (1, height, width).
attr_name (str | None) – Name of the attribute to be added to the cells. If None, a random name will be generated. Default is None.
- Raises:
ValueError – If the shape of the data is not (1, height, width).
- property attributes: set[str]¶
Return the attributes of the cells in the raster layer.
- Returns:
Attributes of the cells in the raster layer.
- Return type:
Set[str]
- coord_iter() Iterator[tuple[Cell, int, int]][source]¶
An iterator that returns coordinates as well as cell contents.
- property crs: CRS | None¶
Return the coordinate reference system of the object.
- classmethod from_file(raster_file: str, cell_cls: type[~mesa_geo.raster_layers.Cell] = <class 'mesa_geo.raster_layers.Cell'>, attr_name: str | None = None) RasterLayer[source]¶
Creates a RasterLayer from a raster file.
- Parameters:
raster_file (str) – Path to the raster file.
cell_cls (Type[Cell]) – The class of the cells in the layer.
attr_name (str | None) – The name of the attribute to use for the cell values. If None, a random name will be generated. Default is None.
- get_cell_list_contents(positions) Any¶
- get_neighborhood(pos: tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) list[tuple[int, int]][source]¶
- get_neighboring_cells(pos: tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) list[Cell][source]¶
- get_raster(attr_name: str | None = None) ndarray[source]¶
Return the values of given attribute.
- Parameters:
attr_name (str | None) – Name of the attribute to be returned. If None, returns all attributes. Default is None.
- Returns:
The values of given attribute as a 2D numpy array with shape (1, height, width).
- Return type:
np.ndarray
- property height: int¶
Return the height of the raster base layer.
- Returns:
Height of the raster base layer.
- Return type:
int
- iter_cell_list_contents(positions) Any¶
- iter_neighborhood(pos: tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) Iterator[tuple[int, int]][source]¶
Return an iterator over cell coordinates that are in the neighborhood of a certain point.
- Parameters:
pos (Coordinate) – Coordinate tuple for the neighborhood to get.
moore (bool) – Whether to use Moore neighborhood or not. If True, return Moore neighborhood (including diagonals). If False, return Von Neumann neighborhood (exclude diagonals).
include_center (bool) – If True, return the (x, y) cell as well. Otherwise, return surrounding cells only. Default is False.
radius (int) – Radius, in cells, of the neighborhood. Default is 1.
- Returns:
An iterator over cell coordinates that are in the neighborhood. For example with radius 1, it will return list with number of elements equals at most 9 (8) if Moore, 5 (4) if Von Neumann (if not including the center).
- Return type:
Iterator[Coordinate]
- iter_neighbors(pos: tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) Iterator[Cell][source]¶
Return an iterator over neighbors to a certain point.
- Parameters:
pos (Coordinate) – Coordinate tuple for the neighborhood to get.
moore (bool) – Whether to use Moore neighborhood or not. If True, return Moore neighborhood (including diagonals). If False, return Von Neumann neighborhood (exclude diagonals).
include_center (bool) – If True, return the (x, y) cell as well. Otherwise, return surrounding cells only. Default is False.
radius (int) – Radius, in cells, of the neighborhood. Default is 1.
- Returns:
An iterator of cells that are in the neighborhood; at most 9 (8) if Moore, 5 (4) if Von Neumann (if not including the center).
- Return type:
Iterator[Cell]
- out_of_bounds(pos: tuple[int, int]) bool¶
Determines whether position is off the grid.
- Parameters:
pos (Coordinate) – Position to check.
- Returns:
True if position is off the grid, False otherwise.
- Return type:
bool
- property resolution: tuple[float, float]¶
Returns the (width, height) of a cell in the units of CRS.
- Returns:
Width and height of a cell in the units of CRS.
- Return type:
Tuple[float, float]
- to_crs(crs, inplace=False) RasterLayer | None[source]¶
Transform the object to a new coordinate reference system.
- Parameters:
crs – The coordinate reference system to transform to.
inplace – Whether to transform the object in place or return a new object. Defaults to False.
- Returns:
The transformed object if not inplace.
- Return type:
GeoBase | None
- to_file(raster_file: str, attr_name: str | None = None, driver: str = 'GTiff') None[source]¶
Writes a raster layer to a file.
- Parameters:
raster_file (str) – The path to the raster file to write to.
attr_name (str | None) – The name of the attribute to write to the raster. If None, all attributes are written. Default is None.
driver (str) – The GDAL driver to use for writing the raster file. Default is ‘GTiff’. See GDAL docs at https://gdal.org/drivers/raster/index.html.
- to_image(colormap) ImageLayer[source]¶
Returns an ImageLayer colored by the provided colormap.
- property total_bounds: ndarray | None¶
Return the bounds of the object in [min_x, min_y, max_x, max_y] format.
- Returns:
The bounds of the object in [min_x, min_y, max_x, max_y] format.
- Return type:
np.ndarray | None
- property transform: Affine¶
Return the affine transformation of the raster base layer.
- Returns:
Affine transformation of the raster base layer.
- Return type:
Affine
- property width: int¶
Return the width of the raster base layer.
- Returns:
Width of the raster base layer.
- Return type:
int