from typing import Tuple
import affine
import rasterio
from rasterio.transform import rowcol
[docs]def get_window(
extent: Tuple[float, float, float, float], transform: affine.Affine
) -> Tuple[Tuple[int, int], Tuple[int, int]]:
"""
Calculate the row and column window indices for the given extent and affine transform.
Parameters:
extent (tuple): A tuple representing the extent (xmin, ymin, xmax, ymax) of the window.
transform (Affine): An affine transformation matrix.
Returns:
tuple: A tuple containing the row and column window indices as (row_indices, col_indices).
"""
row_start, col_start = rowcol(transform, extent[0], extent[-1], op=int)
row_stop, col_stop = rowcol(transform, extent[2], extent[1], op=int)
return (row_start, row_stop), (col_start, col_stop)
[docs]def compute_bounds(
width: int, height: int, transform: affine.Affine
) -> Tuple[float, float, float, float]:
"""
Compute the bounds of an array using its dimensions and affine transformation.
Parameters:
width (int): Width of the array.
height (int): Height of the array.
transform (Affine): An affine transformation matrix.
Returns:
tuple: A tuple containing the computed bounds (xmin, ymin, xmax, ymax).
"""
bounds = rasterio.transform.array_bounds(height, width, transform)
return bounds
[docs]def get_pixel_resolution(transform: affine.Affine) -> Tuple[float, float]:
"""
Get the pixel resolution from an affine transformation matrix.
Parameters:
transform (Affine): An affine transformation matrix.
Returns:
tuple: The pixel resolution as (pixel_width, pixel_height).
"""
return transform[0], -transform[4]