From aa726319cbb0cee8d388f336c88b94b273476a7b Mon Sep 17 00:00:00 2001 From: krishna Date: Thu, 28 Nov 2024 18:01:59 +0100 Subject: [PATCH 1/4] jormungandr: Fix geographic position address --- .../jormungandr/interfaces/v1/Journeys.py | 13 ++++++++ .../jormungandr/scenarios/new_default.py | 23 +++++++++++-- source/jormungandr/jormungandr/utils.py | 14 ++++++++ .../tests/routing_tests_experimental.py | 32 +++++++++++++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/source/jormungandr/jormungandr/interfaces/v1/Journeys.py b/source/jormungandr/jormungandr/interfaces/v1/Journeys.py index c1ad3f657f..11a364f3b2 100644 --- a/source/jormungandr/jormungandr/interfaces/v1/Journeys.py +++ b/source/jormungandr/jormungandr/interfaces/v1/Journeys.py @@ -448,10 +448,23 @@ def wrapper(*args, **kwargs): if g.origin_detail: self.clean_global_origin_destination_detail(g.origin_detail) j['sections'][0]['from'] = g.origin_detail + + # Replace coord by origin position if present in g.request_origin + if hasattr(g, 'request_origin') and g.request_origin: + coord = j['sections'][0]['from'].get('address', {}).get('coord') + if coord: + j['sections'][0]['from']['address']['coord'] = g.request_origin.get('address').get('coord') + if g.destination_detail: self.clean_global_origin_destination_detail(g.destination_detail) j['sections'][-1]['to'] = g.destination_detail + # Replace coord by destination position if present in g.request_destination + if hasattr(g, 'request_destination') and g.request_destination: + coord = j['sections'][-1]['to'].get('address', {}).get('coord') + if coord: + j['sections'][-1]['to']['address']['coord'] = g.request_destination.get('address').get('coord') + return objects return wrapper diff --git a/source/jormungandr/jormungandr/scenarios/new_default.py b/source/jormungandr/jormungandr/scenarios/new_default.py index 47053d9fbc..f4f75851db 100644 --- a/source/jormungandr/jormungandr/scenarios/new_default.py +++ b/source/jormungandr/jormungandr/scenarios/new_default.py @@ -87,6 +87,7 @@ json_address_from_uri, entrypoint_uri_refocus, get_pt_object_coord, + is_different_geographic_position, ) from jormungandr.error import generate_error from jormungandr.utils import Coords @@ -1316,17 +1317,33 @@ def fill_journeys(self, request_type, api_request, instance): ) # we store the origin/destination detail in g to be able to use them after the marshall + # If origin/destination is address and id doesn't match with calculated id then + # we should use the original request address id and update later g.origin_detail = origin_detail - g.destination_detail = destination_detail + request_origin = json_address_from_uri(api_request.get('origin')) + # if request_origin and origin_detail and request_origin.get('id') != origin_detail.get('id'): + if is_different_geographic_position(origin_detail, request_origin): + origin_detail = request_origin + g.request_origin = request_origin + else: + origin_detail = origin_detail or request_origin - origin_detail = origin_detail or json_address_from_uri(api_request.get('origin')) if not origin_detail: return generate_error( TEMPLATE_MSG_UNKNOWN_OBJECT.format(api_request.get('origin')), response_pb2.Error.unknown_object, 404, ) - destination_detail = destination_detail or json_address_from_uri(api_request.get('destination')) + + g.destination_detail = destination_detail + request_destination = json_address_from_uri(api_request.get('destination')) + # if request_destination and destination_detail and request_destination.get('id') != destination_detail.get('id'): + if is_different_geographic_position(destination_detail, request_destination): + destination_detail = request_destination + g.request_destination = request_destination + else: + destination_detail = destination_detail or request_destination + if not destination_detail: return generate_error( TEMPLATE_MSG_UNKNOWN_OBJECT.format(api_request.get('destination')), diff --git a/source/jormungandr/jormungandr/utils.py b/source/jormungandr/jormungandr/utils.py index 172197e2cb..bb634eff27 100644 --- a/source/jormungandr/jormungandr/utils.py +++ b/source/jormungandr/jormungandr/utils.py @@ -1213,3 +1213,17 @@ def content_is_too_large(instance, endpoint, response): return False return True + + +def is_different_geographic_position(addr1, addr2): + """ + Returns True if both params are address with different id (also means different coordinate) else False + :return: boolean + """ + if not (addr1 and addr2): + return False + if addr1.get('embedded_type') != "address": + return False + if addr1.get('id') != addr2.get('id'): + return True + return False diff --git a/source/jormungandr/tests/routing_tests_experimental.py b/source/jormungandr/tests/routing_tests_experimental.py index 6e4e66c6c8..97f9c7d845 100644 --- a/source/jormungandr/tests/routing_tests_experimental.py +++ b/source/jormungandr/tests/routing_tests_experimental.py @@ -676,6 +676,38 @@ class TestDistributedMaxDistanceForDirectPathUpperLimit(NewDefaultScenarioAbstra test_max_taxi_direct_path_distance = _make_function_distance_over_upper_limit(a, b, 'taxi', operator.truth) +@dataset({"main_routing_test": {"scenario": "distributed"}}) +class TestDistributedWithDestinationPositionNotMatchingAutocomplete(NewDefaultScenarioAbstractTestFixture): + """ + + """ + def test_destination_address_id_and_coord(self): + from_coord = '8.98311981954709e-05;8.98311981954709e-05' + to_coord = '0.0018864551621048887;0.0007186495855637672' + + query = ( + 'journeys?' + 'from={from_coord}' + '&to={to_coord}' + '&datetime={datetime}' + '&first_section_mode[]=walking' + '&last_section_mode[]=walking' + '&max_duration=0' + '&_min_bike=0' + '&_min_car=0' + '&_min_taxi=0' + ).format(from_coord=from_coord, to_coord=to_coord, datetime="20120614T080000") + + response = self.query_region(query) + + assert len(response['journeys']) == 1 + journey = response['journeys'][0] + to_address = journey['sections'][-1]['to']['address'] + assert to_address['id'] == '0.001886455162104889;0.0007186495855637672' + assert to_address['coord']['lon'] == '0.0018864551621048887' + assert to_address['coord']['lat'] == '0.0007186495855637672' + + def _make_function_distance_under_lower_limit(from_coord, to_coord, mode): def test_ko_crow_fly_smaller_than_max_mode_direct_path_distance(self): query = ( From a9da2f9ca3f4d9f8f918fe2a6876a87b24e1a376 Mon Sep 17 00:00:00 2001 From: krishna Date: Fri, 29 Nov 2024 08:57:03 +0100 Subject: [PATCH 2/4] Formatting and adding comment --- source/jormungandr/jormungandr/interfaces/v1/Journeys.py | 8 ++++++-- source/jormungandr/jormungandr/scenarios/new_default.py | 1 - source/jormungandr/jormungandr/utils.py | 2 +- source/jormungandr/tests/routing_tests_experimental.py | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/source/jormungandr/jormungandr/interfaces/v1/Journeys.py b/source/jormungandr/jormungandr/interfaces/v1/Journeys.py index 11a364f3b2..7d5372d55c 100644 --- a/source/jormungandr/jormungandr/interfaces/v1/Journeys.py +++ b/source/jormungandr/jormungandr/interfaces/v1/Journeys.py @@ -453,7 +453,9 @@ def wrapper(*args, **kwargs): if hasattr(g, 'request_origin') and g.request_origin: coord = j['sections'][0]['from'].get('address', {}).get('coord') if coord: - j['sections'][0]['from']['address']['coord'] = g.request_origin.get('address').get('coord') + j['sections'][0]['from']['address']['coord'] = g.request_origin.get('address').get( + 'coord' + ) if g.destination_detail: self.clean_global_origin_destination_detail(g.destination_detail) @@ -463,7 +465,9 @@ def wrapper(*args, **kwargs): if hasattr(g, 'request_destination') and g.request_destination: coord = j['sections'][-1]['to'].get('address', {}).get('coord') if coord: - j['sections'][-1]['to']['address']['coord'] = g.request_destination.get('address').get('coord') + j['sections'][-1]['to']['address']['coord'] = g.request_destination.get('address').get( + 'coord' + ) return objects diff --git a/source/jormungandr/jormungandr/scenarios/new_default.py b/source/jormungandr/jormungandr/scenarios/new_default.py index f4f75851db..27e9f6cf08 100644 --- a/source/jormungandr/jormungandr/scenarios/new_default.py +++ b/source/jormungandr/jormungandr/scenarios/new_default.py @@ -1337,7 +1337,6 @@ def fill_journeys(self, request_type, api_request, instance): g.destination_detail = destination_detail request_destination = json_address_from_uri(api_request.get('destination')) - # if request_destination and destination_detail and request_destination.get('id') != destination_detail.get('id'): if is_different_geographic_position(destination_detail, request_destination): destination_detail = request_destination g.request_destination = request_destination diff --git a/source/jormungandr/jormungandr/utils.py b/source/jormungandr/jormungandr/utils.py index bb634eff27..346fffdad4 100644 --- a/source/jormungandr/jormungandr/utils.py +++ b/source/jormungandr/jormungandr/utils.py @@ -1222,7 +1222,7 @@ def is_different_geographic_position(addr1, addr2): """ if not (addr1 and addr2): return False - if addr1.get('embedded_type') != "address": + if addr1.get('embedded_type') != "address": return False if addr1.get('id') != addr2.get('id'): return True diff --git a/source/jormungandr/tests/routing_tests_experimental.py b/source/jormungandr/tests/routing_tests_experimental.py index 97f9c7d845..9e8b075c6c 100644 --- a/source/jormungandr/tests/routing_tests_experimental.py +++ b/source/jormungandr/tests/routing_tests_experimental.py @@ -679,7 +679,7 @@ class TestDistributedMaxDistanceForDirectPathUpperLimit(NewDefaultScenarioAbstra @dataset({"main_routing_test": {"scenario": "distributed"}}) class TestDistributedWithDestinationPositionNotMatchingAutocomplete(NewDefaultScenarioAbstractTestFixture): """ - + Test on geographical position as destination which doesn't match with address found by autocomplete """ def test_destination_address_id_and_coord(self): from_coord = '8.98311981954709e-05;8.98311981954709e-05' From e6dbb554b16ce7bbf1f5df9d204638d35b17e0c7 Mon Sep 17 00:00:00 2001 From: krishna Date: Fri, 29 Nov 2024 09:17:29 +0100 Subject: [PATCH 3/4] Another formatting --- source/jormungandr/jormungandr/interfaces/v1/Journeys.py | 6 +++--- source/jormungandr/tests/routing_tests_experimental.py | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/source/jormungandr/jormungandr/interfaces/v1/Journeys.py b/source/jormungandr/jormungandr/interfaces/v1/Journeys.py index 7d5372d55c..c91c8d8d04 100644 --- a/source/jormungandr/jormungandr/interfaces/v1/Journeys.py +++ b/source/jormungandr/jormungandr/interfaces/v1/Journeys.py @@ -465,9 +465,9 @@ def wrapper(*args, **kwargs): if hasattr(g, 'request_destination') and g.request_destination: coord = j['sections'][-1]['to'].get('address', {}).get('coord') if coord: - j['sections'][-1]['to']['address']['coord'] = g.request_destination.get('address').get( - 'coord' - ) + j['sections'][-1]['to']['address']['coord'] = g.request_destination.get( + 'address' + ).get('coord') return objects diff --git a/source/jormungandr/tests/routing_tests_experimental.py b/source/jormungandr/tests/routing_tests_experimental.py index 9e8b075c6c..e59c697157 100644 --- a/source/jormungandr/tests/routing_tests_experimental.py +++ b/source/jormungandr/tests/routing_tests_experimental.py @@ -681,6 +681,7 @@ class TestDistributedWithDestinationPositionNotMatchingAutocomplete(NewDefaultSc """ Test on geographical position as destination which doesn't match with address found by autocomplete """ + def test_destination_address_id_and_coord(self): from_coord = '8.98311981954709e-05;8.98311981954709e-05' to_coord = '0.0018864551621048887;0.0007186495855637672' From c5762a422c57edf204e3f41ec51320f9d26993b4 Mon Sep 17 00:00:00 2001 From: krishna Date: Fri, 29 Nov 2024 10:06:25 +0100 Subject: [PATCH 4/4] Comments updated --- source/jormungandr/jormungandr/scenarios/new_default.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/jormungandr/jormungandr/scenarios/new_default.py b/source/jormungandr/jormungandr/scenarios/new_default.py index 27e9f6cf08..52eab39c8e 100644 --- a/source/jormungandr/jormungandr/scenarios/new_default.py +++ b/source/jormungandr/jormungandr/scenarios/new_default.py @@ -1317,11 +1317,10 @@ def fill_journeys(self, request_type, api_request, instance): ) # we store the origin/destination detail in g to be able to use them after the marshall - # If origin/destination is address and id doesn't match with calculated id then + # If origin/destination is address and id doesn't match with calculated id (by autocomplete) then # we should use the original request address id and update later g.origin_detail = origin_detail request_origin = json_address_from_uri(api_request.get('origin')) - # if request_origin and origin_detail and request_origin.get('id') != origin_detail.get('id'): if is_different_geographic_position(origin_detail, request_origin): origin_detail = request_origin g.request_origin = request_origin