From 22c6c010077a3bb6f9d32b4f7cc205fa2a6bfa08 Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 24 Jan 2026 17:09:47 -0500 Subject: [PATCH] replaces long/lat with city name on UI --- app.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 58ad6ff..3f4246a 100644 --- a/app.py +++ b/app.py @@ -136,6 +136,29 @@ def get_today_snowfall(lat, lon): except Exception as e: print("Error fetching snowfall:", e) return 0.0, False, "UTC" + +def get_city_name(lat, lon): + """Use OpenStreetMap Nominatim to get the city name for coordinates.""" + try: + r = requests.get( + "https://nominatim.openstreetmap.org/reverse", + params={ + "lat": lat, + "lon": lon, + "format": "json", + "zoom": 10, # city-level + "addressdetails": 1 + }, + headers={"User-Agent": "AreWeBuriedApp/1.0"} + ) + r.raise_for_status() + data = r.json() + address = data.get("address", {}) + city = address.get("city") or address.get("town") or address.get("village") or address.get("county") or f"{lat:.2f},{lon:.2f}" + return city + except Exception as e: + print("Reverse geocoding error:", e) + return f"{lat:.2f},{lon:.2f}" @app.route("/") def index(): @@ -147,13 +170,15 @@ def snowfall_api(): lon = request.args.get("lon", type=float) if lat is None or lon is None: - return jsonify({"error":"Missing lat/lon"}), 400 + return jsonify({"error": "Missing lat/lon"}), 400 inches, snowing, tz_name = get_today_snowfall(lat, lon) today = datetime.now(pytz.timezone(tz_name)).strftime("%B %d, %Y") + city_name = get_city_name(lat, lon) + return jsonify({ - "location": f"{lat:.2f}, {lon:.2f}", + "location": city_name, "lat": lat, "lon": lon, "inches": inches, @@ -163,5 +188,6 @@ def snowfall_api(): "updated": datetime.now().isoformat() }) + if __name__ == "__main__": app.run(host="0.0.0.0", port=5000)