rasteret.core.utils¶
Geometry, CRS, and grid computation utilities.
utils
¶
Classes¶
Functions¶
run_sync
¶
Run a coroutine from synchronous API entrypoints.
Jupyter-safe: if an event loop is already running (e.g. inside a notebook), the coroutine is dispatched to a background thread.
Source code in src/rasteret/core/utils.py
infer_data_source
¶
Infer collection source for URL policy and band mapping.
Returns an empty string when no data source can be inferred.
Source code in src/rasteret/core/utils.py
transform_polygon
¶
Transform a Shapely polygon between coordinate systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geom
|
Geometry
|
Input Shapely polygon geometry. |
required |
src_crs
|
int or str
|
Source CRS (EPSG code or WKT string). |
required |
dst_crs
|
int or str
|
Target CRS (EPSG code or WKT string). |
required |
Returns:
| Type | Description |
|---|---|
Geometry
|
Reprojected Shapely polygon. |
Source code in src/rasteret/core/utils.py
transform_bbox
¶
transform_bbox(
bbox: tuple[float, float, float, float],
src_crs: int | str,
dst_crs: int | str,
) -> tuple[float, float, float, float]
Transform bounding box between coordinate systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
tuple[float, float, float, float]
|
Input bbox |
required |
src_crs
|
int or str
|
Source CRS (EPSG code or WKT string). |
required |
dst_crs
|
int or str
|
Target CRS (EPSG code or WKT string). |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, float]
|
Transformed bbox. |
Source code in src/rasteret/core/utils.py
normalize_transform
¶
Normalize common affine transform representations.
Accepts: - 4 values: (scale_x, translate_x, scale_y, translate_y) - 6 values: GDAL/rasterio affine (a, b, c, d, e, f) for north-up rasters
Returns: (scale_x, translate_x, scale_y, translate_y)
Source code in src/rasteret/core/utils.py
reproject_array
¶
reproject_array(
src_array: ndarray,
src_transform: object,
src_crs: int,
dst_crs: int,
dst_transform: object,
dst_shape: tuple[int, int],
resampling: str = "bilinear",
) -> ndarray
Reproject a 2-D array between coordinate reference systems.
Thin wrapper around rasterio.warp.reproject that works with
in-memory numpy arrays and EPSG codes; no file handle required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_array
|
ndarray
|
Input 2-D array (any numeric dtype; integer dtypes are promoted
to |
required |
src_transform
|
Affine
|
Affine transform for the source grid. |
required |
src_crs
|
int
|
Source EPSG code. |
required |
dst_crs
|
int
|
Target EPSG code. |
required |
dst_transform
|
Affine
|
Affine transform for the destination grid. |
required |
dst_shape
|
tuple[int, int]
|
|
required |
resampling
|
str
|
Resampling method name (default |
'bilinear'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Reprojected 2-D |
Source code in src/rasteret/core/utils.py
compute_dst_grid
¶
compute_dst_grid(
bounds: tuple[float, float, float, float],
res: tuple[float, float],
) -> tuple[object, tuple[int, int]]
Compute an Affine transform and pixel dimensions for a target grid.
The caller must supply res in the destination CRS units. When
the source and destination CRS share the same linear unit (e.g. both
UTM metres) the source resolution can be passed directly. For
cross-unit reprojection (e.g. UTM metres -> EPSG:4326 degrees) use
:func:compute_dst_grid_from_src instead, which delegates to
rasterio.warp.calculate_default_transform.
Args:
bounds: (xmin, ymin, xmax, ymax) in the target CRS.
res: (res_x, res_y) pixel resolution in target CRS units.
Returns:
(affine_transform, (height, width))
Source code in src/rasteret/core/utils.py
compute_dst_grid_from_src
¶
compute_dst_grid_from_src(
src_crs: int,
dst_crs: int,
width: int,
height: int,
src_bounds: tuple[float, float, float, float],
) -> tuple[object, tuple[int, int]]
Compute destination grid via GDAL's suggested-warp-output algorithm.
Wraps rasterio.warp.calculate_default_transform which delegates to
GDAL's GDALSuggestedWarpOutput2. This correctly handles cross-unit
CRS conversions (e.g. UTM metres -> EPSG:4326 degrees) by sampling the
source grid, transforming points, and computing an optimal destination
pixel size that preserves spatial-information density.
Use this instead of :func:compute_dst_grid whenever the source and
destination CRS may have different linear units.
Args:
src_crs: Source EPSG code.
dst_crs: Destination EPSG code.
width: Source raster width (pixels).
height: Source raster height (pixels).
src_bounds: (left, bottom, right, top) in the source CRS.
Returns:
(affine_transform, (height, width)) in the destination CRS.