-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_locations.py
37 lines (28 loc) · 1.03 KB
/
get_locations.py
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
from geopy.geocoders import Nominatim
from geopy import distance
import pandas as pd
geolocator = Nominatim()
## read data
path = "/local/home/kloewer/dbx/Dropbox/CarbonFootprintEGU/data/"
df = pd.read_excel(path+"data_processed.xlsx")
n = len(df["City"])
cities = [df["City"].iloc[i] for i in range(n)]
countries = [df["Country"].iloc[i] for i in range(n)]
## get locations from openstreet map database
for i,(city,country) in enumerate(zip(cities,countries)):
print(i)
loc = geolocator.geocode(city+", "+country,timeout=30)
plt.pause(0.5)
try:
df.at[i,"Latitude"] = loc.latitude
df.at[i,"Longitude"] = loc.longitude
except:
print("ERROR in line "+str(i+2)+" with "+city+", "+country)
## calculate distances
vienna = geolocator.geocode("Vienna, Austria")
vienna_loc = (vienna.latitude,vienna.longitude)
for i in range(n):
loc = (df.at[i,"Latitude"],df.at[i,"Longitude"])
df.at[i,"Distance to Vienna"] = distance.distance(loc,vienna_loc).km
## export data
df.to_excel(path+"data_processed.xlsx")