Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support interval legend types #83

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for /queryables endpoint [#44](https://github.com/microsoft/planetary-computer-apis/pull/44)
- Added `/mosaic/info` endpoint [#48](https://github.com/microsoft/planetary-computer-apis/pull/48)
- Added caching and rate limiting to STAC API [#52](https://github.com/microsoft/planetary-computer-apis/pull/52)
- Added endpoint for interval legend classmap [#83](https://github.com/microsoft/planetary-computer-apis/pull/83)

### Fixed

Expand Down
29 changes: 29 additions & 0 deletions pctiler/pctiler/endpoints/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@
legend_router = APIRouter()


@legend_router.get("/interval/{classmap_name}", response_class=JSONResponse)
async def get_interval_legend(
classmap_name: str,
trim_start: int = 0,
trim_end: int = 0,
) -> JSONResponse:
"""Generate values and color swatches mapping for a given interval classmap.

Args:
trim_start (int, optional): Number of items to trim from the start of the cmap
trim_end (int, optional): Number of items to trim from the end of the cmap
"""
classmap = custom_colormaps.get(classmap_name)

if classmap is None:
raise HTTPException(
status_code=404, detail=f"Classmap {classmap_name} not found"
)

if type(classmap) is not list:
raise HTTPException(
status_code=400, detail=f"Classmap {classmap_name} is not an interval type"
)

trimmed_map = classmap[trim_start : len(classmap) - trim_end] # type: ignore

return JSONResponse(content=trimmed_map)


@legend_router.get("/classmap/{classmap_name}", response_class=JSONResponse)
async def get_classmap_legend(
classmap_name: str,
Expand Down
42 changes: 40 additions & 2 deletions pctiler/tests/endpoints/test_legends.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
import json
import pytest
from httpx import AsyncClient

from pctiler.colormaps.lidarusgs import lidar_colormaps
from pctiler.colormaps.lulc import lulc_colormaps


@pytest.mark.asyncio
async def test_get_invalid_interval(client: AsyncClient) -> None:
response = await client.get("/legend/interval/io-lulc")
assert response.status_code == 400


@pytest.mark.asyncio
async def test_get_interval(client: AsyncClient) -> None:
response = await client.get("/legend/interval/lidar-hag")
assert response.status_code == 200
interval = response.json()

# The interval has been serialized/deserialized which can change
# the sequence type, do the same to the original for comparison
lidar_json = json.loads(json.dumps(lidar_colormaps["lidar-hag"]))
assert interval == lidar_json


@pytest.mark.asyncio
async def test_trim_interval(client: AsyncClient) -> None:
# Trim the first and last entry from the classmap
response = await client.get("/legend/interval/lidar-hag?trim_start=1&trim_end=1")
interval = response.json()

lidar_hag = lidar_colormaps["lidar-hag"]
assert len(interval) == len(lidar_hag) - 2
assert interval[0] != lidar_hag[0]
assert interval[-1] != lidar_hag[-1]


@pytest.mark.asyncio
async def test_get_classmap(client: AsyncClient) -> None:
response = await client.get("/legend/classmap/io-lulc")
assert response.status_code == 200
classmap = response.json()
assert classmap["0"] == [0, 0, 0, 0]

# The classmap has been serialized/deserialized which can change
# the sequence type, do the same to the original for comparison
lulc_json = json.loads(json.dumps(lulc_colormaps["io-lulc"]))
assert classmap == lulc_json


@pytest.mark.asyncio
Expand All @@ -17,7 +55,7 @@ async def test_trim_classmap(client: AsyncClient) -> None:

keys = list(classmap.keys())
key_start = keys[0]
key_end = keys[len(keys) - 1]
key_end = keys[-1]

# Trim the first and last entry from the classmap
response = await client.get("/legend/classmap/io-lulc?trim_start=1&trim_end=1")
Expand Down