Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error (429) Too Many Requests while geocoding with geopy in Python

I have a Pandas dataframe with ~20k rows, and I am trying to geocode by address column into lat/long coordinates.

How do I use time.sleep() or maybe other function to stop OSM Nominatim from Too Many Requests 429 error that I am getting now?

Here's the code I use for this:

from geopy.geocoders import Nominatim
from geopy.distance import vincenty

geolocator = Nominatim()
df['coord'] = df['address'].apply(geolocator.geocode).apply(lambda x: (x.latitude, x.longitude))
df.head()

Thanks in advance!

like image 508
seizethedata Avatar asked Apr 03 '18 22:04

seizethedata


1 Answers

geopy since 1.16.0 includes a RateLimiter class which provides a convenient way to deal with the Too Many Requests 429 error by adding delays between the queries and retrying the failed requests.

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="specify_your_app_name_here")

from geopy.extra.rate_limiter import RateLimiter
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)

df['coord'] = df['address'].apply(geocode).apply(lambda location: (location.latitude, location.longitude))
df.head()

Docs: https://geopy.readthedocs.io/en/1.16.0/#usage-with-pandas

like image 89
KostyaEsmukov Avatar answered Oct 25 '22 08:10

KostyaEsmukov