From b9c0999c4b10d187483af8895330cebc0bbe31cb Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Thu, 14 Mar 2024 18:40:53 +0100 Subject: [PATCH] Enable search on /items and add queryables links --- stac_fastapi/pgstac/core.py | 66 ++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/stac_fastapi/pgstac/core.py b/stac_fastapi/pgstac/core.py index ef20f25..65a9b50 100644 --- a/stac_fastapi/pgstac/core.py +++ b/stac_fastapi/pgstac/core.py @@ -16,7 +16,13 @@ from stac_fastapi.types.core import AsyncBaseCoreClient from stac_fastapi.types.errors import InvalidQueryParameter, NotFoundError from stac_fastapi.types.requests import get_base_url -from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection +from stac_fastapi.types.stac import ( + Collection, + Collections, + Item, + ItemCollection, + LandingPage, +) from stac_pydantic.links import Relations from stac_pydantic.shared import MimeTypes @@ -37,6 +43,30 @@ class CoreCrudClient(AsyncBaseCoreClient): """Client for core endpoints defined by stac.""" + async def landing_page(self, **kwargs) -> LandingPage: + """Landing page. + + Called with `GET /`. + + Returns: + API landing page, serving as an entry point to the API. + """ + request: Request = kwargs["request"] + base_url = get_base_url(request) + landing_page = await super().landing_page(**kwargs) + + if self.extension_is_enabled("FilterExtension"): + landing_page["links"].append( + { + "rel": "http://www.opengis.net/def/rel/ogc/1.0/queryables", + "type": "application/schema+json", + "title": "Queryables", + "href": urljoin(base_url, "queryables"), + } + ) + + return landing_page + async def all_collections(self, request: Request, **kwargs) -> Collections: """Read all collections from the database.""" base_url = get_base_url(request) @@ -55,6 +85,18 @@ async def all_collections(self, request: Request, **kwargs) -> Collections: collection_id=coll["id"], request=request ).get_links(extra_links=coll.get("links")) + if self.extension_is_enabled("FilterExtension"): + coll["links"].append( + { + "rel": "http://www.opengis.net/def/rel/ogc/1.0/queryables", + "type": "application/schema+json", + "title": "Queryables", + "href": urljoin( + base_url, f"collections/{coll['id']}/queryables" + ), + } + ) + linked_collections.append(coll) links = [ @@ -107,6 +149,19 @@ async def get_collection( collection_id=collection_id, request=request ).get_links(extra_links=collection.get("links")) + if self.extension_is_enabled("FilterExtension"): + base_url = get_base_url(request) + collection["links"].append( + { + "rel": "http://www.opengis.net/def/rel/ogc/1.0/queryables", + "type": "application/schema+json", + "title": "Queryables", + "href": urljoin( + base_url, f"collections/{collection_id}/queryables" + ), + } + ) + return Collection(**collection) async def _get_base_item( @@ -277,6 +332,15 @@ async def item_collection( "token": token, } + if self.extension_is_enabled("FilterExtension"): + filter_lang = request.query_params.get("filter-lang", None) + filter = request.query_params.get("filter", "").strip() + + if len(filter) > 0 and filter_lang == "cql2-text": + ast = parse_cql2_text(filter) + base_args["filter"] = orjson.loads(to_cql2(ast)) + base_args["filter-lang"] = "cql2-json" + clean = {} for k, v in base_args.items(): if v is not None and v != []: