91 lines
3.4 KiB
Python
91 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Dict, Iterable, List, Sequence, Tuple
|
|
|
|
from . import config
|
|
from .errors import RequestError
|
|
from .i18n import tr
|
|
from .points import PRECISION, Point # noqa: F401
|
|
from .points import fmt as _fmt
|
|
|
|
|
|
def normalise_bbox(minx: float, miny: float, maxx: float,
|
|
maxy: float) -> Tuple[float, float, float, float]:
|
|
return (min(minx, maxx), min(miny, maxy), max(minx, maxx), max(miny, maxy))
|
|
|
|
|
|
def bbox_area(minx: float, miny: float, maxx: float, maxy: float) -> float:
|
|
minx, miny, maxx, maxy = normalise_bbox(minx, miny, maxx, maxy)
|
|
return abs(maxx - minx) * abs(maxy - miny)
|
|
|
|
|
|
def check_bbox(minx: float, miny: float, maxx: float, maxy: float) -> None:
|
|
minx, miny, maxx, maxy = normalise_bbox(minx, miny, maxx, maxy)
|
|
if maxx <= minx or maxy <= miny:
|
|
raise RequestError(tr("The selected area is empty. Zoom to the region you want to query."))
|
|
area = bbox_area(minx, miny, maxx, maxy)
|
|
if area > config.MAX_BBOX_AREA_DEGREES:
|
|
raise RequestError(
|
|
tr("The area is too large: {area} square degrees, the limit is {limit}. "
|
|
"Zoom in or draw a smaller rectangle.").format(
|
|
area="%.1f" % area, limit="%.0f" % config.MAX_BBOX_AREA_DEGREES))
|
|
|
|
|
|
def bbox_param(minx: float, miny: float, maxx: float, maxy: float) -> Dict[str, str]:
|
|
check_bbox(minx, miny, maxx, maxy)
|
|
minx, miny, maxx, maxy = normalise_bbox(minx, miny, maxx, maxy)
|
|
return {"bbox": ",".join(_fmt(v) for v in (minx, miny, maxx, maxy))}
|
|
|
|
|
|
def around_param(lon: float, lat: float, metres: float) -> Dict[str, str]:
|
|
radius = float(metres)
|
|
if radius <= 0:
|
|
raise RequestError(tr("The radius must be greater than zero."))
|
|
if radius > config.MAX_RADIUS_METRES:
|
|
raise RequestError(
|
|
tr("The radius is too large: {radius} m, the limit is {limit} m.").format(
|
|
radius="%.0f" % radius, limit="%.0f" % config.MAX_RADIUS_METRES))
|
|
return {"around": "%s,%s,%s" % (_fmt(lon), _fmt(lat), _fmt(radius))}
|
|
|
|
|
|
def thin(points: Sequence[Point], limit: int = config.MAX_POLYGON_POINTS) -> List[Point]:
|
|
pts = list(points)
|
|
if limit < 4:
|
|
raise ValueError("limit must be at least 4")
|
|
if len(pts) <= limit:
|
|
return pts
|
|
closed = len(pts) > 2 and pts[0] == pts[-1]
|
|
body = pts[:-1] if closed else pts
|
|
keep = limit - 1 if closed else limit
|
|
step = len(body) / float(keep)
|
|
out = [body[min(len(body) - 1, int(i * step))] for i in range(keep)]
|
|
if closed:
|
|
out.append(out[0])
|
|
return out
|
|
|
|
|
|
def polygon_param(points: Iterable[Point]) -> Dict[str, str]:
|
|
pts = [(float(x), float(y)) for x, y in points]
|
|
if len(pts) < 3:
|
|
raise RequestError(tr("A polygon needs at least three points."))
|
|
pts = thin(pts, config.MAX_POLYGON_POINTS)
|
|
flat = []
|
|
for x, y in pts:
|
|
flat.append(_fmt(x))
|
|
flat.append(_fmt(y))
|
|
return {"polygon": ",".join(flat)}
|
|
|
|
|
|
def describe(area: Dict[str, str]) -> str:
|
|
if "bbox" in area:
|
|
return tr("Rectangle {bbox}").format(bbox=area["bbox"])
|
|
if "around" in area:
|
|
parts = area["around"].split(",")
|
|
if len(parts) == 3:
|
|
return tr("{radius} m around {lon}, {lat}").format(
|
|
radius=parts[2], lon=parts[0], lat=parts[1])
|
|
if "polygon" in area:
|
|
count = len(area["polygon"].split(",")) // 2
|
|
return tr("Polygon with {count} vertices").format(count=count)
|
|
return tr("No area selected")
|