[GIS] Bulk reverse geocoding with Geopy using built-in rate_limiter

geopynominatimpythonreverse-geocoding

I have a dataset with latitude and longitude coordinates, and I would like to get actual addresses for those. Since I'm using Geopy and Nominatim, I am limited to 1 request per second.

Although the examples I have seen to find a way around this involve using time.sleep(1) method, geopy has a RateLimiter class specifically for those purposes.

The only issue is that it doesn't seem to work on reverse geocoding for me, it keeps saying that the RateLimiter doesn't have an attribute reverse.

Am I doing something wrong?

from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter

geolocator = Nominatim(user_agent="application")

geocode = RateLimiter(geolocator, min_delay_seconds=1)

location = geocode.reverse((50.6539239, -120.3385242), language='en', exactly_one=True)

print location.raw

AttributeError: 'RateLimiter' object has no attribute 'reverse'

Best Answer

The correct usage of RateLimiter for reverse geocoding would be:

from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter

geolocator = Nominatim(user_agent="application")

reverse = RateLimiter(geolocator.reverse, min_delay_seconds=1)

location = reverse((50.6539239, -120.3385242), language='en', exactly_one=True)

print location.raw