def ORS_query_directions(query, profile='driving-car', toll_price=True, _id=0, geometry=True): ''' start (class point) end (class point) profile= ["driving-car", "driving-hgv", "foot-walking","foot-hiking", "cycling-regular", "cycling-road","cycling-mountain", "cycling-electric",] ''' ORS_client = start_ORS_client() coord = [query.start_point[::-1], query.end_point[::-1]] # WARNING it seems that [lon,lat] are not in the same order than for other API. try: ORS_step = ORS_client.directions( coord, profile=profile, instructions=False, geometry=geometry, options={'avoid_features': ['ferries']}, ) except: return None geojson = convert.decode_polyline(ORS_step['routes'][0]['geometry']) local_distance = ORS_step['routes'][0]['summary']['distance'] local_emissions = co2_emissions.calculate_co2_emissions(constants.TYPE_COACH, constants.DEFAULT_CITY, constants.DEFAULT_FUEL, constants.DEFAULT_NB_SEATS, constants.DEFAULT_NB_KM) * \ constants.DEFAULT_NB_PASSENGERS * local_distance step = tmw.Journey_step(_id, _type=ORS_profile(profile), label=profile, distance_m=local_distance, duration_s=ORS_step['routes'][0]['summary']['duration'], price_EUR=[ORS_gas_price(ORS_step['routes'][0]['summary']['distance'])], gCO2=local_emissions, geojson=geojson, departure_date=query.departure_date ) # Correct arrival_date based on departure_date step.arrival_date = (step.departure_date + timedelta(seconds=step.duration_s)) # Add toll price (optional) step = ORS_add_toll_price(step) if toll_price else step ors_journey = tmw.Journey(0, departure_date=query.departure_date, arrival_date=step.arrival_date, steps=[step]) # Add category category_journey = list() for step in ors_journey.steps: if step.type not in [constants.TYPE_TRANSFER, constants.TYPE_WAIT]: category_journey.append(step.type) ors_journey.category = list(set(category_journey)) ors_journey.update() ors_journey.arrival_date = ors_journey.departure_date + timedelta(seconds=ors_journey.total_duration) return ors_journey
def create_fake_plane_journey(locations, airport_dep, airport_arrival): """ We create a fake plane journey with only the approximate eqCO2 to be used in the computation in the front end :param query: :return: fake_journey """ geoloc_dep = locations['departure'][locations['departure'].city_sky == airport_dep].sample().geoloc geoloc_arrival = locations['arrival'][locations['arrival'].city_sky == airport_arrival].sample().geoloc distance_m = distance(geoloc_dep, geoloc_arrival).m local_range_km = get_range_km(distance_m) local_emissions = calculate_co2_emissions(constants.TYPE_PLANE, constants.DEFAULT_CITY, constants.DEFAULT_FUEL, constants.NB_SEATS_TEST, local_range_km) * \ constants.DEFAULT_NB_PASSENGERS * distance_m fake_journey_list = list() fake_journey_step = tmw.Journey_step( 0, _type=constants.TYPE_PLANE, label= f'Arrive at the airport {format_timespan(_AIRPORT_WAITING_PERIOD)} before departure', distance_m=0, duration_s=_AIRPORT_WAITING_PERIOD, price_EUR=[0], gCO2=local_emissions, departure_point=geoloc_dep, arrival_point=geoloc_arrival, departure_date=dt.now(), arrival_date=dt.now(), geojson=[], ) fake_journey_list.append(fake_journey_step) fake_journey = tmw.Journey(0, steps=fake_journey_list, departure_date=fake_journey_step.departure_date, arrival_date=fake_journey_step.arrival_date) fake_journey.total_gCO2 = local_emissions fake_journey.is_real_journey = False return fake_journey
def navitia_journeys(json, _id=0): # all journeys loop lst_journeys = list() try: journeys = json['journeys'] except: logger.warning('ERROR {}'.format(json['error'])) return None for j in json['journeys']: i = _id # journey loop lst_sections = list() for section in j['sections']: try: lst_sections.append(navitia_journeys_sections_type(section, _id=i)) except: logger.warning('Navitia ERROR : ') logger.warning('id: {}'.format(i)) logger.warning(section) i = i + 1 lst_journeys.append(tmw.Journey(_id, steps=lst_sections)) return lst_journeys
def create_plane_journey_from_flightradar_data(airports, departure_date): """ We create a fake plane journey with only the approximate eqCO2 to be used in the computation in the front end :param query: :return: fake_journey """ day_of_week = departure_date.weekday() hour_of_day = departure_date.hour relevant_flights = _FLIGHTRADAR_DATA[ _FLIGHTRADAR_DATA.city_sky.isin(airports['departure']) & _FLIGHTRADAR_DATA.city_sky_arr.isin(airports['arrival'])] relevant_flights = relevant_flights[relevant_flights.day_of_week == day_of_week] relevant_flights['hour_dep'] = relevant_flights.apply( lambda x: dt.strptime(x.hour_dep, '%H:%M:%S') + timedelta(hours=1), axis=1) relevant_flights['hour_dep_int'] = relevant_flights.apply( lambda x: x.hour_dep.hour, axis=1) response_flights = pd.DataFrame() for airport_dep in airports['departure']: for airport_arr in airports['arrival']: flights_df = relevant_flights[ (relevant_flights.city_sky == airport_dep) & (relevant_flights.city_sky_arr == airport_arr) & (relevant_flights.hour_dep_int >= hour_of_day)] response_flights = response_flights.append(flights_df) # distance_m = distance(geoloc_dep, geoloc_arrival).m response_flights['local_range_km'] = response_flights.apply( lambda x: get_range_km(x.distance_m), axis=1) response_flights['local_emissions'] = response_flights.apply( lambda x: calculate_co2_emissions( constants.TYPE_PLANE, constants.DEFAULT_CITY, constants. DEFAULT_FUEL, constants.NB_SEATS_TEST, x.local_range_km) * constants.DEFAULT_NB_PASSENGERS * x.distance_m, axis=1) # merge global departure date and flight time to create flight actual departure datetime response_flights['flight_departure_date'] = response_flights.apply( lambda x: dt.combine(departure_date, dt_time(x.hour_dep.hour, x.hour_dep.minute)), axis=1) response_flights['flight_arrival_date'] = response_flights.apply( lambda x: x.flight_departure_date + timedelta(seconds=x.flight_time_s), axis=1) journey_list = list() for index, flight in response_flights.iterrows(): lst_sections = list() # We add a waiting period at the airport of x hours step = tmw.Journey_step( 0, _type=constants.TYPE_WAIT, label= f'Arrive at the airport {format_timespan(_AIRPORT_WAITING_PERIOD)} before departure', distance_m=0, duration_s=_AIRPORT_WAITING_PERIOD, price_EUR=[], gCO2=0, departure_point=[flight.latitude, flight.longitude], arrival_point=[flight.latitude, flight.longitude], departure_date=flight.flight_departure_date - timedelta(seconds=_AIRPORT_WAITING_PERIOD), arrival_date=flight.flight_departure_date, geojson=[], ) lst_sections.append(step) step = tmw.Journey_step( 1, _type=constants.TYPE_PLANE, label=f'Flight {flight.flight_number} to {flight.airport_to_code}', distance_m=flight.distance_m, duration_s=flight.flight_time_s, price_EUR=[], gCO2=flight.local_emissions, departure_point=[flight.latitude, flight.longitude], arrival_point=[flight.latitude_arr, flight.longitude_arr], departure_stop_name=flight.airport_from, arrival_stop_name=flight.airport_to_code, departure_date=flight.flight_departure_date, arrival_date=flight.flight_arrival_date, trip_code=flight.flight_number, geojson=[], ) lst_sections.append(step) departure_date_formated = dt.strptime( str(lst_sections[0].departure_date)[0:10], '%Y-%m-%d') departure_date_formated = str(departure_date_formated.year)[2:4]+\ ('0'+str(departure_date_formated.month))[-2:]+\ ('0'+str(departure_date_formated.day))[-2:] journey_flightradar = tmw.Journey( 0, steps=lst_sections, departure_date=lst_sections[0].departure_date, arrival_date=lst_sections[-1].arrival_date, booking_link= f'https://www.skyscanner.fr/transport/vols/{flight.airport_from}/{flight.airport_to_code}/{departure_date_formated}/' ) journey_flightradar.category = [constants.TYPE_PLANE] journey_flightradar.update() journey_flightradar.is_real_journey = False journey_list.append(journey_flightradar) return journey_list
def skyscanner_journeys(df_response, _id=0): """ This function takes in a dataframe with detailled information on the plane journeys returned by Skyscanner API and returns a list containing one TMW journey object for each of those plane journey """ # affect a price to each leg df_response[ 'price_step'] = df_response.PriceTotal_AR / df_response.nb_segments df_response['DepartureDateTime'] = pd.to_datetime( df_response['DepartureDateTime']) df_response['ArrivalDateTime'] = pd.to_datetime( df_response['ArrivalDateTime']) # Compute distance for each leg df_response['distance_step'] = df_response.apply( lambda x: distance(x.geoloc_origin_seg, x.geoloc_destination_seg).m, axis=1) lst_journeys = list() # all itineraries : for itinerary_id in df_response.itinerary_id.unique(): itinerary = df_response[df_response.itinerary_id == itinerary_id].reset_index(drop=True) i = _id # boolean to know whether and when there will be a transfer after the leg itinerary['next_departure'] = itinerary.DepartureDateTime.shift(-1) itinerary['next_stop_name'] = itinerary.Name_origin_seg.shift(-1) itinerary['next_geoloc'] = itinerary.geoloc_origin_seg.shift(-1) # get the slugs to create the booking link departure_slug = itinerary.departure_slug.unique()[0].lower()[0:4] arrival_slug = itinerary.arrival_slug.unique()[0].lower()[0:4] lst_sections = list() # We add a waiting period at the airport of x hours step = tmw.Journey_step( i, _type=constants.TYPE_WAIT, label= f'Arrive at the airport {format_timespan(_AIRPORT_WAITING_PERIOD)} before departure', distance_m=0, duration_s=_AIRPORT_WAITING_PERIOD, price_EUR=[0], gCO2=0, departure_point=itinerary.geoloc.iloc[0], arrival_point=itinerary.geoloc.iloc[0], departure_date=itinerary.DepartureDateTime[0] - timedelta(seconds=_AIRPORT_WAITING_PERIOD), arrival_date=itinerary.DepartureDateTime[0], geojson=[], ) lst_sections.append(step) i = i + 1 for index, leg in itinerary.sort_values( by='DepartureDateTime').iterrows(): local_distance_m = leg.distance_step local_range_km = get_range_km(local_distance_m) local_emissions = calculate_co2_emissions(constants.TYPE_PLANE, constants.DEFAULT_CITY, constants.DEFAULT_FUEL, constants.NB_SEATS_TEST, local_range_km) * \ constants.DEFAULT_NB_PASSENGERS * local_distance_m step = tmw.Journey_step( i, _type=constants.TYPE_PLANE, label=f'Flight {leg.FlightNumber_rich} to {leg.Name}', distance_m=leg.distance_step, duration_s=leg.Duration_seg * 60, price_EUR=[leg.price_step], gCO2=local_emissions, departure_point=leg.geoloc_origin_seg, arrival_point=leg.geoloc_destination_seg, departure_stop_name=leg.Name_origin_seg, arrival_stop_name=leg.Name, departure_date=leg.DepartureDateTime, arrival_date=leg.ArrivalDateTime, trip_code=leg.FlightNumber_rich, geojson=[], ) lst_sections.append(step) i = i + 1 # add transfer steps if not pd.isna(leg.next_departure): #duration = dt.strptime(leg['next_departure'], '%Y-%m-%dT%H:%M:%S') - \ # dt.strptime(leg['ArrivalDateTime'], '%Y-%m-%dT%H:%M:%S') step = tmw.Journey_step( i, _type=constants.TYPE_TRANSFER, label=f'Transfer at {leg.Name}', distance_m=0, duration_s=(leg.next_departure - leg.ArrivalDateTime).seconds, price_EUR=[0], departure_point=leg.geoloc_destination_seg, arrival_point=leg.next_geoloc, departure_date=leg.ArrivalDateTime, arrival_date=leg.next_departure, departure_stop_name=leg.Name, arrival_stop_name=leg.next_stop_name, gCO2=0, geojson=[], ) lst_sections.append(step) i = i + 1 departure_date_formated = dt.strptime( str(lst_sections[0].departure_date)[0:10], '%Y-%m-%d') departure_date_formated = str(departure_date_formated.year)[2:4]+\ ('0'+str(departure_date_formated.month))[-2:]+\ ('0'+str(departure_date_formated.day))[-2:] journey_sky = tmw.Journey( _id, steps=lst_sections, departure_date=lst_sections[0].departure_date, arrival_date=lst_sections[-1].arrival_date, booking_link= f'https://www.skyscanner.fr/transport/vols/{departure_slug}/{arrival_slug}/{departure_date_formated}/' ) # journey_sky = tmw.Journey(_id, steps=lst_sections) # Add category category_journey = list() for step in journey_sky.steps: if step.type not in [constants.TYPE_TRANSFER, constants.TYPE_WAIT]: category_journey.append(step.type) journey_sky.category = list(set(category_journey)) lst_journeys.append(journey_sky) for journey in lst_journeys: journey.update() return lst_journeys
def ouibus_journeys(df_response, _id=0): """ This function takes in a DF with detailled info about all the OuiBus trips It returns a list of TMW journey objects """ # affect a price to each leg df_response['price_step'] = df_response.price_cents / ( df_response.nb_segments * 100) # Compute distance for each leg # print(df_response.columns) df_response['distance_step'] = df_response.apply( lambda x: distance(x.geoloc_origin_seg, x.geoloc_destination_seg).m, axis=1) lst_journeys = list() # all itineraries : # logger.info(f'nb itinerary : {df_response.id.nunique()}') for itinerary_id in df_response.id.unique(): itinerary = df_response[df_response.id == itinerary_id].reset_index( drop=True) # boolean to know whether and when there will be a transfer after the leg itinerary['next_departure'] = itinerary.departure_seg.shift(-1) itinerary['next_stop_name'] = itinerary.short_name_origin_seg.shift(1) itinerary['next_geoloc'] = itinerary.geoloc_origin_seg.shift(-1) # get the slugs to create the booking link origin_slug = itinerary.origin_slug.unique()[0] destination_slug = itinerary.destination_slug.unique()[0] i = _id lst_sections = list() # We add a waiting period at the station of 15 minutes step = tmw.Journey_step( i, _type=constants.TYPE_WAIT, label= f'Arrive at the station {format_timespan(_STATION_WAITING_PERIOD)} before departure', distance_m=0, duration_s=_STATION_WAITING_PERIOD, price_EUR=[0], gCO2=0, departure_point=itinerary.geoloc.iloc[0], arrival_point=itinerary.geoloc.iloc[0], departure_date=itinerary.departure_seg[0] - timedelta(seconds=_STATION_WAITING_PERIOD), arrival_date=itinerary.departure_seg[0], geojson=[], ) lst_sections.append(step) i = i + 1 for index, leg in itinerary.iterrows(): local_distance_m = leg.distance_step local_emissions = calculate_co2_emissions(constants.TYPE_COACH, constants.DEFAULT_CITY, constants.DEFAULT_FUEL, constants.DEFAULT_NB_SEATS, constants.DEFAULT_NB_KM) *\ constants.DEFAULT_NB_PASSENGERS*local_distance_m step = tmw.Journey_step( i, _type=constants.TYPE_COACH, label= f'Coach OuiBus {leg.bus_number} to {leg.short_name_destination_seg}', distance_m=local_distance_m, duration_s=(leg.arrival_seg - leg.departure_seg).seconds, price_EUR=[leg.price_step], gCO2=local_emissions, departure_point=leg.geoloc_origin_seg, arrival_point=leg.geoloc_destination_seg, departure_stop_name=leg.short_name_origin_seg, arrival_stop_name=leg.short_name_destination_seg, departure_date=leg.departure_seg, arrival_date=leg.arrival_seg, trip_code='OuiBus ' + leg.bus_number, geojson=[], ) lst_sections.append(step) i = i + 1 # add transfer steps if not pd.isna(leg.next_departure): step = tmw.Journey_step( i, _type=constants.TYPE_TRANSFER, label=f'Transfer at {leg.short_name_destination_seg}', distance_m=distance(leg.geoloc_destination_seg, leg.next_geoloc).m, duration_s=(leg['next_departure'] - leg['arrival_seg']).seconds, price_EUR=[0], departure_point=leg.geoloc_destination_seg, arrival_point=leg.next_geoloc, departure_stop_name=leg.short_name_destination_seg, arrival_stop_name=leg.next_stop_name, gCO2=0, geojson=[], ) lst_sections.append(step) i = i + 1 departure_date_formated = dt.strptime( str(lst_sections[0].departure_date)[0:15], '%Y-%m-%d %H:%M').strftime('%Y-%m-%d %H:00') journey_ouibus = tmw.Journey( _id, steps=lst_sections, booking_link= f'https://fr.ouibus.com/recherche?origin={origin_slug}&destination={destination_slug}&outboundDate={departure_date_formated}' ) # Add category category_journey = list() for step in journey_ouibus.steps: if step.type not in [constants.TYPE_TRANSFER, constants.TYPE_WAIT]: category_journey.append(step.type) journey_ouibus.category = list(set(category_journey)) lst_journeys.append(journey_ouibus) # for journey in lst_journeys: # journey.update() return lst_journeys
def trainline_journeys(df_response, _id=0): """ This function takes in a DF with detailled info about all the Trainline trips It returns a list of TMW journey objects """ # affect a price to each leg (otherwise we would multiply the price by the number of legs df_response['price_step'] = df_response.cents / (df_response.nb_segments*100) # Compute distance for each leg # print(df_response.columns) df_response['distance_step'] = df_response.apply(lambda x: distance(x.geoloc_depart_seg, x.geoloc_arrival_seg).m, axis=1) df_response['trip_code'] = df_response.train_name + ' ' + df_response.train_number tranportation_mean_to_type = { 'coach': constants.TYPE_COACH, 'train': constants.TYPE_TRAIN, } lst_journeys = list() # all itineraries : # print(f'nb itinerary : {df_response.id_global.nunique()}') for itinerary_id in df_response.id_global.unique(): itinerary = df_response[df_response.id_global == itinerary_id].reset_index(drop=True) # boolean to know whether and when there will be a transfer after the leg itinerary['next_departure'] = itinerary.departure_date_seg.shift(-1) itinerary['next_stop_name'] = itinerary.name_depart_seg.shift(-1) itinerary['next_geoloc'] = itinerary.geoloc_depart_seg.shift(-1) itinerary['trip_code'] = itinerary.trip_code.fillna('') # get the slugs to create the booking link origin_slug = itinerary.origin_slug.unique()[0] destination_slug = itinerary.destination_slug.unique()[0] i = _id lst_sections = list() # We add a waiting period at the station of 15 minutes step = tmw.Journey_step(i, _type=constants.TYPE_WAIT, label=f'Arrive at the station {format_timespan(_STATION_WAITING_PERIOD)} before departure', distance_m=0, duration_s=_STATION_WAITING_PERIOD, price_EUR=[0], gCO2=0, departure_point=[itinerary.latitude.iloc[0], itinerary.longitude.iloc[0]], arrival_point=[itinerary.latitude.iloc[0], itinerary.longitude.iloc[0]], departure_date=itinerary.departure_date_seg[0] - timedelta(seconds=_STATION_WAITING_PERIOD), arrival_date=itinerary.departure_date_seg[0], bike_friendly=True, geojson=[], ) lst_sections.append(step) i = i + 1 # Go through all steps of the journey for index, leg in itinerary.iterrows(): local_distance_m = distance(leg.geoloc_depart_seg, leg.geoloc_arrival_seg).m local_transportation_type = tranportation_mean_to_type[leg.transportation_mean] local_emissions = co2_emissions.calculate_co2_emissions(local_transportation_type, constants.DEFAULT_CITY, constants.DEFAULT_FUEL, constants.DEFAULT_NB_SEATS, constants.DEFAULT_NB_KM) * \ constants.DEFAULT_NB_PASSENGERS * local_distance_m step = tmw.Journey_step(i, _type=local_transportation_type, label=f'{leg.trip_code} to {leg.name_arrival_seg}', distance_m=local_distance_m, duration_s=(leg.arrival_date_seg - leg.departure_date_seg).seconds, price_EUR=[leg.price_step], gCO2=local_emissions, departure_point=leg.geoloc_depart_seg, arrival_point=leg.geoloc_arrival_seg, departure_stop_name=leg.name_depart_seg, arrival_stop_name=leg.name_arrival_seg, departure_date=leg.departure_date_seg, arrival_date=leg.arrival_date_seg, trip_code=leg.trip_code, bike_friendly='bicycle' in leg.bike_friendliness, geojson=[], ) lst_sections.append(step) i = i + 1 # add transfer steps if not pd.isna(leg.next_departure): step = tmw.Journey_step(i, _type=constants.TYPE_TRANSFER, label=f'Transfer at {leg.name_arrival_seg}', distance_m=0, duration_s=(leg['next_departure'] - leg['arrival_date_seg']).seconds, price_EUR=[0], departure_point=leg.geoloc_arrival_seg, arrival_point=leg.next_geoloc, departure_stop_name=leg.name_depart_seg, arrival_stop_name=leg.name_arrival_seg, departure_date=leg.arrival_date_seg, arrival_date=leg.next_departure, gCO2=0, bike_friendly=True, geojson=[], ) lst_sections.append(step) i = i + 1 departure_date_formated = dt.strptime(str(lst_sections[0].departure_date)[0:15], '%Y-%m-%d %H:%M').strftime('%Y-%m-%d %H:00') journey_train = tmw.Journey(_id, steps=lst_sections, departure_date= lst_sections[0].departure_date, arrival_date= lst_sections[-1].arrival_date, booking_link=f'https://www.trainline.fr/search/{origin_slug}/{destination_slug}/{departure_date_formated}') # Add category category_journey = list() for step in journey_train.steps: if step.type not in [constants.TYPE_TRANSFER, constants.TYPE_WAIT]: category_journey.append(step.type) journey_train.category = list(set(category_journey)) lst_journeys.append(journey_train) # for journey in lst_journeys: # journey.update() return lst_journeys
def blablacar_journey(df_response, departure_date, start_point, end_point): """ This function takes in a DF with detailled info about all the BlaBlaCar trips It returns a list of TMW journey objects """ lst_journeys = list() # all itineraries : # print(f'nb itinerary : {df_response.id_global.nunique()}') _id = 0 for trip_id in df_response.trip_id.unique(): itinerary = df_response[df_response.trip_id == trip_id] # Get the arrival info on the same line itinerary['date_time_arrival'] = itinerary.date_time.shift(-1) itinerary['city_arrival'] = itinerary.city.shift(-1) itinerary['address_arrival'] = itinerary.address.shift(-1) itinerary['latitude_arrival'] = itinerary.latitude.shift(-1) itinerary['longitude_arrival'] = itinerary.longitude.shift(-1) # boolean to know whether and when there will be a transfer after the leg itinerary['next_departure'] = itinerary.date_time.shift(1) # Get rid of the "last line" for the last leg of the blablacar trip itinerary = itinerary[~pd.isna(itinerary.city_arrival)] # Divide price between legs weighted by distance and distance itinerary['total_distance'] = itinerary.distance_in_meters.sum() itinerary['price'] = float(itinerary['price']) itinerary['price_leg'] = itinerary.apply( lambda x: x.distance_in_meters / x.total_distance * x.price, axis=1) i = _id lst_sections = list() # We add a waiting period at the pick up point of 15 minutes #print(itinerary.date_time.get_values()) #print(type(itinerary.date_time.get_value(0))) #print(type(timedelta(seconds=_BLABLACAR_WAITING_PERIOD))) #print(itinerary.date_time.get_value(0)-timedelta(seconds=_BLABLACAR_WAITING_PERIOD)) step = tmw.Journey_step( i, _type=constants.TYPE_WAIT, label= f'Arrive at pick up point {format_timespan(_BLABLACAR_WAITING_PERIOD)} before departure', distance_m=0, duration_s=_BLABLACAR_WAITING_PERIOD, price_EUR=[0], gCO2=0, departure_point=[ itinerary.latitude.iloc[0], itinerary.longitude.iloc[0] ], arrival_point=[ itinerary.latitude.iloc[0], itinerary.longitude.iloc[0] ], departure_date=itinerary.date_time.iat[0] - timedelta(seconds=_BLABLACAR_WAITING_PERIOD), arrival_date=itinerary.date_time.iat[0], bike_friendly=True, geojson=[], ) lst_sections.append(step) i = i + 1 # Go through all steps of the journey for index, leg in itinerary.iterrows(): local_distance_m = leg.distance_in_meters local_transportation_type = constants.TYPE_CAR local_emissions = co2_emissions.calculate_co2_emissions(local_transportation_type, constants.DEFAULT_CITY, constants.DEFAULT_FUEL, constants.DEFAULT_NB_SEATS, constants.DEFAULT_NB_KM) * \ constants.DEFAULT_NB_PASSENGERS * local_distance_m step = tmw.Journey_step( i, _type=constants.TYPE_CARPOOOLING, label=f'BlablaCar trip from {leg.city} to {leg.city_arrival}', distance_m=local_distance_m, duration_s=leg.duration_in_seconds, price_EUR=[leg.price_leg], gCO2=local_emissions, departure_point=[leg.latitude, leg.longitude], arrival_point=[leg.latitude_arrival, leg.longitude_arrival], departure_stop_name=leg.address + ' ' + leg.city, arrival_stop_name=leg.address_arrival + ' ' + leg.city_arrival, departure_date=leg.date_time, arrival_date=leg.date_time_arrival, trip_code='BlaBlaCar_' + str(leg.trip_id), bike_friendly=False, geojson=[], ) lst_sections.append(step) i = i + 1 # add transfer steps if not pd.isna(leg.next_departure): step = tmw.Journey_step( i, _type=constants.TYPE_TRANSFER, label=f'Transfer at {leg.name_arrival_seg}', distance_m=0, duration_s=(leg['next_departure'] - leg['arrival_date_seg']).seconds, price_EUR=[0], departure_point=[ leg.latitude_arrival, leg.longitude_arrival ], arrival_point=[ leg.latitude_arrival, leg.longitude_arrival ], departure_stop_name=leg.address_arrival + ' ' + leg.city_arrival, arrival_stop_name=leg.address_arrival + ' ' + leg.city_arrival, departure_date=leg.date_time_arrival, arrival_date=leg.next_departure, gCO2=0, bike_friendly=False, geojson=[], ) lst_sections.append(step) i = i + 1 journey_blablacar = tmw.Journey( _id, steps=lst_sections, departure_date=lst_sections[0].departure_date, arrival_date=lst_sections[-1].arrival_date, booking_link=leg.link) # Add category category_journey = list() for step in journey_blablacar.steps: if step.type not in [constants.TYPE_TRANSFER, constants.TYPE_WAIT]: category_journey.append(step.type) journey_blablacar.category = list(set(category_journey)) lst_journeys.append(journey_blablacar) # for journey in lst_journeys: # journey.update() return lst_journeys
def get_ferries(date_departure, date_return, departure_point, arrival_point): """ We create a ferry journey based on the ferry database we scraped """ # Find relevant ports port_deps, port_arrs = get_ports_from_geo_locs(departure_point, arrival_point) # Find journeys journeys = _FERRY_DATA[ (_FERRY_DATA.port_dep.isin(port_deps.port_clean.unique())) & _FERRY_DATA.port_arr.isin(port_arrs.port_clean.unique())] journeys['date_dep'] = pd.to_datetime(journeys.date_dep) journeys = journeys[journeys.date_dep > date_departure] if len(journeys) == 0: logger.info(f'No ferry journey was found') return None journey_list = list() for index, row in journeys.iterrows(): distance_m = row.distance_m local_emissions = calculate_co2_emissions(constants.TYPE_PLANE, constants.DEFAULT_CITY, constants.DEFAULT_FUEL, constants.NB_SEATS_TEST, constants.DEFAULT_NB_KM) * \ constants.DEFAULT_NB_PASSENGERS * distance_m journey_steps = list() journey_step = tmw.Journey_step( 0, _type=constants.TYPE_WAIT, label= f'Arrive at the port {format_timespan(_PORT_WAITING_PERIOD)} before departure', distance_m=0, duration_s=_PORT_WAITING_PERIOD, price_EUR=[0], gCO2=0, departure_point=[row.lat_clean_dep, row.long_clean_dep], arrival_point=[row.lat_clean_dep, row.long_clean_dep], departure_date=row.date_dep - timedelta(seconds=_PORT_WAITING_PERIOD), arrival_date=row.date_dep, geojson=[], ) journey_steps.append(journey_step) journey_step = tmw.Journey_step( 1, _type=constants.TYPE_FERRY, label=f'Sail Ferry from {row.port_dep} to {row.port_arr}', distance_m=distance_m, duration_s=(row.date_arr - row.date_dep).seconds, price_EUR=[row.price_clean_ar_eur / 2], gCO2=local_emissions, departure_point=[row.lat_clean_dep, row.long_clean_dep], arrival_point=[row.lat_clean_arr, row.long_clean_arr], departure_date=row.date_dep, arrival_date=row.date_arr, geojson=[], ) journey_steps.append(journey_step) journey = tmw.Journey( 0, steps=journey_steps, departure_date=journey_steps[0].departure_date, arrival_date=journey_steps[1].arrival_date, ) journey.total_gCO2 = local_emissions journey.category = constants.CATEGORY_FERRY_JOURNEY journey.booking_link = 'https://www.ferrysavers.co.uk/ferry-routes.htm' journey.departure_point = [row.lat_clean_dep, row.long_clean_dep] journey.arrival_point = [row.lat_clean_arr, row.long_clean_arr] journey.update() journey_list.append(journey) return journey_list