Crocolil commited on
Commit
532821c
·
verified ·
1 Parent(s): 73921fc

Upload folder using huggingface_hub

Browse files
utils/__pycache__/geo.cpython-313.pyc CHANGED
Binary files a/utils/__pycache__/geo.cpython-313.pyc and b/utils/__pycache__/geo.cpython-313.pyc differ
 
utils/geo.py CHANGED
@@ -1,21 +1,29 @@
1
- import requests
2
- def city_to_coordinates(city):
3
- url = "https://nominatim.openstreetmap.org/search"
4
-
5
- params = {
6
- "q": city,
7
- "format": "json",
8
- "limit": 1
9
- }
10
-
11
- headers = {
12
- "User-Agent": "PlantWise"
13
- }
14
-
15
- response = requests.get(url, params=params, headers=headers)
16
- data = response.json()
17
-
18
- if not data:
19
- return None
20
-
21
- return float(data[0]["lat"]), float(data[0]["lon"])
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ _geo_cache = {}
4
+
5
+ def city_to_coordinates(city):
6
+ """Convert a city name to (lat, lon) using Open-Meteo's geocoding API."""
7
+ if city in _geo_cache:
8
+ return _geo_cache[city]
9
+
10
+ url = "https://geocoding-api.open-meteo.com/v1/search"
11
+ params = {
12
+ "name": city.strip(),
13
+ "count": 1,
14
+ "format": "json"
15
+ }
16
+
17
+ try:
18
+ response = requests.get(url, params=params, timeout=10)
19
+ response.raise_for_status()
20
+ data = response.json()
21
+ results = data.get("results")
22
+ if not results:
23
+ return None
24
+ coords = (results[0]["latitude"], results[0]["longitude"])
25
+ _geo_cache[city] = coords
26
+ return coords
27
+ except (requests.RequestException, ValueError) as e:
28
+ print(f"city_to_coordinates failed for '{city}': {e}")
29
+ return None