Update services/geocoding.py
Browse files- services/geocoding.py +12 -7
services/geocoding.py
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
-
UA = {"User-Agent": "HF-Space-Trip-Planner/1.0(contact: example@example.com)"}
|
| 4 |
|
| 5 |
def geocode_city(city: str):
|
| 6 |
url = "https://nominatim.openstreetmap.org/search"
|
| 7 |
-
params = {"q": city, "format": "json", "limit":1}
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
+
UA = {"User-Agent": "HF-Space-Trip-Planner/1.0 (contact: example@example.com)"}
|
| 4 |
|
| 5 |
def geocode_city(city: str):
|
| 6 |
url = "https://nominatim.openstreetmap.org/search"
|
| 7 |
+
params = {"q": city, "format": "json", "limit": 1}
|
| 8 |
+
try:
|
| 9 |
+
r = requests.get(url, params=params, headers=UA, timeout=20)
|
| 10 |
+
r.raise_for_status()
|
| 11 |
+
data = r.json()
|
| 12 |
+
if not data:
|
| 13 |
+
return None
|
| 14 |
+
j = data[0]
|
| 15 |
+
return {"lat": float(j["lat"]), "lon": float(j["lon"])}
|
| 16 |
+
except Exception:
|
| 17 |
+
return None
|