Skip to content

Types

aef_bng.types

Type definitions used.

Currently focuses on bounding box definitions and helper methods for simple use.

BoundingBox(minx, miny, maxx, maxy) dataclass

as_tuple()

Returns the bounding box coordinates as a tuple.

Source code in aef_bng/types.py
def as_tuple(self) -> tuple[float, float, float, float]:
    """Returns the bounding box coordinates as a tuple."""
    return (self.minx, self.miny, self.maxx, self.maxy)

as_list()

Returns the bounding box coordinates as a list.

Source code in aef_bng/types.py
def as_list(self) -> list[float]:
    """Returns the bounding box coordinates as a list."""
    return [self.minx, self.miny, self.maxx, self.maxy]

to_geojson()

Returns the bounding box as a GeoJSON Polygon geometry dictionary. Note: GeoJSON expects coordinates in EPSG:4326 (WGS84).

Source code in aef_bng/types.py
def to_geojson(self) -> dict[str, Any]:
    """
    Returns the bounding box as a GeoJSON Polygon geometry dictionary.
    Note: GeoJSON expects coordinates in EPSG:4326 (WGS84).
    """
    return {
        "type": "Polygon",
        "coordinates": [
            [
                [self.minx, self.miny],
                [self.maxx, self.miny],
                [self.maxx, self.maxy],
                [self.minx, self.maxy],
                [self.minx, self.miny],
            ]
        ],
    }

to_geodataframe(crs=None)

Returns the bounding box as a GeoPandas GeoDataFrame.

Parameters:

Name Type Description Default
crs str | int | CRS | None

The Coordinate Reference System of the bounding box.

None

Returns:

Type Description
GeoDataFrame

A GeoDataFrame containing the bounding box as a single polygon geometry.

Source code in aef_bng/types.py
def to_geodataframe(self, crs: str | int | CRS | None = None) -> geopandas.GeoDataFrame:
    """
    Returns the bounding box as a GeoPandas GeoDataFrame.

    Args:
        crs: The Coordinate Reference System of the bounding box.

    Returns:
        A GeoDataFrame containing the bounding box as a single polygon geometry.
    """

    geom = box(self.minx, self.miny, self.maxx, self.maxy)
    return geopandas.GeoDataFrame({"geometry": [geom]}, geometry="geometry", crs=crs)

reproject(from_crs, to_crs)

Reprojects the bounding box coordinates to a new CRS.

Parameters:

Name Type Description Default
from_crs str | int

The source CRS (e.g., "EPSG:4326" or 4326).

required
to_crs str | int

The target CRS (e.g., "EPSG:27700" or 27700).

required

Returns:

Type Description
BoundingBox

A new BoundingBox instance with transformed coordinates.

Source code in aef_bng/types.py
def reproject(self, from_crs: str | int, to_crs: str | int) -> BoundingBox:
    """
    Reprojects the bounding box coordinates to a new CRS.

    Args:
        from_crs: The source CRS (e.g., "EPSG:4326" or 4326).
        to_crs: The target CRS (e.g., "EPSG:27700" or 27700).

    Returns:
        A new BoundingBox instance with transformed coordinates.
    """
    transformer = Transformer.from_crs(
        crs_from=CRS.from_user_input(from_crs),
        crs_to=CRS.from_user_input(to_crs),
        always_xy=True,
    )

    new_xs, new_ys = transformer.transform(xx=[self.minx, self.maxx], yy=[self.miny, self.maxy])

    return BoundingBox(
        minx=min(new_xs),
        miny=min(new_ys),
        maxx=max(new_xs),
        maxy=max(new_ys),
    )