88import requests
99import numpy as np
1010
11- from retry import retry
11+ from google . api_core import retry
1212from PIL import Image
1313from pyproj import Transformer
1414from pygeotile .point import Point as PygeoPoint
2727logging .basicConfig (level = logging .INFO )
2828logger = logging .getLogger (__name__ )
2929
30- #TODO: need to add pyproj, pygeotile, retry to dependencies
31-
3230
3331class EPSG (Enum ):
3432 """ Provides the EPSG for tiled image assets that are currently supported.
@@ -147,14 +145,10 @@ def as_raster_data(self,
147145 if self .tile_bounds .epsg == EPSG .SIMPLEPIXEL :
148146 xstart , ystart , xend , yend = self ._get_simple_image_params (zoom )
149147
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.
153148 elif self .tile_bounds .epsg == EPSG .EPSG3857 :
154149 xstart , ystart , xend , yend = self ._get_3857_image_params (zoom )
155150 else :
156- raise ValueError (
157- f"Unsupported epsg found...{ self .tile_bounds .epsg } " )
151+ raise ValueError (f"Unsupported epsg found: { self .tile_bounds .epsg } " )
158152
159153 self ._validate_num_tiles (xstart , ystart , xend , yend , max_tiles )
160154
@@ -244,47 +238,33 @@ def _fetch_image_for_bounds(self,
244238
245239 If a tile cannot be fetched, a padding of expected tile size is instead added.
246240 """
247- tiles = {}
241+
248242 if multithread :
243+ tiles = {}
249244 with ThreadPoolExecutor (
250245 max_workers = TILE_DOWNLOAD_CONCURRENCY ) as exc :
251246 for x in range (x_tile_start , x_tile_end + 1 ):
252247 for y in range (y_tile_start , y_tile_end + 1 ):
253248 tiles [(x , y )] = exc .submit (self ._fetch_tile , x , y , zoom )
254249
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 = []
268253 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 ))
284264
285265 return np .vstack (rows )
286266
287- @retry ( delay = 1 , tries = 5 , backoff = 2 , max_delay = 8 )
267+ @retry . Retry ( initial = 1 , maximum = 16 , multiplier = 2 )
288268 def _fetch_tile (self , x : int , y : int , z : int ) -> np .ndarray :
289269 """
290270 Fetches the image and returns an np array.
@@ -293,9 +273,7 @@ def _fetch_tile(self, x: int, y: int, z: int) -> np.ndarray:
293273 data .raise_for_status ()
294274 decoded = np .array (Image .open (BytesIO (data .content )))[..., :3 ]
295275 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 } ." )
299277 return decoded
300278
301279 def _crop_to_bounds (
0 commit comments