diff --git a/climate.py b/climate.py index c1266ab..5db0b81 100644 --- a/climate.py +++ b/climate.py @@ -488,17 +488,25 @@ def reverse_geocode(lat: float, lon: float) -> str | None: try: r = _request( "https://nominatim.openstreetmap.org/reverse", - {"lat": lat, "lon": lon, "format": "jsonv2", "zoom": 10, + # zoom 14 resolves to the suburb/neighbourhood level so we can lead + # with it when OSM has one (zoom 10 only ever returns the city). + {"lat": lat, "lon": lon, "format": "jsonv2", "zoom": 14, "addressdetails": 1}, 15, phase="reverse_geocode", headers={"User-Agent": "Thermograph/0.1 (local weather grading app)"}, ) a = r.json().get("address", {}) or {} + # Lead with the neighbourhood when available, then the city, then region. + neighborhood = (a.get("neighbourhood") or a.get("suburb") + or a.get("quarter") or a.get("city_district") + or a.get("borough")) city = (a.get("city") or a.get("town") or a.get("village") - or a.get("hamlet") or a.get("suburb") or a.get("county")) + or a.get("hamlet") or a.get("county")) region = a.get("state") or a.get("province") or a.get("region") - label = ", ".join(p for p in (city, region) if p) or None + # dict.fromkeys drops duplicates (e.g. neighbourhood == city) in order. + parts = dict.fromkeys(p for p in (neighborhood, city, region) if p) + label = ", ".join(parts) or None except Exception: # noqa: BLE001 - reverse geocoding is a nicety, never fatal label = None _REVGEO_CACHE[key] = label