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

rename MultAssetReader and fix issue 229 #230

Merged
merged 1 commit into from
Aug 17, 2020
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ print(tile.shape)
> (1, 256, 256)
```

Note: STACReader is based on `rio_tiler.io.base.MultiAssetsReader` class.
Note: STACReader is based on `rio_tiler.io.base.MultiBaseReader` class.

## Working with multiple assets

Expand Down Expand Up @@ -444,16 +444,16 @@ data, mask = multi_part(assets, bbox, ...)
data, mask = multi_preview(assets, ...)
```

You can also use `rio_tiler.io.base.MultiAssetsReader` to build a custom asset reader:
You can also use `rio_tiler.io.base.MultiBaseReader` to build a custom asset reader:

```python
import attr
from rio_tiler.io.base import MultiAssetsReader
from rio_tiler.io.base import MultiBaseReader
from rio_tiler.io import COGReader, BaseReader


@attr.s
class CustomReader(MultiAssetsReader):
class CustomReader(MultiBaseReader):

directory: str = attr.ib() # required arg
reader: Type[BaseReader] = attr.ib(default=COGReader) # the default reader is COGReader
Expand Down
2 changes: 1 addition & 1 deletion rio_tiler/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""rio-tiler.io"""

from .base import BaseReader, MultiAssetsReader # noqa
from .base import BaseReader, MultiBaseReader # noqa
from .cogeo import COGReader # noqa
from .stac import STACReader # noqa
16 changes: 4 additions & 12 deletions rio_tiler/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class BaseReader(metaclass=abc.ABCMeta):
"""Rio-tiler.io BaseReader."""

bounds: Tuple[float, float, float, float] = attr.ib(init=False)
minzoom: int = attr.ib(init=False)
maxzoom: int = attr.ib(init=False)

@abc.abstractmethod
def __enter__(self):
Expand All @@ -30,16 +32,6 @@ def __exit__(self, exc_type, exc_value, traceback):
"""Support using with Context Managers."""
...

@property
def minzoom(self) -> int:
"""Dataset Min zoom."""
...

@property
def maxzoom(self) -> int:
"""Dataset Max zoom."""
...

@property
def center(self) -> Tuple[float, float, int]:
"""Dataset center + minzoom."""
Expand Down Expand Up @@ -108,8 +100,8 @@ def point(self, lon: float, lat: float, **kwargs: Any) -> List:


@attr.s
class MultiAssetsReader(metaclass=abc.ABCMeta):
"""MultiAssets Reader."""
class MultiBaseReader(BaseReader, metaclass=abc.ABCMeta):
"""MultiBaseReader Reader."""

reader: Type[BaseReader] = attr.ib()
reader_options: Dict = attr.ib(factory=dict)
Expand Down
40 changes: 12 additions & 28 deletions rio_tiler/io/cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ class COGReader(BaseReader):
dataset: Optional[
Union[DatasetReader, DatasetWriter, MemoryFile, WarpedVRT]
] = attr.ib(default=None)
_minzoom: Optional[int] = attr.ib(default=None)
_maxzoom: Optional[int] = attr.ib(default=None)
_colormap: Optional[Dict] = attr.ib(default=None)
minzoom: int = attr.ib(default=None)
maxzoom: int = attr.ib(default=None)
colormap: Dict = attr.ib(default=None)

# Define global options to be forwarded to functions reading the data (e.g rio_tiler.reader._read)
nodata: Optional[Union[float, int, str]] = attr.ib(default=None)
Expand All @@ -115,6 +115,11 @@ def __enter__(self):
self.bounds: Tuple[float, float, float, float] = transform_bounds(
self.dataset.crs, constants.WGS84_CRS, *self.dataset.bounds, densify_pts=21
)
if self.minzoom is None or self.maxzoom is None:
self._get_zooms()

if self.colormap is None:
self._get_colormap()

return self

Expand All @@ -126,39 +131,18 @@ def __exit__(self, exc_type, exc_value, traceback):
def _get_zooms(self):
"""Calculate raster min/max zoom level."""
minzoom, maxzoom = get_zooms(self.dataset)
self._minzoom = self._minzoom or minzoom
self._maxzoom = self._maxzoom or maxzoom
self.minzoom = self.minzoom or minzoom
self.maxzoom = self.maxzoom or maxzoom
return

def _get_colormap(self):
"""Retrieve the internal colormap."""
try:
self._colormap = self.dataset.colormap(1)
self.colormap = self.dataset.colormap(1)
except ValueError:
self._colormap = {}
self.colormap = {}
pass

@property
def colormap(self) -> Dict[int, Tuple[int, int, int, int]]:
"""COG internal Colormap."""
if self._colormap is None:
self._get_colormap()
return self._colormap

@property
def minzoom(self) -> int:
"""COG Min zoom."""
if self._minzoom is None:
self._get_zooms()
return self._minzoom

@property
def maxzoom(self) -> int:
"""COG Max zoom."""
if self._maxzoom is None:
self._get_zooms()
return self._maxzoom

def info(self) -> Dict:
"""Return COG info."""

Expand Down
4 changes: 2 additions & 2 deletions rio_tiler/io/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from ..errors import InvalidAssetName
from ..utils import aws_get_object
from .base import BaseReader, MultiAssetsReader
from .base import BaseReader, MultiBaseReader
from .cogeo import COGReader

DEFAULT_VALID_TYPE = {
Expand Down Expand Up @@ -74,7 +74,7 @@ def _get_assets(


@attr.s
class STACReader(MultiAssetsReader):
class STACReader(MultiBaseReader):
"""
STAC + Cloud Optimized GeoTIFF Reader.

Expand Down
24 changes: 22 additions & 2 deletions tests/test_io_cogeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,26 @@ def test_spatial_info_valid():
"""Should work as expected (get spatial info)"""
with COGReader(COG_NODATA) as cog:
meta = cog.spatial_info
assert meta.get("minzoom")
assert meta.get("maxzoom")
assert meta.get("minzoom") == 5
assert meta.get("maxzoom") == 8
assert meta.get("center")
assert len(meta.get("bounds")) == 4

with COGReader(COG_NODATA, minzoom=3) as cog:
meta = cog.spatial_info
assert meta.get("minzoom") == 3
assert meta.get("maxzoom") == 8

with COGReader(COG_NODATA, maxzoom=12) as cog:
meta = cog.spatial_info
assert meta.get("minzoom") == 5
assert meta.get("maxzoom") == 12

with COGReader(COG_NODATA, minzoom=3, maxzoom=12) as cog:
meta = cog.spatial_info
assert meta.get("minzoom") == 3
assert meta.get("maxzoom") == 12


def test_bounds_valid():
"""Should work as expected (get bounds)"""
Expand All @@ -52,6 +67,11 @@ def test_info_valid():
meta = cog.info()
assert meta.get("colormap")

with COGReader(COG_NODATA, colormap={1: [0, 0, 0, 0]}) as cog:
assert cog.colormap
meta = cog.info()
assert meta.get("colormap")

with COGReader(COG_TAGS) as cog:
meta = cog.info()
assert meta.get("bounds")
Expand Down