from __future__ import annotations from typing import Any, Dict, List, Mapping, Optional, Sequence from .i18n import later, tr HOUSE = "#002060" GREY = "#94a3b8" LEG_COLORS: Sequence[str] = ( "#1d4ed8", "#0891b2", "#15803d", "#ca8a04", "#ea580c", "#b91c1c", "#9333ea", "#be185d", "#0f766e", ) SIGHT_COLORS: Mapping[str, str] = { "clear": "#16a34a", "blocked": "#dc2626", } SLOPE_COLORS: Mapping[str, str] = { "gentle": "#16a34a", "moderate": "#eab308", "steep": "#dc2626", "unknown": GREY, } SLOPE_GENTLE = 3.0 SLOPE_MODERATE = 8.0 SLOPE_ORDER: Sequence[str] = ("gentle", "moderate", "steep", "unknown") SLOPE_LABELS: Mapping[str, str] = { "gentle": later("under {limit} %"), "moderate": later("{low} to {high} %"), "steep": later("over {limit} %"), "unknown": later("no elevation data"), } POINT_SIZE = 12.0 POINT_OUTLINE = 1.0 POINT_OPACITY = 0.7 LINE_WIDTH = 4.0 AREA_OUTLINE = 2.0 AREA_OPACITY = 0.5 def leg_color(index: Any) -> str: try: number = int(index) except (TypeError, ValueError): return HOUSE return LEG_COLORS[number % len(LEG_COLORS)] def slope_class(slope: Any) -> str: if slope is None or isinstance(slope, bool): return "unknown" try: value = abs(float(slope)) except (TypeError, ValueError): return "unknown" if value != value: return "unknown" if value < SLOPE_GENTLE: return "gentle" if value <= SLOPE_MODERATE: return "moderate" return "steep" def slope_color(slope: Any) -> str: return SLOPE_COLORS[slope_class(slope)] def sight_color(blocked: Any) -> str: return SIGHT_COLORS["blocked" if blocked else "clear"] def slope_label(name: str) -> str: pattern = tr(SLOPE_LABELS.get(name, "")) return pattern.format(limit=SLOPE_GENTLE if name == "gentle" else SLOPE_MODERATE, low=SLOPE_GENTLE, high=SLOPE_MODERATE) def _single(geometry: str, color: str) -> Dict[str, Any]: return {"kind": "single", "geometry": geometry, "color": color} def _categories(geometry: str, field: str, entries: Sequence[Mapping[str, Any]]) -> Optional[Dict[str, Any]]: if not entries: return None return {"kind": "categories", "geometry": geometry, "field": field, "entries": [dict(entry) for entry in entries]} def _distinct(properties: Sequence[Mapping[str, Any]], field: str, label_of, color_of) -> List[Dict[str, Any]]: out: List[Dict[str, Any]] = [] seen = set() for props in properties or []: if not isinstance(props, Mapping): continue value = props.get(field) if value is None: continue try: key = (type(value).__name__, value) if key in seen: continue seen.add(key) except TypeError: continue out.append({"value": value, "color": color_of(props) or HOUSE, "label": label_of(props)}) return out def _own_color(props: Mapping[str, Any]) -> Optional[str]: color = props.get("color") if isinstance(color, str) and color.strip(): return color.strip() return None def isochrone_plan(properties: Sequence[Mapping[str, Any]]) -> Optional[Dict[str, Any]]: return _categories("Polygon", "label", _distinct(properties, "label", lambda props: str(props.get("label") or ""), _own_color)) def route_leg_plan(properties: Sequence[Mapping[str, Any]]) -> Optional[Dict[str, Any]]: def label(props: Mapping[str, Any]) -> str: try: number = int(props.get("leg")) + 1 except (TypeError, ValueError): return tr("Leg") return tr("Leg {number}").format(number=number) return _categories("Line", "leg", _distinct(properties, "leg", label, _own_color)) def route_stop_plan(properties: Sequence[Mapping[str, Any]]) -> Optional[Dict[str, Any]]: def label(props: Mapping[str, Any]) -> str: role = props.get("role") try: number = int(props.get("stop")) + 1 except (TypeError, ValueError): return str(role or "") return "%d ยท %s" % (number, role) if role else str(number) return _categories("Point", "stop", _distinct(properties, "stop", label, _own_color)) def profile_segment_plan() -> Dict[str, Any]: return { "kind": "categories", "geometry": "Line", "field": "slope_class", "entries": [{"value": name, "color": SLOPE_COLORS[name], "label": slope_label(name)} for name in SLOPE_ORDER], } def profile_point_plan() -> Dict[str, Any]: return _single("Point", HOUSE) def sight_segment_plan() -> Dict[str, Any]: return { "kind": "categories", "geometry": "Line", "field": "visible", "entries": [ {"value": True, "color": SIGHT_COLORS["clear"], "label": tr("clear view")}, {"value": False, "color": SIGHT_COLORS["blocked"], "label": tr("blocked")}, ], } def sight_obstacle_plan() -> Dict[str, Any]: return _single("Point", SIGHT_COLORS["blocked"]) def elevation_plan() -> Dict[str, Any]: return _single("Point", HOUSE) def osm_plan(geometry: str) -> Dict[str, Any]: return _single(geometry, HOUSE) KEYS = ("isochrone", "route", "profile", "sight", "elevation", "osm") def plan(key: str, geometry: str, properties: Sequence[Mapping[str, Any]] = ()) -> Optional[Dict[str, Any]]: if key == "isochrone" and geometry == "Polygon": return isochrone_plan(properties) if key == "route": if geometry == "Line": return route_leg_plan(properties) if geometry == "Point": return route_stop_plan(properties) if key == "profile": if geometry == "Line": return profile_segment_plan() if geometry == "Point": return profile_point_plan() if key == "sight": if geometry == "Line": return sight_segment_plan() if geometry == "Point": return sight_obstacle_plan() if key == "elevation" and geometry == "Point": return elevation_plan() if key == "osm": return osm_plan(geometry) return None