def finish(self, edges): translate = { 'WALK': TransportType.WALK, 'RAIL': TransportType.TRAIN, 'TRAM': TransportType.TRAM, 'BUS': TransportType.BUS, } points = set() points.update(s.from_vertex for s in edges) points.update(s.to_vertex for s in edges) new_edges = [] for it in self.search_request.result['itineraries']: prev_point = self.start for index, leg in enumerate(it['legs']): transport_type = translate.get(leg['mode']) if not transport_type: continue # print(leg) start_time = datetime.fromtimestamp( int(leg['startTime']) / 1000) end_time = datetime.fromtimestamp(int(leg['endTime']) / 1000) if 'departure' in leg[ 'to']: # plannerstack can include 1 second of waiting, which we want to ignore end_dep_time = datetime.fromtimestamp( int(leg['to']['departure']) / 1000) if end_dep_time - end_time <= timedelta(seconds=1): end_time = end_dep_time p_loc_name = leg['to']['name'] if p_loc_name == 'Destination': loc = self.end.location else: p_loc_lat = float(leg['to']['lat']) p_loc_lon = float(leg['to']['lon']) loc = self.location_holder.create_location( p_loc_name, (p_loc_lat, p_loc_lon)) p_end = get_or_add(points, Point(loc, end_time)) if index == 0: # walk to first stop will be added later prev_point = p_end continue # logger.info('Arrival time ' + str(int(leg['to']['arrival'])) + ' ' + str(start_time)) if start_time > prev_point.time: p_start = get_or_add( points, Point(prev_point.location, start_time)) new_edges.append( Segment(TransportType.WAIT, prev_point, p_start)) else: p_start = prev_point segment = Segment(transport_type, p_start, p_end) if 'routeShortName' in leg: segment.route_name = transport_type.to_dutch( ) + ' ' + leg['routeShortName'] if 'platformCode' in leg['from']: segment.platform_code = leg['from']['platformCode'] new_edges.append(segment) prev_point = p_end for s in new_edges: if not any(se.has_same_points_and_type(s) for se in edges): # logger.info('Adding segment ' + str(s)) edges.append(s)
def create_segment(start, end, fix_time, transport_type, option=None): time_sec, map_url = WalkGenerator.get_bike_travel_time(start.location, end.location, transport_type) delta_t = timedelta(seconds=time_sec) if fix_time == FixTime.START: new_point = Point(end.location, start.time + delta_t) segment = Segment(transport_type, start, new_point) else: new_point = Point(start.location, end.time - delta_t) segment = Segment(transport_type, new_point, end) segment.map_url = map_url return segment, new_point
def create_edges(self, start, end, fix_time, edges): st_start_loc = self.closest_stations(start.location, 2) st_end_loc = self.closest_stations(end.location, 2) stops_1 = [ Point(s, start.time + timedelta(minutes=50)) for s in st_start_loc ] stops_2 = [ Point(s, start.time + timedelta(minutes=140)) for s in st_end_loc ] for s1 in stops_1: for s2 in stops_2: # take the train edges.append(Segment(TransportType.TRAIN, s1, s2))
def create_wait_and_move_segments(generator, start, end, fix, transport_type, min_time_sec=0, max_time_sec=None, option=None): segment, new_point = generator.create_segment(start, end, fix, transport_type, option) if segment.time_sec < min_time_sec: return [] if max_time_sec and segment.time_sec > max_time_sec: return [] edges = [] if fix == FixTime.START: # move then wait if new_point.time < end.time: edges.append(segment) edges.append(Segment(TransportType.WAIT, new_point, end)) else: # wait then move if new_point.time > start.time: edges.append(Segment(TransportType.WAIT, start, new_point)) edges.append(segment) return edges
def finish(self, edges): loc_1 = Location.midpoint(self.start.location, self.end.location, 0.01) loc_2 = Location.midpoint(self.start.location, self.end.location, 0.5) loc_3 = Location.midpoint(self.start.location, self.end.location, 0.99) loc_4 = self.end.location if self.fix_time == FixTime.START: start_time = self.start.time else: start_time = self.end.time - timedelta(hours=1) time_1 = start_time + 0.25 * timedelta(hours=1) time_2 = start_time + 0.5 * timedelta(hours=1) time_3 = start_time + 0.75 * timedelta(hours=1) time_4 = start_time + 0.99 * timedelta(hours=1) p1 = Point(loc_1, time_1) p2 = Point(loc_2, time_2) p3 = Point(loc_3, time_3) p4 = Point(loc_4, time_4) edges += [ Segment(TransportType.BUS, p1, p2), Segment(TransportType.TRAIN, p2, p3), Segment(TransportType.WALK, p3, p4), ]
def create_edges(self, start, end, fix_time): b = Point('b', start.time + datetime.timedelta(seconds=20)) c = Point('c', start.time + datetime.timedelta(seconds=80)) d = Point('d', start.time + datetime.timedelta(seconds=50)) edges = [ Segment(TransportType.BIKE, start, b), Segment(TransportType.TRAIN, start, c), Segment(TransportType.TRAIN, start, d), Segment(TransportType.TRAIN, b, c), Segment(TransportType.BIKE, c, end), Segment(TransportType.TRAIN, d, end), Segment(TransportType.CAR, start, end) ] return edges
def get_plans(self): t0 = datetime(year=2000, month=1, day=1) points = { 'a': Point('a', t0), 'z1': Point('z', t0 + timedelta(hours=3)), 'z2': Point('z', t0 + timedelta(hours=3, minutes=1)), 'c': Point('c', t0 + timedelta(hours=2)), 'z3': Point('z', t0 + timedelta(hours=5)), 'a2': Point('a', t0 + timedelta(hours=12)), 'c2': Point('c', t0 + timedelta(hours=13)), 'z4': Point('z', t0 + timedelta(hours=14)), 'z5': Point('z', t0 + timedelta(hours=6)), 'z6': Point('z', t0 + timedelta(hours=2, minutes=59)), } carplan = self.create_plan([ Segment(TransportType.CAR, points['a'], points['z1']), ]) bikeplan = self.create_plan([ Segment(TransportType.BIKE, points['a'], points['z2']), ]) publicplan1 = self.create_plan([ Segment(TransportType.WALK, points['a'], points['c']), Segment(TransportType.TRAIN, points['c'], points['z3']), ]) publicplan2 = self.create_plan([ Segment(TransportType.WAIT, points['a'], points['a2']), Segment(TransportType.WALK, points['a2'], points['c2']), Segment(TransportType.TRAIN, points['c2'], points['z4']), ]) walkplan = self.create_plan([ Segment(TransportType.WALK, points['a'], points['z5']), ]) bikeplan2 = self.create_plan([ Segment(TransportType.BIKE, points['a'], points['z6']), ]) return bikeplan, carplan, publicplan1, publicplan2, walkplan, bikeplan2
def create_segment(self, start, end, fix, transport_type=TransportType.CAR, option=False): route_params = TomTomApi.RouteParams(start=start.location, end=end.location, avoid_highways=option) travel_time, delay = self.tomtom.travel_time(route_params) delta_t = timedelta(seconds=travel_time) map_url = self.tomtom.map_url(route_params) if fix == FixTime.START: new_point = Point(end.location, start.time + delta_t) segment = Segment(transport_type, start, new_point) else: new_point = Point(start.location, end.time - delta_t) segment = Segment(transport_type, new_point, end) segment.delay = delay / 60 segment.map_url = map_url return segment, new_point
def solve(self, start_loc_str, start_hike_time, user_preferences=UserTravelPreferences()): logger.info('BEGIN') log_start = time.time() start = Point(Location(start_loc_str), start_hike_time - timedelta(hours=12)) trail = get_trail(start.location) hike_start_loc = Location('wandeling vertrek', (trail.begin_lat, trail.begin_lon)) hike_end_loc = Location('wandeling aankomst', (trail.end_lat, trail.end_lon)) if hike_start_loc.distance_to(hike_end_loc).meters < 100: hike_end_loc = hike_start_loc hike_start_loc.loc_str = 'wandeling vertrek/aankomst' hike_start = Point(hike_start_loc, start_hike_time) hike_end = Point( hike_end_loc, start_hike_time + timedelta(seconds=trail.distance / WALKING_SPEED)) end = Point(Location(start_loc_str), hike_end.time + timedelta(hours=12)) logger.info('start=' + str(start)) logger.info('hike_start=' + str(hike_start)) logger.info('hike_end=' + str(hike_end)) logger.info('end=' + str(end)) segments = self.generator.create_edges(start, hike_start, FixTime.END) segments += self.generator.create_edges(hike_end, end, FixTime.START) hiking_segment = Segment(TransportType.WALK, hike_start, hike_end) hiking_segment.route_name = trail.title hiking_segment.map_url = trail.wandelpagina_url segments.append(hiking_segment) # for e in segments: # logger.info('Edge: ' + str(e)) vehicle_positions = VehiclePositions() # put a bicycle and car at home... vehicle_positions.add_vehicle( start.location, Vehicle(TransportType.BIKE, VehicleType.BIKE)) vehicle_positions.add_vehicle( start.location, Vehicle(TransportType.CAR, VehicleType.CAR)) # add OV fietsen ov_fiets_stations = OvFietsStations() vehicle_positions = ov_fiets_stations.add_default_ovfiets_positions( vehicle_positions, segments) plans = self.router.make_plans(start, end, segments, vehicle_positions) plans = list(filter(Planner.has_no_double_biking, plans)) order_and_select(plans, user_preferences, None) Planner.remove_unnecessary_waiting(plans, FixTime.START) log_end = time.time() logger.info('END - time: ' + str(log_end - log_start)) return plans, trail