geobbox package

geobbox

A python library for georeferenced bounding boxes.

class geobbox.GeoBoundingBox(left: float, bottom: float, right: float, top: float, crs: CRS = CRS.from_epsg(4326))

Bases: object

A georeferenced bounding box.

This is a Coordinate Reference System (crs) and the bounding box’s left, bottom, right and top borders, expressed in that CRS.

left, bottom, right, top

The borders of the bounding box.

Type:

float

crs

The CRS of the GeoBoundingBox. Defaults to WGS84.

Type:

CRS

property ul: tuple[float, float]

Compute the coordinate of the upper-left corner of a bounding box, in northing/easting format.

property ur: tuple[float, float]

Compute the coordinate of the upper-right corner of a bounding box, in northing/easting format.

property ll: tuple[float, float]

Compute the coordinate of the lower-left corner of a bounding box, in northing/easting format.

property lr: tuple[float, float]

Compute the coordinate of the lower-right corner of a bounding box, in northing/easting format.

property center: tuple[float, float]

Compute the center coordinate of a bounding box, in northing/easting format.

Returns:

center – The center of the bbox expressed in the same CRS.

Return type:

Coordinate

property area: float

Simple estimation of the area of the bounding box, expressed in the units of its CRS.

property hypotenuse: float

Length of the bounding box hypotenuse.

property is_empty: bool

Check if a bounding box has an empty interior.

property is_not_empty: bool

Check if a bounding box has a non empty interior.

with_(left: float | None = None, bottom: float | None = None, right: float | None = None, top: float | None = None) Self

Returns a modification of the bounding box with specified changes.

iou(other: Self) float

Computes the IoU (Intersection over Union) of two bounding boxes.

Parameters:

other (GeoBoundingBox) – An other bounding box, in the same CRS.

Returns:

The IoU, a value between 0 and 1.

Return type:

float

intersects(other: Self) bool

Check if a bounding box has non-empty intersection with another.

Parameters:

other (GeoBoundingBox) – An other bounding box, in the same CRS.

Returns:

True if the two bounding boxes have non empty intersection.

Return type:

bool

is_contained(other: Self) bool

Check if a bounding box is fully contained in another.

Parameters:

other (BoundingeBox) – An other bounding box, in the same CRS.

Returns:

True if the first bounding box is fully contained in the other.

Return type:

bool

buffer(buff: float) Self

Returns a bounding box increased by a given buffer in all directions.

Parameters:

buff (float)

Returns:

The buffered bounding box.

Return type:

GeoBoundingBox

unbuffer(buff: float) Self

Returns a bounding box decreased by a given buffer in all directions, that is, the same bounding box with its outer perimeter of given width removed.

Parameters:

buff (float)

Returns:

The unbuffered bounding box.

Return type:

GeoBoundingBox

to_ee_geometry() Geometry

Translate a bounding box as a ee.Geometry polygon.

Returns:

The polygon representing the bbox in Google Earth Engine, in the same CRS.

Return type:

ee.Geometry

to_shapely_polygon(in_native_crs: bool = False) Polygon

Translate a bounding box as a ee.Geometry polygon.

Parameters:

in_native_crs (bool) – Whether to use the bbox CRS (True) or WGS84 coordinates (False). Defaults to False.

Returns:

The shapely polygon representing the bbox coordinates in its CRS or WGS84 (depending on in_native_crs).

Return type:

shapely.Polygon

Warning

Georeferencement information is lost. The shapely polygon is just a mathematical object.

to_latlon() tuple[tuple[float, float], tuple[float, float]]

Convert a bounding box to a tuple of the form (lat_min, lon_min), (lat_max, lon_max), as expected by folium.

Returns:

(lat_min, lon_min), (lat_max, lon_max) – Coordinates of the bottom left and top right box corners, in that order.

Return type:

Coordinate, Coordinate

transform(dst_crs: CRS) Self

Transform a bounding box to dst_crs.

If mapping to the new CRS generates distortions, the smallest box encapsulating the corners of the distorted box is returned. This is in general the smallest encapsulating box of the distorted box.

Parameters:

dst (CRS) – Target CRS.

Returns:

bbox – Bounding box in dst_crs.

Return type:

GeoBoundingBox

shape(scale: int) tuple[int, int]

Compute the shape that would have an image at resolution scale fitting the bbox.

Parameters:

scale (int) – A pixel side-length, in meter.

Returns:

height, width

Return type:

int

classmethod from_latlon(cmin: tuple[float, float], cmax: tuple[float, float], crs: CRS = CRS.from_epsg(4326)) Self

Convert a bounding box of the form (lat_min, lon_min), (lat_max, lon_max), as expected by folium, to a GeoBoundingBox.

Parameters:
  • cmin (Coordinate) – Bottom left corner coordinates.

  • cmax (Coordinate) – Top right corner coordinates.

  • crs (CRS (optional)) – The CRS in which the coordinates are expressed. Default is WGS84.

Return type:

GeoBoundingBox

classmethod ee_image_bbox(image: Image) Self

Compute the bounding box of a GEE image in WGS84 CRS.

Parameters:

image (ee.Image) – A GEE image.

Return type:

GeoBoundingBox

classmethod from_rio(bbox: BoundingBox, crs: CRS = CRS.from_epsg(4326)) Self

Get a bounding box from a rasterio bounding box.

Parameters:
  • bbox (rio.coords.BoundingBox) – A rasterio bounding box object.

  • crs (CRS (optional)) – The CRS in which the bbox is expressed. Default is WGS84.

Return type:

GeoBoundingBox

classmethod from_geofile(path: Path) Self

Get a bounding box from a rasterio-compatible Geo file.

Parameters:

path (Path) – A path to a Geo file.

Returns:

The bounding box of the geodata contained in the file.

Return type:

GeoBoundingBox

classmethod from_utm(utm: UTM) Self

Get the bounding box of a UTM zone in WGS84.

Parameters:

utm (UTM) – The UTM zone.

Returns:

The bounding box expressed in WGS84.

Return type:

GeoBoundingBox

to_utms() Iterator[UTM]

Computes the UTM zones that the bounding box intersects.

class geobbox.UTM(zone: int, letter: str)

Bases: object

Class to represent a UTM zone.

zone

The zone number, between 1 and 60.

Type:

int

letter

The zone letter.

Type:

str

property crs: CRS

The CRS associated to the UTM zone.

property hemisphere: str

The hemisphere of the UTM region.

Returns:

“N” for northern hemisphere and “S” for southern.

Return type:

str

left() Self

Computes the UTM region left of the current region.

right() Self

Computes the UTM region right of the current region.

classmethod from_latlon(lat: float, lon: float) Self

Computes the UTM zone encapsulating the point at the given coordinate.

Parameters:
  • lat (float) – Point latitude, between 80°S and 84°N (i.e. -80 <= lat <= 84).

  • lon (float) – Point longitude, between 180°W and 180°E (i.e. -180 <= lon <= 180).

Return type:

Self

classmethod utm_hemisphere_from_crs(crs: CRS) str

Computes the UTM hemisphere defined by the given crs.

Parameters:

crs (CRS) – The Coordinate Reference System, expected to be EPSG:326** or EPSG:327**.

Returns:

The hemisphere of the given UTM CRS.

Return type:

str

classmethod utm_zone_from_crs(crs: CRS) int

Computes the UTM zone defined by the given crs.

Parameters:

crs (CRS) – The Coordinate Reference System, expected to be EPSG:326** or EPSG:327**.

Returns:

The zone number of the given CRS.

Return type:

int

classmethod is_utm_crs(crs: CRS) bool

Whether the given crs is a UTM local CRS.