def setUp(self): start_point_1 = (37.77264255,-122.399714854263) end_point_1 = (37.42870635,-122.140926605802) start_point_2 = (37.42870635,-122.140926605802) end_point_2 = (37.76624, -122.43456) start_point_3 = (37.76976, -122.43422) end_point_3 = (37.87119, -122.27388) mode_1 = "TRANSIT" mode_2 = 'CAR' mode_3 = 'CAR' curr_time = datetime.datetime.now() curr_month = curr_time.month curr_year = curr_time.year curr_minute = curr_time.minute curr_day = random.randint(1, 9) hour_1 = random.randint(0, 10) hour_2 = hour_1 + 5 hour_3 = hour_2 + 5 date = "%s-%s-%s" % (curr_month, curr_day, curr_year) time_1 = "%s:%s" % (hour_1, curr_minute) time_2 = "%s:%s" % (hour_2, curr_minute) time_3 = "%s:%s" % (hour_3, curr_minute) self.opt_trip_1 = otp.OTP(start_point_1, end_point_1, mode_1, date, time_1, bike=True) self.opt_trip_2 = otp.OTP(start_point_2, end_point_2, mode_2, date, time_2, bike=False) self.opt_trip_3 = otp.OTP(start_point_2, end_point_2, mode_2, date, time_3, bike=False)
def get_all_trips(self, start, end, curr_time=None): c = gmaps.client.Client(GOOGLE_MAPS_KEY) if curr_time is None: curr_time = datetime.datetime.now() curr_month = curr_time.month curr_year = curr_time.year curr_minute = curr_time.minute curr_day = curr_time.day curr_hour = curr_time.hour mode = "WALK" if self.has_bike: mode = "BICYCLE" walk_otp = otp.OTP(start, end, "WALK", write_day(curr_month, curr_day, curr_year), write_time(curr_hour, curr_minute), False) lst_of_trips = walk_otp.get_all_trips(0, 0, 0) our_gmaps = gmaps.GoogleMaps(GOOGLE_MAPS_KEY) mode = "walking" if self.has_bike: mode = "bicycling" jsn = our_gmaps.directions(start, end, mode) gmaps_options = gmcommon.google_maps_to_our_trip_list(jsn, 0, 0, 0, mode, curr_time) ## Throw in a random waypoint to make things more interesting waypoint = get_one_random_point_in_radius(CENTER_OF_CAMPUS, RANDOM_RADIUS) gmaps_way_points_jsn = our_gmaps.directions(start, end, mode, waypoints=waypoint) way_points_options = gmcommon.google_maps_to_our_trip_list(gmaps_way_points_jsn, 0, 0, 0, mode, curr_time) tot_trips = lst_of_trips + gmaps_options + way_points_options return tot_trips
def obtain_alternatives(trip_id, user_id): db = edb.get_trip_db() trip = ecwt.E_Mission_Trip.trip_from_json( db.find_one({ "trip_id": trip_id, "user_id": user_id })) logging.debug(trip.sections) start_coord = trip.trip_start_location.maps_coordinate() end_coord = trip.trip_end_location.maps_coordinate() logging.debug("Start: %s " % start_coord) logging.debug("End: %s " % end_coord) curr_time = datetime.datetime.now() curr_year = curr_time.year curr_month = curr_time.month curr_day = curr_time.day curr_hour = curr_time.hour curr_minute = curr_time.minute otp_modes = ['CAR', 'WALK', 'BICYCLE', 'TRANSIT'] for mode in otp_modes: try: otp_trip = otp.OTP(os.environ("OTP_SERVER")).route( start_coord, end_coord, mode, write_day(curr_month, curr_day, curr_year), write_time(curr_hour, curr_minute), False) otp_trip = otp_trip.turn_into_trip(None, user_id, trip_id) otp_trip.save_to_db() except otp.PathNotFoundException as e: logging.info("No alternatives found in OTP, saving nothing") '''
def obtain_alternatives(trip_id, user_id): db = edb.get_trip_db() trip = ecwt.E_Mission_Trip.trip_from_json( db.find_one({ "trip_id": trip_id, "user_id": user_id })) logging.debug(trip.sections) start_coord = trip.trip_start_location.maps_coordinate() end_coord = trip.trip_end_location.maps_coordinate() logging.debug("Start: %s " % start_coord) logging.debug("End: %s " % end_coord) curr_time = datetime.datetime.now() curr_year = curr_time.year curr_month = curr_time.month curr_day = curr_time.day curr_hour = curr_time.hour curr_minute = curr_time.minute otp_modes = ['CAR', 'WALK', 'BICYCLE', 'TRANSIT'] for mode in otp_modes: try: otp_trip = otp.OTP(os.environ("OTP_SERVER")).route( start_coord, end_coord, mode, write_day(curr_month, curr_day, curr_year), write_time(curr_hour, curr_minute), False) otp_trip = otp_trip.turn_into_trip(None, user_id, trip_id) otp_trip.save_to_db() except otp.PathNotFoundException as e: #modes = ['driving', 'walking', 'bicycling', 'transit'] logging.debug("Got error %s from OTP, defaulting to Google Maps" % e) otp_to_google_mode = { "CAR": "driving", "WALK": "walking", "BICYCLE": "bicycling", "TRANSIT": "transit" } mode = otp_to_google_mode[mode] gmaps = gmaps_lib.googlemaps.GoogleMaps( 'AIzaSyBEkw4PXVv_bsAdUmrFwatEyS6xLw3Bd9c') try: result = gmaps.directions(origin=start_coord, destination=end_coord, mode=mode) gmaps_trip = gmaps_lib.common.google_maps_to_our_trip( result, None, user_id, trip_id, mode, curr_time) gmaps_trip.save_to_db() except gmaps_lib.googlemaps.GoogleMapsError as ge: logging.info( "No alternatives found in either OTP or google maps, saving nothing" ) '''
def testE2E(self): base = eum.UserBase() josh = eum.UserModel() josh.increase_utility_by_n("time", 10) josh.increase_utility_by_n("noise", 10) start, end = (37.504712, -122.314189), (37.509580, -122.321560 ) ## GPS points josh.add_to_last(start, end) curr_time = datetime.datetime.now() curr_month = curr_time.month curr_year = curr_time.year curr_minute = curr_time.minute curr_day = curr_time.day curr_hour = curr_time.hour self.base_url = os.environ("OTP_SERVER") walk_otp = otp.OTP(self.base_url).route( start, end, "WALK", eum.write_day(curr_month, curr_day, curr_year), eum.write_time(curr_hour, curr_minute), False) bike_otp = otp.OTP(self.base_url).route( start, end, "BICYCLE", eum.write_day(curr_month, curr_day, curr_year), eum.write_time(curr_hour, curr_minute), True) choices = walk_otp.get_all_trips(0, 0, 0) + bike_otp.get_all_trips( 0, 0, 0) trips = josh.get_top_choices_lat_lng(start, end, tot_trips=choices) end = (37.512048, -122.316614) self.assertTrue(josh.delta(start, end)) josh.add_to_last(start, end) self.assertFalse(josh.delta(start, end)) josh.increase_utility_by_n("time", -200) josh.add_to_last(start, end) new_choices = josh.get_top_choices_lat_lng(start, end, tot_trips=choices)
def testNormalizationTimes(self): base = eum.UserBase() josh = eum.UserModel() josh.increase_utility_by_n("scenery", 100) josh.increase_utility_by_n("noise", 10) start, end = (37.504712, -122.314189), (37.509580, -122.321560 ) ## GPS points josh.add_to_last(start, end) curr_time = datetime.datetime.now() curr_month = curr_time.month curr_year = curr_time.year curr_minute = curr_time.minute curr_day = curr_time.day curr_hour = curr_time.hour self.base_url = os.environ("OTP_SERVER") walk_otp = otp.OTP(self.base_url).route( start, end, "WALK", eum.write_day(curr_month, curr_day, curr_year), eum.write_time(curr_hour, curr_minute), False) bike_otp = otp.OTP(self.base_url).route( start, end, "BICYCLE", eum.write_day(curr_month, curr_day, curr_year), eum.write_time(curr_hour, curr_minute), False) choices = walk_otp.get_all_trips(0, 0, 0) + bike_otp.get_all_trips( 0, 0, 0) normal_times = eum.get_normalized_times(choices) normal_sweat = eum.get_normalized_sweat(choices, True) normal_beauty = eum.get_normalized_beauty(choices) print("normal times = %s" % normal_times) print("normal sweat = %s" % normal_sweat) print("normal beauty = %s" % normal_beauty) self.assertAlmostEqual(sum(normal_times), 1) self.assertAlmostEqual(sum(normal_sweat), 1) self.assertAlmostEqual(sum(normal_beauty), 1)
def get_all_trips(self, start, end, curr_time=None): if curr_time is None: curr_time = datetime.datetime.now() curr_month = curr_time.month curr_year = curr_time.year curr_minute = curr_time.minute curr_day = curr_time.day curr_hour = curr_time.hour mode = "WALK" if self.has_bike: mode = "BICYCLE" walk_otp = otp.OTP(os.environ("OTP_SERVER")).route( start, end, "WALK", write_day(curr_month, curr_day, curr_year), write_time(curr_hour, curr_minute), False) lst_of_trips = walk_otp.get_all_trips(0, 0, 0) tot_trips = lst_of_trips return tot_trips
def __init__(self, tour_date, person): self._tour_date = tour_date self._measurements_cache = [] self._tour = person["plan"]["act"] self._tour_place_pairs = zip(self._tour, self._tour[1:]) self._otp_stub = otp.OTP(os.environ["OTP_SERVER"])