Strassenverwaltung plus: Teilen von Strassenzügen
This commit is contained in:
1 parent
d54a986c14
commit
3eb3b746ab
22 files changed
+17391
-158
No files matched your search
@@ -0,0 +1,112 @@
|
||||
"""
|
||||
Hilfsfunktionen für Bounding Boxes und Koordinaten.
|
||||
"""
|
||||
import json
|
||||
import math
|
||||
|
||||
|
||||
def to_float(value):
|
||||
"""Wandelt einen Wert in eine endliche Fließkommazahl um."""
|
||||
try:
|
||||
number = float(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
return number if math.isfinite(number) else None
|
||||
|
||||
|
||||
def parse_bbox(raw):
|
||||
"""Liefert ``(lat_min, lat_max, lon_min, lon_max)`` oder ``None``.
|
||||
|
||||
Unterstützt das im Projekt verwendete Format
|
||||
``[[lon, lat], [lon, lat]]`` sowie ``[west, south, east, north]`` und
|
||||
Dict-Varianten mit ``south``/``west``/``north``/``east``.
|
||||
"""
|
||||
if raw is None or raw == '':
|
||||
return None
|
||||
|
||||
if isinstance(raw, str):
|
||||
try:
|
||||
value = json.loads(raw)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
else:
|
||||
value = raw
|
||||
|
||||
if isinstance(value, (list, tuple)):
|
||||
if (len(value) == 2
|
||||
and all(isinstance(item, (list, tuple)) and len(item) >= 2
|
||||
for item in value)):
|
||||
first_lon = to_float(value[0][0])
|
||||
first_lat = to_float(value[0][1])
|
||||
second_lon = to_float(value[1][0])
|
||||
second_lat = to_float(value[1][1])
|
||||
if None not in (first_lon, first_lat, second_lon, second_lat):
|
||||
return (
|
||||
min(first_lat, second_lat),
|
||||
max(first_lat, second_lat),
|
||||
min(first_lon, second_lon),
|
||||
max(first_lon, second_lon),
|
||||
)
|
||||
elif len(value) == 4:
|
||||
numbers = [to_float(item) for item in value]
|
||||
if all(number is not None for number in numbers):
|
||||
west, south, east, north = numbers
|
||||
return (
|
||||
min(south, north),
|
||||
max(south, north),
|
||||
min(west, east),
|
||||
max(west, east),
|
||||
)
|
||||
|
||||
if isinstance(value, dict):
|
||||
south = _first_number(value, 'south', 'minLat')
|
||||
north = _first_number(value, 'north', 'maxLat')
|
||||
west = _first_number(value, 'west', 'minLon')
|
||||
east = _first_number(value, 'east', 'maxLon')
|
||||
if None not in (south, north, west, east):
|
||||
return (
|
||||
min(south, north),
|
||||
max(south, north),
|
||||
min(west, east),
|
||||
max(west, east),
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def bbox_from_lonlat(points):
|
||||
"""Erzeugt den Bounding-Box-String aus ``(lon, lat)``-Paaren."""
|
||||
valid = [
|
||||
(lon, lat)
|
||||
for lon, lat in points
|
||||
if lon is not None and lat is not None
|
||||
]
|
||||
if not valid:
|
||||
return None
|
||||
|
||||
min_lon = min(point[0] for point in valid)
|
||||
max_lon = max(point[0] for point in valid)
|
||||
min_lat = min(point[1] for point in valid)
|
||||
max_lat = max(point[1] for point in valid)
|
||||
return json.dumps([
|
||||
[min_lon, max_lat],
|
||||
[max_lon, min_lat],
|
||||
])
|
||||
|
||||
|
||||
def bbox_center(raw):
|
||||
"""Mittelpunkt einer Bounding Box als ``(lat, lon)``."""
|
||||
bounds = parse_bbox(raw)
|
||||
if bounds is None:
|
||||
return None
|
||||
lat_min, lat_max, lon_min, lon_max = bounds
|
||||
return ((lat_min + lat_max) / 2, (lon_min + lon_max) / 2)
|
||||
|
||||
|
||||
def _first_number(mapping, *keys):
|
||||
for key in keys:
|
||||
if key in mapping:
|
||||
number = to_float(mapping[key])
|
||||
if number is not None:
|
||||
return number
|
||||
return None
|
||||
Reference in new issue
Block a user