Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept to allow iterable instead of sequence for mosaics. #651

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions iterable_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 5,
"id": "760550cf-4feb-44b4-9f7f-ce065fde279b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-93.01025390625037, 44.99588261816585, -92.98828125000037, 45.01141864227766]\n",
"https://planetarycomputer.microsoft.com/api/stac/v1/search?limit=10&bbox=-93.01025390625037%2C44.99588261816585%2C-92.98828125000037%2C45.01141864227766&collections=naip&fields=\n",
"4 4 4\n",
"4\n",
"CPU times: user 383 ms, sys: 7.44 ms, total: 391 ms\n",
"Wall time: 1.08 s\n"
]
}
],
"source": [
"%%time\n",
"from pystac_client import Client\n",
"from rio_tiler.mosaic import mosaic_reader\n",
"from rio_tiler.io import Reader\n",
"from rio_tiler.models import ImageData\n",
"import morecantile\n",
"\n",
"tms = morecantile.tms.get(\"WebMercatorQuad\")\n",
"x, y, z = tms.tile(-93,45,14)\n",
"bbox = list(tms.bounds(morecantile.Tile(x, y, z)))\n",
"print(bbox)\n",
"\n",
"def reader(asset: str, *args, **kwargs) -> ImageData:\n",
" with Reader(asset) as src:\n",
" return src.tile(*args, **kwargs)\n",
"\n",
"\n",
"catalog = Client.open('https://planetarycomputer.microsoft.com/api/stac/v1')\n",
"results = catalog.search(\n",
" limit=10,\n",
" max_items=100,\n",
" bbox=bbox,\n",
" collections=[\"naip\"],\n",
" fields={\"include\":[\"assets.image.href\"], \"exclude\":[\"links\"]}\n",
")\n",
"print(results.url_with_parameters())\n",
"items=results.items_as_dicts()\n",
"\n",
"assets = (item['assets']['image']['href'] for item in items)\n",
"\n",
"img, used = mosaic_reader(assets, reader, x, y, z, threads=4)\n",
"print(len(used))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python-3.10.12",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
10 changes: 6 additions & 4 deletions rio_tiler/mosaic/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import warnings
from inspect import isclass
from typing import Any, Callable, List, Optional, Sequence, Tuple, Type, Union, cast
from typing import Any, Callable, List, Optional, Sequence, Tuple, Type, Union, cast, Iterable

import numpy
from rasterio.crs import CRS
Expand All @@ -23,7 +23,7 @@


def mosaic_reader( # noqa: C901
mosaic_assets: Sequence,
mosaic_assets: Iterable,
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
reader: Callable[..., ImageData],
*args: Any,
pixel_selection: Union[Type[MosaicMethodBase], MosaicMethodBase] = FirstMethod,
Expand Down Expand Up @@ -76,15 +76,17 @@ def mosaic_reader( # noqa: C901
"'rio_tiler.mosaic.methods.base.MosaicMethodBase'"
)

if not chunk_size:
chunk_size = threads if threads > 1 else len(mosaic_assets)
# if not chunk_size:
# chunk_size = threads if threads > 1 else len(mosaic_assets)
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
chunk_size = threads

assets_used: List = []
crs: Optional[CRS]
bounds: Optional[BBox]
band_names: List[str]

for chunks in _chunks(mosaic_assets, chunk_size):
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
print(threads, len(chunks), chunk_size)
tasks = create_tasks(reader, chunks, threads, *args, **kwargs)
for img, asset in filter_tasks(
tasks,
Expand Down
9 changes: 5 additions & 4 deletions rio_tiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import warnings
from io import BytesIO
from typing import Any, Dict, Generator, List, Optional, Sequence, Tuple, Union
from typing import Any, Dict, Generator, List, Optional, Sequence, Tuple, Union, Iterable

import numpy
import rasterio
Expand All @@ -24,12 +24,13 @@
from rio_tiler.constants import WEB_MERCATOR_CRS
from rio_tiler.errors import RioTilerError
from rio_tiler.types import BBox, ColorMapType, IntervalTuple, RIOResampling
import itertools


def _chunks(my_list: Sequence, chuck_size: int) -> Generator[Sequence, None, None]:
def _chunks(my_list: Iterable, chuck_size: int) -> Generator[Sequence, None, None]:
"""Yield successive n-sized chunks from l."""
for i in range(0, len(my_list), chuck_size):
yield my_list[i : i + chuck_size]
while chunk:= tuple(itertools.islice(my_list, chuck_size)):
yield chunk


def get_array_statistics(
Expand Down