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" ?> " "<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="Graphhopper" version="1.1"> " "<metadata> " "<copyright author="OpenStreetMap contributors"/> " "<text>GraphHopper GPX</text> ") f.write("<uid>"+userid+"</uid> ") #要改uid f.write("<time>"+time+"T00:00:00+00:00</time> ") #要改时间 f.write("</metadata> " "<trk><name>GraphHopper</name><trkseg> ") 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> " 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