geopandas / contextily

Context geo-tiles in Python
https://contextily.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
493 stars 81 forks source link

Add timeout parameter to add_basemap #216

Open anitagraser opened 1 year ago

anitagraser commented 1 year ago

I recently discovered that in some setups (for me specifically: WSL in my company VPN), add_basemap gets stuck forever. It seems to fail to load the tiles and there is no way to specify a timeout.

This is my current workaround but it would be much more convenient to just be able to specify timeout=5 in add_basemap:

import signal

class TimeoutError(Exception):
    pass

def timeout_handler(signum, frame):
    raise TimeoutError("Timed out")

ax = gdf().to_crs(3857).plot()

# Set a timer for 5 seconds
try:  # SIGALRM is only available on UNIX
    signal.signal(signal.SIGALRM, timeout_handler)
    signal.alarm(5)
except AttributeError:
    pass 

try:  # add_basemap gets stuck on WSL when in VPN, therefore
    cx.add_basemap(ax)
except TimeoutError:
    print("Timed out while running add_basemap")

ax.figure.savefig('plot.png')
martinfleis commented 1 year ago

Hi, yes, it would make sense to expose a timeout keyword anywhere where we query some online resource.

I think that this could be a right place https://github.com/geopandas/contextily/blob/main/contextily/tile.py#L374 (but there may be more).

Contributions are welcome :).