Hi Everyone so I am trying to Geocode using the Mapquest API. I want to do reverse geocoding by giving the LAT/LONG CSV and process it through a Python Script using Mapquest API. I created the script below but when I get my output from the script I get no response. Can anyone tinker with my script so that I get the JSON out of it and I can parse the address data from the geocoding to another file. Thanks
Here is my input CSV sample Data
objectID lat lon
1 52.36732733 4.9491406
My Python Script
import pandas as pd
import json
import requests
df = pd.read_csv('/Users/albertgonzalobautista/Desktop/Testing_MPQ.csv')
# create new columns
df['geocode_data'] = ''
df['address']=''
# function that handles the geocoding requests
def reverseGeocode(latlng):
result = {}
url = 'http://www.mapquestapi.com/geocoding/v1/address?key={1}'
apikey = 'XXX'
request = url.format(latlng, apikey)
data = json.loads(requests.get(request).text)
if len(data['results']) > 0:
result = data['results'][0]
return result
for i, row in df.iterrows():
df['geocode_data'][i] = reverseGeocode(df['lat'][i].astype(str) + ',' + df['lon'][i].astype(str))
df.to_csv('test8.csv', encoding='utf-8', index=False)
The output of my script
objectID lat lon geocode_data
1 52.36732733 4.9491406 {'providedLocation': {}, 'locations': []}
Copyright Notice:Content Author:「Gonzalo68」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31929066/reverse-geocoding-using-mapquest-api-and-python