Skip to content

Commit

Permalink
infer OGC Tiles links /collections and /collections/{collectionId} re…
Browse files Browse the repository at this point in the history
…sponse
  • Loading branch information
vincentsarago committed Feb 25, 2025
1 parent 5d48cd7 commit 2d0d0bd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin
* renamed `tipg.collections.get_collection_index` to `tipg.collections.pg_get_collection_index` and change the function to use `DatabaseSettings` instance directly instead of keyword option
* update `tipg.collections.pg_get_collection_index` to return a list of PgCollection instead of a Catalog
* update `tipg.collections.register_collection_catalog` to pass `db_settings` to `pg_get_collection_index` function
* add `tilesets` and `viewer` links in `/collections` and `/collections/{collectionId}` response links

## [0.11.0] - TBD

Expand Down
11 changes: 11 additions & 0 deletions tests/routes/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def test_collections(app):
assert body["numberMatched"] >= collection_number
assert body["numberReturned"] >= collection_number

col = body["collections"][0]
link_titles = [link.get("title", "") for link in col["links"]]
assert "Collection TileSets" in link_titles
assert "Collection TileSet (Template URL)" in link_titles
assert "Collection Map viewer (Template URL)" in link_titles

ids = [x["id"] for x in body["collections"]]
assert "public.landsat_wrs" in ids
assert "public.my_data" in ids
Expand Down Expand Up @@ -167,6 +173,11 @@ def test_collections_landsat(app):
)
assert not body["extent"].get("temporal")

link_titles = [link.get("title", "") for link in body["links"]]
assert "Collection TileSets" in link_titles
assert "Collection TileSet (Template URL)" in link_titles
assert "Collection Map viewer (Template URL)" in link_titles

response = app.get("/collections/public.landsat_wrs?f=html")
assert response.status_code == 200
assert "text/html" in response.headers["content-type"]
Expand Down
65 changes: 64 additions & 1 deletion tipg/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from starlette.datastructures import QueryParams
from starlette.requests import Request
from starlette.responses import HTMLResponse, Response, StreamingResponse
from starlette.routing import compile_path, replace_params
from starlette.routing import NoMatchFound, compile_path, replace_params
from starlette.templating import Jinja2Templates, _TemplateResponse

tms_settings = TMSSettings()
Expand Down Expand Up @@ -416,6 +416,65 @@ def links(self, request: Request) -> List[model.Link]:
),
]

def _additional_collection_tiles_links(
self, request: Request, collection: Collection
) -> List[model.Link]:
links = []
base_url = str(request.base_url)
try:
links.append(
model.Link(
rel="data",
title="Collection TileSets",
type=MediaType.json,
href=str(
request.app.url_path_for(
"collection_tileset_list",
collectionId=collection.id,
).make_absolute_url(base_url=base_url)
),
),
)
links.append(
model.Link(
rel="data",
title="Collection TileSet (Template URL)",
type=MediaType.json,
templated=True,
href=str(
request.app.url_path_for(
"collection_tileset",
collectionId=collection.id,
tileMatrixSetId="{tileMatrixSetId}",
).make_absolute_url(base_url=base_url)
),
),
)
except NoMatchFound:
pass

try:
links.append(
model.Link(
title="Collection Map viewer (Template URL)",
href=str(
request.app.url_path_for(
"viewer_endpoint",
collectionId=collection.id,
tileMatrixSetId="{tileMatrixSetId}",
).make_absolute_url(base_url=base_url)
),
type=MediaType.html,
rel="data",
templated=True,
)
)

except NoMatchFound:
pass

return links

def register_routes(self):
"""Register OGC Features endpoints."""
self._collections_route()
Expand Down Expand Up @@ -530,6 +589,9 @@ def collections(
rel="queryables",
type=MediaType.schemajson,
),
*self._additional_collection_tiles_links(
request, collection
),
],
)
for collection in collection_list["collections"]
Expand Down Expand Up @@ -620,6 +682,7 @@ def collection(
"rel": "queryables",
"type": "application/schema+json",
},
*self._additional_collection_tiles_links(request, collection),
],
}
)
Expand Down

0 comments on commit 2d0d0bd

Please sign in to comment.