Layer
This module defines the Raster object data structure.
It synchronizes pixel data (NumPy array) with geospatial context (CRS, Transform).
Raster
In-memory raster data container with geospatial metadata.
A Raster synchronizes: 1. Pixel Data: A NumPy array in (Bands, Height, Width) format 2. Geospatial Context: CRS, Affine Transform, NoData value
Unlike a rasterio dataset, this object holds data in RAM, enabling high-performance operation chaining without disk I/O.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
ndarray
|
Pixel array in (Bands, Height, Width) format |
transform |
Affine
|
Affine transform matrix (pixel → coordinates) |
crs |
CRS
|
Coordinate Reference System |
nodata |
float | int | None
|
Value representing missing data |
band_names |
Dict[str, int]
|
Mapping of semantic names to 1-based band indices |
Source code in src/phytospatial/raster/layer.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
bounds
property
Bounding box in CRS coordinates.
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float, float]
|
Tuple[float, float, float, float]: (left, bottom, right, top) |
count
property
Number of bands.
data
property
writable
Access the raw pixel data.
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: 3D array in (Bands, Height, Width) format |
height
property
Raster height in pixels.
memory_size
property
Estimate memory size in bytes.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Memory size in bytes |
profile
property
Generate a rasterio-compliant profile for saving.
This profile can be used with rasterio.open() to write the raster to disk. NOTE: Can override specific keys (compress='deflate') when saving using the **profile_kwargs in the save() function.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Rasterio profile dictionary |
shape
property
Raster dimensions.
Returns:
| Type | Description |
|---|---|
Tuple[int, int, int]
|
Tuple[int, int, int]: (Bands, Height, Width) |
width
property
Raster width in pixels.
__array__()
NumPy array interface. Allows direct use in NumPy functions:
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The underlying data array |
__eq__(other)
Check equality, first based on metadata only and, if necessary, based on pixel data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Object to compare with |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if rasters are identical |
Source code in src/phytospatial/raster/layer.py
__init__(data, transform, crs, nodata=None, band_names=None)
Initialize a Raster object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Pixel array. Must be 2D (Height, Width) or 3D (Bands, Height, Width). 2D arrays are automatically promoted to 3D (1, Height, Width). |
required |
transform
|
Affine
|
Geospatial transform (maps pixel coords to CRS coords) |
required |
crs
|
Union[str, CRS]
|
Coordinate Reference System (EPSG code, proj string, or CRS object) |
required |
nodata
|
Optional[Union[float, int]]
|
Value indicating missing/invalid data |
None
|
band_names
|
Optional[Dict[str, int]]
|
Optional mapping of semantic names to 1-based band indices Format:{"red": 1, "green": 2, "blue": 3} |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If data or transform have wrong types |
ValueError
|
If data dimensions are invalid |
Source code in src/phytospatial/raster/layer.py
__repr__()
String representation for debugging.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Human-readable description of the Raster |
Source code in src/phytospatial/raster/layer.py
copy()
Create a deep copy of the Raster.
Returns:
| Name | Type | Description |
|---|---|---|
Raster |
Raster
|
Independent copy with duplicated data and metadata |
Source code in src/phytospatial/raster/layer.py
get_band(identifier)
Retrieve a specific band by index or name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
Union[int, str]
|
Either a 1-based band index (int) or semantic name (str) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: 2D array of the requested band |
Raises:
| Type | Description |
|---|---|
KeyError
|
If band name not found |
IndexError
|
If band index out of range |