Spaces:
Running
Running
Create geo_locate.py
Browse files- geo_locate.py +17 -0
geo_locate.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
from urllib.parse import quote
|
4 |
+
|
5 |
+
|
6 |
+
def get_geo_coords(address, access_token):
|
7 |
+
base_url = "https://us1.locationiq.com/v1/search"
|
8 |
+
headers= {"key: " + f"{access_token}", "Content-Type: application/json",}
|
9 |
+
url = f"{base_url}?key={access_token}&q={quote(address)}&format=json"
|
10 |
+
response = requests.get(url)
|
11 |
+
rjson = json.loads(response.content)
|
12 |
+
return (float(rjson[0]['lat']), float(rjson[0]['lon']))
|
13 |
+
|
14 |
+
if __name__ == "__main__":
|
15 |
+
address = 'Grytviken, South Georgia' #'Rio de Janerio, Argentina'
|
16 |
+
(lat, lon) = get_geo_coords(address) #'307 Pauly Drive, Englewood, Ohio, USA')
|
17 |
+
print(f'{address}: Lat = {float(lat):.2f}, Lon = {float(lon):.2f}')
|