8
8
import requests
9
9
import numpy as np
10
10
11
- from retry import retry
11
+ from google . api_core import retry
12
12
from PIL import Image
13
13
from pyproj import Transformer
14
14
from pygeotile .point import Point as PygeoPoint
27
27
logging .basicConfig (level = logging .INFO )
28
28
logger = logging .getLogger (__name__ )
29
29
30
- #TODO: need to add pyproj, pygeotile, retry to dependencies
31
-
32
30
33
31
class EPSG (Enum ):
34
32
""" Provides the EPSG for tiled image assets that are currently supported.
@@ -147,14 +145,10 @@ def as_raster_data(self,
147
145
if self .tile_bounds .epsg == EPSG .SIMPLEPIXEL :
148
146
xstart , ystart , xend , yend = self ._get_simple_image_params (zoom )
149
147
150
- # Currently our editor doesn't support anything other than 3857.
151
- # Since the user provided projection is ignored by the editor
152
- # we will ignore it here and assume that the projection is 3857.
153
148
elif self .tile_bounds .epsg == EPSG .EPSG3857 :
154
149
xstart , ystart , xend , yend = self ._get_3857_image_params (zoom )
155
150
else :
156
- raise ValueError (
157
- f"Unsupported epsg found...{ self .tile_bounds .epsg } " )
151
+ raise ValueError (f"Unsupported epsg found: { self .tile_bounds .epsg } " )
158
152
159
153
self ._validate_num_tiles (xstart , ystart , xend , yend , max_tiles )
160
154
@@ -244,47 +238,33 @@ def _fetch_image_for_bounds(self,
244
238
245
239
If a tile cannot be fetched, a padding of expected tile size is instead added.
246
240
"""
247
- tiles = {}
241
+
248
242
if multithread :
243
+ tiles = {}
249
244
with ThreadPoolExecutor (
250
245
max_workers = TILE_DOWNLOAD_CONCURRENCY ) as exc :
251
246
for x in range (x_tile_start , x_tile_end + 1 ):
252
247
for y in range (y_tile_start , y_tile_end + 1 ):
253
248
tiles [(x , y )] = exc .submit (self ._fetch_tile , x , y , zoom )
254
249
255
- rows = []
256
- for y in range (y_tile_start , y_tile_end + 1 ):
257
- row = []
258
- for x in range (x_tile_start , x_tile_end + 1 ):
259
- try :
260
- row .append (tiles [(x , y )].result ())
261
- except :
262
- row .append (
263
- np .zeros (shape = (self .tile_size , self .tile_size , 3 ),
264
- dtype = np .uint8 ))
265
- rows .append (np .hstack (row ))
266
- #no multithreading
267
- else :
250
+ rows = []
251
+ for y in range (y_tile_start , y_tile_end + 1 ):
252
+ row = []
268
253
for x in range (x_tile_start , x_tile_end + 1 ):
269
- for y in range (y_tile_start , y_tile_end + 1 ):
270
- try :
271
- tiles [(x , y )] = self ._fetch_tile (x , y , zoom )
272
- except :
273
- tiles [(x , y )] = np .zeros (shape = (self .tile_size ,
274
- self .tile_size , 3 ),
275
- dtype = np .uint8 )
276
-
277
- rows = []
278
- for y in range (y_tile_start , y_tile_end + 1 ):
279
- rows .append (
280
- np .hstack ([
281
- tiles [(x , y )]
282
- for x in range (x_tile_start , x_tile_end + 1 )
283
- ]))
254
+ try :
255
+ if multithread :
256
+ row .append (tiles [(x , y )].result ())
257
+ else :
258
+ row .append (self ._fetch_tile (x , y , zoom ))
259
+ except :
260
+ row .append (
261
+ np .zeros (shape = (self .tile_size , self .tile_size , 3 ),
262
+ dtype = np .uint8 ))
263
+ rows .append (np .hstack (row ))
284
264
285
265
return np .vstack (rows )
286
266
287
- @retry ( delay = 1 , tries = 5 , backoff = 2 , max_delay = 8 )
267
+ @retry . Retry ( initial = 1 , maximum = 16 , multiplier = 2 )
288
268
def _fetch_tile (self , x : int , y : int , z : int ) -> np .ndarray :
289
269
"""
290
270
Fetches the image and returns an np array.
@@ -293,9 +273,7 @@ def _fetch_tile(self, x: int, y: int, z: int) -> np.ndarray:
293
273
data .raise_for_status ()
294
274
decoded = np .array (Image .open (BytesIO (data .content )))[..., :3 ]
295
275
if decoded .shape [:2 ] != (self .tile_size , self .tile_size ):
296
- logger .warning (
297
- f"Unexpected tile size { decoded .shape } . Results aren't guarenteed to be correct."
298
- )
276
+ logger .warning (f"Unexpected tile size { decoded .shape } ." )
299
277
return decoded
300
278
301
279
def _crop_to_bounds (
0 commit comments