replaces long/lat with city name on UI
This commit is contained in:
30
app.py
30
app.py
@@ -137,6 +137,29 @@ def get_today_snowfall(lat, lon):
|
|||||||
print("Error fetching snowfall:", e)
|
print("Error fetching snowfall:", e)
|
||||||
return 0.0, False, "UTC"
|
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("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
return render_template_string(HTML)
|
return render_template_string(HTML)
|
||||||
@@ -147,13 +170,15 @@ def snowfall_api():
|
|||||||
lon = request.args.get("lon", type=float)
|
lon = request.args.get("lon", type=float)
|
||||||
|
|
||||||
if lat is None or lon is None:
|
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)
|
inches, snowing, tz_name = get_today_snowfall(lat, lon)
|
||||||
today = datetime.now(pytz.timezone(tz_name)).strftime("%B %d, %Y")
|
today = datetime.now(pytz.timezone(tz_name)).strftime("%B %d, %Y")
|
||||||
|
|
||||||
|
city_name = get_city_name(lat, lon)
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"location": f"{lat:.2f}, {lon:.2f}",
|
"location": city_name,
|
||||||
"lat": lat,
|
"lat": lat,
|
||||||
"lon": lon,
|
"lon": lon,
|
||||||
"inches": inches,
|
"inches": inches,
|
||||||
@@ -163,5 +188,6 @@ def snowfall_api():
|
|||||||
"updated": datetime.now().isoformat()
|
"updated": datetime.now().isoformat()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=5000)
|
app.run(host="0.0.0.0", port=5000)
|
||||||
|
|||||||
Reference in New Issue
Block a user