1

I am trying to reverse geocode 500 lat and long random points using google API. I wrote the code below but I notice there are some errors that I need help with. I want to create a output CSV that has the Lat/Long and complete address of the reverse geocode and also the JSON geo_Data. Some of the errors is that my Ouput CSV is not being written and I don't know how to parse out just the address to my output CSV. Can anyone help me?

Script cited below:

import pandas as pd
import json
import requests 

df = pd.read_csv('/Users/albertgonzalobautista/Desktop/Amsterdam_RP500_2.csv')

# create new columns
df['geocode_data'] = ''
df['address']=''

# function that handles the geocoding requests

def reverseGeocode(latlng):
    result = {}
    apikey = 'XXX'
    url = f'https://maps.googleapis.com/maps/api/geocode/json?latlng={latlng}&key={apikey}'
    r = requests.get(url)
    r.raise_for_status()
    data = r.json()
    if data['results']:
        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))


for i, row in df.iterrows():
    if 'address_components' in row['geocode_data']:
        for component in row['geocode_data']['address_components']:  

df.to_csv('testingGEO233.csv', encoding='utf-8', index=False)

1 Answer 1

1

You don't have any code in this for loop

for component in row['geocode_data']['address_components']:

maybe try this

for component in row['geocode_data']['address_components']:
    df['address'] = row['geocode_data']['address_components']

You might actually want a string instead of a list of the components:

for component in row['geocode_data']['address_components']:
    df['address'] = row['geocode_data']['formatted_address']
Sign up to request clarification or add additional context in comments.

5 Comments

What about the CSV writer. Do you have any suggestions for that?
What's the error? When I run it (with my changes) it outputs to csv just fine. If you only want to output a single field, you can do something like df['address'].to_csv
Nevermind I got it working there where some minuscule errors when I was working on it. I am getting black fields though. Is that because I reached my limit or I am not doing it right?
It's tough to say without more information...but you can determine if you're over the limit by checking the status field. If you are, it will be set to OVER_QUERY_LIMIT Pretty sure if you verify your identity you can increase the limit to something ridiculous like 25k/day instead of the default, which I think is 1k/day.
So I am still getting black data when running 500 responses to be geocoded but when I run smaller number it allows me. How can I fix this problem? Is it because I am not delaying my responses?

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.