@@ -21,8 +21,8 @@ def add_latlon(data):
2121 continue
2222 elif "location" in q :
2323 # If location is already present, add it to the cache
24- cache [q ["place" ]] = q ["location" ]
25- continue
24+ cache [q ["place" ]] = q ["location" ][ 0 ]
25+ # continue
2626 else :
2727 # Add to the copy if location is not present for speed
2828 data_copy .append ((i , q ))
@@ -36,30 +36,44 @@ def add_latlon(data):
3636 tqdm .write (f"IndexError: { q ['place' ]} " )
3737
3838 # Check if the location is already in the cache
39- if q ["place" ] in cache and cache [q ["place" ]] is not None :
40- data [i ]["location" ] = {
41- "latitude" : cache [q ["place" ]]["latitude" ],
42- "longitude" : cache [q ["place" ]]["longitude" ],
43- }
44-
45- else :
46- # Get the location from Openstreetmaps
47- url = "https://nominatim.openstreetmap.org/search" + "?format=json&q=" + urllib .parse .quote (q ["place" ])
48- response = requests .get (url )
39+ places = [q ["place" ]]
40+ if "extra_places" in q :
41+ places += q ["extra_places" ]
4942
50- if response :
51- try :
52- response = response .json ()
53- data [i ]["location" ] = {
54- "latitude" : float (response [0 ]["lat" ]),
55- "longitude" : float (response [0 ]["lon" ]),
43+ new_location = []
44+ for place in places :
45+ place = place .strip ()
46+ if place in cache and cache [place ] is not None :
47+ new_location += [
48+ {
49+ "latitude" : cache [place ]["latitude" ],
50+ "longitude" : cache [place ]["longitude" ],
5651 }
57- cache [q ["place" ]] = data [i ]["location" ]
58- except IndexError :
59- cache [q ["place" ]] = None
60- tqdm .write (f"No response from Openstreetmaps for { q ['place' ]} " )
61- time .sleep (2 )
52+ ]
53+
6254 else :
63- cache [q ["place" ]] = None
64- tqdm .write (f"No response from Openstreetmaps for { q ['place' ]} " )
55+ # Get the location from Openstreetmaps
56+ url = "https://nominatim.openstreetmap.org/search" + "?format=json&q=" + urllib .parse .quote (place )
57+ response = requests .get (url )
58+
59+ if response :
60+ try :
61+ response = response .json ()
62+ new_location += [
63+ {
64+ "latitude" : float (response [0 ]["lat" ]),
65+ "longitude" : float (response [0 ]["lon" ]),
66+ }
67+ ]
68+ cache [place ] = new_location [- 1 ]
69+ except IndexError :
70+ cache [place ] = None
71+ tqdm .write (f"No response from Openstreetmaps for { q ['place' ]} " )
72+ time .sleep (2 )
73+ else :
74+ cache [place ] = None
75+ tqdm .write (f"No response from Openstreetmaps for { q ['place' ]} " )
76+ else :
77+ if new_location :
78+ data [i ]["location" ] = new_location
6579 return data
0 commit comments