Skip to content

Add STAC filter / sort search parameters #29

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

Merged
merged 2 commits into from
Feb 24, 2025
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
65 changes: 37 additions & 28 deletions titiler/stacapi/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,34 +152,28 @@ def ItemIdParams(
)


def STACSearchParams(
request: Request,
collection_id: Annotated[
str,
Path(description="STAC Collection Identifier"),
],
ids: Annotated[Optional[str], Query(description="Filter by Ids.")] = None,
def STACQueryParams(
bbox: Annotated[
Optional[str],
Query(description="Spatial Filter."),
] = None,
datetime: Annotated[Optional[str], Query(description="Temporal Filter.")] = None,
# sortby: Annotated[
# Optional[str],
# Query(
# description="Column Sort the items by Column (ascending (default) or descending).",
# ),
# ] = None,
# query: Annotated[
# Optional[str], Query(description="CQL2 Filter", alias="filter")
# ] = None,
# filter_lang: Annotated[
# Optional[Literal["cql2-text", "cql2-json"]],
# Query(
# description="CQL2 Language (cql2-text, cql2-json). Defaults to cql2-text.",
# alias="filter-lang",
# ),
# ] = None,
sortby: Annotated[
Optional[str],
Query(
description="Column Sort the items by Column (ascending (default) or descending).",
),
] = None,
query: Annotated[
Optional[str], Query(description="CQL2 Filter", alias="filter")
] = None,
filter_lang: Annotated[
Optional[Literal["cql2-text", "cql2-json"]],
Query(
description="CQL2 Language (cql2-text, cql2-json). Defaults to cql2-text.",
alias="filter-lang",
),
] = None,
limit: Annotated[
Optional[int],
Query(description="Limit the number of items per page search (default: 10)"),
Expand All @@ -191,13 +185,28 @@ def STACSearchParams(
) -> Dict:
"""Dependency to construct STAC API Search Query."""
return {
"collections": [collection_id],
"ids": ids.split(",") if ids else None,
"bbox": list(map(float, bbox.split(","))) if bbox else None,
"datetime": datetime,
# "sortby": sortby,
# "filter": query,
# "filter-lang": filter_lang,
"sortby": sortby,
"filter": query,
"filter-lang": filter_lang,
"limit": limit or 10,
"max_items": max_items or 100,
}


def STACSearchParams(
request: Request,
collection_id: Annotated[
str,
Path(description="STAC Collection Identifier"),
],
ids: Annotated[Optional[str], Query(description="Filter by Ids.")] = None,
query_params=Depends(STACQueryParams),
) -> Dict:
"""Dependency to construct STAC API Search Query."""
return {
"collections": [collection_id],
"ids": ids.split(",") if ids else None,
**query_params,
}
22 changes: 16 additions & 6 deletions titiler/stacapi/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@
from titiler.core.utils import render_image
from titiler.mosaic.factory import PixelSelectionParams
from titiler.stacapi.backend import STACAPIBackend
from titiler.stacapi.dependencies import APIParams, STACApiParams, STACSearchParams
from titiler.stacapi.dependencies import (
APIParams,
STACApiParams,
STACQueryParams,
STACSearchParams,
)
from titiler.stacapi.models import FeatureInfo, LayerDict
from titiler.stacapi.pystac import Client
from titiler.stacapi.settings import CacheSettings, RetrySettings
Expand Down Expand Up @@ -727,6 +732,8 @@ class OGCWMTSFactory(BaseTilerFactory):
# https://developmentseed.org/cogeo-mosaic/advanced/backends/
reader: Type[BaseBackend] = STACAPIBackend

query_dependency: Callable[..., Any] = STACQueryParams

# Because the endpoints should work with STAC Items,
# the `layer_dependency` define which query parameters are mandatory/optional to `display` images
# Defaults to `titiler.core.dependencies.AssetsBidxExprParams`, `assets=` or `expression=` is required
Expand Down Expand Up @@ -803,9 +810,7 @@ def get_tile( # noqa: C901
###########################################################
# STAC Query parameter provided by the the render extension and QueryParameters
###########################################################
search_query: Dict[str, Any] = {
"collections": [layer["collection"]],
}
query_params = copy(layer.get("render")) or {}

if req_time:
start_datetime = python_datetime.datetime.strptime(
Expand All @@ -814,16 +819,21 @@ def get_tile( # noqa: C901
).replace(tzinfo=python_datetime.timezone.utc)
end_datetime = start_datetime + python_datetime.timedelta(days=1)

search_query[
query_params[
"datetime"
] = f"{start_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')}/{end_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')}"

query_params = copy(layer.get("render")) or {}
if "color_formula" in req:
query_params["color_formula"] = req["color_formula"]
if "expression" in req:
query_params["expression"] = req["expression"]

search_query = get_dependency_params(
dependency=self.query_dependency,
query_params=query_params,
)
search_query["collections"] = [layer["collection"]]

layer_params = get_dependency_params(
dependency=self.layer_dependency,
query_params=query_params,
Expand Down