Python: faster way to do Geocoding API

32 views Asked by At

It almost takes 1 mimute to convert 60 addresses to coordinates with my code. I want the result to contain address, latitude and longtitude in order. Is there other faster ways? Thanks

code:

import time
import requests
import json
import pandas as pd
import numpy as np
import string

addressList=['xxx','xx1'] #330,000 addresses in total

def get_latitude_longtitude(address, GOOGLE_PLACES_API_KEY): 
    url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address + '&key=' + GOOGLE_PLACES_API_KEY
    while True:
        res = requests.get(url)
        js = json.loads(res.text)

        if js['status'] != 'OVER_QUERY_LIMIT':   
            time.sleep(1) 
            break

    result = js['results'][0]['geometry']['location']
    lat = result['lat'] 
    lng = result['lng'] 

    return address, lat, lng

lst=[]
for address in addressList:
    GOOGLE_PLACES_API_KEY = 'kkkkkkk'
    res = get_latitude_longtitude(address,GOOGLE_PLACES_API_KEY)
    #print(res)
    address=res[0]
    lat=res[1]
    lng=res[2]
    lst.append(address)
    lst.append(lat)
    lst.append(lng)
1

There are 1 answers

0
miguev On

You can use Python multiprocessing (or similar) to run a fixed number of processes in parallel, then have each process send requests at a fixed rate (e.g. 1 request per second), and thus adjusting the number of processes you can adjust the overall request rate.

Google Maps Platform APIs have default usage limits, see https://developers.google.com/maps/faq#usage_apis

If you intend on going as fast as possible, see also the Best Practices Using Directions API Web Services and in particular the Polite Use of Google APIs section. TL;DR: if you try to go too fast you'll end up going slower (due to retries).