replaces long/lat with city name on UI
This commit is contained in:
28
app.py
28
app.py
@@ -137,6 +137,29 @@ def get_today_snowfall(lat, lon):
|
||||
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():
|
||||
return render_template_string(HTML)
|
||||
@@ -152,8 +175,10 @@ def snowfall_api():
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user