I'm working on a personal project visualizing location data, and here I reverse geocode location data from Google through the Geocoding API, by feeding it coordinates and retrieving a City Name and Country.
It's a CSV file, with 2 columns: "Location" (Latitude and Longitude) and "Time" (Date+Time). There are 8533 rows.
Sample Data:
Location Time
--------------------------------------------------
| 41.2911084,2.0779035 | 4/15/2015 10:58 |
--------------------------------------------------
| 41.2885014,2.0725591 | 4/15/2015 10:07 |
--------------------------------------------------
| 41.3484125,2.1442487 | 4/15/2015 9:56 |
--------------------------------------------------
I'm having a problem with the API where I keep getting an error. Let me show the code, first.
# import necessary modules
import pandas as pd
import json, requests, logging
# configure logging for our tool
lfh = logging.FileHandler('reverseGeocoder.log')
lfh.setFormatter(logging.Formatter('%(levelname)s %(asctime)s %(message)s'))
log = logging.getLogger('reverseGeocoder')
log.setLevel(logging.INFO)
log.addHandler(lfh)
# load the gps coordinate data
df = pd.read_csv('LocationHistory.csv')
# create new columns
df['geocode_data'] = ''
df['city'] = ''
df['country'] = ''
df.head()
# function that handles the geocoding requests
def reverseGeocode(latlng):
result = {}
url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng={0}&key={1}'
apikey = 'API_KEY_GOES_HERE'
request = url.format(latlng, apikey)
log.info(request)
data = json.loads(requests.get(request).text)
log.info(data)
result = data['results'][0]['address_components']
return {
'city': result[3]['long_name'],
'country': result[6]['long_name']
}
# comment out the following line of code to geocode the entire dataframe
#df = df.head()
for i, row in df.iterrows():
# for each row in the dataframe, geocode the lat-long data
revGeocode = reverseGeocode(df['Location'][i])
df['geocode_data'][i] = revGeocode
df['city'] = revGeocode['city']
df['country'] = revGeocode['country']
# once every 100 loops print a counter
#if i % 100 == 0:
print i
df.head()
df.to_csv('LocationHistory2.csv', encoding='utf-8', index=False)
The error in question that I keep receiving:
Traceback (most recent call last):
File "D:\...\ReverseGeocoding.py", line 45, in <module>
revGeocode = reverseGeocode(df['Location'][i])
File "D:\...\ReverseGeocoding.py", line 37, in reverseGeocode
'country': result[6]['long_name']
IndexError: list index out of range
I think that part of the problem is that I need a check in place, in-case the API doesn't return anything for the locations. Why it wouldn't return anything, I have no idea.
I'm quite new to the world of APIs (and Python), but how could I get this code to a running state?