refactor from disgusting monolith
This commit is contained in:
17
utils/geocode.py
Normal file
17
utils/geocode.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import requests
|
||||
|
||||
def get_city_name(lat, lon):
|
||||
try:
|
||||
r = requests.get(
|
||||
"https://nominatim.openstreetmap.org/reverse",
|
||||
params={"lat": lat, "lon": lon, "format": "json", "zoom": 10, "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}"
|
||||
Reference in New Issue
Block a user