+ @staticmethod
+ def from_param(inp: Any) -> 'Point':
+ """ Create a point from an input parameter. The parameter
+ may be given as a point, a string or a sequence of
+ strings or floats. Raises a UsageError if the format is
+ not correct.
+ """
+ if isinstance(inp, Point):
+ return inp
+
+ seq: Sequence[str]
+ if isinstance(inp, str):
+ seq = inp.split(',')
+ elif isinstance(inp, abc.Sequence):
+ seq = inp
+
+ if len(seq) != 2:
+ raise UsageError('Point parameter needs 2 coordinates.')
+ try:
+ x, y = filter(math.isfinite, map(float, seq))
+ except ValueError as exc:
+ raise UsageError('Point parameter needs to be numbers.') from exc
+
+ if x < -180.0 or x > 180.0 or y < -90.0 or y > 90.0:
+ raise UsageError('Point coordinates invalid.')
+
+ return Point(x, y)
+
+
+ def to_wkt(self) -> str:
+ """ Return the WKT representation of the point.
+ """
+ return f'POINT({self.x} {self.y})'
+
+
+
+AnyPoint = Union[Point, Tuple[float, float]]
+
+WKB_BBOX_HEADER_LE = b'\x01\x03\x00\x00\x20\xE6\x10\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00'
+WKB_BBOX_HEADER_BE = b'\x00\x20\x00\x00\x03\x00\x00\x10\xe6\x00\x00\x00\x01\x00\x00\x00\x05'
+
+class Bbox:
+ """ A bounding box in WSG84 projection.
+
+ The coordinates are available as an array in the 'coord'
+ property in the order (minx, miny, maxx, maxy).
+ """
+ def __init__(self, minx: float, miny: float, maxx: float, maxy: float) -> None:
+ self.coords = (minx, miny, maxx, maxy)
+
+
+ @property
+ def minlat(self) -> float:
+ """ Southern-most latitude, corresponding to the minimum y coordinate.
+ """
+ return self.coords[1]
+
+
+ @property
+ def maxlat(self) -> float:
+ """ Northern-most latitude, corresponding to the maximum y coordinate.
+ """
+ return self.coords[3]
+
+
+ @property
+ def minlon(self) -> float:
+ """ Western-most longitude, corresponding to the minimum x coordinate.
+ """
+ return self.coords[0]
+
+
+ @property
+ def maxlon(self) -> float:
+ """ Eastern-most longitude, corresponding to the maximum x coordinate.
+ """
+ return self.coords[2]
+
+
+ @property
+ def area(self) -> float:
+ """ Return the area of the box in WGS84.
+ """
+ return (self.coords[2] - self.coords[0]) * (self.coords[3] - self.coords[1])
+
+
+ def contains(self, pt: Point) -> bool:
+ """ Check if the point is inside or on the boundary of the box.
+ """
+ return self.coords[0] <= pt[0] and self.coords[1] <= pt[1]\
+ and self.coords[2] >= pt[0] and self.coords[3] >= pt[1]
+
+
+ def to_wkt(self) -> str:
+ """ Return the WKT representation of the Bbox. This
+ is a simple polygon with four points.
+ """
+ return 'POLYGON(({0} {1},{0} {3},{2} {3},{2} {1},{0} {1}))'\
+ .format(*self.coords) # pylint: disable=consider-using-f-string
+
+
+ @staticmethod
+ def from_wkb(wkb: Union[None, str, bytes]) -> 'Optional[Bbox]':
+ """ Create a Bbox from a bounding box polygon as returned by
+ the database. Return s None if the input value is None.
+ """
+ if wkb is None:
+ return None
+
+ if isinstance(wkb, str):
+ wkb = unhexlify(wkb)
+
+ if len(wkb) != 97:
+ raise ValueError("WKB must be a bounding box polygon")
+ if wkb.startswith(WKB_BBOX_HEADER_LE):
+ x1, y1, _, _, x2, y2 = unpack('<dddddd', wkb[17:65])
+ elif wkb.startswith(WKB_BBOX_HEADER_BE):
+ x1, y1, _, _, x2, y2 = unpack('>dddddd', wkb[17:65])
+ else:
+ raise ValueError("WKB has wrong header")
+
+ return Bbox(min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))
+
+
+ @staticmethod
+ def from_point(pt: Point, buffer: float) -> 'Bbox':
+ """ Return a Bbox around the point with the buffer added to all sides.
+ """
+ return Bbox(pt[0] - buffer, pt[1] - buffer,
+ pt[0] + buffer, pt[1] + buffer)
+
+
+ @staticmethod
+ def from_param(inp: Any) -> 'Bbox':
+ """ Return a Bbox from an input parameter. The box may be
+ given as a Bbox, a string or a list or strings or integer.
+ Raises a UsageError if the format is incorrect.
+ """
+ if isinstance(inp, Bbox):
+ return inp
+
+ seq: Sequence[str]
+ if isinstance(inp, str):
+ seq = inp.split(',')
+ elif isinstance(inp, abc.Sequence):
+ seq = inp
+
+ if len(seq) != 4:
+ raise UsageError('Bounding box parameter needs 4 coordinates.')
+ try:
+ x1, y1, x2, y2 = filter(math.isfinite, map(float, seq))
+ except ValueError as exc:
+ raise UsageError('Bounding box parameter needs to be numbers.') from exc
+
+ if x1 < -180.0 or x1 > 180.0 or y1 < -90.0 or y1 > 90.0 \
+ or x2 < -180.0 or x2 > 180.0 or y2 < -90.0 or y2 > 90.0:
+ raise UsageError('Bounding box coordinates invalid.')
+
+ if x1 == x2 or y1 == y2:
+ raise UsageError('Bounding box with invalid parameters.')
+
+ return Bbox(min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))
+
+