Skip to content

Commit

Permalink
add map endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Feb 8, 2024
1 parent 9121f84 commit 5e4f72c
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 147 deletions.
13 changes: 8 additions & 5 deletions titiler/cmr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,14 @@ def get_assets(
**kwargs: Any,
) -> List[Asset]:
"""Find assets."""
results = earthaccess.search_data(
bounding_box=(xmin, ymin, xmax, ymax),
count=limit,
**kwargs,
)
try:
results = earthaccess.search_data(
bounding_box=(xmin, ymin, xmax, ymax),
count=limit,
**kwargs,
)
except RuntimeError:
return []

assets: List[Asset] = []
for r in results:
Expand Down
82 changes: 70 additions & 12 deletions titiler/cmr/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from rio_tiler.io import Reader
from rio_tiler.types import RIOResampling, WarpResampling
from starlette.requests import Request
from starlette.responses import Response
from starlette.responses import HTMLResponse, Response
from starlette.routing import compile_path, replace_params
from starlette.templating import Jinja2Templates, _TemplateResponse
from typing_extensions import Annotated
Expand Down Expand Up @@ -142,6 +142,7 @@ def __post_init__(self):
self.register_conformance()
self.register_tilematrixsets()
self.register_tiles()
self.register_map()

def register_landing(self) -> None:
"""register landing page endpoint."""
Expand Down Expand Up @@ -577,20 +578,14 @@ def tiles_endpoint(
return Response(content, media_type=media_type)

@self.router.get(
"/collections/{collectionId}/{tileMatrixSetId}/tilejson.json",
"/{tileMatrixSetId}/tilejson.json",
response_model=TileJSON,
responses={200: {"description": "Return a tilejson"}},
response_model_exclude_none=True,
tags=["TileJSON"],
)
def tilejson_endpoint( # type: ignore
request: Request,
collectionId: Annotated[
str,
Path(
description="A CMR concept id, in the format <concept-type-prefix> <unique-number> '-' <provider-id>"
),
],
tileMatrixSetId: Annotated[
Literal[tuple(self.supported_tms.list())],
Path(description="Identifier for a supported TileMatrixSet"),
Expand Down Expand Up @@ -620,9 +615,9 @@ def tilejson_endpoint( # type: ignore
query=Depends(cmr_query),
###################################################################
backend: Annotated[
Literal["cog", "xarray"],
Literal["rasterio", "xarray"],
Query(description="Backend to read the CMR dataset"),
] = "cog",
] = "rasterio",
###################################################################
# ZarrReader Options
###################################################################
Expand Down Expand Up @@ -732,10 +727,10 @@ def tilejson_endpoint( # type: ignore

tms = self.supported_tms.get(tileMatrixSetId)

# TODO: can we get metadata from the collection?
# TODO: can we get metadata from the CMR dataset?
with CMRBackend(
auth=request.app.state.cmr_auth,
tms=tms,
auth=request.app.state.cmr_auth,
) as src_dst:
minx, miny, maxx, maxy = zip(
[-180, -90, 180, 90], list(src_dst.geographic_bounds)
Expand All @@ -748,3 +743,66 @@ def tilejson_endpoint( # type: ignore
"maxzoom": maxzoom if maxzoom is not None else src_dst.maxzoom,
"tiles": [tiles_url],
}

def register_map(self): # noqa: C901
"""Register map endpoints."""

@self.router.get(
"/{tileMatrixSetId}/map",
response_class=HTMLResponse,
responses={200: {"description": "Return a Map document"}},
tags=["Map"],
)
def map_endpoint( # type: ignore
request: Request,
tileMatrixSetId: Annotated[
Literal[tuple(self.supported_tms.list())],
Path(description="Identifier for a supported TileMatrixSet"),
],
minzoom: Annotated[
Optional[int],
Query(description="Overwrite default minzoom."),
] = None,
maxzoom: Annotated[
Optional[int],
Query(description="Overwrite default maxzoom."),
] = None,
) -> Dict:
"""Return Map document."""
tilejson_url = self.url_for(
request,
"tilejson_endpoint",
tileMatrixSetId=tileMatrixSetId,
)
if request.query_params._list:
tilejson_url += f"?{urlencode(request.query_params._list)}"

tms = self.supported_tms.get(tileMatrixSetId)

base_url = str(request.base_url).rstrip("/")
if self.router_prefix:
prefix = self.router_prefix.lstrip("/")
# If we have prefix with custom path param we check and replace them with
# the path params provided
if "{" in prefix:
_, path_format, param_convertors = compile_path(prefix)
prefix, _ = replace_params(
path_format, param_convertors, request.path_params.copy()
)
base_url += prefix

return self.templates.TemplateResponse(
name="map.html",
context={
"request": request,
"tilejson_endpoint": tilejson_url,
"tms": tms,
"resolutions": [matrix.cellSize for matrix in tms],
"template": {
"api_root": base_url,
"params": request.query_params,
"title": "Map",
},
},
media_type="text/html",
)
1 change: 0 additions & 1 deletion titiler/cmr/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<div class="dropdown-menu" aria-labelledby="account">
<a class="dropdown-item" href="{{ template.api_root }}/">Home</a>
<a class="dropdown-item" href="{{ template.api_root }}/conformance">Conformance</a>
<a class="dropdown-item" href="{{ template.api_root }}/collections">Collections</a>
</div>
</li>
</ul>
Expand Down
Loading

0 comments on commit 5e4f72c

Please sign in to comment.