Last active
February 2, 2020 05:20
-
-
Save Perishleaf/407322beffc18cc5ee325662455c9011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add coordinates for each area in the list for the latest table sheet | |
# As there are limit for free account, we only call coordinates for the latest table sheet | |
from opencage.geocoder import OpenCageGeocode | |
key = 'Your key number' # get api key from: https://opencagedata.com | |
geocoder = OpenCageGeocode(key) | |
list_lat = [] # create empty lists | |
list_long = [] | |
for index, row in dfs[keyList[0]].iterrows(): # iterate over rows in dataframe | |
City = row['Province/State'] | |
State = row['Country/Region'] | |
if City: | |
if City == 'Macau' or City == 'Hong Kong': | |
query = str(City)+','+'China' | |
results = geocoder.geocode(query) | |
lat = results[0]['geometry']['lat'] | |
long = results[0]['geometry']['lng'] | |
list_lat.append(lat) | |
list_long.append(long) | |
else: | |
query = str(City)+','+str(State) | |
results = geocoder.geocode(query) | |
lat = results[0]['geometry']['lat'] | |
long = results[0]['geometry']['lng'] | |
list_lat.append(lat) | |
list_long.append(long) | |
else: | |
query = str(State) | |
results = geocoder.geocode(query) | |
lat = results[0]['geometry']['lat'] | |
long = results[0]['geometry']['lng'] | |
list_lat.append(lat) | |
list_long.append(long) | |
# create new columns from lists | |
dfs[keyList[0]]['lat'] = list_lat | |
dfs[keyList[0]]['lon'] = list_long | |
dfs[keyList[0]].to_csv("{}_data.csv".format(keyList[0]), index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment