Skip to content

Commit 780c7fd

Browse files
authored
refactor: STAC filters available in EODAG (#86)
The STAC filter parameters `datetime`, `intersects` and `limit` are now available in EODAG. Therefore, conversion is not required anymore.
1 parent 6e32d83 commit 780c7fd

4 files changed

Lines changed: 63 additions & 97 deletions

File tree

helm/stac-fastapi-eodag/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ providers: ""
8787
# type: UsgsApi
8888
# need_auth: true
8989
# pagination:
90-
# max_items_per_page: 5000
90+
# max_limit: 5000
9191
# total_items_nb_key_path: '$.totalHits'
9292
# metadata_mapping:
9393
# id:

stac_fastapi/eodag/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
CACHE_KEY_QUERYABLES = "queryables"
2424

2525
#: default number of items per page from stac-fastapi
26-
DEFAULT_ITEMS_PER_PAGE = 10
26+
DEFAULT_LIMIT = 10

stac_fastapi/eodag/core.py

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
from eodag.utils.exceptions import NoMatchingCollection as EodagNoMatchingCollection
5050
from stac_fastapi.eodag.client import CustomCoreClient
5151
from stac_fastapi.eodag.config import get_settings
52-
from stac_fastapi.eodag.constants import DEFAULT_ITEMS_PER_PAGE
52+
from stac_fastapi.eodag.constants import DEFAULT_LIMIT
5353
from stac_fastapi.eodag.cql_evaluate import EodagEvaluator
5454
from stac_fastapi.eodag.errors import NoMatchingCollection, ResponseSearchError
5555
from stac_fastapi.eodag.models.item import create_stac_item
@@ -588,67 +588,48 @@ def prepare_search_base_args(search_request: BaseSearchPostRequest, model: type[
588588
:param model: the model used to validate stac metadata
589589
:returns: a dictionary containing arguments for the eodag search
590590
"""
591-
base_args = (
592-
{
593-
"token": search_request.token,
594-
"items_per_page": search_request.limit,
595-
"raise_errors": False,
596-
"count": get_settings().count,
597-
}
598-
if search_request.ids is None
599-
else {}
600-
)
601-
602-
if search_request.spatial_filter is not None:
603-
base_args["geom"] = search_request.spatial_filter.wkt
604-
# Also check datetime to bypass persistent dates between searches
605-
# until https://github.com/stac-utils/stac-pydantic/pull/171 is merged
606-
if search_request.datetime is not None and search_request.start_date is not None:
607-
base_args["start"] = search_request.start_date.isoformat().replace("+00:00", "Z")
608-
if search_request.datetime is not None and search_request.end_date is not None:
609-
base_args["end"] = search_request.end_date.isoformat().replace("+00:00", "Z")
591+
if search_request.ids is None:
592+
base_args = search_request.model_dump()
593+
base_args["raise_errors"] = False
594+
base_args["count"] = get_settings().count
595+
else:
596+
base_args = {}
610597

611598
# parse "sortby" search request attribute if it exists to make it work for an eodag search
612599
sort_by = {}
613-
if sortby := getattr(search_request, "sortby", None):
614-
sort_by_special_fields = {
615-
"start": "start_datetime",
616-
"end": "end_datetime",
617-
}
600+
if sortby := base_args.pop("sortby", None):
618601
param_tuples = []
619602
for param in sortby:
620-
dumped_param = param.model_dump(mode="json")
621603
param_tuples.append(
622604
(
623-
sort_by_special_fields.get(
624-
model.to_eodag(dumped_param["field"]),
625-
model.to_eodag(dumped_param["field"]),
626-
),
627-
dumped_param["direction"],
605+
model.to_eodag(param["field"]),
606+
param["direction"],
628607
)
629608
)
630609
sort_by["sort_by"] = param_tuples
631610

632611
eodag_query = {}
633-
if query_attr := getattr(search_request, "query", None):
612+
if query_attr := base_args.pop("query", None):
634613
parsed_query = parse_query(query_attr)
635614
eodag_query = {model.to_eodag(k): v for k, v in parsed_query.items()}
636615

637616
# get the extracted CQL2 properties dictionary if the CQL2 filter exists
638617
eodag_filter = {}
639-
if f := getattr(search_request, "filter_expr", None):
618+
base_args.pop("filter_lang", None)
619+
if f := base_args.pop("filter_expr", None):
640620
parsed_filter = parse_cql2(f)
641621
eodag_filter = {model.to_eodag(k): v for k, v in parsed_filter.items()}
642622

643623
# EODAG search support a single collection
644-
if search_request.collections:
645-
base_args["collection"] = search_request.collections[0]
624+
if collections := base_args.pop("collections", search_request.collections):
625+
base_args["collection"] = collections[0]
646626

647627
if search_request.ids:
648628
base_args["ids"] = search_request.ids
649629

650630
# merge all eodag search arguments
651631
base_args = base_args | sort_by | eodag_filter | eodag_query
632+
base_args = {k: v for k, v in base_args.items() if v is not None} # remove parameters with value None
652633

653634
return base_args
654635

@@ -774,7 +755,7 @@ def eodag_search_next_page(dag, eodag_args):
774755
next_page_token_key = getattr(search_plugin.config, "pagination", {}).get("next_page_token_key", "page")
775756
eodag_args.pop("count", None)
776757
search_result = SearchResult(
777-
[EOProduct(provider, {"id": "_"})] * int(eodag_args.get("items_per_page", DEFAULT_ITEMS_PER_PAGE)),
758+
[EOProduct(provider, {"id": "_"})] * int(eodag_args.get("limit", DEFAULT_LIMIT)),
778759
next_page_token=next_page_token,
779760
next_page_token_key=next_page_token_key,
780761
search_params=eodag_args,

0 commit comments

Comments
 (0)