GeoDonkey for QGIS 1.12

This commit is contained in:
GeoDonkey committed 2026-12-01 19:59:12 +01:00
commit 20ab32da30
63 files changed
+11157

No files matched your search

+140
View File
@@ -0,0 +1,140 @@
from __future__ import annotations
import math
from typing import Any, Iterable, Iterator, List, Sequence, Tuple
from . import config
from .errors import RequestError
from .i18n import tr
Point = Tuple[float, float]
PRECISION = 6
def fmt(value: float) -> str:
text = "%.*f" % (PRECISION, float(value))
if "." in text:
text = text.rstrip("0").rstrip(".")
return text or "0"
def _number(value: Any, label: str) -> float:
try:
number = float(value)
except (TypeError, ValueError):
raise RequestError(tr("“{value}” is not a number.").format(value=value))
if math.isnan(number) or math.isinf(number):
raise RequestError(tr("“{value}” is not a number.").format(value=label))
return number
def check(lon: float, lat: float) -> Point:
x = _number(lon, "lon")
y = _number(lat, "lat")
if not -180.0 <= x <= 180.0:
raise RequestError(
tr("Longitude {value} is outside the valid range of −180 to 180.").format(
value=fmt(x)))
if not -90.0 <= y <= 90.0:
raise RequestError(
tr("Latitude {value} is outside the valid range of −90 to 90.").format(
value=fmt(y)))
return (x, y)
def latlon(lon: float, lat: float) -> str:
x, y = check(lon, lat)
return "%s,%s" % (fmt(y), fmt(x))
def pretty(lon: float, lat: float, places: int = 5) -> str:
x, y = check(lon, lat)
return "%.*f° %s, %.*f° %s" % (
places, abs(y), "N" if y >= 0 else "S",
places, abs(x), "E" if x >= 0 else "W")
def series(points: Sequence[Point], limit: int = 0, label: str = "") -> str:
pairs = list(points)
if not pairs:
raise RequestError(tr("No points given."))
if limit and len(pairs) > limit:
raise RequestError(
tr("{count} points given, at most {limit} are allowed.").format(
count=len(pairs), limit=limit))
return ";".join(latlon(x, y) for x, y in pairs)
def lonlat(lon: float, lat: float) -> List[float]:
x, y = check(lon, lat)
return [round(x, PRECISION), round(y, PRECISION)]
GERMANY = config.ELEVATION_BBOX
def in_germany(lon: float, lat: float) -> bool:
west, south, east, north = GERMANY
try:
x, y = float(lon), float(lat)
except (TypeError, ValueError):
return False
if math.isnan(x) or math.isnan(y):
return False
return west <= x <= east and south <= y <= north
def germany_message() -> str:
west, south, east, north = GERMANY
return tr(
"Elevation data is available for Germany only "
"(longitude {west} to {east}, latitude {south} to {north})."
).format(west=fmt(west), east=fmt(east), south=fmt(south), north=fmt(north))
def check_germany(lon: float, lat: float) -> Point:
x, y = check(lon, lat)
if not in_germany(x, y):
raise RequestError(
"%s %s" % (germany_message(),
tr("The point {lon}, {lat} lies outside.").format(
lon=fmt(x), lat=fmt(y))))
return (x, y)
def split_germany(points: Iterable[Point]) -> Tuple[List[int], List[int]]:
inside: List[int] = []
outside: List[int] = []
for index, pair in enumerate(points):
try:
x, y = pair
except (TypeError, ValueError):
outside.append(index)
continue
(inside if in_germany(x, y) else outside).append(index)
return inside, outside
def along(start: Point, end: Point, fraction: float) -> Point:
x1, y1 = float(start[0]), float(start[1])
x2, y2 = float(end[0]), float(end[1])
t = max(0.0, min(1.0, float(fraction)))
return (x1 + (x2 - x1) * t, y1 + (y2 - y1) * t)
EARTH_RADIUS_METRES = 6371008.8
def distance(start: Point, end: Point) -> float:
lon1, lat1 = math.radians(float(start[0])), math.radians(float(start[1]))
lon2, lat2 = math.radians(float(end[0])), math.radians(float(end[1]))
a = (math.sin((lat2 - lat1) / 2.0) ** 2
+ math.cos(lat1) * math.cos(lat2) * math.sin((lon2 - lon1) / 2.0) ** 2)
return 2.0 * EARTH_RADIUS_METRES * math.asin(min(1.0, math.sqrt(a)))
def chunks(items: Sequence[Any], size: int) -> Iterator[List[Any]]:
step = max(1, int(size))
for start in range(0, len(items), step):
yield list(items[start:start + step])