0

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': []}
6
  • It looks like you are not passing the latlng variable into the request url. Commented Aug 10, 2015 at 21:06
  • In general, you should do a bit more work to narrow down where the error is occurring in your script. Commented Aug 10, 2015 at 21:06
  • Suggest looking at the documentation again: mapquestapi.com/geocoding/#reverse Commented Aug 10, 2015 at 21:09
  • Basically the error in my script is that I am not a getting a returned Address for my location as seen in the output of my script section. With that how do I pass the latlng variable though the request url? Commented Aug 10, 2015 at 21:09
  • What I mean is, your script is doing a lot of different things: * iterating over a dataframe * reading single values out of a dataframe * assigning single values to a dataframe * making an API call etc. If you can narrow down which of those is the problem, you can ask a better question. Commented Aug 10, 2015 at 21:10

1 Answer 1

1

The problem is in the following three lines in your function:

url = 'http://www.mapquestapi.com/geocoding/v1/address?key={1}'
apikey = 'XXX'
request = url.format(latlng, apikey)

The url that you are sending to the mapquest API does not include the latitude and longitude! You are just sending them your apikey.

If you take a look at their documentation, you can see what format the URL needs to be in: http://www.mapquestapi.com/geocoding/#reverse

Sign up to request clarification or add additional context in comments.

5 Comments

so I am trying to modify the URL to fit my code. Can you suggest on how I can do it?
I was hoping to use the JSON too so I can parse the information out if that is possible.
If you follow the link I posted, it gives an example URL (the section "Reverse Geocode Sample"). What is the difference between that URL and the one in your script?
How to add the code in folium?
@AnonymousUser I think that's a different enough topic that you should ask it as a separate question, it would require a lot more context about your situation to answer well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.