def _update_fallback_sections(journey, fallback_dp, fallback_period_extremity, fallback_type): """ Replace journey's fallback sections with the given fallback_dp. Note: the replacement is done in place of the journey """ aligned_fallback = _align_fallback_direct_path_datetime( fallback_dp, fallback_period_extremity) fallback_sections = aligned_fallback.journeys[0].sections # update the 'id' which isn't set _rename_fallback_sections_ids(fallback_sections) if fallback_type == StreetNetworkPathType.BEGINNING_FALLBACK: section_to_replace = journey.sections[0] else: section_to_replace = journey.sections[-1] journey.sections.remove(section_to_replace) # We have to create the link between the fallback and the pt part manually here if fallback_type == StreetNetworkPathType.BEGINNING_FALLBACK: fallback_sections[-1].destination.CopyFrom(journey.sections[0].origin) else: fallback_sections[0].origin.CopyFrom(journey.sections[-1].destination) journey.sections.extend(fallback_sections) journey.sections.sort(SectionSorter())
def extend_journey_for_build_to_test(): # create a journey with 4 sections having PUBLIC_TRANSPORT at the begining and at the end pt_journey = create_journeys_with_pt() fallback_extremity = PeriodExtremity(pt_journey.arrival_date_time, True) # create a response having on single section CROW_FLY journey cf_origin = make_pt_object(type_pb2.STOP_POINT, 4.0, 4.0, "stop_point_4_bis") cf_destination = make_pt_object(type_pb2.ADDRESS, 5.0, 5.0, "address_1") response = create_response_with_crow_fly(start_date="20180618T075000", end_date="20180618T075500", origin=cf_origin, destination=cf_destination) _extend_journey(pt_journey, response, fallback_extremity) pt_journey.sections.sort(SectionSorter()) assert len(pt_journey.sections) == 5 # We should have CROW_FLY section at the end of last section PUBLIC_TRANSPORT crowfly_section = pt_journey.sections[-1] pt_section = pt_journey.sections[-2] assert crowfly_section.type == response_pb2.CROW_FLY assert pt_section.type == response_pb2.PUBLIC_TRANSPORT # We should have the same object used for the last pt_section.destination and crowfly_section.origin assert crowfly_section.origin == pt_section.destination assert crowfly_section.origin.uri == "stop_point_4"
def extend_journey_for_build_from_test(): # create a journey with 4 sections having PUBLIC_TRANSPORT at the begining and at the end pt_journey = create_journeys_with_pt() fallback_extremity = PeriodExtremity(pt_journey.departure_date_time, False) # create a response having on single section CROW_FLY journey cf_origin = make_pt_object(type_pb2.ADDRESS, 1.0, 1.0, "address_1") cf_destination = make_pt_object(type_pb2.STOP_POINT, 2.0, 2.0, "stop_point_1_bis") response = create_response_with_crow_fly(start_date="20180618T060000", end_date="20180618T060500", origin=cf_origin, destination=cf_destination) _extend_journey(pt_journey, response, fallback_extremity) pt_journey.sections.sort(SectionSorter()) assert len(pt_journey.sections) == 5 # We should have CROW_FLY section at the begining of the journey following by a section PUBLIC_TRANSPORT crowfly_section = pt_journey.sections[0] pt_section = pt_journey.sections[1] assert crowfly_section.type == response_pb2.CROW_FLY assert pt_section.type == response_pb2.PUBLIC_TRANSPORT # We should have the same object used for crowfly_section.destination and pt_section.origin assert crowfly_section.destination == pt_section.origin assert crowfly_section.destination.uri == "stop_point_1"
def _update_journey(self, journey, additional_section): journey.duration += additional_section.duration journey.durations.total += additional_section.duration journey.arrival_date_time += additional_section.duration journey.sections.extend([additional_section]) journey.sections.sort(key=cmp_to_key(SectionSorter())) journey.nb_sections += 1
def _build_to( requested_dest_obj, pt_journeys, arr_mode, streetnetwork_path_pool, dest_accessible_by_crowfly, dest_fallback_durations_pool, request, ): accessibles_by_crowfly = dest_accessible_by_crowfly.wait_and_get() fallback_durations = dest_fallback_durations_pool.wait_and_get(arr_mode) for pt_journey in pt_journeys.journeys: pt_destination = pt_journey.sections[-1].destination last_section_end = pt_journey.sections[-1].end_date_time if requested_dest_obj.uri != pt_destination.uri: fallback_period_extremity = PeriodExtremity(pt_journey.arrival_date_time, True) # extend the journey with the fallback routing path direct_path_type = StreetNetworkPathType.ENDING_FALLBACK fallback_dp = streetnetwork_path_pool.wait_and_get( pt_destination, requested_dest_obj, arr_mode, fallback_period_extremity, direct_path_type, request=request, ) if pt_destination.uri in accessibles_by_crowfly.odt: pt_journey.sections[-1].destination.CopyFrom(requested_dest_obj) elif _is_crowfly_needed( pt_destination.uri, fallback_durations, accessibles_by_crowfly.crowfly, fallback_dp ): crowfly_arrival_dt = ( pt_journey.arrival_date_time + fallback_durations[pt_destination.uri].duration ) pt_journey.sections.extend( [ _create_crowfly( pt_journey, pt_destination, requested_dest_obj, last_section_end, crowfly_arrival_dt, arr_mode, ) ] ) else: _extend_journey(pt_journey, fallback_dp, fallback_period_extremity) pt_journey.sections.sort(SectionSorter()) pt_journey.arrival_date_time = pt_journey.sections[-1].end_date_time return pt_journeys
def _extend_with_car_park(fallback_dp, pt_journey, fallback_type, walking_speed, car_park, car_park_duration, car_park_crowfly_duration): dp_journey = fallback_dp.journeys[0] if fallback_type == StreetNetworkPathType.BEGINNING_FALLBACK: dp_journey.sections[-1].destination.CopyFrom(car_park) dp_extremity = dp_journey.sections[-1].destination # the first section of pt_journey is a fake crowfly section, # the origin of the second section is the real start of the public transport journey pt_journey_extrimity = pt_journey.sections[1].origin car_park_section = dp_journey.sections.add() car_park_to_sp_section = dp_journey.sections.add() _make_beginning_car_park_sections( car_park_section, car_park_to_sp_section, dp_extremity, dp_journey.arrival_date_time, pt_journey_extrimity, car_park, car_park_duration, car_park_crowfly_duration, ) dp_journey.arrival_date_time = car_park_to_sp_section.end_date_time elif fallback_type == StreetNetworkPathType.ENDING_FALLBACK: dp_journey.sections[0].origin.CopyFrom(car_park) dp_extremity = dp_journey.sections[0].origin # the last section of pt_journey is a fake crowfly section, # the destination of the second section before the last is the real end of the public transport journey pt_journey_extrimity = pt_journey.sections[-2].destination car_park_section = dp_journey.sections.add() car_park_to_sp_section = dp_journey.sections.add() _make_ending_car_park_sections( car_park_section, car_park_to_sp_section, dp_extremity, dp_journey.departure_date_time, pt_journey_extrimity, car_park, car_park_duration, car_park_crowfly_duration, ) dp_journey.departure_date_time = car_park_to_sp_section.begin_date_time dp_journey.sections.sort(key=cmp_to_key(SectionSorter())) dp_journey.duration += CAR_PARK_DURATION + car_park_crowfly_duration dp_journey.durations.walking += car_park_crowfly_duration dp_journey.distances.walking += int(walking_speed * car_park_crowfly_duration)
def _build_from( requested_orig_obj, pt_journeys, dep_mode, streetnetwork_path_pool, orig_accessible_by_crowfly, orig_fallback_durations_pool, request, ): accessibles_by_crowfly = orig_accessible_by_crowfly.wait_and_get() fallback_durations = orig_fallback_durations_pool.wait_and_get(dep_mode) for pt_journey in pt_journeys.journeys: pt_origin = pt_journey.sections[0].origin fallback_period_extremity = PeriodExtremity(pt_journey.departure_date_time, False) # extend the journey with the fallback routing path direct_path_type = StreetNetworkPathType.BEGINNING_FALLBACK fallback_dp = streetnetwork_path_pool.wait_and_get( requested_orig_obj, pt_origin, dep_mode, fallback_period_extremity, direct_path_type, request=request ) if requested_orig_obj.uri != pt_origin.uri: if pt_origin.uri in accessibles_by_crowfly.odt: pt_journey.sections[0].origin.CopyFrom(requested_orig_obj) elif _is_crowfly_needed( pt_origin.uri, fallback_durations, accessibles_by_crowfly.crowfly, fallback_dp ): crowfly_departure_dt = ( pt_journey.departure_date_time - fallback_durations[pt_origin.uri].duration ) pt_journey.sections.extend( [ _create_crowfly( pt_journey, requested_orig_obj, pt_origin, crowfly_departure_dt, pt_journey.sections[0].begin_date_time, dep_mode, ) ] ) else: # extend the journey with the fallback routing path _extend_journey(pt_journey, fallback_dp, fallback_period_extremity) pt_journey.sections.sort(SectionSorter()) pt_journey.departure_date_time = pt_journey.sections[0].begin_date_time return pt_journeys
def _build_crowflies(pt_journeys, orig, dest): """ Update all journeys in pt_journeys with a crowfly as origin and destination fallback """ if pt_journeys is None: return for pt_journey in pt_journeys.journeys: crowflies = [_build_crowfly(pt_journey, **orig), _build_crowfly(pt_journey, **dest)] # Filter out empty crowflies crowflies = [c for c in crowflies if c] pt_journey.sections.extend(crowflies) pt_journey.sections.sort(key=cmp_to_key(SectionSorter())) pt_journey.departure_date_time = pt_journey.sections[0].begin_date_time pt_journey.arrival_date_time = pt_journey.sections[-1].end_date_time
def _update_fallback_sections(pt_journey, fallback_dp, fallback_period_extremity): """ Replace pt_journey's fallback sections with the given fallback_dp. Note: the replacement is done in place of the pt_journey """ aligned_fallback = _align_fallback_direct_path_datetime( fallback_dp, fallback_period_extremity) fallback_sections = aligned_fallback.journeys[0].sections # update the 'id' which isn't set _rename_fallback_sections_ids(fallback_sections) if fallback_period_extremity.represents_start: section_to_update = pt_journey.sections[-1] else: section_to_update = pt_journey.sections[0] pt_journey.sections.remove(section_to_update) pt_journey.sections.extend(fallback_sections) pt_journey.sections.sort(SectionSorter())
def _update_fallback_sections(journey, fallback_dp, fallback_period_extremity, fallback_type, via_access_point): """ Replace journey's fallback sections with the given fallback_dp. Note: the replacement is done in place of the journey """ aligned_fallback = _align_fallback_direct_path_datetime(fallback_dp, fallback_period_extremity) fallback_sections = aligned_fallback.journeys[0].sections # update the 'id' which isn't set _rename_fallback_sections_ids(fallback_sections) if fallback_type == StreetNetworkPathType.BEGINNING_FALLBACK: section_to_replace = journey.sections[0] else: section_to_replace = journey.sections[-1] journey.sections.remove(section_to_replace) # We have to create the link between the fallback and the pt part manually here if fallback_type == StreetNetworkPathType.BEGINNING_FALLBACK: fallback_sections[-1].destination.CopyFrom(journey.sections[0].origin) else: fallback_sections[0].origin.CopyFrom(journey.sections[-1].destination) if ( isinstance(via_access_point, type_pb2.PtObject) and via_access_point.embedded_type == type_pb2.ACCESS_POINT ): if fallback_type == StreetNetworkPathType.BEGINNING_FALLBACK: fallback_sections[-1].vias.add().CopyFrom(via_access_point.access_point) else: fallback_sections[0].vias.add().CopyFrom(via_access_point.access_point) journey.sections.extend(fallback_sections) journey.sections.sort(key=cmp_to_key(SectionSorter()))