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

use rio-tiler native function for multiple assets point reading #168

Merged
merged 2 commits into from
Feb 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
* raise an error when trying to pass `mosaic_def` in read-only backend (https://github.com/developmentseed/cogeo-mosaic/pull/162)
* add `MemoryBackend` (https://github.com/developmentseed/cogeo-mosaic/pull/163)

**breaking**

* Updated the backends `.point()` methods to return a list in form of `[(asset1, values)]` (https://github.com/developmentseed/cogeo-mosaic/pull/168)

## 3.0.0b1 (2020-12-18)

* remove `overview` command (https://github.com/developmentseed/cogeo-mosaic/issues/71#issuecomment-748265645)
Expand Down
24 changes: 8 additions & 16 deletions cogeo_mosaic/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from cachetools import TTLCache, cached
from cachetools.keys import hashkey
from morecantile import TileMatrixSet
from rio_tiler.constants import MAX_THREADS, WEB_MERCATOR_TMS
from rio_tiler.constants import WEB_MERCATOR_TMS
from rio_tiler.errors import PointOutsideBounds
from rio_tiler.io import BaseReader, COGReader
from rio_tiler.models import ImageData
from rio_tiler.mosaic import mosaic_reader
from rio_tiler.tasks import create_tasks, filter_tasks
from rio_tiler.tasks import multi_values

from cogeo_mosaic.backends.utils import find_quadkeys, get_hash
from cogeo_mosaic.cache import cache_config
Expand Down Expand Up @@ -159,13 +159,8 @@ def _reader(asset: str, x: int, y: int, z: int, **kwargs: Any) -> ImageData:
return mosaic_reader(mosaic_assets, _reader, x, y, z, **kwargs)

def point(
self,
lon: float,
lat: float,
threads=MAX_THREADS,
reverse: bool = False,
**kwargs: Any,
) -> List[Dict]:
self, lon: float, lat: float, reverse: bool = False, **kwargs: Any,
) -> List:
"""Get Point value from multiple observation."""
mosaic_assets = self.assets_for_point(lon, lat)
if not mosaic_assets:
Expand All @@ -178,13 +173,10 @@ def _reader(asset: str, lon: float, lat: float, **kwargs) -> Dict:
with self.reader(asset, **self.reader_options) as src_dst:
return src_dst.point(lon, lat, **kwargs)

tasks = create_tasks(_reader, mosaic_assets, threads, lon, lat, **kwargs)
return [
{"asset": asset, "values": pt}
for pt, asset in filter_tasks(
tasks, allowed_exceptions=(PointOutsideBounds,)
)
]
if "allowed_exceptions" not in kwargs:
kwargs.update({"allowed_exceptions": (PointOutsideBounds,)})

return list(multi_values(mosaic_assets, _reader, lon, lat, **kwargs).items())

def info(self, quadkeys: bool = False) -> Info: # type: ignore
"""Mosaic info."""
Expand Down
10 changes: 7 additions & 3 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from click.testing import CliRunner
from pydantic import ValidationError
from requests.exceptions import HTTPError, RequestException
from rio_tiler.errors import PointOutsideBounds

from cogeo_mosaic.backends import MosaicBackend
from cogeo_mosaic.backends.dynamodb import DynamoDBBackend
Expand Down Expand Up @@ -670,16 +671,19 @@ def test_InMemoryReader():

pts = mosaic.point(-73, 45)
assert len(pts) == 2
assert pts[0]["asset"]
assert pts[1]["values"]
assert pts[0][0].endswith(".tif")
assert len(pts[0][1]) == 3

ptsR = mosaic.point(-73, 45, reverse=True)
assert len(ptsR) == 2
assert ptsR[0]["asset"] == pts[-1]["asset"]
assert ptsR[0][0] == pts[-1][0]

pts = mosaic.point(-72.5, 46)
assert len(pts) == 1

with pytest.raises(PointOutsideBounds):
mosaic.point(-72.5, 46, allowed_exceptions=None)

with pytest.raises(NoAssetFoundError):
mosaic.point(-60, 45)

Expand Down