Skip to content

Commit 8d9bd44

Browse files
committed
Merge branch 'main' of https://github.com/stac-utils/stac-fastapi-pgstac into refactor/optional-writer-connection-pool
2 parents 060bd00 + 9736027 commit 8d9bd44

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- disable transaction and bulk_transactions extensions by default **breaking change**
3838
- update `stac-fastapi-*` version requirements to `>=5.2,<6.0`
3939
- add pgstac health-check in `/_mgmt/health`
40+
- switch from using pygeofilter to cql2
4041

4142
### Added
4243

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"asyncpg",
1616
"buildpg",
1717
"brotli_asgi",
18-
"pygeofilter>=0.2",
18+
"cql2>=0.3.6",
1919
"pypgstac>=0.8,<0.10",
2020
"typing_extensions>=4.9.0",
2121
]

stac_fastapi/pgstac/core.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
import orjson
1010
from asyncpg.exceptions import InvalidDatetimeFormatError
1111
from buildpg import render
12+
from cql2 import Expr
1213
from fastapi import HTTPException, Request
1314
from pydantic import ValidationError
14-
from pygeofilter.backends.cql2_json import to_cql2
15-
from pygeofilter.parsers.cql2_text import parse as parse_cql2_text
1615
from pypgstac.hydration import hydrate
1716
from stac_fastapi.api.models import JSONResponse
1817
from stac_fastapi.types.core import AsyncBaseCoreClient, Relations
@@ -556,11 +555,12 @@ def _clean_search_args( # noqa: C901
556555
"""Clean up search arguments to match format expected by pgstac"""
557556
if filter_query:
558557
if filter_lang == "cql2-text":
559-
filter_query = to_cql2(parse_cql2_text(filter_query))
560-
filter_lang = "cql2-json"
561-
562-
base_args["filter"] = orjson.loads(filter_query)
563-
base_args["filter_lang"] = filter_lang
558+
e = Expr(filter_query)
559+
base_args["filter"] = e.to_json()
560+
base_args["filter_lang"] = "cql2-json"
561+
else:
562+
base_args["filter"] = orjson.loads(filter_query)
563+
base_args["filter_lang"] = filter_lang
564564

565565
if datetime:
566566
base_args["datetime"] = datetime

tests/resources/test_item.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,12 @@ async def test_item_search_post_filter_extension_cql2(
850850
)
851851
assert resp.status_code == 201
852852

853+
# make sure we have 2 items
854+
resp = await app_client.post("/search", json={})
855+
resp_json = resp.json()
856+
assert resp.status_code == 200
857+
assert len(resp_json.get("features")) == 2
858+
853859
# EPSG is a JSONB key
854860
params = {
855861
"collections": [test_item["collection"]],
@@ -887,6 +893,39 @@ async def test_item_search_post_filter_extension_cql2(
887893
== test_item["properties"]["proj:epsg"]
888894
)
889895

896+
# Test IN operator
897+
params = {
898+
"collections": [test_item["collection"]],
899+
"filter-lang": "cql2-json",
900+
"filter": {
901+
"op": "in",
902+
"args": [
903+
{"property": "proj:epsg"},
904+
[test_item["properties"]["proj:epsg"]],
905+
],
906+
},
907+
}
908+
resp = await app_client.post("/search", json=params)
909+
resp_json = resp.json()
910+
assert resp.status_code == 200
911+
assert len(resp_json.get("features")) == 1
912+
913+
params = {
914+
"collections": [test_item["collection"]],
915+
"filter-lang": "cql2-json",
916+
"filter": {
917+
"op": "in",
918+
"args": [
919+
{"property": "proj:epsg"},
920+
[test_item["properties"]["proj:epsg"] + 1],
921+
],
922+
},
923+
}
924+
resp = await app_client.post("/search", json=params)
925+
resp_json = resp.json()
926+
assert resp.status_code == 200
927+
assert len(resp_json.get("features")) == 0
928+
890929

891930
async def test_item_search_post_filter_extension_cql2_with_query_fails(
892931
app_client, load_test_data, load_test_collection
@@ -904,7 +943,7 @@ async def test_item_search_post_filter_extension_cql2_with_query_fails(
904943
)
905944
assert resp.status_code == 201
906945

907-
# EPSG is a JSONB key
946+
# Cannot use `query` and `filter`
908947
params = {
909948
"collections": [test_item["collection"]],
910949
"filter-lang": "cql2-json",

0 commit comments

Comments
 (0)