8

How to avoid the GeocoderTimedOut('Service timed out') error in GeoPy using the OSM Nominatim geocoder? I am not interested in using an exception and skip some entries. I already use a time delay of 1.1 seconds in my script using time.sleep(1.1). Total database is 10,000 entries.

Nominatim Usage Policy

1
  • Eventually I used the script with a 1 second delay. It took a while but the results are good.
    – RutgerH
    Commented Dec 15, 2015 at 23:41

2 Answers 2

13

You could make a recursive function. Just typing Python-esque pseudo-code:

from geopy.exc import GeocoderTimedOut

def do_geocode(address, attempt=1, max_attempts=5):
    try:
        return geopy.geocode(address)
    except GeocoderTimedOut:
        if attempt <= max_attempts:
            return do_geocode(address, attempt=attempt+1)
        raise

Documentation for geopy.exc.GeocoderTimedOut.

This will keep retrying the do_geocode function until it manages to return without a Timeout exception being raised. You might want to limit the number of attempts, or also set a waiting period after a failed attempt. You might also want to think about why a timeout is occurring. Geopy allows you to set the length of time to wait before raising a timeout error, for any of the geocoding methods, with the timeout keyword argument. The documentation says that "some services are consistently slow", and recommends that this be increased.

3
  • thanks. this was indeed part of the solution. Make sure to import: from geopy.exc import GeocoderTimedOut
    – RutgerH
    Commented Mar 11, 2016 at 15:19
  • Hi, this did not work for me, I'm getting: HTTPSConnectionPool(host='nominatim.openstreetmap.org', port=443): Max retries exceeded with url: /reverse?lat=32.782023&lon=35.478867&format=json&accept-language=en&addressdetails=1 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001F7AA876240>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))
    – user88484
    Commented Sep 1, 2020 at 6:50
  • @user88484 You may have exceeded their rate limits. operations.osmfoundation.org/policies/nominatim Maximum of one request per second. Commented Sep 1, 2020 at 22:49
-1

Pass None to disable the timeout.

location = geolocator.geocode(county, timeout=None)

This solution will work in case of instability in the internet connection, or the remote server is slow to respond. Even if you put your device to sleep (of course not exiting your notebook.), it'll continue from the same spot where you left it.

In short, you're removing the limit of the number of requests by the API.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.