Partition
This module provides partitioning strategies for raster data.
It includes functions for block-based, tile-based, and window-based iteration, as well as a TileStitcher class for reassembling raster tiles into a single raster file on disk.
TileStitcher
Reassembles Raster tiles into a single file on disk.
Acts as a context manager to ensure the output file is closed properly. Checks bounds to ensure tiles are written exactly where they are intended.
Source code in src/phytospatial/raster/partition.py
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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
__init__(output_path, profile, **profile_overrides)
Open a new raster file for writing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
Union[str, Path]
|
Destination path. |
required |
profile
|
Dict[str, Any]
|
Rasterio profile (metadata). |
required |
**profile_overrides
|
Any
|
Changes to the profile (dtype, compression, etc.). |
{}
|
Raises:
| Type | Description |
|---|---|
IOError
|
If the file cannot be created/opened. |
Source code in src/phytospatial/raster/partition.py
add_tile(window, tile, indexes=None)
Write a tile to the output file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window
|
Window
|
The specific window in the output file where this data goes. |
required |
tile
|
Raster
|
The Raster object containing the data. |
required |
indexes
|
Optional[List[int]]
|
Specific bands to write to. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tile shape does not match the window shape. |
RuntimeError
|
If the stitcher is closed. |
Source code in src/phytospatial/raster/partition.py
iter_blocks(source, bands=None)
Stream data using the file's native internal block structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, Path, DatasetReader]
|
An open rasterio.DatasetReader, or a path to the raster file. |
required |
bands
|
Optional[Union[int, List[int]]]
|
Specific band(s) to load (None=all, int=single, list=subset). |
None
|
Yields:
| Type | Description |
|---|---|
Tuple[Window, Raster]
|
Tuple[Window, Raster]: A window and corresponding Raster object. |
Source code in src/phytospatial/raster/partition.py
iter_core_halo(source, tile_mode='auto', tile_size=1024, overlap=64)
Streams spatial data using a Core-Halo architecture. Provides overlapping read buffers to prevent boundary truncation, while supplying a strict core bounding box for geometry deduplication.
Source code in src/phytospatial/raster/partition.py
iter_tiles(source, tile_size=512, overlap=0, bands=None)
Stream data using a virtual grid of fixed-size tiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, Path, DatasetReader]
|
An open rasterio.DatasetReader, or a path to the raster file. |
required |
tile_size
|
Union[int, Tuple[int, int]]
|
Dimensions (width, height) or single int for square tiles. |
512
|
overlap
|
int
|
Pixels of overlap between tiles. |
0
|
bands
|
Optional[Union[int, List[int]]]
|
Specific band(s) to load (None=all, int=single, list=subset). |
None
|
Yields:
| Type | Description |
|---|---|
Tuple[Window, Raster]
|
Tuple[Window, Raster]: A window and corresponding Raster object. |
Source code in src/phytospatial/raster/partition.py
iter_windows(raster, tile_size=512, overlap=0)
Partition an in-memory Raster object into smaller Raster tiles.
Useful for batch processing a loaded raster, notably for neural networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster
|
Raster
|
The source Raster object (already in memory). |
required |
tile_size
|
Union[int, Tuple[int, int]]
|
Dimensions (width, height) or single int for square tiles. |
512
|
overlap
|
int
|
Pixels of overlap. |
0
|
Yields:
| Type | Description |
|---|---|
Tuple[Window, Raster]
|
Tuple[Window, Raster]: A deep copy of the sliced data as a new Raster. |