Skip to content

Commit 5c59b9b

Browse files
Merge pull request #29 from jverrydt/main
Add STAC filter / sort search parameters
2 parents 270c08d + 161916b commit 5c59b9b

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

Diff for: titiler/stacapi/dependencies.py

+37-28
Original file line numberDiff line numberDiff line change
@@ -152,34 +152,28 @@ def ItemIdParams(
152152
)
153153

154154

155-
def STACSearchParams(
156-
request: Request,
157-
collection_id: Annotated[
158-
str,
159-
Path(description="STAC Collection Identifier"),
160-
],
161-
ids: Annotated[Optional[str], Query(description="Filter by Ids.")] = None,
155+
def STACQueryParams(
162156
bbox: Annotated[
163157
Optional[str],
164158
Query(description="Spatial Filter."),
165159
] = None,
166160
datetime: Annotated[Optional[str], Query(description="Temporal Filter.")] = None,
167-
# sortby: Annotated[
168-
# Optional[str],
169-
# Query(
170-
# description="Column Sort the items by Column (ascending (default) or descending).",
171-
# ),
172-
# ] = None,
173-
# query: Annotated[
174-
# Optional[str], Query(description="CQL2 Filter", alias="filter")
175-
# ] = None,
176-
# filter_lang: Annotated[
177-
# Optional[Literal["cql2-text", "cql2-json"]],
178-
# Query(
179-
# description="CQL2 Language (cql2-text, cql2-json). Defaults to cql2-text.",
180-
# alias="filter-lang",
181-
# ),
182-
# ] = None,
161+
sortby: Annotated[
162+
Optional[str],
163+
Query(
164+
description="Column Sort the items by Column (ascending (default) or descending).",
165+
),
166+
] = None,
167+
query: Annotated[
168+
Optional[str], Query(description="CQL2 Filter", alias="filter")
169+
] = None,
170+
filter_lang: Annotated[
171+
Optional[Literal["cql2-text", "cql2-json"]],
172+
Query(
173+
description="CQL2 Language (cql2-text, cql2-json). Defaults to cql2-text.",
174+
alias="filter-lang",
175+
),
176+
] = None,
183177
limit: Annotated[
184178
Optional[int],
185179
Query(description="Limit the number of items per page search (default: 10)"),
@@ -191,13 +185,28 @@ def STACSearchParams(
191185
) -> Dict:
192186
"""Dependency to construct STAC API Search Query."""
193187
return {
194-
"collections": [collection_id],
195-
"ids": ids.split(",") if ids else None,
196188
"bbox": list(map(float, bbox.split(","))) if bbox else None,
197189
"datetime": datetime,
198-
# "sortby": sortby,
199-
# "filter": query,
200-
# "filter-lang": filter_lang,
190+
"sortby": sortby,
191+
"filter": query,
192+
"filter-lang": filter_lang,
201193
"limit": limit or 10,
202194
"max_items": max_items or 100,
203195
}
196+
197+
198+
def STACSearchParams(
199+
request: Request,
200+
collection_id: Annotated[
201+
str,
202+
Path(description="STAC Collection Identifier"),
203+
],
204+
ids: Annotated[Optional[str], Query(description="Filter by Ids.")] = None,
205+
query_params=Depends(STACQueryParams),
206+
) -> Dict:
207+
"""Dependency to construct STAC API Search Query."""
208+
return {
209+
"collections": [collection_id],
210+
"ids": ids.split(",") if ids else None,
211+
**query_params,
212+
}

Diff for: titiler/stacapi/factory.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@
4545
from titiler.core.utils import render_image
4646
from titiler.mosaic.factory import PixelSelectionParams
4747
from titiler.stacapi.backend import STACAPIBackend
48-
from titiler.stacapi.dependencies import APIParams, STACApiParams, STACSearchParams
48+
from titiler.stacapi.dependencies import (
49+
APIParams,
50+
STACApiParams,
51+
STACQueryParams,
52+
STACSearchParams,
53+
)
4954
from titiler.stacapi.models import FeatureInfo, LayerDict
5055
from titiler.stacapi.pystac import Client
5156
from titiler.stacapi.settings import CacheSettings, RetrySettings
@@ -727,6 +732,8 @@ class OGCWMTSFactory(BaseTilerFactory):
727732
# https://developmentseed.org/cogeo-mosaic/advanced/backends/
728733
reader: Type[BaseBackend] = STACAPIBackend
729734

735+
query_dependency: Callable[..., Any] = STACQueryParams
736+
730737
# Because the endpoints should work with STAC Items,
731738
# the `layer_dependency` define which query parameters are mandatory/optional to `display` images
732739
# Defaults to `titiler.core.dependencies.AssetsBidxExprParams`, `assets=` or `expression=` is required
@@ -803,9 +810,7 @@ def get_tile( # noqa: C901
803810
###########################################################
804811
# STAC Query parameter provided by the the render extension and QueryParameters
805812
###########################################################
806-
search_query: Dict[str, Any] = {
807-
"collections": [layer["collection"]],
808-
}
813+
query_params = copy(layer.get("render")) or {}
809814

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

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

821-
query_params = copy(layer.get("render")) or {}
822826
if "color_formula" in req:
823827
query_params["color_formula"] = req["color_formula"]
824828
if "expression" in req:
825829
query_params["expression"] = req["expression"]
826830

831+
search_query = get_dependency_params(
832+
dependency=self.query_dependency,
833+
query_params=query_params,
834+
)
835+
search_query["collections"] = [layer["collection"]]
836+
827837
layer_params = get_dependency_params(
828838
dependency=self.layer_dependency,
829839
query_params=query_params,

0 commit comments

Comments
 (0)