Skip to content

Commit

Permalink
update and test TileJSON model (developmentseed#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago authored Sep 1, 2021
1 parent 9774b88 commit 216e5b1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

## Next

### titiler.core

- Update the TileJSON model for better validation and to match with the specification (center is optional) (https://github.com/developmentseed/titiler/pull/363)

## 0.3.6 (2021-08-23)

### titiler.core
Expand Down
26 changes: 26 additions & 0 deletions src/titiler/core/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""test titiler.models."""

import pytest
from pydantic import ValidationError

from titiler.core.models.mapbox import TileJSON


def test_tilejson_model():
"""Make sure TileJSON model validates input and return default."""
tj = TileJSON(tiles=["https://something.xyz/{x}/{y}/{z}"])
assert tj.center == (0.0, 0.0, 0)
assert tj.bounds == [-180, -90, 180, 90]
assert tj.minzoom == 0
assert tj.maxzoom == 30
assert tj.scheme == "xyz"

tj = TileJSON(
tiles=["https://something.xyz/{x}/{y}/{z}"], center=(10, 10, 4), scheme="tms"
)
assert tj.center == (10.0, 10.0, 4)
assert tj.bounds == [-180, -90, 180, 90]
assert tj.scheme == "tms"

with pytest.raises(ValidationError):
TileJSON(tiles=["https://something.xyz/{x}/{y}/{z}"], scheme="abc")
4 changes: 0 additions & 4 deletions src/titiler/core/titiler/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,8 @@ def tilejson(

with rasterio.Env(**self.gdal_config):
with self.reader(src_path, tms=tms, **self.reader_options) as src_dst:
center = list(src_dst.center)
if minzoom:
center[-1] = minzoom
tjson = {
"bounds": src_dst.bounds,
"center": tuple(center),
"minzoom": minzoom if minzoom is not None else src_dst.minzoom,
"maxzoom": maxzoom if maxzoom is not None else src_dst.maxzoom,
"name": urlparse(src_path).path.lstrip("/") or "cogeotif",
Expand Down
31 changes: 28 additions & 3 deletions src/titiler/core/titiler/core/models/mapbox.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
"""Common response models."""

from enum import Enum
from typing import List, Optional, Tuple

from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, root_validator


class SchemeEnum(str, Enum):
"""TileJSON scheme choice."""

xyz = "xyz"
tms = "tms"


class TileJSON(BaseModel):
Expand All @@ -20,11 +28,28 @@ class TileJSON(BaseModel):
attribution: Optional[str]
template: Optional[str]
legend: Optional[str]
scheme: str = "xyz"
scheme: SchemeEnum = SchemeEnum.xyz
tiles: List[str]
grids: Optional[List[str]]
data: Optional[List[str]]
minzoom: int = Field(0, ge=0, le=30)
maxzoom: int = Field(30, ge=0, le=30)
bounds: List[float] = [-180, -90, 180, 90]
center: Tuple[float, float, int]
center: Optional[Tuple[float, float, int]]

@root_validator
def compute_center(cls, values):
"""Compute center if it does not exist."""
bounds = values["bounds"]
if not values.get("center"):
values["center"] = (
(bounds[0] + bounds[2]) / 2,
(bounds[1] + bounds[3]) / 2,
values["minzoom"],
)
return values

class Config:
"""TileJSON model configuration."""

use_enum_values = True
2 changes: 1 addition & 1 deletion src/titiler/mosaic/titiler/mosaic/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def tilejson(

with self.reader(src_path, **self.backend_options) as src_dst:
center = list(src_dst.center)
if minzoom:
if minzoom is not None:
center[-1] = minzoom
return {
"bounds": src_dst.bounds,
Expand Down

0 comments on commit 216e5b1

Please sign in to comment.