def next_stop(self): ''' Discover who is the next stop. How it works: - Calculate Bus distante from last stop. - Calculate all distances between all stops and last stop. - Get the stop that is more far from last stop and the stop distance is smaller then bus distance from last stop. - If there's none between, return the last stop. ''' if not self.location_available: return None stops = self.route.stops.all() if stops.count() > 0: bus_lat, bus_lon = self.current_location_cached to_lat, to_lon = self.route.to_stop.location bus_distance = get_directions(bus_lat, bus_lon, to_lat, to_lon).meters pos_list = [(x.latitude, x.longitude) for x in stops] distances = get_distances([(to_lat, to_lon)], pos_list) if distances: d = distances.values() nexts = filter(lambda d: d.meters < bus_distance, d) if len(nexts) > 0: distance = sorted(nexts, key=lambda item: item.meters)[0] key = distances.keys()[distances.values().index(distance)] index = pos_list.index(key[1]) return stops[index] return self.route.to_stop
def estimated_arrival_to(self, latitude, longitude): ''' Returns the estimated time to the given latitude and longitude based on the position of the bus. ''' if not self.location_available: return 0 bus_lat, bus_lon = self.current_location return get_directions(bus_lat, bus_lon, latitude, longitude)
def percent_complete_of_route(self): ''' Return the amount (%) which has covered. How it works: - Get the distance from first stop to the last stop. - Get the distance from bus to the last stop. - Calculate the remaining in %. ''' if not self.location_available: return 0.0 bus_lat, bus_lon = self.current_location_cached from_lat = self.route.from_stop.latitude from_lon = self.route.from_stop.longitude to_lat = self.route.to_stop.latitude to_lon = self.route.to_stop.longitude from_distance_to = get_directions(from_lat, from_lon, to_lat, to_lon).meters bus_distance = get_directions(bus_lat, bus_lon, to_lat, to_lon).meters d = from_distance_to - bus_distance return d * 100 / from_distance_to