gws.lib.bounds

Bounds utilities.

A gws.Bounds object is an extent together with its CRS. This package creates bounds from extents and OGC BBOX request parameters, transforms them between CRS, and combines, compares and buffers them. Functions that combine bounds in different CRS transform them to the CRS of the first argument. Extents without a CRS are handled by gws.lib.extent.

When bounds are created from external input, the axis order of the CRS is respected: for a CRS with the YX (lat/lon) axis order, the coordinates are swapped, unless the input is known to be in the XY order.

Example:

b = gws.lib.bounds.from_request_bbox('7,50,8,51', gws.lib.crs.WGS84, always_xy=True)
b = gws.lib.bounds.transform(b, gws.lib.crs.WEBMERCATOR)
b = gws.lib.bounds.buffer(b, 100)
wgs = gws.lib.bounds.wgs_extent(b)

Source code: gws.lib.bounds

Package Contents

gws.lib.bounds.buffer(b: gws.Bounds, buf_size: float) → gws.Bounds

Enlarge or shrink bounds by a buffer.

Parameters:
  • b – Bounds.

  • buf_size – Buffer size in CRS units. A positive buffer enlarges the bounds, a negative one shrinks them.

Returns:

New bounds, or b itself if the buffer is 0.

gws.lib.bounds.copy(b: gws.Bounds) → gws.Bounds

Copy bounds.

Parameters:

b – Bounds.

Returns:

New bounds with the same CRS and extent.

gws.lib.bounds.from_extent(extent: gws.Extent, crs: gws.Crs, always_xy=False) → gws.Bounds

Create bounds from an extent.

If the CRS has the YX axis order, the extent coordinates are swapped, unless always_xy is set.

Parameters:
  • extent – Extent.

  • crs – CRS of the extent.

  • always_xy – If True, coordinates are assumed to be in the XY (lon/lat) order.

Returns:

Bounds.

gws.lib.bounds.from_request_bbox(bbox: str, default_crs: gws.Crs = None, always_xy=False) → gws.Bounds | None

Create bounds from a KVP BBOX parameter.

See OGC 06-121r9, 10.2.3 Bounding box KVP encoding.

Parameters:
  • bbox – Four comma-separated coordinates, optionally followed by a CRS name.

  • default_crs – CRS to use if the parameter has no CRS.

  • always_xy – If True, coordinates are assumed to be in the XY (lon/lat) order.

Returns:

Bounds, or None if the parameter is empty or invalid, or there is no CRS.

gws.lib.bounds.intersect(b1: gws.Bounds, b2: gws.Bounds) → bool

Check if two bounds intersect.

Parameters:
  • b1 – First bounds.

  • b2 – Second bounds, transformed to the CRS of b1 for the check.

Returns:

True if the bounds intersect.

gws.lib.bounds.transform(b: gws.Bounds, crs_to: gws.Crs) → gws.Bounds

Transform bounds to a different CRS.

Parameters:
  • b – Bounds.

  • crs_to – Target CRS.

Returns:

Bounds in the target CRS, or b itself if it is already in that CRS.

gws.lib.bounds.union(bs: list[gws.Bounds]) → gws.Bounds

Create the smallest bounds that contain all given bounds.

Parameters:

bs – A non-empty list of bounds.

Returns:

Bounds in the CRS of the first element of bs.

gws.lib.bounds.wgs_extent(b: gws.Bounds, pad: bool = False) → gws.Extent | None

Transform bounds to a WGS84 extent.

Parameters:
  • b – Bounds.

  • pad – Enlarge the extent slightly before the transformation, so that features on the edges of a data-derived extent survive the round trip through WGS84.

Returns:

A WGS84 extent, or None if the result is invalid.