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

[jormungandr] implement access_points in transfer path #3872

Merged
merged 8 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/ed/build_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ void builder::generate_dummy_basis() {
this->data->pt_data->get_or_create_commercial_mode("Bus", "Bus");
this->data->pt_data->get_or_create_commercial_mode("Car", "Car");
this->data->pt_data->get_or_create_commercial_mode("Coach", "Autocar");
this->data->pt_data->get_or_create_commercial_mode("RapidTransit", "RER");

for (navitia::type::CommercialMode* mt : this->data->pt_data->commercial_modes) {
data->pt_data->get_or_create_physical_mode("physical_mode:" + mt->uri, mt->name, get_co2_emission(mt->uri));
Expand Down
4 changes: 2 additions & 2 deletions source/jormungandr/jormungandr/georef.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ def get_stop_points_for_stop_area(self, uri, request_id):
)
return result.stop_points

def get_stop_points_from_uri(self, uri, request_id):
def get_stop_points_from_uri(self, uri, request_id, depth=0):
req = request_pb2.Request()
req.requested_api = type_pb2.PTREFERENTIAL
req.ptref.requested_type = type_pb2.STOP_POINT
req.ptref.count = 100
req.ptref.start_page = 0
req.ptref.depth = 0
req.ptref.depth = depth
req.ptref.filter = 'stop_point.uri = {uri}'.format(uri=uri)
result = self.instance.send_and_receive(req, request_id=request_id)
return result.stop_points
Expand Down
4 changes: 3 additions & 1 deletion source/jormungandr/jormungandr/scenarios/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ def finalise_journeys(self, future_manager, request, responses, context, instanc

journeys_to_complete = get_journeys_to_complete(responses, context, is_debug)

transfer_pool = TransferPool(future_manager=future_manager, instance=instance, request=request)
transfer_pool = TransferPool(
future_manager=future_manager, instance=instance, request=request, request_id=request_id
)

if request['_transfer_path'] is True:
for journey in journeys_to_complete:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
SectionSorter,
get_pt_object_coord,
generate_id,
get_overriding_mode,
)
from jormungandr.street_network.utils import crowfly_distance_between
from jormungandr.fallback_modes import FallbackModes, all_fallback_modes
Expand Down Expand Up @@ -247,6 +246,34 @@ def _extend_with_car_park(
dp_journey.distances.walking += int(walking_speed * car_park_crowfly_duration)


def append_path_item_with_access_point(path_items, stop_point, access_point):
via = path_items.add()
via.duration = access_point.traversal_time
via.length = access_point.length
via.name = access_point.name
# Use label in stead of name???
via.instruction = u"Then Enter {} via {}.".format(stop_point.label, access_point.name)
via.via_uri = access_point.uri


def prepend_path_item_with_access_point(path_items, stop_point, access_point):
via = path_items.add()
via.duration = access_point.traversal_time
via.length = access_point.length
via.name = access_point.name
# Use label in stead of name???
via.instruction = u"Exit {} via {}.".format(stop_point.label, access_point.name)
via.via_uri = access_point.uri

# we cannot insert an element at the beginning of a list :(
# a little algo to move the last element to the beginning
tmp_item = response_pb2.PathItem()
for i in range(len(path_items)):
tmp_item.CopyFrom(path_items[i])
path_items[i].CopyFrom(path_items[-1])
path_items[-1].CopyFrom(tmp_item)


def _extend_with_via_access_point(fallback_dp, pt_object, fallback_type, via_access_point):
if via_access_point is None:
return
Expand All @@ -266,37 +293,21 @@ def _extend_with_via_access_point(fallback_dp, pt_object, fallback_type, via_acc
dp_journey.sections[0].street_network.duration += traversal_time
dp_journey.sections[0].street_network.length += length

via = dp_journey.sections[-1].street_network.path_items.add()
via.duration = traversal_time
via.length = length
via.name = via_access_point.name
# Use label in stead of name???
via.instruction = u"Then Enter {} via {}.".format(pt_object.stop_point.label, via_access_point.name)
via.via_uri = via_access_point.uri
append_path_item_with_access_point(
dp_journey.sections[-1].street_network.path_items,
pt_object.stop_point,
via_access_point.access_point,
)

elif fallback_type == StreetNetworkPathType.ENDING_FALLBACK:
dp_journey.sections[-1].end_date_time += traversal_time
dp_journey.sections[-1].duration += traversal_time
dp_journey.sections[-1].street_network.duration += traversal_time
dp_journey.sections[-1].street_network.length += length

path_items = dp_journey.sections[0].street_network.path_items

via = dp_journey.sections[0].street_network.path_items.add()
via.duration = traversal_time
via.length = length
via.name = via_access_point.name
# Use label in stead of name???
via.instruction = u"Exit {} via {}.".format(pt_object.stop_point.label, via_access_point.name)
via.via_uri = via_access_point.uri

# we cannot insert an element at the beginning of a list :(
# a little algo to move the last element to the beginning
tmp_item = response_pb2.PathItem()
for i in range(len(path_items)):
tmp_item.CopyFrom(path_items[i])
path_items[i].CopyFrom(path_items[-1])
path_items[-1].CopyFrom(tmp_item)
prepend_path_item_with_access_point(
dp_journey.sections[0].street_network.path_items, pt_object.stop_point, via_access_point.access_point
)


def _update_fallback_sections(journey, fallback_dp, fallback_period_extremity, fallback_type, via_access_point):
Expand Down Expand Up @@ -766,3 +777,49 @@ def complete_transfer(pt_journey, transfer_pool):
if section.type != response_pb2.TRANSFER:
continue
transfer_pool.wait_and_complete(section)


def is_valid_direct_path_streetwork(dp):
if not dp or not dp.journeys or not dp.journeys[0].sections:
return False

# a valid journey's must comprise at least two coordinates
nb_coords = sum((len(sec.street_network.coordinates) for sec in dp.journeys[0].sections))
if nb_coords < 2:
return False
return True


def prepend_first_coord(dp, pt_obj):
"""
prepend pt_object's coord to journeys' coordinates (geojson)
"""
if not is_valid_direct_path_streetwork(dp):
return

starting_coords = dp.journeys[0].sections[0].street_network.coordinates
# we are inserting the coord of the origin at the beginning of the geojson
coord = get_pt_object_coord(pt_obj)
if starting_coords and coord != starting_coords[0]:
starting_coords.add(lon=coord.lon, lat=coord.lat)
# we cannot insert an element at the beginning of a list :(
# a little algo to move the last element to the beginning
tmp = type_pb2.GeographicalCoord()
for i in range(len(starting_coords)):
tmp.CopyFrom(starting_coords[i])
starting_coords[i].CopyFrom(starting_coords[-1])
starting_coords[-1].CopyFrom(tmp)


def append_last_coord(dp, pt_obj):
"""
append pt_object's coord to journeys' coordinates (geojson)
"""
if not is_valid_direct_path_streetwork(dp):
return

ending_coords = dp.journeys[0].sections[-1].street_network.coordinates
# we are appending the coord of the destination at the end of the geojson
coord = get_pt_object_coord(pt_obj)
if ending_coords and coord != ending_coords[-1]:
ending_coords.add(lon=coord.lon, lat=coord.lat)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from jormungandr import utils, new_relic
from jormungandr.street_network.street_network import StreetNetworkPathType
import logging
from .helper_utils import timed_logger
from .helper_utils import timed_logger, prepend_first_coord, append_last_coord
from navitiacommon import type_pb2, response_pb2
from jormungandr.exceptions import GeoveloTechnicalError
from .helper_exceptions import StreetNetworkException
Expand Down Expand Up @@ -101,33 +101,6 @@ def _direct_path_with_fp(self):
logging.getLogger(__name__).exception('')
return None

def _add_first_and_last_coord(self, dp):
if not dp or not dp.journeys or not dp.journeys[0].sections:
return

nb_coords = sum((len(sec.street_network.coordinates) for sec in dp.journeys[0].sections))
if nb_coords < 2:
return

starting_coords = dp.journeys[0].sections[0].street_network.coordinates
# we are inserting the coord of the origin at the beginning of the geojson
coord = utils.get_pt_object_coord(self._orig_obj)
if starting_coords and coord != starting_coords[0]:
starting_coords.add(lon=coord.lon, lat=coord.lat)
# we cannot insert an element at the beginning of a list :(
# a little algo to move the last element to the beginning
tmp = type_pb2.GeographicalCoord()
for i in range(len(starting_coords)):
tmp.CopyFrom(starting_coords[i])
starting_coords[i].CopyFrom(starting_coords[-1])
starting_coords[-1].CopyFrom(tmp)

ending_coords = dp.journeys[0].sections[-1].street_network.coordinates
# we are appending the coord of the destination at the end of the geojson
coord = utils.get_pt_object_coord(self._dest_obj)
if ending_coords and coord != ending_coords[-1]:
ending_coords.add(lon=coord.lon, lat=coord.lat)

def _do_request(self):
self._logger.debug(
"requesting %s direct path from %s to %s by %s",
Expand All @@ -138,8 +111,8 @@ def _do_request(self):
)

dp = self._direct_path_with_fp(self._streetnetwork_service)

self._add_first_and_last_coord(dp)
prepend_first_coord(dp, self._orig_obj)
append_last_coord(dp, self._dest_obj)

if getattr(dp, "journeys", None):
dp.journeys[0].internal_id = str(utils.generate_id())
Expand Down
Loading