Skip to content

Commit

Permalink
remove asyncio callback (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
geospatial-jeff authored Jun 14, 2020
1 parent 21f66ad commit 9963224
Showing 1 changed file with 16 additions and 33 deletions.
49 changes: 16 additions & 33 deletions aiocogeo/partial_reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,28 +208,6 @@ def _init_array(self, img_tiles: TileMetadata) -> NpArrayType:
fused = np.ma.masked_array(fused)
return fused

@staticmethod
def _stitch_image_tile_callback(
fut: asyncio.Future,
fused_arr: NpArrayType,
idx: int,
idy: int,
tile_width: int,
tile_height: int,
) -> None:
"""Internal asyncio callback used to mosaic each image tile into a larger array (see ``_init_array``)"""
img_arr = fut.result()
fused_arr[
:,
idy * tile_height : (idy + 1) * tile_height,
idx * tile_width : (idx + 1) * tile_width,
] = img_arr
if np.ma.is_masked(img_arr):
fused_arr.mask[
:,
idy * tile_height : (idy + 1) * tile_height,
idx * tile_width : (idx + 1) * tile_width,
] = img_arr.mask

@staticmethod
def _stitch_image_tile(
Expand All @@ -253,24 +231,29 @@ def _stitch_image_tile(
idx * tile_width : (idx + 1) * tile_width,
] = arr.mask


async def _get_and_stitch_tile(
self,
xtile: int,
ytile: int,
idx: int,
idy: int,
img_tiles: TileMetadata,
fused_arr: NpArrayType
) -> None:
"""Asynchronously request an internal tile and stitch into an array"""
tile = await self.get_tile(xtile, ytile, img_tiles.ovr_level)
self._stitch_image_tile(tile, fused_arr, idx, idy, img_tiles.tile_width, img_tiles.tile_height)


async def _request_tiles(self, img_tiles: TileMetadata) -> NpArrayType:
"""Concurrently request the image tiles and mosaic into a larger array"""
img_arr = self._init_array(img_tiles)
tile_tasks = []
for idx, xtile in enumerate(range(img_tiles.xmin, img_tiles.xmax + 1)):
for idy, ytile in enumerate(range(img_tiles.ymin, img_tiles.ymax + 1)):
get_tile_task = asyncio.create_task(
self.get_tile(xtile, ytile, img_tiles.ovr_level)
)
get_tile_task.add_done_callback(
partial(
self._stitch_image_tile_callback,
fused_arr=img_arr,
idx=idx,
idy=idy,
tile_width=img_tiles.tile_width,
tile_height=img_tiles.tile_height,
)
self._get_and_stitch_tile(xtile, ytile, idx, idy, img_tiles, img_arr)
)
tile_tasks.append(get_tile_task)
await asyncio.gather(*tile_tasks)
Expand Down

0 comments on commit 9963224

Please sign in to comment.