Skip to content

Commit

Permalink
Merge pull request #51 from stac-utils/useMosaicMetadata
Browse files Browse the repository at this point in the history
Insert mosaic metadata `min/max zoom` and `bounds` in tilejson
  • Loading branch information
vincentsarago authored Apr 11, 2022
2 parents 0dbe220 + d2ba837 commit 1c5bd82
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## Unreleased

* Insert mosaic metadata `min/max zoom` and `bounds` in tilejson

## 0.1.0.a7 (2022-04-05) Pre-Release

* add `feature()` method to `PGSTACBackend` mosaic backend
Expand Down
6 changes: 6 additions & 0 deletions tests/test_mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ async def test_query_with_metadata(app):
"maxzoom": 2,
}

response = await app.get(f"/mosaic/{cql2_id}/tilejson.json?assets=cog")
assert response.status_code == 200
resp = response.json()
assert resp["minzoom"] == 1
assert resp["maxzoom"] == 2


@patch("rio_tiler.io.cogeo.rasterio")
@pytest.mark.asyncio
Expand Down
21 changes: 16 additions & 5 deletions titiler/pgstac/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
from dataclasses import dataclass, field
from typing import Callable, Dict, List, Optional, Tuple, Type, Union
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
from urllib.parse import urlencode

import rasterio
Expand Down Expand Up @@ -32,6 +32,11 @@
from starlette.responses import Response


def _first_value(values: List[Any], default: Any = None):
"""Return the first not None value."""
return next(filter(lambda x: x is not None, values), default)


@dataclass
class MosaicTilerFactory(BaseTilerFactory):
"""Custom MosaicTiler for PgSTAC Mosaic Backend."""
Expand Down Expand Up @@ -258,11 +263,17 @@ def tilejson(
if qs:
tiles_url += f"?{urlencode(qs)}"

minzoom = _first_value([minzoom, search_info.metadata.minzoom], tms.minzoom)
maxzoom = _first_value([maxzoom, search_info.metadata.maxzoom], tms.maxzoom)
bounds = _first_value(
[search_info.input_search.get("bbox"), search_info.metadata.bounds],
tms.bbox,
)
return {
"bounds": search_info.input_search.get("bbox", tms.bbox),
"minzoom": minzoom if minzoom is not None else tms.minzoom,
"maxzoom": maxzoom if maxzoom is not None else tms.maxzoom,
"name": search_info.id,
"bounds": bounds,
"minzoom": minzoom,
"maxzoom": maxzoom,
"name": search_info.metadata.name or search_info.id,
"tiles": [tiles_url],
}

Expand Down

0 comments on commit 1c5bd82

Please sign in to comment.