import os import pandas as pd import numpy as np gpx_save_dir = "D:/data/Geolife Trajectories 1.3/Data_gpx" csv_file_dir = "D:/data/Geolife Trajectories 1.3/Data_csv" txt_files = os.listdir(csv_file_dir) col_names = [‘lat‘, ‘lon‘, ‘datatime‘, ‘uid‘] dtype_dic = {‘lat‘: object, ‘lon‘: object, ‘datatime‘: object, ‘uid‘: str} for txt in txt_files: # print(txt) txt_path = os.path.join(csv_file_dir, txt) data = pd.read_csv(txt_path, header=None, names=col_names, dtype=dtype_dic) data = data.round(decimals=7) time = data.datatime.values[0].split(" ")[0] userid = np.unique(data.uid.values)[0] gpx = txt.split(".")[0] + ".gpx" gpx_save_file = os.path.join(gpx_save_dir, gpx) f = open(gpx_save_file, ‘a‘) f.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n" "<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"Graphhopper\" version=\"1.1\">\n" "<metadata>\n" "<copyright author=\"OpenStreetMap contributors\"/>\n" "<text>GraphHopper GPX</text>\n") f.write("<uid>"+userid+"</uid>\n") #要改uid f.write("<time>"+time+"T00:00:00+00:00</time>\n") #要改时间 f.write("</metadata>\n" "<trk><name>GraphHopper</name><trkseg>\n") fwrite_str = "" for i_d in range(data.shape[0]): data_row = data.values[i_d] lat, lng, datetime, uid = data_row[0], data_row[1], data_row[2], data_row[3] lat = ‘{}‘.format(lat) lng = ‘{}‘.format(lng) ts_str = ‘{}‘.format(datetime) ts_str = ts_str.split(" ") write_str = "<trkpt lat=\""+lat+"\" lon=\""+lng+"\"><time>"+ts_str[0]+"T"+ts_str[-1]+"+00:00</time></trkpt>\n" fwrite_str += write_str fwrite_str += "</trkseg></trk></gpx>" # print(fwrite_str) f.write(fwrite_str) f.close()
MapMatching 见:本博文---> https://www.cnblogs.com/jeshy/p/14350196.html
将trj保存成.gpx文件方便进行地图匹配(来自徐博士的支援)
原文:https://www.cnblogs.com/jeshy/p/14590028.html