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

return options from _get_reader() #728

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Unreleased

* Enable dynamic definition of Asset **reader** in `MultiBaseReader` (https://github.com/cogeotiff/rio-tiler/pull/711/)
* Enable dynamic definition of Asset **reader** in `MultiBaseReader` (https://github.com/cogeotiff/rio-tiler/pull/711/, https://github.com/cogeotiff/rio-tiler/pull/728)

# 6.7.0 (2024-09-05)

Expand Down
34 changes: 17 additions & 17 deletions rio_tiler/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ def _get_asset_info(self, asset: str) -> AssetInfo:
"""Validate asset name and construct url."""
...

def _get_reader(self, asset_info: AssetInfo) -> Type[BaseReader]:
"""Get Asset Reader."""
return self.reader
def _get_reader(self, asset_info: AssetInfo) -> Tuple[Type[BaseReader], Dict]:
"""Get Asset Reader and options."""
return self.reader, {}

def parse_expression(self, expression: str, asset_as_band: bool = False) -> Tuple:
"""Parse rio-tiler band math expression."""
Expand Down Expand Up @@ -347,13 +347,13 @@ def info(

def _reader(asset: str, **kwargs: Any) -> Dict:
asset_info = self._get_asset_info(asset)
reader = self._get_reader(asset_info)
reader, options = self._get_reader(asset_info)

with self.ctx(**asset_info.get("env", {})):
with reader(
asset_info["url"],
tms=self.tms,
**self.reader_options,
**{**self.reader_options, **options},
) as src:
return src.info()

Expand Down Expand Up @@ -390,13 +390,13 @@ def statistics(

def _reader(asset: str, *args: Any, **kwargs: Any) -> Dict:
asset_info = self._get_asset_info(asset)
reader = self._get_reader(asset_info)
reader, options = self._get_reader(asset_info)

with self.ctx(**asset_info.get("env", {})):
with reader(
asset_info["url"],
tms=self.tms,
**self.reader_options,
**{**self.reader_options, **options},
) as src:
return src.statistics(
*args,
Expand Down Expand Up @@ -514,13 +514,13 @@ def _reader(asset: str, *args: Any, **kwargs: Any) -> ImageData:
idx = asset_indexes.get(asset) or indexes

asset_info = self._get_asset_info(asset)
reader = self._get_reader(asset_info)
reader, options = self._get_reader(asset_info)

with self.ctx(**asset_info.get("env", {})):
with reader(
asset_info["url"],
tms=self.tms,
**self.reader_options,
**{**self.reader_options, **options},
) as src:
data = src.tile(*args, indexes=idx, **kwargs)

Expand Down Expand Up @@ -598,13 +598,13 @@ def _reader(asset: str, *args: Any, **kwargs: Any) -> ImageData:
idx = asset_indexes.get(asset) or indexes

asset_info = self._get_asset_info(asset)
reader = self._get_reader(asset_info)
reader, options = self._get_reader(asset_info)

with self.ctx(**asset_info.get("env", {})):
with reader(
asset_info["url"],
tms=self.tms,
**self.reader_options,
**{**self.reader_options, **options},
) as src:
data = src.part(*args, indexes=idx, **kwargs)

Expand Down Expand Up @@ -680,13 +680,13 @@ def _reader(asset: str, **kwargs: Any) -> ImageData:
idx = asset_indexes.get(asset) or indexes

asset_info = self._get_asset_info(asset)
reader = self._get_reader(asset_info)
reader, options = self._get_reader(asset_info)

with self.ctx(**asset_info.get("env", {})):
with reader(
asset_info["url"],
tms=self.tms,
**self.reader_options,
**{**self.reader_options, **options},
) as src:
data = src.preview(indexes=idx, **kwargs)

Expand Down Expand Up @@ -766,13 +766,13 @@ def _reader(asset: str, *args: Any, **kwargs: Any) -> PointData:
idx = asset_indexes.get(asset) or indexes

asset_info = self._get_asset_info(asset)
reader = self._get_reader(asset_info)
reader, options = self._get_reader(asset_info)

with self.ctx(**asset_info.get("env", {})):
with reader(
asset_info["url"],
tms=self.tms,
**self.reader_options,
**{**self.reader_options, **options},
) as src:
data = src.point(*args, indexes=idx, **kwargs)

Expand Down Expand Up @@ -844,13 +844,13 @@ def _reader(asset: str, *args: Any, **kwargs: Any) -> ImageData:
idx = asset_indexes.get(asset) or indexes

asset_info = self._get_asset_info(asset)
reader = self._get_reader(asset_info)
reader, options = self._get_reader(asset_info)

with self.ctx(**asset_info.get("env", {})):
with reader(
asset_info["url"],
tms=self.tms,
**self.reader_options,
**{**self.reader_options, **options},
) as src:
data = src.feature(*args, indexes=idx, **kwargs)

Expand Down
8 changes: 7 additions & 1 deletion rio_tiler/io/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import os
import warnings
from typing import Any, Dict, Iterator, Optional, Set, Type, Union
from typing import Any, Dict, Iterator, Optional, Set, Tuple, Type, Union
from urllib.parse import urlparse

import attr
Expand Down Expand Up @@ -275,6 +275,10 @@ def _minzoom(self):
def _maxzoom(self):
return self.tms.maxzoom

def _get_reader(self, asset_info: AssetInfo) -> Tuple[Type[BaseReader], Dict]:
"""Get Asset Reader."""
return self.reader, {}

def _get_asset_info(self, asset: str) -> AssetInfo:
"""Validate asset names and return asset's info.

Expand All @@ -300,9 +304,11 @@ def _get_asset_info(self, asset: str) -> AssetInfo:
if asset_info.media_type:
info["media_type"] = asset_info.media_type

# https://github.com/stac-extensions/file
if head := extras.get("file:header_size"):
info["env"] = {"GDAL_INGESTED_BYTES_AT_OPEN": head}

# https://github.com/stac-extensions/raster
if bands := extras.get("raster:bands"):
stats = [
(b["statistics"]["minimum"], b["statistics"]["maximum"])
Expand Down
4 changes: 2 additions & 2 deletions rio_tiler/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""rio-tiler types."""

from typing import Dict, Literal, Optional, Sequence, Tuple, TypedDict, Union
from typing import Any, Dict, Literal, Optional, Sequence, Tuple, TypedDict, Union

import numpy

Expand Down Expand Up @@ -58,7 +58,7 @@
class AssetInfo(TypedDict, total=False):
"""Asset Reader Options."""

url: str
url: Any
media_type: str
env: Optional[Dict]
metadata: Optional[Dict]
Expand Down
12 changes: 6 additions & 6 deletions tests/test_io_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json
import os
from typing import Set, Type
from typing import Dict, Set, Tuple, Type
from unittest.mock import patch

import attr
Expand Down Expand Up @@ -905,22 +905,22 @@ def test_get_reader():
class CustomSTACReader(STACReader):
include_asset_types: Set[str] = attr.ib(default=valid_types)

def _get_reader(self, asset_info: AssetInfo) -> Type[BaseReader]:
def _get_reader(self, asset_info: AssetInfo) -> Tuple[Type[BaseReader], Dict]:
"""Get Asset Reader."""
asset_type = asset_info.get("media_type", None)
if asset_type and asset_type in [
"application/x-netcdf",
]:
return XarrayReader
return XarrayReader, {}

return Reader
return Reader, {}

with CustomSTACReader(STAC_RASTER_PATH) as stac:
assert stac.assets == ["red", "green", "blue", "netcdf"]
info = stac._get_asset_info("netcdf")
assert info["media_type"] == "application/x-netcdf"
assert stac._get_reader(info) == XarrayReader
assert stac._get_reader(info) == (XarrayReader, {})

info = stac._get_asset_info("red")
assert info["media_type"] == "image/tiff; application=geotiff"
assert stac._get_reader(info) == Reader
assert stac._get_reader(info) == (Reader, {})
Loading