pydsstools.core.gridinfo.transforms
Coordinate transformation and grid utility functions.
This module provides utilities for: - Converting between affine transforms and grid coordinates - Calculating grid extents - Checking grid spatial relationships - Validating grid consistency
Functions
|
Calculate geographic extent of a grid. |
|
Check if two grids overlap spatially. |
|
Calculate lower-left cell indices from minimum coordinates. |
|
Calculate lower-left cell indices from affine transform. |
|
Calculate minimum (x, y) coordinates from affine transform and shape. |
|
Check grid metadata for common configuration errors. |
- pydsstools.core.gridinfo.transforms.minxy_from_transform_shape(transform, shape)[source]
Calculate minimum (x, y) coordinates from affine transform and shape.
- Parameters:
transform (tuple) – 6-element affine transform: (dx, 0, xmin, 0, dy, ymax). dy is typically negative for north-up orientation.
shape (tuple[int, int]) – Grid dimensions as (rows, cols).
- Returns:
Minimum (x, y) coordinates of grid extent.
- Return type:
tuple[float, float]
Warning
Logs warning if cell sizes in x and y directions differ.
Examples
>>> transform = (1000, 0, 500000, 0, -1000, 4200000) >>> shape = (200, 300) >>> minxy = minxy_from_transform_shape(transform, shape) >>> print(minxy) (500000.0, 4000000.0)
Notes
The transform follows GDAL convention: - transform[0]: pixel width (dx) - transform[2]: x-coordinate of upper-left corner - transform[4]: pixel height (dy, negative for north-up) - transform[5]: y-coordinate of upper-left corner
- pydsstools.core.gridinfo.transforms.lower_left_cell_from_minxy(minxy, shape, cellsize, xcoord_cell0=0, ycoord_cell0=0)[source]
Calculate lower-left cell indices from minimum coordinates.
Computes the cell indices for the lower-left grid cell based on minimum coordinates and projection origin.
- Parameters:
minxy (tuple[float, float]) – Minimum (x, y) coordinates of grid extent.
shape (tuple[int, int]) – Grid dimensions as (rows, cols).
cellsize (float) – Cell size in projection units.
xcoord_cell0 (float, optional) – X-coordinate of projection origin (false easting), default 0.
ycoord_cell0 (float, optional) – Y-coordinate of projection origin (false northing), default 0.
- Returns:
Lower-left cell indices as (x_cell, y_cell).
- Return type:
tuple[int, int]
Examples
For SHG/Albers grid:
>>> minxy = (-1500000, 500000) # In EPSG:5070 coordinates >>> shape = (100, 150) >>> cellsize = 2000.0 >>> llc = lower_left_cell_from_minxy(minxy, shape, cellsize, 0, 0) >>> print(llc) (-750, 250)
Notes
Used primarily for Albers/SHG grids where the projection origin (xcoord_cell0, ycoord_cell0) is typically (0, 0) and different from the grid’s actual spatial extent.
The calculation: - Converts coordinates to easting/northing relative to origin - Divides by cell size to get cell indices - Uses floor() to get integer cell indices
- pydsstools.core.gridinfo.transforms.lower_left_cell_from_transform(transform, shape, xcoord_cell0=0, ycoord_cell0=0)[source]
Calculate lower-left cell indices from affine transform.
- Parameters:
transform (tuple) – 6-element affine transform: (dx, 0, xmin, 0, dy, ymax).
shape (tuple[int, int]) – Grid dimensions as (rows, cols).
xcoord_cell0 (float, optional) – X-coordinate of projection origin (false easting), default 0.
ycoord_cell0 (float, optional) – Y-coordinate of projection origin (false northing), default 0.
- Returns:
Lower-left cell indices as (x_cell, y_cell).
- Return type:
tuple[int, int]
Warning
Logs warning if cell sizes in x and y directions differ.
Examples
>>> transform = (2000, 0, -1500000, 0, -2000, 700000) >>> shape = (100, 150) >>> llc = lower_left_cell_from_transform(transform, shape, 0, 0) >>> print(llc) (-750, 250)
See also
lower_left_cell_from_minxyCalculate from coordinates
minxy_from_transform_shapeExtract min_xy from transform
References
- pydsstools.core.gridinfo.transforms.grid_extent(gridinfo)[source]
Calculate geographic extent of a grid.
- Parameters:
gridinfo (GridInfo or subclass) – Grid metadata object.
- Returns:
Grid extent as (xmin, ymin, xmax, ymax) in projection coordinates.
- Return type:
tuple[float, float, float, float]
- Raises:
ValueError – If gridinfo.min_xy is None.
Examples
>>> from pydsstools.core.gridinfo import GridInfoCreate, GridType, DataType >>> info = GridInfoCreate( ... grid_type=GridType.specified, ... data_type=DataType.inst_val, ... shape=(200, 300), ... cell_size=1000.0, ... min_xy=(500000, 4000000), ... crs="EPSG:32610", ... nodata=-9999.0 ... ) >>> extent = grid_extent(info) >>> print(extent) (500000.0, 4000000.0, 800000.0, 4200000.0)
See also
grids_overlapCheck if two grids overlap spatially
- pydsstools.core.gridinfo.transforms.grids_overlap(gridinfo1, gridinfo2, tolerance=0.001)[source]
Check if two grids overlap spatially.
- Parameters:
- Returns:
True if grids overlap, False otherwise.
- Return type:
bool
- Raises:
ValueError – If either gridinfo has min_xy = None.
Examples
>>> from pydsstools.core.gridinfo import GridInfoCreate, GridType, DataType >>> info1 = GridInfoCreate( ... grid_type=GridType.undefined, ... data_type=DataType.inst_val, ... shape=(100, 100), ... cell_size=1000.0, ... min_xy=(0, 0) ... ) >>> info2 = GridInfoCreate( ... grid_type=GridType.undefined, ... data_type=DataType.inst_val, ... shape=(100, 100), ... cell_size=1000.0, ... min_xy=(50000, 50000) ... ) >>> grids_overlap(info1, info2) True
Notes
This function only checks spatial overlap based on extents. It does not check: - CRS compatibility - Cell size compatibility - Grid alignment
See also
grid_extentCalculate grid extent
- pydsstools.core.gridinfo.transforms.validate_grid_consistency(gridinfo)[source]
Check grid metadata for common configuration errors.
Validates: - Coordinate consistency (min_xy, coords_cell0, lower_left_cell) - Cell size reasonableness - Grid dimensions - CRS definition (for specified grids) - Statistics (min <= max, etc.)
- Parameters:
gridinfo (GridInfo or subclass) – Grid metadata object to validate.
- Returns:
List of validation issues found. Empty list if no issues.
- Return type:
list[str]
Examples
>>> from pydsstools.core.gridinfo import GridInfoCreate, GridType, DataType >>> info = GridInfoCreate( ... grid_type=GridType.albers, ... data_type=DataType.inst_val, ... shape=(100, 150), ... cell_size=2000.0, ... min_xy=(-1500000, 500000) ... ) >>> issues = validate_grid_consistency(info) >>> if issues: ... for issue in issues: ... print(f"Warning: {issue}")
Notes
This is a non-exhaustive validation. It catches common errors but may not detect all possible inconsistencies.
See also
grid_extentCalculate grid extent for spatial validation