def _retreive_shape(self, json, field): shape = [] decoded_shape = decode_polyline(json.get(field), precision=5) if decoded_shape: shape.extend((type_pb2.GeographicalCoord(lon=c[0], lat=c[1]) for c in decoded_shape)) return shape
def _get_response(cls, json_response, pt_object_origin, pt_object_destination, fallback_extremity): ''' :param fallback_extremity: is a PeriodExtremity (a datetime and it's meaning on the fallback period) ''' # map giving the change of direction (degrees) from geovelo indications # see: http://developers.geovelo.fr/#/documentation/compute map_instructions_direction = { 'HEAD_ON': 0, 'GO_STRAIGHT': 0, 'REACHED_YOUR_DESTINATION': 0, 'REACH_VIA_LOCATION': 0, 'ELEVATOR': 0, 'CROSSING': 0, 'ENTER_ROUND_ABOUT': 90, 'LEAVE_ROUND_ABOUT': 90, 'STAY_ON_ROUND_ABOUT': 0, 'ENTER_AGAINST_ALLOWED_DIRECTION': 0, 'LEAVE_AGAINST_ALLOWED_DIRECTION': 0, 'TURN_SLIGHT_RIGHT': 45, 'TURN_SLIGHT_LEFT': -45, 'TURN_RIGHT': 90, 'TURN_LEFT': -90, 'TURN_SHARP_RIGHT': 135, 'TURN_SHARP_LEFT': -135, 'U-TURN': 180, } resp = response_pb2.Response() resp.status_code = 200 resp.response_type = response_pb2.ITINERARY_FOUND for geovelo_response in json_response: journey = resp.journeys.add() journey.tags.append(cls.get_geovelo_tag(geovelo_response)) journey.duration = int(geovelo_response['duration']) datetime, represents_start_fallback = fallback_extremity if represents_start_fallback: journey.departure_date_time = datetime journey.arrival_date_time = datetime + journey.duration else: journey.departure_date_time = datetime - journey.duration journey.arrival_date_time = datetime journey.durations.total = journey.duration journey.durations.bike = journey.duration journey.distances.bike = int(geovelo_response['distances']['total']) previous_section_endtime = journey.departure_date_time for index, geovelo_section in enumerate(geovelo_response['sections']): section = journey.sections.add() section.type = response_pb2.STREET_NETWORK section.duration = int(geovelo_section['duration']) section.begin_date_time = previous_section_endtime section.end_date_time = section.begin_date_time + section.duration previous_section_endtime = section.end_date_time section.id = 'section_{}'.format(index) section.length = int(geovelo_section['details']['distances']['total']) if index == 0: section.origin.CopyFrom(pt_object_origin) if index == len(geovelo_response['sections']) - 1: section.destination.CopyFrom(pt_object_destination) section.street_network.duration = section.duration section.street_network.length = section.length section.street_network.mode = response_pb2.Bike speed = section.length / section.duration if section.duration != 0 else 0 for geovelo_instruction in itertools.islice( geovelo_section['details']['instructions'], 1, sys.maxsize ): path_item = section.street_network.path_items.add() path_item.name = geovelo_instruction[1] path_item.length = geovelo_instruction[2] path_item.duration = round(path_item.length / speed) if speed != 0 else 0 path_item.direction = map_instructions_direction.get(geovelo_instruction[0], 0) street_info = StreetInformation() street_info.geojson_offset = geovelo_instruction[5] street_info.cycle_path_type = cls.get_geovelo_cycle_path_type(geovelo_instruction[4]) section.street_network.street_information.append(street_info) shape = decode_polyline(geovelo_response['sections'][0]['geometry']) for sh in shape: section.street_network.coordinates.add(lon=sh[0], lat=sh[1]) elevations = geovelo_section.get('details', {}).get('elevations', []) or [] for geovelo_elevation in itertools.islice(elevations, 1, sys.maxsize): elevation = section.street_network.elevations.add() elevation.distance_from_start = geovelo_elevation[0] elevation.elevation = geovelo_elevation[1] elevation.geojson_index = geovelo_elevation[2] return resp
def _get_response(cls, json_response, mode, pt_object_origin, pt_object_destination, fallback_extremity): ''' :param fallback_extremity: is a PeriodExtremity (a datetime and it's meaning on the fallback period) ''' # map giving the change of direction (degrees) from geovelo indications # see: http://developers.geovelo.fr/#/documentation/compute map_instructions_direction = { 'HEAD_ON': 0, 'GO_STRAIGHT': 0, 'REACHED_YOUR_DESTINATION': 0, 'REACH_VIA_LOCATION': 0, 'ELEVATOR': 0, 'CROSSING': 0, 'ENTER_ROUND_ABOUT': 90, 'LEAVE_ROUND_ABOUT': 90, 'STAY_ON_ROUND_ABOUT': 0, 'ENTER_AGAINST_ALLOWED_DIRECTION': 0, 'LEAVE_AGAINST_ALLOWED_DIRECTION': 0, 'TURN_SLIGHT_RIGHT': 45, 'TURN_SLIGHT_LEFT': -45, 'TURN_RIGHT': 90, 'TURN_LEFT': -90, 'TURN_SHARP_RIGHT': 135, 'TURN_SHARP_LEFT': -135, 'U-TURN': 180 } resp = response_pb2.Response() resp.status_code = 200 resp.response_type = response_pb2.ITINERARY_FOUND geovelo_resp = json_response[0] journey = resp.journeys.add() journey.duration = geovelo_resp['duration'] datetime, represents_start_fallback = fallback_extremity if represents_start_fallback: journey.departure_date_time = datetime journey.arrival_date_time = datetime + journey.duration else: journey.departure_date_time = datetime - journey.duration journey.arrival_date_time = datetime journey.durations.total = journey.duration previous_section_endtime = journey.departure_date_time for index, geovelo_section in enumerate(geovelo_resp['sections']): section = journey.sections.add() section.type = response_pb2.STREET_NETWORK section.duration = geovelo_section['duration'] section.begin_date_time = previous_section_endtime section.end_date_time = section.begin_date_time + section.duration previous_section_endtime = section.end_date_time section.id = 'section_{}'.format(index) section.length = int( geovelo_section['details']['distances']['total']) if index == 0: section.origin.CopyFrom(pt_object_origin) if index == len(geovelo_resp['sections']) - 1: section.destination.CopyFrom(pt_object_destination) section.street_network.duration = section.duration section.street_network.length = section.length section.street_network.mode = response_pb2.Bike speed = section.length / section.duration for geovelo_instruction in geovelo_section['details'][ 'instructions'][1:]: path_item = section.street_network.path_items.add() path_item.name = geovelo_instruction[1] path_item.length = geovelo_instruction[2] path_item.duration = round(path_item.length * speed) path_item.direction = map_instructions_direction.get( geovelo_instruction[0], 0) shape = decode_polyline(geovelo_resp['sections'][0]['geometry']) for sh in shape: coord = section.street_network.coordinates.add() coord.lon = sh[0] coord.lat = sh[1] return resp
def _get_response( cls, json_resp, mode, pt_object_origin, pt_object_destination, fallback_extremity, direct_path_type, mode_park_cost=None, ): """ :param fallback_extremity: is a PeriodExtremity (a datetime and it's meaning on the fallback period) """ map_mode = { "walking": response_pb2.Walking, "car": response_pb2.Car, "bike": response_pb2.Bike } resp = response_pb2.Response() resp.status_code = 200 resp.response_type = response_pb2.ITINERARY_FOUND journey = resp.journeys.add() journey.duration = json_resp['trip']['summary']['time'] + ( mode_park_cost or 0) datetime, represents_start_fallback = fallback_extremity offset = 0 # offset if there is a leaving park section if represents_start_fallback: journey.departure_date_time = datetime journey.arrival_date_time = datetime + journey.duration else: journey.departure_date_time = datetime - journey.duration journey.arrival_date_time = datetime beginning_fallback = ( direct_path_type == StreetNetworkPathType.BEGINNING_FALLBACK or direct_path_type == StreetNetworkPathType.DIRECT) if not beginning_fallback and mode_park_cost is not None: # we also add a LEAVE_PARKING section if needed fill_park_section( section=journey.sections.add(), point=pt_object_origin, type=response_pb2.LEAVE_PARKING, begin_dt=journey.departure_date_time, duration=mode_park_cost, ) offset = mode_park_cost journey.durations.total = journey.duration if mode == 'walking': journey.durations.walking = journey.duration previous_section_endtime = journey.departure_date_time + offset for index, leg in enumerate(json_resp['trip']['legs']): section = journey.sections.add() section.type = response_pb2.STREET_NETWORK section.duration = leg['summary']['time'] section.begin_date_time = previous_section_endtime section.end_date_time = section.begin_date_time + section.duration previous_section_endtime = section.end_date_time section.id = 'section_{}'.format(index) section.length = int(kilometers_to_meters( leg['summary']['length'])) if index == 0: section.origin.CopyFrom(pt_object_origin) if index == len(json_resp['trip']['legs']) - 1: section.destination.CopyFrom(pt_object_destination) section.street_network.length = section.length section.street_network.duration = section.duration section.street_network.mode = map_mode[mode] for maneuver in leg['maneuvers']: path_item = section.street_network.path_items.add() if 'street_names' in maneuver and len( maneuver['street_names']) > 0: path_item.name = maneuver['street_names'][0] path_item.length = kilometers_to_meters(maneuver['length']) path_item.duration = maneuver['time'] # TODO: calculate direction path_item.direction = 0 shape = decode_polyline(leg['shape']) for sh in shape: coord = section.street_network.coordinates.add() coord.lon = sh[0] coord.lat = sh[1] if beginning_fallback and mode_park_cost is not None: fill_park_section( section=journey.sections.add(), point=pt_object_destination, type=response_pb2.PARK, begin_dt=previous_section_endtime, duration=mode_park_cost, ) return resp
def _make_response(self, raw_json): raw_journeys = raw_json.get('journeys') if not raw_journeys: return [] ridesharing_journeys = [] for j in self._get_ridesharing_journeys(raw_journeys): for p in j.get('paths'): if p.get('mode') != 'RIDESHARINGAD': continue res = rsj.RidesharingJourney() res.metadata = self.journey_metadata res.distance = j.get('distance') res.ridesharing_ad = j.get('url') ridesharing_ad = p['rideSharingAd'] from_data = p['from'] res.pickup_place = rsj.Place(addr=from_data.get('name'), lat=from_data.get('lat'), lon=from_data.get('lon')) to_data = p['to'] res.dropoff_place = rsj.Place(addr=to_data.get('name'), lat=to_data.get('lat'), lon=to_data.get('lon')) # shape is a list of type_pb2.GeographicalCoord() res.shape = [] shape = decode_polyline(p.get('shape'), precision=5) if not shape or res.pickup_place.lon != shape[0][ 0] or res.pickup_place.lat != shape[0][1]: coord = type_pb2.GeographicalCoord() coord.lon = res.pickup_place.lon coord.lat = res.pickup_place.lat res.shape.append(coord) for c in shape: coord = type_pb2.GeographicalCoord() coord.lon = c[0] coord.lat = c[1] res.shape.append(coord) if not shape or res.dropoff_place.lon != shape[0][ 0] or res.dropoff_place.lat != shape[0][1]: coord = type_pb2.GeographicalCoord() coord.lon = res.dropoff_place.lon coord.lat = res.dropoff_place.lat res.shape.append(coord) res.pickup_date_time = utils.make_timestamp_from_str( p['departureDate']) res.dropoff_date_time = utils.make_timestamp_from_str( p['arrivalDate']) user = ridesharing_ad['user'] gender = user.get('gender') gender_map = { 'MALE': rsj.Gender.MALE, 'FEMALE': rsj.Gender.FEMALE } res.driver = rsj.Individual( alias=user.get('alias'), gender=gender_map.get(gender, rsj.Gender.UNKNOWN), image=user.get('imageUrl'), rate=user.get('rating', {}).get('rate'), rate_count=user.get('rating', {}).get('count')) # the usual form of the price for InstantSystem is: "170 EUR" # which means "170 EURO cents" also equals "1.70 EURO" # In Navitia so far prices are in "centime" so we transform it to: "170 centime" price = ridesharing_ad['price'] res.price = price.get('amount') if price.get('currency') == "EUR": res.currency = "centime" else: res.currency = price.get('currency') res.available_seats = ridesharing_ad['vehicle'][ 'availableSeats'] res.total_seats = None ridesharing_journeys.append(res) return ridesharing_journeys
def _get_response(cls, json_resp, mode, pt_object_origin, pt_object_destination, fallback_extremity): ''' :param fallback_extremity: is a PeriodExtremity (a datetime and it's meaning on the fallback period) ''' map_mode = { "walking": response_pb2.Walking, "car": response_pb2.Car, "bike": response_pb2.Bike } resp = response_pb2.Response() resp.status_code = 200 resp.response_type = response_pb2.ITINERARY_FOUND journey = resp.journeys.add() journey.duration = json_resp['trip']['summary']['time'] datetime, represents_start_fallback = fallback_extremity if represents_start_fallback: journey.departure_date_time = datetime journey.arrival_date_time = datetime + journey.duration else: journey.departure_date_time = datetime - journey.duration journey.arrival_date_time = datetime journey.durations.total = journey.duration if mode == 'walking': journey.durations.walking = journey.duration previous_section_endtime = journey.departure_date_time for index, leg in enumerate(json_resp['trip']['legs']): section = journey.sections.add() section.type = response_pb2.STREET_NETWORK section.duration = leg['summary']['time'] section.begin_date_time = previous_section_endtime section.end_date_time = section.begin_date_time + section.duration previous_section_endtime = section.end_date_time section.id = 'section_{}'.format(index) section.length = int(kilometers_to_meters( leg['summary']['length'])) if index == 0: section.origin.CopyFrom(pt_object_origin) if index == len(json_resp['trip']['legs']) - 1: section.destination.CopyFrom(pt_object_destination) section.street_network.length = section.length section.street_network.duration = section.duration section.street_network.mode = map_mode[mode] for maneuver in leg['maneuvers']: path_item = section.street_network.path_items.add() if 'street_names' in maneuver and len( maneuver['street_names']) > 0: path_item.name = maneuver['street_names'][0] path_item.length = kilometers_to_meters(maneuver['length']) path_item.duration = maneuver['time'] # TODO: calculate direction path_item.direction = 0 shape = decode_polyline(leg['shape']) for sh in shape: coord = section.street_network.coordinates.add() coord.lon = sh[0] coord.lat = sh[1] return resp