def _vehicle_journey_position(self, vehicle_journey_position, response_json): if not response_json: logging.getLogger(__name__).debug( "Vehicle position not found for vehicle_journey {}".format( vehicle_journey_position.vehicle_journey.uri)) return if len(response_json) >= 2: logging.getLogger(__name__).warning( "Multiple vehicles positions for vehicle_journey {}".format( vehicle_journey_position.vehicle_journey.uri)) if response_json: first_vehicle_position = response_json[0] vehicle_journey_position.coord.lon = first_vehicle_position.get( "longitude") vehicle_journey_position.coord.lat = first_vehicle_position.get( "latitude") vehicle_journey_position.bearing = first_vehicle_position.get( "bearing") vehicle_journey_position.speed = first_vehicle_position.get( "speed") vehicle_journey_position.data_freshness = type_pb2.REALTIME occupancy = first_vehicle_position.get("occupancy", None) if occupancy: vehicle_journey_position.occupancy = occupancy feed_created_at = first_vehicle_position.get( "feed_created_at", None) if feed_created_at: vehicle_journey_position.feed_created_at = utils.make_timestamp_from_str( feed_created_at)
def _make_response(self, raw_json, from_coord, to_coord, instance_params): 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'] # departure coord lat, lon = from_coord.split(',') departure_coord = Coords(lat=lat, lon=lon) # pick up coord from_data = p['from'] pickup_lat = from_data.get('lat') pickup_lon = from_data.get('lon') pickup_coord = Coords(lat=pickup_lat, lon=pickup_lon) res.pickup_place = rsj.Place(addr=from_data.get('name'), lat=pickup_lat, lon=pickup_lon) res.origin_pickup_distance = int( jormungandr.street_network.utils.crowfly_distance_between( departure_coord, pickup_coord)) # we choose to calculate with speed=1.12 the average speed for a walker res.origin_pickup_duration = jormungandr.street_network.utils.get_manhattan_duration( res.origin_pickup_distance, instance_params.walking_speed) res.origin_pickup_shape = None # Not specified # drop off coord to_data = p['to'] dropoff_lat = to_data.get('lat') dropoff_lon = to_data.get('lon') dropoff_coord = Coords(lat=dropoff_lat, lon=dropoff_lon) res.dropoff_place = rsj.Place(addr=to_data.get('name'), lat=dropoff_lat, lon=dropoff_lon) # arrival coord lat, lon = to_coord.split(',') arrival_coord = Coords(lat=lat, lon=lon) res.dropoff_dest_distance = int( jormungandr.street_network.utils.crowfly_distance_between( dropoff_coord, arrival_coord)) # we choose to calculate with speed=1.12 the average speed for a walker res.dropoff_dest_duration = jormungandr.street_network.utils.get_manhattan_duration( res.dropoff_dest_distance, instance_params.walking_speed) res.dropoff_dest_shape = None # Not specified res.shape = self._retreive_main_shape(p, 'shape', res.pickup_place, res.dropoff_place) res.pickup_date_time = make_timestamp_from_str( p['departureDate']) res.departure_date_time = res.pickup_date_time - res.origin_pickup_duration res.dropoff_date_time = make_timestamp_from_str( p['arrivalDate']) res.arrival_date_time = res.dropoff_date_time + res.dropoff_dest_duration res.duration = res.dropoff_date_time - res.pickup_date_time 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 _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