PatchModule (layer)
Bases: Module
, RasterBase
The spatial sub-module base class.
Inherit from this class to create a submodule.
This tutorial shows the model structure.
This is also a raster layer, inherited from the 'mesa-geo.RasterLayer' class.
ABSESpy extends this class, so it can:
1. place agents (by _CellAgentsContainer
class.)
2. work with xarray
, rasterio
packages for better data I/O workflow.
Attributes:
Name | Type | Description |
---|---|---|
cell_properties |
set[str]
|
The accessible attributes of cells stored in this layer.
When a |
attributes |
set[str]
|
All accessible attributes from this layer, including cell_properties. |
shape2d |
Coordinate
|
Raster shape in 2D (heigh, width). |
shape3d |
Coordinate
|
Raster shape in 3D (1, heigh, width),
this is for compatibility with |
array_cells |
ndarray
|
Array type of the |
coords |
Coordinate
|
Coordinate system of the raster data.
This is useful when working with |
random |
ListRandom
|
A random proxy by calling the cells as an |
Source code in abses/patch.py
cell_properties
property
¶
The accessible attributes of cells stored in this layer.
All PatchCell
methods decorated by raster_attribute
should be appeared here.
shape2d
property
¶
Raster shape in 2D (height, width).
This is useful when working with 2d numpy.array
.
shape3d
property
¶
Raster shape in 3D (1, heigh, width).
This is useful when working with rasterio
band.
coords
property
¶
Coordinate system of the raster data.
This is useful when working with xarray.DataArray
.
transform_coord ¶
Transforming the row, col to the real-world coordinate.
to_crs ¶
Converting the raster data to a another CRS.
Source code in abses/patch.py
dynamic_var ¶
Update and get dynamic variable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr_name |
str
|
The dynamic variable to retrieve. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
2D numpy.ndarray data of the variable. |
Source code in abses/patch.py
get_rasterio ¶
Gets the Rasterio raster layer corresponding to the attribute. Save to a temporary rasterio memory file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr_name |
Optional[str]
|
The attribute name for creating the rasterio file. |
None
|
Returns:
Type | Description |
---|---|
MemoryFile
|
The rasterio tmp memory file of raster. |
Source code in abses/patch.py
get_xarray ¶
Get the xarray raster layer with spatial coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr_name |
Optional[str]
|
The attribute to retrieve. If None (by default), return all available attributes (3D DataArray). Otherwise, 2D DataArray of the chosen attribute. |
None
|
Returns:
Type | Description |
---|---|
DataArray
|
Xarray.DataArray data with spatial coordinates of the chosen attribute. |
Source code in abses/patch.py
select ¶
Select cells from this layer.
Also has a shortcut alias for this method: .sel
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
where |
Optional[CellFilter]
|
The condition to select cells. If None (by default), select all cells. If a string, select cells by the attribute name. If a numpy.ndarray, select cells by the mask array. If a Shapely Geometry, select cells by the intersection with the geometry. |
None
|
Raises:
Type | Description |
---|---|
TypeError
|
If the input type is not supported. |
Returns:
Type | Description |
---|---|
ActorsList[PatchCell]
|
An |
Source code in abses/patch.py
apply ¶
Apply a function to array cells.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ufunc |
Callable[..., Any]
|
A function to apply. |
required |
*args |
Any
|
Positional arguments to pass to the function. |
()
|
**kwargs |
Any
|
Keyword arguments to pass to the function. |
{}
|
Returns:
Type | Description |
---|---|
ndarray
|
The result of the function applied to the array cells. |
Source code in abses/patch.py
coord_iter ¶
apply_raster ¶
Apply raster data to the cells.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Raster
|
np.ndarray data: 2D numpy array with shape (1, height, width). xr.DataArray data: xarray DataArray with spatial coordinates. xr.Dataset data: xarray Dataset with spatial coordinates. |
required |
attr_name |
Optional[str]
|
Name of the attribute to be added to the cells. If None, a random name will be generated. Default is None. |
required |
**kwargs |
Any
|
cover_crs: Whether to cover the crs of the input data. If False, it assumes the input data has crs info. If True, it will cover the crs of the input data by the crs of this layer. Default is False. resampling_method: The resampling method when re-projecting the input data. Default is "nearest". flipud: Whether to flip the input data upside down. Set to True when the input data is not in the same direction as the raster layer. Default is False. |
{}
|
Source code in abses/patch.py
get_raster ¶
Obtaining the Raster layer by attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr_name |
Optional[str]
|
The attribute to retrieve. If None (by default), retrieve all attributes as a 3D array. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
A 3D array of attribute. |
Source code in abses/patch.py
reproject ¶
Reproject the xarray data to the same CRS as this layer.
Source code in abses/patch.py
get_neighborhood
cached
¶
Getting neighboring positions of the given coordinate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos |
Coordinate
|
The coordinate to get the neighborhood. |
required |
moore |
bool
|
Whether to use Moore neighborhood. If False, use Von Neumann neighborhood. |
required |
include_center |
bool
|
Whether to include the center cell. Default is False. |
False
|
radius |
int
|
The radius of the neighborhood. Default is 1. |
1
|
annular |
bool
|
Whether to use annular (ring) neighborhood. Default is False. |
False
|
return_mask |
bool
|
Whether to return a mask array.
If True, return a mask array of the neighboring locations.
Otherwise, return a |
False
|
Returns:
Type | Description |
---|---|
ActorsList[PatchCell] | ndarray
|
An |
ActorsList[PatchCell] | ndarray
|
Where the mask array is a boolean array with the same shape as the raster layer. |
ActorsList[PatchCell] | ndarray
|
The True value indicates the cell is in the neighborhood. |
Source code in abses/patch.py
to_file ¶
Writes a raster layer to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster_file |
str
|
The path to the raster file to write to. |
required |
attr_name |
Optional[str]
|
The name of the attribute to write to the raster. If None, all attributes are written. Default is None. |
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. |
'GTiff'
|
Source code in abses/patch.py
out_of_bounds ¶
Determines whether position is off the grid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos |
Coordinate
|
Position to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if position is off the grid, False otherwise. |