-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08e64a0
commit e86229e
Showing
5 changed files
with
195 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# flake8: noqa | ||
|
||
import json | ||
from typing import Callable, List, Literal, Optional, Sequence, Union | ||
|
||
from fastapi import HTTPException, Query | ||
from rio_tiler.colormap import ColorMaps, parse_color | ||
from rio_tiler.types import ColorMapType | ||
from typing_extensions import Annotated | ||
|
||
|
||
# Port of titiler.core.dependencies.create_colormap_dependency (0.18.3) which | ||
# support case-sensitive keys in QueryParams and pydantic validation responses | ||
def create_colormap_dependency( | ||
cmap: ColorMaps, original_casing_keys: List[str] | ||
) -> Callable: | ||
"""Create Colormap Dependency.""" | ||
|
||
def deps( # type: ignore | ||
colormap_name: Annotated[ # type: ignore | ||
Literal[tuple(original_casing_keys)], | ||
Query(description="Colormap name"), | ||
] = None, | ||
colormap: Annotated[ | ||
Optional[str], Query(description="JSON encoded custom Colormap") | ||
] = None, | ||
) -> Union[ColorMapType, None]: | ||
if colormap_name: | ||
return cmap.get(colormap_name.lower()) | ||
|
||
if colormap: | ||
try: | ||
c = json.loads( | ||
colormap, | ||
object_hook=lambda x: { | ||
int(k): parse_color(v) for k, v in x.items() | ||
}, | ||
) | ||
|
||
# Make sure to match colormap type | ||
if isinstance(c, Sequence): | ||
c = [(tuple(inter), parse_color(v)) for (inter, v) in c] | ||
|
||
return c | ||
except json.JSONDecodeError as e: | ||
raise HTTPException( | ||
status_code=400, detail="Could not parse the colormap value." | ||
) from e | ||
|
||
return None | ||
|
||
return deps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
import pytest | ||
from httpx import AsyncClient | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_colormap_uppercasing(client: AsyncClient) -> None: | ||
response = await client.get("/legend/colormap/modis-10A2") | ||
""" | ||
Test mixed casing colormap_name which matches the original key defined | ||
(and used in public render-configs) | ||
""" | ||
params = { | ||
"collection": "naip", | ||
"item": "al_m_3008506_nw_16_060_20191118_20200114", | ||
"assets": "image", | ||
"asset_bidx": "image|1", | ||
"colormap_name": "modis-10A2", | ||
} | ||
response = await client.get( | ||
"/item/tiles/WebMercatorQuad/15/8616/13419@1x", params=params | ||
) | ||
assert response.status_code == 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters