def get_closest_bus_stop(self, bus_info: CdsRouteBus): threshold = 0.005 bus_stop = next( (x for x in self.bus_stops if bus_info.bus_station_ and x.NAME_ == bus_info.bus_station_), None) if bus_stop and bus_info.distance(bus_stop) < threshold: return bus_stop bus_positions = self.last_bus_data[bus_info.name_] if not bus_positions: bus_positions.append(bus_info.get_bus_position()) closest_on_route = self.get_closest_bus_stop_checked( bus_info.route_name_, bus_positions) if closest_on_route and bus_info.distance( closest_on_route) < threshold: return closest_on_route closest_stop = min(self.bus_stops, key=bus_info.distance) if closest_stop and bus_info.distance(closest_stop) < threshold: return closest_stop result = min((bus_stop, closest_on_route, closest_stop), key=bus_info.distance) if not bus_info.bus_station_: self.logger.debug(f"Empty station: {bus_info.short()} {result}") return result
def get_closest_bus_stop(self, bus_info: CdsRouteBus): if not bus_info.is_valid_coords(): return threshold = 0.5 bus_stop = self.bus_stops_dict_name.get(bus_info.bus_station_) if bus_stop and bus_info.distance_km(bus_stop): return bus_stop elif self.now() - bus_info.last_time_ > timedelta(minutes=15): return self.get_nearest(bus_info.last_lat_, bus_info.last_lon_) bus_positions = self.last_bus_data[bus_info.name_] if not bus_positions: bus_positions.append(bus_info.get_bus_position()) closest_on_route = self.get_closest_bus_stop_checked( bus_info.route_name_, bus_positions) if closest_on_route and bus_info.distance( closest_on_route) < threshold: return closest_on_route closest_stop = self.get_nearest(bus_info.last_lat_, bus_info.last_lon_) if closest_stop and bus_info.distance(closest_stop) < threshold: return closest_stop result = min((bus_stop, closest_on_route, closest_stop), key=bus_info.distance) if not bus_info.bus_station_: self.logger.debug(f"Empty station: {bus_info.short()} {result}") return result