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 is_moving(device_id): ''' Returns True if the Bus has moved more then 250m in the last 5 minutes. ''' lat, lon = get_last_lat_lon(device_id) positions = list(get_last_positions(device_id)) if lat and lon and positions: distances = get_distances([(lat, lon)], positions) distance = reduce(lambda acc, item: acc + item.meters, distances) / len(positions) return distance > 250 return False
def is_moving(device_id): ''' Returns True if the Bus has moved more then 1000m in the last 5 minutes. ''' lat, lon = get_last_lat_lon(device_id) positions = list(get_last_positions(device_id)) if lat and lon and len(positions) > 1: distances = get_distances([(lat, lon)], [positions[0], positions[-1]]) if distances: distances = list(distances.values()) distance = abs(distances[0].meters - distances[-1].meters) return distance >= 1000 return False