Skip to content

Commit

Permalink
Raise error in _get_reader
Browse files Browse the repository at this point in the history
  • Loading branch information
abarciauskas-bgse committed Jun 4, 2024
1 parent 829c39c commit ff72c2c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 73 deletions.
4 changes: 2 additions & 2 deletions tests/test_asset_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def test_asset_info():
def test_get_reader_any():
"""Test reader is rio_tiler.io.Reader"""
asset_info = AssetInfo(url="https://file.tif")
empty_stac_reader = AssetReader({})
empty_stac_reader = AssetReader({'bbox': [], 'assets': []})
assert empty_stac_reader._get_reader(asset_info) == Reader


def test_get_reader_netcdf():
"""Test reader attribute is titiler.stacapi.XarrayReader"""
asset_info = AssetInfo(url="https://file.nc", type="application/netcdf")
empty_stac_reader = AssetReader({})
empty_stac_reader = AssetReader({'bbox': [], 'assets': []})
assert empty_stac_reader._get_reader(asset_info) == XarrayReader


Expand Down
37 changes: 12 additions & 25 deletions titiler/stacapi/asset_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import attr
import rasterio
from morecantile import TileMatrixSet
from rio_tiler.constants import WEB_MERCATOR_TMS
from rio_tiler.constants import WEB_MERCATOR_TMS, WGS84_CRS
from rio_tiler.errors import (
AssetAsBandError,
ExpressionMixingWarning,
Expand All @@ -22,7 +22,6 @@

from titiler.stacapi.models import AssetInfo
from titiler.stacapi.settings import CacheSettings, RetrySettings, STACSettings
from titiler.stacapi.xarray import XarrayReader

cache_config = CacheSettings()
retry_config = RetrySettings()
Expand All @@ -49,6 +48,7 @@ class AssetReader(MultiBaseReader):
Asset reader for STAC items.
"""

# bounds and assets are required
input: Any = attr.ib()
tms: TileMatrixSet = attr.ib(default=WEB_MERCATOR_TMS)
minzoom: int = attr.ib()
Expand All @@ -68,6 +68,12 @@ def _minzoom(self):
@maxzoom.default
def _maxzoom(self):
return self.tms.maxzoom

def __attrs_post_init__(self):
# MultibaseReader includes the spatial mixin so these attributes are required to assert that the tile exists inside the bounds of the item
self.crs = WGS84_CRS # Per specification STAC items are in WGS84
self.bounds = self.input["bbox"]
self.assets = list(self.input["assets"])

def _get_reader(self, asset_info: AssetInfo) -> Type[BaseReader]:
"""Get Asset Reader."""
Expand All @@ -80,7 +86,7 @@ def _get_reader(self, asset_info: AssetInfo) -> Type[BaseReader]:
"application/x-netcdf",
"application/netcdf",
]:
return XarrayReader
raise NotImplementedError("XarrayReader not yet implemented")

return Reader

Expand Down Expand Up @@ -111,6 +117,8 @@ def _get_asset_info(self, asset: str) -> AssetInfo:
if asset_info.get("type"):
info["type"] = asset_info["type"]

# there is a file STAC extension for which `header_size` is the size of the header in the file
# if this value is present, we want to use the GDAL_INGESTED_BYTES_AT_OPEN env variable to read that many bytes at file open.
if header_size := asset_info.get("file:header_size"):
info["env"]["GDAL_INGESTED_BYTES_AT_OPEN"] = header_size # type: ignore

Expand All @@ -132,7 +140,6 @@ def tile( # noqa: C901
tile_z: int,
assets: Union[Sequence[str], str] = (),
expression: Optional[str] = None,
asset_indexes: Optional[Dict[str, Indexes]] = None, # Indexes for each asset
asset_as_band: bool = False,
**kwargs: Any,
) -> ImageData:
Expand All @@ -144,7 +151,6 @@ def tile( # noqa: C901
tile_z (int): Tile's zoom level index.
assets (sequence of str or str, optional): assets to fetch info from.
expression (str, optional): rio-tiler expression for the asset list (e.g. asset1/asset2+asset3).
asset_indexes (dict, optional): Band indexes for each asset (e.g {"asset1": 1, "asset2": (1, 2,)}).
kwargs (optional): Options to forward to the `self.reader.tile` method.
Returns:
Expand Down Expand Up @@ -174,13 +180,7 @@ def tile( # noqa: C901
"assets must be passed either via `expression` or `assets` options."
)

asset_indexes = asset_indexes or {}

# We fall back to `indexes` if provided
indexes = kwargs.pop("indexes", None)

def _reader(asset: str, *args: Any, **kwargs: Any) -> ImageData:
idx = asset_indexes.get(asset) or indexes # type: ignore

asset_info = self._get_asset_info(asset)
reader = self._get_reader(asset_info)
Expand All @@ -189,20 +189,7 @@ def _reader(asset: str, *args: Any, **kwargs: Any) -> ImageData:
with reader(
asset_info["url"], tms=self.tms, **self.reader_options
) as src:
if type(src) == XarrayReader:
raise NotImplementedError("XarrayReader not yet implemented")
data = src.tile(*args, indexes=idx, **kwargs)

self._update_statistics(
data,
indexes=idx,
statistics=asset_info.get("dataset_statistics"),
)

metadata = data.metadata or {}
if m := asset_info.get("metadata"):
metadata.update(m)
data.metadata = {asset: metadata}
data = src.tile(*args, **kwargs)

if asset_as_band:
if len(data.band_names) > 1:
Expand Down
53 changes: 26 additions & 27 deletions titiler/stacapi/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,35 +195,34 @@ def tile(
scale = scale or 1

tms = self.supported_tms.get(tileMatrixSetId)
with rasterio.Env(**env):
with self.reader(
url=api_params["api_url"],
headers=api_params.get("headers", {}),
tms=tms,
reader_options={**reader_params},
**backend_params,
) as src_dst:
if MOSAIC_STRICT_ZOOM and (
z < src_dst.minzoom or z > src_dst.maxzoom
):
raise HTTPException(
400,
f"Invalid ZOOM level {z}. Should be between {src_dst.minzoom} and {src_dst.maxzoom}",
)

image, assets = src_dst.tile(
x,
y,
z,
search_query=search_query,
tilesize=scale * 256,
pixel_selection=pixel_selection,
threads=MOSAIC_THREADS,
**tile_params,
**layer_params,
**dataset_params,
with self.reader(
url=api_params["api_url"],
headers=api_params.get("headers", {}),
tms=tms,
reader_options={**reader_params},
**backend_params,
) as src_dst:
if MOSAIC_STRICT_ZOOM and (
z < src_dst.minzoom or z > src_dst.maxzoom
):
raise HTTPException(
400,
f"Invalid ZOOM level {z}. Should be between {src_dst.minzoom} and {src_dst.maxzoom}",
)

image, assets = src_dst.tile(
x,
y,
z,
search_query=search_query,
tilesize=scale * 256,
pixel_selection=pixel_selection,
threads=MOSAIC_THREADS,
**tile_params,
**layer_params,
**dataset_params,
)

if post_process:
image = post_process(image)

Expand Down
19 changes: 0 additions & 19 deletions titiler/stacapi/xarray.py

This file was deleted.

0 comments on commit ff72c2c

Please sign in to comment.