def get_stop_details(self, queue: dict, lock: threading.RLock): while len(queue): # 保存下来的的公交站点名为 XXX站 # 根据站名查找时需要去掉'站' # routes 为 ('route_name', [[route_stops], [route_stops]]) lock.acquire() if len(queue): routes = queue.popitem() else: lock.release() break lock.release() for route in routes[1]: route_id = Route.get_id_by_raw_name(routes[0], route[0], route[-1]) for stop_name in route: stop_id = Stop.get_id_by_name(stop_name) if not stop_id: print("正在爬取 %s 的详细信息" % stop_name) result = self.find_stop_by_name(stop_name) if len(result): for item in result: item.create() else: self._add_stop_route(stop_id, route_id) # 爬取后再将剩余队列保存到缓存文件 lock.acquire() with open(self.stops_queue, 'w') as f: json.dump(queue, f, ensure_ascii=False) lock.release()
def __init__(self, stop_times, vehicle_2_stops_times, trips, logger_verbosity): self._logger = Logger(logger_verbosity) self._times = dict(stop_times, **vehicle_2_stops_times) self._trip_by_id = dict(map(lambda t: (t.id, t), trips)) trip_to_stop_list = lambda t: [ Stop(t.pickup_stop_id, t.id, StopType.PICKUP), Stop(t.dropoff_stop_id, t.id, StopType.DROPOFF) ] self._stops = [ stop for stop_list in map(trip_to_stop_list, trips) for stop in stop_list ] self._logger(3, 'Initialized with:') self._logger(3, 'times:', self._times) self._logger(3, 'trips_by_id:', self._trip_by_id) self._logger(3, 'stops:', self._stops)
def create_stop(city_name, route, created_at, is_start, is_end, stay_length, lat, lng): stop = Stop(city_name=city_name, route=route, created_at=created_at, is_start=is_start, is_end=is_end, stay_length=stay_length, lat=lat, lng=lng) db.session.add(stop) db.session.commit() return stop
def adds_stops_to_db(unique_stops): """all all unique stops to db""" for stop in unique_stops: name = stop[0] stop_code = stop[1] lat = unique_stops[stop]['lat'] lon = unique_stops[stop]['lon'] stop = Stop( stop_code=stop_code, name=str(name), lat=lat, lon=lon, ) db.session.add(stop) db.session.commit() print "Stops Added to DB"
def _parse_stop(self, content: dict, name: str) -> typing.List[Stop]: stops = [] for item in content['items']: if item['name'] == name: for stop in item['stops']: stop_id = stop['stopId'] if stop_id in self.stop_ids: continue else: self.stop_ids.append(stop_id) a_stop = Stop() a_stop.name = name a_stop.stop_id = stop['stopId'] if 'amapId' in stop: a_stop.amap_id = stop['amapId'] a_stop.lng = stop['lng'] a_stop.lat = stop['lat'] a_stop.routes = ','.join( self._parse_stop_routes(stop_id, stop['routeInfos'])) stops.append(a_stop) return stops
def load_stops(): """Loading bus routes into database""" print "Stops" # Read the routes.text file to Stop.query.delete() for row in open("seed_data/stops.txt"): row = row.rstrip() stop_id, stop_name, stop_lat, stop_lon = row.split(",")[:4] stop = Stop(stop_id=stop_id, stop_name=stop_name, stop_lat=stop_lat, stop_lon=stop_lon) print stop db.session.add(stop) db.session.commit()
def _save_departure(self, event): dt = datetime.datetime.now(datetime.timezone.utc) dt = dt - datetime.timedelta(seconds = dt.second, microseconds = dt.microsecond) stop = Stop(stop_point_ref = event.stop_point_ref, stop_point_name = event.stop_point_name, timetabled_time = event.timetabled_time, real_time = dt, stop_seq_number = event.stop_sequence_number, operating_day_ref = event.operating_day_ref, journey_ref = event.journey_ref, line_ref = event.line_ref, direction_ref = event.direction_ref, pt_mode = event.pt_mode, submode = event.submode, published_line_name = event.line_name, operator_ref = "kvv", route_description = event.route_description, origin_stop_point_ref = event.origin_stop_point_ref, destination_text = event.destination_text) self.session.add(stop) self.session.commit()
def create_stop(city_name, route, stay_length, lat, lng, country_code, is_start, is_end, highlights): stop = Stop(city_name=city_name, route=route, stay_length=stay_length, lat=lat, lng=lng, country_code=country_code, is_start=is_start, is_end=is_end, highlights=highlights) if stop.is_end == True: route.is_completed = True route.trip_length += int(stop.stay_length) db.session.add(stop) db.session.commit() return stop
def convert(lng, lat): x_PI = 3.14159265358979324 * 3000.0 / 180.0 z = math.sqrt(lng * lng + lat * lat) + 0.00002 * math.sin(lat * x_PI) theta = math.atan2(lat, lng) + 0.000003 * math.cos(lng * x_PI) bd_lng = z * math.cos(theta) + 0.0065 bd_lat = z * math.sin(theta) + 0.006 return [bd_lng, bd_lat] m = folium.Map( location=[30.314192,120.343222], tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}', zoom_start=12, attr='高德地图' ) stop_data = Stop.get_all() def parse_zhch(chinese_str: str): """将中文转码,以便能正常显示 """ return str(str(chinese_str).encode('ascii' , 'xmlcharrefreplace'))[2:-1] for stop in stop_data: folium.CircleMarker( location=[stop.lat, stop.lng], radius=2, popup=parse_zhch(stop.name) ).add_to(m) @app.route('/') def index():