-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_geocode.py
More file actions
41 lines (35 loc) · 1.38 KB
/
reverse_geocode.py
File metadata and controls
41 lines (35 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from geopy.geocoders import Nominatim
from geopy.distance import geodesic
import sys
import json
def get_city(lat, lon):
geolocator = Nominatim(user_agent="AStar-GeoCoder")
try:
location = geolocator.reverse((lat, lon), exactly_one=True, language="en")
if location and 'city' in location.raw['address']:
return location.raw['address']['city']
elif location and 'town' in location.raw['address']:
return location.raw['address']['town']
elif location and 'village' in location.raw['address']:
return location.raw['address']['village']
else:
return find_closest_city(lat, lon)
except Exception as e:
return find_closest_city(lat, lon)
def find_closest_city(lat, lon):
# Load global cities from a dataset
with open("saudi_cities.json", "r") as file:
cities = json.load(file)
closest_city = None
min_distance = float("inf")
for city in cities:
city_coords = (city["latitude"], city["longitude"])
distance = geodesic((lat, lon), city_coords).kilometers
if distance < min_distance:
min_distance = distance
closest_city = city["name"]
return closest_city if closest_city else "Unknown"
if __name__ == "__main__":
latitude = float(sys.argv[1])
longitude = float(sys.argv[2])
print(get_city(latitude, longitude))