Skip to content

Commit

Permalink
Merge pull request #4324 from hove-io/fix_geographic_position_coord
Browse files Browse the repository at this point in the history
jormungandr: Fix geographic position address
  • Loading branch information
azime authored Dec 2, 2024
2 parents 81ef42c + c5762a4 commit d389810
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
17 changes: 17 additions & 0 deletions source/jormungandr/jormungandr/interfaces/v1/Journeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,27 @@ 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
Expand Down
21 changes: 18 additions & 3 deletions source/jormungandr/jormungandr/scenarios/new_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1316,17 +1317,31 @@ 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 (by autocomplete) 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 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 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')),
Expand Down
14 changes: 14 additions & 0 deletions source/jormungandr/jormungandr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 33 additions & 0 deletions source/jormungandr/tests/routing_tests_experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,39 @@ 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):
"""
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'

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 = (
Expand Down

0 comments on commit d389810

Please sign in to comment.