14
14
15
15
import numpy as np
16
16
import rasterio as rio
17
- from PIL import Image
17
+ from PIL import Image , UnidentifiedImageError
18
18
from joblib import Memory as _Memory
19
19
from joblib import Parallel , delayed
20
20
from rasterio .transform import from_origin
@@ -288,11 +288,7 @@ def _process_source(source):
288
288
289
289
290
290
def _fetch_tile (tile_url , wait , max_retries ):
291
- request = _retryer (tile_url , wait , max_retries )
292
- with io .BytesIO (request .content ) as image_stream :
293
- image = Image .open (image_stream ).convert ("RGBA" )
294
- array = np .asarray (image )
295
- image .close ()
291
+ array = _retryer (tile_url , wait , max_retries )
296
292
return array
297
293
298
294
@@ -412,7 +408,7 @@ def _warper(img, transform, s_crs, t_crs, resampling):
412
408
413
409
def _retryer (tile_url , wait , max_retries ):
414
410
"""
415
- Retry a url many times in attempt to get a tile
411
+ Retry a url many times in attempt to get a tile and read the image
416
412
417
413
Arguments
418
414
---------
@@ -428,25 +424,32 @@ def _retryer(tile_url, wait, max_retries):
428
424
429
425
Returns
430
426
-------
431
- request object containing the web response.
427
+ array of the tile
432
428
"""
433
429
try :
434
430
request = requests .get (tile_url , headers = {"user-agent" : USER_AGENT })
435
431
request .raise_for_status ()
436
- except requests .HTTPError :
432
+ with io .BytesIO (request .content ) as image_stream :
433
+ image = Image .open (image_stream ).convert ("RGBA" )
434
+ array = np .asarray (image )
435
+ image .close ()
436
+
437
+ return array
438
+
439
+ except (requests .HTTPError , UnidentifiedImageError ):
437
440
if request .status_code == 404 :
438
441
raise requests .HTTPError (
439
442
"Tile URL resulted in a 404 error. "
440
443
"Double-check your tile url:\n {}" .format (tile_url )
441
444
)
442
- elif request .status_code == 104 :
445
+ elif request .status_code == 104 or request . status_code == 200 :
443
446
if max_retries > 0 :
444
447
os .wait (wait )
445
448
max_retries -= 1
446
449
request = _retryer (tile_url , wait , max_retries )
447
450
else :
448
451
raise requests .HTTPError ("Connection reset by peer too many times." )
449
- return request
452
+
450
453
451
454
452
455
def howmany (w , s , e , n , zoom , verbose = True , ll = False ):
0 commit comments