Skip to content

Commit

Permalink
update for rio-tiler 7.0 (#235)
Browse files Browse the repository at this point in the history
* update for rio-tiler 7.0

* fix tests
  • Loading branch information
vincentsarago authored Oct 22, 2024
1 parent 65b85cf commit 522f132
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 59 deletions.
33 changes: 33 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
## Unreleased

## 8.0.0 (2024-10-21)

* remove deprecated methods

* update morecantile requirement to `>=5.0,<7.0`

* update rio-tiler requirement to `>=7.0,<8.0`

* update `Info` model

```python
# before
class Info(BaseModel):
bounds: BBox = Field(default=(-180, -90, 180, 90))
center: Optional[Tuple[float, float, int]] = None
minzoom: int = Field(0, ge=0, le=30)
maxzoom: int = Field(30, ge=0, le=30)
name: Optional[str] = None
quadkeys: List[str] = []
tilematrixset: Optional[str] = None

# now
class Info(BaseModel):
bounds: BBox = Field(default=(-180, -90, 180, 90))
crs: str
center: Optional[Tuple[float, float, int]] = None
name: Optional[str] = None
quadkeys: List[str] = []
mosaic_tilematrixset: Optional[str] = None
mosaic_minzoom: int = Field(0, ge=0, le=30)
mosaic_maxzoom: int = Field(30, ge=0, le=30)
```

## 7.2.0 (2024-10-04)

* update BaseBackend to use a default coord_crs from the tms (author @AndrewAnnex, https://github.com/developmentseed/cogeo-mosaic/pull/234)
Expand Down
2 changes: 1 addition & 1 deletion cogeo_mosaic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Cogeo_mosaic."""

__version__ = "7.2.0"
__version__ = "8.0.0"
29 changes: 16 additions & 13 deletions cogeo_mosaic/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
from morecantile import Tile, TileMatrixSet
from rasterio.crs import CRS
from rasterio.warp import transform, transform_bounds
from rio_tiler.constants import WEB_MERCATOR_TMS, WGS84_CRS
from rio_tiler.constants import WEB_MERCATOR_TMS
from rio_tiler.errors import PointOutsideBounds
from rio_tiler.io import BaseReader, MultiBandReader, MultiBaseReader, Reader
from rio_tiler.models import ImageData, PointData
from rio_tiler.mosaic import mosaic_reader
from rio_tiler.tasks import multi_values
from rio_tiler.types import BBox
from rio_tiler.utils import CRS_to_uri

from cogeo_mosaic.backends.utils import get_hash
from cogeo_mosaic.cache import cache_config
Expand Down Expand Up @@ -63,11 +65,8 @@ class BaseBackend(BaseReader):
] = attr.ib(default=Reader)
reader_options: Dict = attr.ib(factory=dict)

bounds: Tuple[float, float, float, float] = attr.ib(
init=False, default=(-180, -90, 180, 90)
)
crs: CRS = attr.ib(init=False, default=WGS84_CRS)
geographic_crs: CRS = attr.ib(init=False, default=WGS84_CRS)
bounds: BBox = attr.ib(init=False)
crs: CRS = attr.ib(init=False)

_backend_name: str
_file_byte_size: Optional[int] = 0
Expand All @@ -81,10 +80,8 @@ def __attrs_post_init__(self):
mosaic_tms = self.mosaic_def.tilematrixset or WEB_MERCATOR_TMS

# By mosaic definition, its `bounds` is defined using the mosaic's TMS
# Geographic CRS so we define both `crs` and `geographic_crs` using the mosaic
# TMS geographic_crs.
# Geographic CRS.
self.crs = mosaic_tms.rasterio_geographic_crs
self.geographic_crs = mosaic_tms.rasterio_geographic_crs

# if we open the Mosaic with a TMS which is not the mosaic TMS
# the min/max zoom will default to the TMS (read) zooms
Expand Down Expand Up @@ -177,8 +174,10 @@ def assets_for_point(
) -> List[str]:
"""Retrieve assets for point."""
mosaic_tms = self.mosaic_def.tilematrixset or WEB_MERCATOR_TMS

# default coord_crs should be the TMS's geographic CRS
coord_crs = coord_crs or self.tms.rasterio_geographic_crs

# If coord_crs is not the same as the mosaic's geographic CRS
# we reproject the coordinates
if coord_crs != mosaic_tms.rasterio_geographic_crs:
Expand All @@ -202,8 +201,10 @@ def assets_for_bbox(
) -> List[str]:
"""Retrieve assets for bbox."""
mosaic_tms = self.mosaic_def.tilematrixset or WEB_MERCATOR_TMS

# default coord_crs should be the TMS's geographic CRS
coord_crs = coord_crs or self.tms.rasterio_geographic_crs

# If coord_crs is not the same as the mosaic's geographic CRS
# we reproject the bounding box
if coord_crs != mosaic_tms.rasterio_geographic_crs:
Expand Down Expand Up @@ -324,6 +325,7 @@ def point(
"""Get Point value from multiple observation."""
# default coord_crs should be the TMS's geographic CRS
coord_crs = coord_crs or self.tms.rasterio_geographic_crs

mosaic_assets = self.assets_for_point(lon, lat, coord_crs=coord_crs)
if not mosaic_assets:
raise NoAssetFoundError(f"No assets found for point ({lon},{lat})")
Expand All @@ -347,13 +349,14 @@ def _reader(
def info(self, quadkeys: bool = False) -> Info: # type: ignore
"""Mosaic info."""
return Info(
bounds=self.geographic_bounds,
bounds=self.bounds,
crs=CRS_to_uri(self.crs) or self.crs.to_wkt(),
center=self.center,
maxzoom=self.maxzoom,
minzoom=self.minzoom,
name=self.mosaic_def.name if self.mosaic_def.name else "mosaic",
quadkeys=[] if not quadkeys else self._quadkeys,
tilematrixset=repr(self.mosaic_def.tilematrixset or WEB_MERCATOR_TMS),
mosaic_tilematrixset=repr(self.mosaic_def.tilematrixset or WEB_MERCATOR_TMS),
mosaic_minzoom=self.mosaic_def.minzoom,
mosaic_maxzoom=self.mosaic_def.maxzoom,
)

@property
Expand Down
8 changes: 3 additions & 5 deletions cogeo_mosaic/backends/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 Dict, List, Optional, Sequence, Tuple, Type
from typing import Dict, List, Optional, Sequence, Type

import attr
import httpx
Expand All @@ -12,6 +12,7 @@
from rasterio.crs import CRS
from rio_tiler.constants import WEB_MERCATOR_TMS, WGS84_CRS
from rio_tiler.io import STACReader
from rio_tiler.types import BBox

from cogeo_mosaic.backends.base import BaseBackend
from cogeo_mosaic.cache import cache_config
Expand Down Expand Up @@ -66,11 +67,8 @@ class STACBackend(BaseBackend):
reader: Type[STACReader] = attr.ib(default=STACReader)
reader_options: Dict = attr.ib(factory=dict)

bounds: Tuple[float, float, float, float] = attr.ib(
init=False, default=(-180, -90, 180, 90)
)
bounds: BBox = attr.ib(init=False, default=(-180, -90, 180, 90))
crs: CRS = attr.ib(init=False, default=WGS84_CRS)
geographic_crs: CRS = attr.ib(init=False, default=WGS84_CRS)

# STAC API related options
# max_items | next_link_key | limit
Expand Down
2 changes: 1 addition & 1 deletion cogeo_mosaic/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CacheSettings(BaseSettings):
# TTL of the cache in seconds
ttl: int = 300

# Maximum size of the LRU cache in MB
# Maximum size of the LRU cache in Number of Items
maxsize: int = 512

# Whether or not caching is enabled
Expand Down
19 changes: 6 additions & 13 deletions cogeo_mosaic/models.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
"""cogeo-mosaic models."""

import warnings
from typing import List, Optional, Tuple

from pydantic import BaseModel, Field
from rio_tiler.types import BBox


class Info(BaseModel):
"""Mosaic info responses."""

bounds: Tuple[float, float, float, float] = Field((-180, -90, 180, 90))
bounds: BBox = Field(default=(-180, -90, 180, 90))
crs: str
center: Optional[Tuple[float, float, int]] = None
minzoom: int = Field(0, ge=0, le=30)
maxzoom: int = Field(30, ge=0, le=30)
name: Optional[str] = None
quadkeys: List[str] = []
tilematrixset: Optional[str] = None

def __getitem__(self, item):
"""Access item like in Dict."""
warnings.warn(
"'key' access will has been deprecated and will be removed in cogeo-mosaic 8.0.",
DeprecationWarning,
)
return self.__dict__[item]
mosaic_tilematrixset: Optional[str] = None
mosaic_minzoom: int = Field(0, ge=0, le=30)
mosaic_maxzoom: int = Field(30, ge=0, le=30)
3 changes: 2 additions & 1 deletion cogeo_mosaic/mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import click
import morecantile
from pydantic import BaseModel, Field, field_validator, model_validator
from rio_tiler.types import BBox
from shapely import linearrings, polygons, total_bounds
from shapely.strtree import STRtree
from supermorecado import burnTiles
Expand Down Expand Up @@ -71,7 +72,7 @@ class MosaicJSON(BaseModel, validate_assignment=True):
minzoom: int = Field(0, ge=0, le=30)
maxzoom: int = Field(30, ge=0, le=30)
quadkey_zoom: Optional[int] = None
bounds: Tuple[float, float, float, float] = Field(default=(-180, -90, 180, 90))
bounds: BBox = Field(default=(-180, -90, 180, 90))
center: Optional[Tuple[float, float, int]] = None
tiles: Dict[str, List[str]]
tilematrixset: Optional[morecantile.TileMatrixSet] = None
Expand Down
8 changes: 2 additions & 6 deletions cogeo_mosaic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ def get_dataset_info(
tms: morecantile.TileMatrixSet = WEB_MERCATOR_TMS,
) -> Dict:
"""Get rasterio dataset meta."""
with Reader(
src_path,
tms=tms,
geographic_crs=tms.rasterio_geographic_crs,
) as src:
bounds = src.geographic_bounds
with Reader(src_path, tms=tms) as src:
bounds = src.get_geographic_bounds(tms.rasterio_geographic_crs)
return {
"geometry": {
"type": "Polygon",
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ classifiers = [
dynamic = ["version", "readme"]
dependencies = [
"attrs",
"morecantile>=5.0,<6.0",
"morecantile",
"shapely>=2.0,<3.0",
"pydantic~=2.0",
"pydantic-settings~=2.0",
"httpx",
"rasterio",
"rio-tiler>=6.0,<7.0",
"rio-tiler>=7.0,<8.0",
"supermorecado",
"cachetools",
"numpy",
Expand Down Expand Up @@ -155,7 +155,7 @@ max-complexity = 14
"tests/*.py" = ["D1"]

[tool.bumpversion]
current_version = "7.2.0"
current_version = "8.0.0"
search = "{current_version}"
replace = "{new_version}"
regex = false
Expand Down
30 changes: 16 additions & 14 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,16 @@ def test_file_backend():

info = mosaic.info()
assert not info.quadkeys
with pytest.warns(DeprecationWarning):
assert not info["quadkeys"]

assert list(info.model_dump()) == [
"bounds",
"crs",
"center",
"minzoom",
"maxzoom",
"name",
"quadkeys",
"tilematrixset",
"mosaic_tilematrixset",
"mosaic_minzoom",
"mosaic_maxzoom",
]

info = mosaic.info(quadkeys=True)
Expand Down Expand Up @@ -652,12 +651,13 @@ def test_dynamoDB_backend(client):
assert not info.quadkeys
assert list(info.model_dump()) == [
"bounds",
"crs",
"center",
"minzoom",
"maxzoom",
"name",
"quadkeys",
"tilematrixset",
"mosaic_tilematrixset",
"mosaic_minzoom",
"mosaic_maxzoom",
]

info = mosaic.info(quadkeys=True)
Expand Down Expand Up @@ -998,12 +998,13 @@ def test_InMemoryReader():
info = mosaic.info()
assert list(info.model_dump()) == [
"bounds",
"crs",
"center",
"minzoom",
"maxzoom",
"name",
"quadkeys",
"tilematrixset",
"mosaic_tilematrixset",
"mosaic_minzoom",
"mosaic_maxzoom",
]

mosaic_oneasset = MosaicJSON.from_urls([asset1], quiet=True)
Expand Down Expand Up @@ -1032,12 +1033,13 @@ def test_sqlite_backend():
assert not info.quadkeys
assert list(info.model_dump()) == [
"bounds",
"crs",
"center",
"minzoom",
"maxzoom",
"name",
"quadkeys",
"tilematrixset",
"mosaic_tilematrixset",
"mosaic_minzoom",
"mosaic_maxzoom",
]

info = mosaic.info(quadkeys=True)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ def test_mars_mosaic_create():
MARS_MERCATOR,
extent_crs=MARS2000_SPHERE,
title="Web Mercator Mars",
geographic_crs=MARS2000_SPHERE,
)
# load the mars_ctx_stac_assset.json file
with open(mars_ctx_asset, "r") as f:
Expand All @@ -202,7 +201,6 @@ def test_mars_mosaic_create():
assert isinstance(mosaic, FileBackend)
assert mosaic.tms == mars_tms
assert mosaic.crs == mars_tms.rasterio_geographic_crs
assert mosaic.geographic_crs == mars_tms.rasterio_geographic_crs
assert len(mosaic.assets_for_point(77.28, 18, mars_tms.geographic_crs)) > 0
assert (
len(mosaic.assets_for_bbox(77.2, 17.5, 77.4, 18.5, mars_tms.geographic_crs))
Expand Down

0 comments on commit 522f132

Please sign in to comment.