Skip to content
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

i.eodag: handle cases when sorting parameters are not available #1170

Merged
merged 10 commits into from
Aug 20, 2024
58 changes: 27 additions & 31 deletions src/imagery/i.eodag/i.eodag.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,7 @@ def no_fallback_search(search_parameters, provider):
try:
if int(eodag.__version__.split(".")[0]) < 3:
server_poke = dag.search(**search_parameters, provider=provider)
elif (
int(eodag.__version__.split(".")[0]) >= 3
and server_poke.number_matched == 0
):
elif int(eodag.__version__.split(".")[0]) >= 3:
server_poke = dag.search(**search_parameters, provider=provider, count=True)
if (int(eodag.__version__.split(".")[0]) < 3 and server_poke[1] == 0) or (
int(eodag.__version__.split(".")[0]) >= 3
Expand Down Expand Up @@ -825,33 +822,32 @@ def sort_result(search_result):
:return: Sorted EO products
:rtype: class:'eodag.api.search_result.SearchResult'
"""
sort_keys = options["sort"].split(",")
sort_order = options["order"]

# Sort keys and sort orders are matched respectively
def products_compare(first, second):
for sort_key in sort_keys:
if sort_key == "ingestiondate":
if "startTimeFromAscendingNode" not in first.properties:
continue
first_value = first.properties["startTimeFromAscendingNode"]
second_value = second.properties["startTimeFromAscendingNode"]
elif sort_key == "cloudcover":
if "cloudCover" not in first.properties:
continue
first_value = first.properties["cloudCover"]
second_value = second.properties["cloudCover"]
elif sort_key == "footprint":
# Sort by title lexicographically
first_value = first.properties["title"]
second_value = second.properties["title"]
if first_value < second_value:
return 1 if sort_order == "desc" else -1
elif first_value > second_value:
return -1 if sort_order == "desc" else 1
return 0

search_result.sort(key=cmp_to_key(products_compare))
sort_keys = list()
for sort_key in options["sort"].split(","):
if sort_key == "ingestiondate":
sort_keys.append(
(
"startTimeFromAscendingNode",
(
"3000-12-31T00:00:00"
HamedElgizery marked this conversation as resolved.
Show resolved Hide resolved
if options["order"] == "asc"
else "1000-01-01T00:00:00"
),
)
)
if sort_key == "title":
sort_keys.append(("title", "Z" * 100 if options["order"] == "asc" else "A"))
if sort_key == "cloudcover":
sort_keys.append(
("cloudCover", int(1e9) * 1 if options["order"] == "asc" else -1)
)
# Default values are used to put scenes with missing sorting key at the end
search_result.sort(
reverse=options["order"] == "desc",
key=lambda product: [
product.properties.get(sort_key, default) for sort_key, default in sort_keys
],
)
return search_result


Expand Down