def save_road(xy_list): fp = open("road.txt", 'w') bl_list = [] for x, y in xy_list: lat, lng = xy2bl(x, y) bl_list.append("{0:.6f},{1:.6f}".format(lng, lat)) str_line = ";".join(bl_list) fp.write(str_line) fp.close()
def print_data(trace, bi, ei): fp = open('./data/1.txt', 'w') lx, ly = -1, -1 for data in trace[bi:ei + 1]: b, l = xy2bl(data.px, data.py) if lx != -1: dist = calc_dist([lx, ly], [data.px, data.py]) else: dist = 10.01 if dist > 10: fp.write("{0},{1},{2},{3}\n".format(data.px, data.py, l, b)) lx, ly = data.px, data.py # print data.state, data.car_state, data.speed, data.stime, data.px, data.py, data.dist fp.close()
def simplify(point_dict): pid = 0 for sid, pt_list in point_dict.iteritems(): xy_list = [] for pt in pt_list: x, y = bl2xy(pt.lati, pt.longti) xy_list.append([x, y]) path = dog_last(xy_list) new_pt_list = [] for i, xy in enumerate(path): x, y = xy[:] lat, lng = xy2bl(x, y) pt = Point(pid, sid, i, lng, lat) pid += 1 new_pt_list.append(pt) point_dict[sid] = new_pt_list return point_dict
def split_road(): conn1 = sqlite3.connect("../map_info/hz1.db") cursor = conn1.cursor() sql = "select * from TB_SEG_POINT order by s_id, seq;" cursor.execute(sql) roads = defaultdict(list) for item in cursor: s_id, seq, lng, lat = item[1:] x, y = bl2xy(lat, lng) roads[s_id].append([x, y]) pt_tup_list = [] for sid, pt_list in roads.iteritems(): last_pt = None new_pt_list = [] for pt in pt_list: if last_pt is not None: dist = calc_dist(pt, last_pt) if dist > DIST_THREAD: for n_pt in add_pt(last_pt, pt): new_pt_list.append(n_pt) last_pt = pt new_pt_list.append(pt) roads[sid] = new_pt_list for i, pt in enumerate(new_pt_list): b, l = xy2bl(pt[0], pt[1]) t = (sid, i + 1, l, b) pt_tup_list.append(t) cursor.close() conn1.close() conn2 = sqlite3.connect("../map_info/hz2.db") cursor = conn2.cursor() sql2 = "insert into tb_seg_point(point_id, s_id, seq, longi, lati) values(0,?,?,?,?)" cursor.executemany(sql2, pt_tup_list) conn2.commit() cursor.close()
def gene_from_gps_data(): """ 从gps点记录里面获取轨迹,并生成出发点和到达点 :return: """ try: gps_data = {} # gps_data 用于存放每辆车的轨迹 fp = open('./road/road.txt', 'w') conn = cx_Oracle.connect('hz', 'hz', '192.168.11.88/orcl') sql = "select vehicle_num, px, py from tb_gps_1805 where speed_time > to_date(" \ "'2018-05-01 9:00:00', 'yyyy-mm-dd hh24:mi:ss')" \ "and speed_time < to_date('2018-05-01 10:00:00', 'yyyy-mm-dd hh24:mi:ss')" cursor = conn.cursor() bt = time.clock() cursor.execute(sql) cnt = 0 for item in cursor: data = map(float, item[1:3]) lng, lat = data[0:2] if lng > 121 or lng < 119 or lat > 31 or lat < 29: continue x, y = bl2xy(lat, lng) veh = item[0][-6:] try: gps_data[veh].append([x, y]) except KeyError: gps_data[veh] = [[x, y]] cnt += 1 conn.close() et = time.clock() print et - bt s = 0 req_list = [] for veh, data_list in gps_data.iteritems(): s += len(data_list) use_list = data_list[::10] for i in range(len(use_list) - 1): req_list.append([data_list[i][0], data_list[i][1], data_list[i + 1][0], data_list[i + 1][1]]) print s pa_list, cnt, idx = [], 0, 0 for req in req_list: x0, y0, x1, y1 = req[0:4] if calc_dist([x0, y0], [x1, y1]) > 2000: b0, l0 = xy2bl(x0, y0) b1, l1 = xy2bl(x1, y1) pa_list.append([l0, b0, l1, b1]) idx += 1 if idx >= 20: batch_path_fetch(pa_list, fp) time.sleep(0.2) pa_list = [] idx = 0 cnt += 1 print "batch" + str(cnt) if cnt >= 1000: break fp.close() except Exception as e: print e.message