Bündel 2: Zentrale Helper für DRY-Cleanup, SW by-v1114
NEUE BACKEND-MODULE:
math_utils.py
- haversine_km(lat1, lon1, lat2, lon2) — Distanz in km
- haversine_m(...) — Convenience-Wrapper in Metern
- bbox_deg_from_km(lat, radius_km) — Bounding-Box-Approximation
für SQL-Vorfilter (statt Haversine im Python-Loop)
config.py
- DB_PATH, MEDIA_DIR, BREEDER_DOCS_DIR, SCANINPUT_DIR
- API_TIMEOUT_SHORT (5s) / DEFAULT (10s) / LONG (30s)
- HTTP_USER_AGENT, HTTP_HEADERS
errors.py
- not_found(msg), forbidden(msg), bad_request(msg), unauthorized(msg)
- conflict(msg), too_many_requests(msg, retry_after), service_unavailable(msg)
- require_or_404(row, msg) — Convenience-Helper
UI.JS ERWEITERUNGEN:
UI.time erweitert:
- formatDate(d) → "15.03.2026"
- formatDateTime(d) → "15.03.2026, 14:30"
- weekday(d) → "Di"
- parseISO(str) → {year, month, day}
UI.text (neu):
- truncate(str, maxLen, ellipsis='…')
- slug(str) — URL-Slug aus String (mit DE-Umlauten)
UI.money (neu):
- format(value) → "12,34 €" (de-DE, EUR)
- formatWithSuffix(value, '/Jahr')
HAVERSINE-MIGRATION (13 Backend-Routen):
alerts.py, services.py, places.py, events.py, diary.py, playdate.py,
lost.py, poison.py, adoption.py, gassi_zeiten.py, sitting.py, routen.py,
walks.py
- Alle lokalen def _haversine/haversine_km entfernt
- Aufrufe ersetzt durch haversine_km/haversine_m je nach Einheit
- from math_utils import haversine_km|haversine_m in jeder Datei
Tests 19/19 grün.
Hinweis: Migrationen für MEDIA_DIR (19 Stellen), API-Timeouts (12),
Date-Formatter im Frontend (24) und UI.text.truncate (5) sind als
Folge-Sprints möglich. Helper sind verfügbar.
This commit is contained in:
parent
c517c9281d
commit
297bd22f96
22 changed files with 225 additions and 202 deletions
37
backend/math_utils.py
Normal file
37
backend/math_utils.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
"""Mathematische Helper-Funktionen — zentral statt 13× dupliziert."""
|
||||
import math
|
||||
|
||||
|
||||
# Erdradius in Kilometern
|
||||
EARTH_RADIUS_KM = 6371.0
|
||||
|
||||
|
||||
def haversine_km(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
|
||||
"""Distanz zwischen zwei GPS-Koordinaten in km (Haversine-Formel).
|
||||
|
||||
Funktioniert für beliebige Punkte auf der Erde. Genauigkeit reicht
|
||||
für App-Zwecke (Umkreissuche etc.).
|
||||
"""
|
||||
lat1_rad = math.radians(lat1)
|
||||
lat2_rad = math.radians(lat2)
|
||||
dlat = math.radians(lat2 - lat1)
|
||||
dlon = math.radians(lon2 - lon1)
|
||||
a = (math.sin(dlat / 2) ** 2
|
||||
+ math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dlon / 2) ** 2)
|
||||
return 2 * EARTH_RADIUS_KM * math.asin(math.sqrt(a))
|
||||
|
||||
|
||||
def haversine_m(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
|
||||
"""Distanz in Metern (Convenience-Wrapper)."""
|
||||
return haversine_km(lat1, lon1, lat2, lon2) * 1000.0
|
||||
|
||||
|
||||
def bbox_deg_from_km(lat: float, radius_km: float):
|
||||
"""Bounding-Box-Approximation in Grad für radius_km um (lat, lon).
|
||||
|
||||
Returns (lat_delta, lon_delta) — beide in Grad.
|
||||
Verwendung: WHERE lat BETWEEN ?-lat_delta AND ?+lat_delta etc.
|
||||
"""
|
||||
lat_delta = radius_km / 111.0
|
||||
lon_delta = radius_km / (111.0 * max(abs(math.cos(math.radians(lat))), 0.01))
|
||||
return lat_delta, lon_delta
|
||||
Loading…
Add table
Add a link
Reference in a new issue