Skip to content

Grid

aef_bng.grid

BNG output grid definition and chunk enumeration.

Enumerates 10km BNG chunks covering the requested bounds, computing the affine transform, pixel shape, and WGS84 bounds for each chunk.

ChunkSpec(bng_10km_ref, bounds_bng, bounds_wgs84, shape=(1000, 1000), transform=None) dataclass

Specification for a single 10km BNG processing chunk.

Attributes:

Name Type Description
bng_10km_ref str

10km BNG grid reference string (e.g. "SU14").

bounds_bng tuple[int, int, int, int]

BNG bounding box (minx, miny, maxx, maxy) in EPSG:27700.

bounds_wgs84 tuple[float, float, float, float]

Same bounds transformed to WGS84 (EPSG:4326).

shape tuple[int, int]

Pixel dimensions (rows, cols) - always (1000, 1000) for 10km at 10m.

transform Affine | None

Affine transform for this chunk's raster grid.

__post_init__()

Compute the affine transform from bounds if not explicitly provided.

Source code in aef_bng/grid.py
def __post_init__(self) -> None:
    """Compute the affine transform from bounds if not explicitly provided."""
    if self.transform is None:
        minx, _miny, _maxx, maxy = self.bounds_bng
        computed = Affine(BNG_RESOLUTION, 0, minx, 0, -BNG_RESOLUTION, maxy)
        object.__setattr__(self, "transform", computed)

BNGOutputGrid(bounds=BNG_BOUNDS, chunk_size=CHUNK_SIZE)

Enumerates 10km BNG grid chunks within given bounds.

Parameters:

Name Type Description Default
bounds tuple[int, int, int, int]

BNG bounding box (minx, miny, maxx, maxy) in EPSG:27700.

BNG_BOUNDS
chunk_size int

Chunk size in metres (default 10,000 for 10km).

CHUNK_SIZE
Source code in aef_bng/grid.py
def __init__(
    self,
    bounds: tuple[int, int, int, int] = BNG_BOUNDS,
    chunk_size: int = CHUNK_SIZE,
) -> None:
    self.bounds = bounds
    self.chunk_size = chunk_size
    self._transformer = Transformer.from_crs(BNG_CRS, "EPSG:4326", always_xy=True)

enumerate_chunks()

Enumerate all 10km BNG chunks within the configured bounds.

Uses osbng.bbox_to_bng to generate valid 10km grid references, then builds a ChunkSpec for each with aligned affine transforms.

Returns:

Type Description
list[ChunkSpec]

List of ChunkSpec objects covering the bounds.

Source code in aef_bng/grid.py
def enumerate_chunks(self) -> list[ChunkSpec]:
    """Enumerate all 10km BNG chunks within the configured bounds.

    Uses ``osbng.bbox_to_bng`` to generate valid 10km grid references,
    then builds a ``ChunkSpec`` for each with aligned affine transforms.

    Returns:
        List of ChunkSpec objects covering the bounds.
    """
    resolution = self.chunk_size
    resolution_label = f"{resolution // 1000}km"

    bng_refs: list[BNGReference] = bbox_to_bng(*self.bounds, resolution_label)

    chunks: list[ChunkSpec] = []
    pixels_per_side = resolution // BNG_RESOLUTION

    for ref in bng_refs:
        ref_str = ref.bng_ref_compact
        bounds_bng = bng_to_bbox(ref)
        bounds_wgs84 = self._bng_bounds_to_wgs84(bounds_bng)

        minx, _miny, _maxx, maxy = bounds_bng
        transform = Affine(BNG_RESOLUTION, 0, minx, 0, -BNG_RESOLUTION, maxy)

        chunks.append(
            ChunkSpec(
                bng_10km_ref=ref_str,
                bounds_bng=bounds_bng,
                bounds_wgs84=bounds_wgs84,
                shape=(pixels_per_side, pixels_per_side),
                transform=transform,
            )
        )

    return chunks