def getRoadPoints(): """ 获取两站点之间GPS点集 """ stationList = getStationList(mon_db) label_time = "2019/07/11" line_cursor = mon_db.kctest_line_info.find({}, { "LINE_NO": 1, "IS_UP_DOWN": 1 }, no_cursor_timeout=True) for line in line_cursor: line_id = line["LINE_NO"] + "_" + str(line["IS_UP_DOWN"]) if line_id != "13_1": continue midpoints_cursor = mon_db.bus_mid_split.find({ "LINE_NO": line["LINE_NO"], "IS_UP_DOWN": line["IS_UP_DOWN"], "LABEL_TIME": label_time }) stations = stationList[line_id] for midpoints in midpoints_cursor: #先取11-4时段的点 locs = midpoints["LOCS"] if locs[0]["site_time"] > "2019/07/11 11:00:00" and locs[0][ "site_time"] < "2019/07/11 16:00:00": dists = calcPointStationDist(locs, stations) minIndexes = getMinIndexes(dists) saveRoadPoints(line_id, locs, stations, minIndexes)
def calcStationDist(): """ 计算两站点之间直线距离及估计距离 """ stationList = getStationList(mon_db) line_cursor = mon_db.kctest_line_info.find({}, { "LINE_NO": 1, "IS_UP_DOWN": 1 }, no_cursor_timeout=True) for line in line_cursor: line_id = line["LINE_NO"] + "_" + str(line["IS_UP_DOWN"]) if line_id != "13_1": continue stations = stationList[line_id]["LOCS"] for i in range(len(stations) - 1): last_station = stations[i] next_station = stations[i + 1] line_info = line_id + "_" + str( last_station["LABEL_NO"]) + "_" + str(next_station["LABEL_NO"]) LINEAR_DIST = getDistanceWGS(float(last_station["LNG"]), float(last_station["LAT"]), float(next_station["LNG"]), float(next_station["LAT"])) ESTIMATE_DIST = calcMeanEstimateDist(line_info) item = { "LINE_INFO": line_info, "LINEAR_DIST": LINEAR_DIST, "ESTIMATE_DIST": ESTIMATE_DIST, "AMAP_DIST": 0 } mon_db.station_road_dist.update({"LINE_INFO": item["LINE_INFO"]}, {'$set': item}, True)
def main(): start_day = datetime(2019, 7, 10) d = date.today() end_day = datetime(d.year, d.month, d.day) bus_mid_info = mon_db.bus_mid_info #存储中间结果 bus_mid_split = mon_db.bus_mid_split #存储班次分隔结果 stationLineInfo = getStationList(mon_db) try: #先按天获取所有线路的中间数据 while start_day < end_day: d = date.today() end_day = datetime(d.year, d.month, d.day) line_cursor = mon_db.kctest_line_info.find({}, { "LINE_NO": 1, "IS_UP_DOWN": 1 }, no_cursor_timeout=True) label_time = start_day.strftime("%Y/%m/%d") start_time = time.time() for line in line_cursor: bus_mid_set = bus_mid_info.find( { "LINE_NO": line["LINE_NO"], "IS_UP_DOWN": line["IS_UP_DOWN"], "LABEL_TIME": label_time }, no_cursor_timeout=True) line_key = line["LINE_NO"] + "_" + str(line["IS_UP_DOWN"]) #print(line_key,label_time) if len(stationLineInfo[line_key]["LOCS"]) == 0: continue first_station = stationLineInfo[line_key]["LOCS"][ 0] #始发站 {"LABEL_NO","LNG","LAT"} for item in bus_mid_set: #利用站点之间的时间间隔来分隔每趟车每个班次 locs = item[ "LOCS"] #[{"lng","lat","site_time"},{"lng","lat","site_time"},{"lng","lat","site_time"}] busNo = item["BUS_NO"] locsNum = len(locs) split_index = [] for i in range(locsNum - 1): last_time = datetime.strptime(locs[i]["site_time"], "%Y/%m/%d %H:%M:%S") next_time = datetime.strptime(locs[i + 1]["site_time"], "%Y/%m/%d %H:%M:%S") delta = (next_time - last_time).seconds if delta > LOOP_TIME_DELTA and line[ "LINE_NO"] in LOOP_LINE_NO: #环线分割,分割点在起始站附近 first_station_dist = getDistanceWGS( float(locs[i]['lng']), float(locs[i]['lat']), float(first_station["LNG"]), float(first_station["LAT"])) if first_station_dist < 200: split_index.append(i + 1) elif delta > TIME_DELTA: #当两点之间超过半个小时,且下一时刻比上一时刻更靠近始发站,且距离相差较大 first_station_dist1 = getDistanceWGS( float(locs[i]['lng']), float(locs[i]['lat']), float(first_station["LNG"]), float(first_station["LAT"])) first_station_dist2 = getDistanceWGS( float(locs[i + 1]['lng']), float(locs[i + 1]['lat']), float(first_station["LNG"]), float(first_station["LAT"])) if first_station_dist1 > first_station_dist2 + DIST_DELTA: split_index.append(i + 1) split_index.append(locsNum) #分隔后计算站点之间时间差 stationNum = len(stationLineInfo[line_key]['LOCS']) last_index = 0 for i in range(len(split_index)): next_index = split_index[i] temp_locs = locs[last_index:next_index] if len(temp_locs) <= 20: continue bus_mid_split.insert({ "LINE_NO": item["LINE_NO"], "IS_UP_DOWN": item["IS_UP_DOWN"], "BUS_NO": item["BUS_NO"], "LABEL_TIME": item["LABEL_TIME"], "LOCS": temp_locs }) calcMinDistAndTimeDelta(temp_locs, stationLineInfo[line_key], stationNum, busNo, label_time) last_index = next_index #记录量车每天跑了多少趟及总耗时 start_day += timedelta(days=1) with open("bus_time_process.log", "a+") as f: f.write("get bus time delta on:%s, cost time:%f\n" % (label_time, time.time() - start_time)) if start_day >= end_day: with open("bus_time_process.log", "a+") as f: f.write( "local time:" + time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()) + "\t time sleep!" + "\n") time.sleep(3600 * 24) d = date.today() end_day = datetime(d.year, d.month, d.day) except Exception as e: with open("bus_time_process.log", "a+") as f: f.write("local time:" + time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()) + "\t main occur error: " + str(e) + "\n")
#coding=utf-8 from pymongo import MongoClient import numpy as np import time import os from datetime import datetime, date, timedelta from utils.getStations import getStationList from utils.getHolidayUtils import getHoliday import pandas as pd mon_ipAdrr = "10.10.116.200" mon_port = 27017 mon_conn = MongoClient(mon_ipAdrr, mon_port) mon_db = mon_conn.kctest stationList = getStationList(mon_db) def getOneHourTimeList(): startTime = datetime(2019, 7, 3, 6) #print(startTime.hour) hourList = [] while startTime.hour < 23: nextTime = startTime + timedelta(hours=1) startTimeStr = startTime.strftime("%Y/%m/%d %H:%M:%S").split(" ") nextTimeStr = nextTime.strftime("%Y/%m/%d %H:%M:%S").split(" ") startTime = nextTime hourList.append({ "start_time": startTimeStr[-1], "next_time": nextTimeStr[-1], "htimes": [], "wtimes": []
def main(): line_no = '18' is_up_down = 1 line_info = mon_db.kctest_line_info.find({ "LINE_NO": line_no, "IS_UP_DOWN": is_up_down }) print(line_info[0]["STATION_FIRST"], line_info[0]["STATION_LAST"]) line_key = line_no + "_" + str(is_up_down) stationList = getStationList(mon_db) #strptime("2019/07/05 08:00:00","%Y/%m/%d %H:%M:%S") while True: #获取该路线上的公交车数据 """start_time=datetime.now()-timedelta(minutes=2) begin_time=(start_time-timedelta(seconds=30)).strftime("%Y/%m/%d %H:%M:%S") print(begin_time) end_time=start_time.strftime("%Y/%m/%d %H:%M:%S") bus_infos=getRealTimeBusInfo(line_no,is_up_down,begin_time,end_time)""" bus_infos = [{ "LINE_NO": line_no, "LNG": 119.281283333, "LAT": 26.072966666666666, "SITE_TIME": "2019/08/02 15:56:01", "BUS_NO": "1235" }] begin_time = "2019/08/02 15:56:01" #确认时间段 time_info = begin_time.split(" ") isHoliday = getHoliday(time_info[0]) stations = stationList[line_key]["LOCS"] for bus_info in bus_infos: #计算各趟公交车处于哪个路段 pointInfo = calcNearestStation(line_key, bus_info, stations) #已知公交车处于哪个路段后,开始计算到各站点的到站时间 stationArrivingTime = calcArrivalStationTimeAndDist( bus_info, pointInfo, stations, line_key, time_info, isHoliday) printStationArrivingTime(stationArrivingTime, pointInfo, line_no, is_up_down, bus_info["BUS_NO"]) """ 后续逐步优化 将上一时刻的数据存在数据库中: RECORD_ID,LS_INDEX,NS_INDEX,lng,lat,site_time,arriving_next_time,OFFSET_LABEL OFFSET_LABEL:表示是否偏离的GPS点,True表示偏离,FALSE不偏离 """ last_info = { "RECORD_ID": line_key + "_" + str(bus_info["BUS_NO"]), "LS_INDEX": int(pointInfo["last_station_index"]), "NS_INDEX": int(pointInfo["next_station_index"]), "LNG": bus_info["LNG"], "LAT": bus_info["LAT"], "SITE_TIME": bus_info["SITE_TIME"], "ARR_NEXT_TIME": stationArrivingTime[0], "ARR_NEXT_DIST": pointInfo["dist"][pointInfo["next_station_index"]], "OFFSET_LABEL": False } print(last_info) #mon_db.bus_arriving_station_time.update({"RECORD_ID":last_info["RECORD_ID"],"SITE_TIME":last_info["SITE_TIME"]},{'$set':last_info},True) #start_time=start_time+timedelta(seconds=30) time.sleep(25)