Skip to content

Commit ca64804

Browse files
authored
Merge pull request #196 from ecmwf-projects/COPDS-1647-geo-location-labels
Add labels for geolocation widget
2 parents 94aeed6 + 2e367db commit ca64804

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

cads_processing_api_service/translators.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
from typing import Any
1818

1919
import fastapi
20+
import structlog
2021

2122
from . import config
2223

24+
logger: structlog.stdlib.BoundLogger = structlog.get_logger(__name__)
25+
2326

2427
def extract_groups_labels(
2528
groups: list[Any], values: dict[str, str] | None = None
@@ -133,7 +136,7 @@ def translate_cds_form(
133136

134137

135138
def make_request_labels(
136-
input_value_ids: list[str],
139+
input_value_ids: Any,
137140
cds_input_schema: dict[str, Any],
138141
) -> list[str]:
139142
if cds_input_schema["type"] in (
@@ -147,6 +150,21 @@ def make_request_labels(
147150
input_value_ids,
148151
)
149152
]
153+
elif cds_input_schema["type"] == "GeographicLocationWidget":
154+
location = input_value_ids[0]
155+
try:
156+
latitude = f"{location['latitude']}°"
157+
longitude = f"{location['longitude']}°"
158+
except Exception as e:
159+
logger.error(
160+
"Error extracting latitude and longitude from geographic location",
161+
error=e,
162+
)
163+
latitude = longitude = "Unknown"
164+
request_labels = [
165+
f"Latitude: {latitude}",
166+
f"Longitude: {longitude}",
167+
]
150168
else:
151169
input_value_label = extract_labels(cds_input_schema)
152170
request_labels = []

tests/test_10_translators.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# type: ignore
2+
13
# Copyright 2022, European Union.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -41,6 +43,10 @@
4143
"details": {"default": [1, 2, 3, 4]},
4244
"type": "GeographicExtentMapWidget",
4345
},
46+
"geographic_location": {
47+
"details": {},
48+
"type": "GeographicLocationWidget",
49+
},
4450
"string_list_array_groups": {
4551
"details": {
4652
"groups": [
@@ -205,6 +211,14 @@ def test_make_request_labels() -> None:
205211
)
206212
assert res_output == exp_output
207213

214+
test_input_value_ids = [{"latitude": 10, "longitude": 10}]
215+
test_input_cds_schema = TEST_INPUT_CDS_SCHEMAS["geographic_location"]
216+
exp_output = ["Latitude: 10°", "Longitude: 10°"]
217+
res_output = cads_processing_api_service.translators.make_request_labels(
218+
test_input_value_ids, test_input_cds_schema
219+
)
220+
assert res_output == exp_output
221+
208222
test_input_value_ids = ["val1", "val2"]
209223
test_input_cds_schema = TEST_INPUT_CDS_SCHEMAS["string_list"]
210224
exp_output = ["Val1", "Val2"]

0 commit comments

Comments
 (0)