|
1 | 1 | """Tools for downloading map tiles from coordinates."""
|
| 2 | + |
2 | 3 | from __future__ import absolute_import, division, print_function
|
3 | 4 |
|
4 | 5 | import uuid
|
|
7 | 8 | import requests
|
8 | 9 | import atexit
|
9 | 10 | import io
|
10 |
| -import os |
| 11 | +import time |
11 | 12 | import shutil
|
12 | 13 | import tempfile
|
13 | 14 | import warnings
|
@@ -140,8 +141,17 @@ def bounds2raster(
|
140 | 141 | w, s = _sm2ll(w, s)
|
141 | 142 | e, n = _sm2ll(e, n)
|
142 | 143 | # Download
|
143 |
| - Z, ext = bounds2img(w, s, e, n, zoom=zoom, source=source, ll=True, n_connections=n_connections, |
144 |
| - use_cache=use_cache) |
| 144 | + Z, ext = bounds2img( |
| 145 | + w, |
| 146 | + s, |
| 147 | + e, |
| 148 | + n, |
| 149 | + zoom=zoom, |
| 150 | + source=source, |
| 151 | + ll=True, |
| 152 | + n_connections=n_connections, |
| 153 | + use_cache=use_cache, |
| 154 | + ) |
145 | 155 |
|
146 | 156 | # Write
|
147 | 157 | # ---
|
@@ -171,7 +181,18 @@ def bounds2raster(
|
171 | 181 |
|
172 | 182 |
|
173 | 183 | def bounds2img(
|
174 |
| - w, s, e, n, zoom="auto", source=None, ll=False, wait=0, max_retries=2, n_connections=1, use_cache=True, zoom_adjust=None |
| 184 | + w, |
| 185 | + s, |
| 186 | + e, |
| 187 | + n, |
| 188 | + zoom="auto", |
| 189 | + source=None, |
| 190 | + ll=False, |
| 191 | + wait=0, |
| 192 | + max_retries=2, |
| 193 | + n_connections=1, |
| 194 | + use_cache=True, |
| 195 | + zoom_adjust=None, |
175 | 196 | ):
|
176 | 197 | """
|
177 | 198 | Take bounding box and zoom and return an image with all the tiles
|
@@ -251,16 +272,17 @@ def bounds2img(
|
251 | 272 | tile_urls = [provider.build_url(x=tile.x, y=tile.y, z=tile.z) for tile in tiles]
|
252 | 273 | # download tiles
|
253 | 274 | if n_connections < 1 or not isinstance(n_connections, int):
|
254 |
| - raise ValueError( |
255 |
| - f"n_connections must be a positive integer value." |
256 |
| - ) |
| 275 | + raise ValueError(f"n_connections must be a positive integer value.") |
257 | 276 | # Use threads for a single connection to avoid the overhead of spawning a process. Use processes for multiple
|
258 | 277 | # connections if caching is enabled, as threads lead to memory issues when used in combination with the joblib
|
259 | 278 | # memory caching (used for the _fetch_tile() function).
|
260 |
| - preferred_backend = "threads" if (n_connections == 1 or not use_cache) else "processes" |
| 279 | + preferred_backend = ( |
| 280 | + "threads" if (n_connections == 1 or not use_cache) else "processes" |
| 281 | + ) |
261 | 282 | fetch_tile_fn = memory.cache(_fetch_tile) if use_cache else _fetch_tile
|
262 | 283 | arrays = Parallel(n_jobs=n_connections, prefer=preferred_backend)(
|
263 |
| - delayed(fetch_tile_fn)(tile_url, wait, max_retries) for tile_url in tile_urls) |
| 284 | + delayed(fetch_tile_fn)(tile_url, wait, max_retries) for tile_url in tile_urls |
| 285 | + ) |
264 | 286 | # merge downloaded tiles
|
265 | 287 | merged, extent = _merge_tiles(tiles, arrays)
|
266 | 288 | # lon/lat extent --> Spheric Mercator
|
@@ -444,14 +466,13 @@ def _retryer(tile_url, wait, max_retries):
|
444 | 466 | )
|
445 | 467 | elif request.status_code == 104 or request.status_code == 200:
|
446 | 468 | if max_retries > 0:
|
447 |
| - os.wait(wait) |
| 469 | + time.sleep(wait) |
448 | 470 | max_retries -= 1
|
449 | 471 | request = _retryer(tile_url, wait, max_retries)
|
450 | 472 | else:
|
451 | 473 | raise requests.HTTPError("Connection reset by peer too many times.")
|
452 | 474 |
|
453 | 475 |
|
454 |
| - |
455 | 476 | def howmany(w, s, e, n, zoom, verbose=True, ll=False):
|
456 | 477 | """
|
457 | 478 | Number of tiles required for a given bounding box and a zoom level
|
|
0 commit comments