static void updateAndInterpolateCarIDTxts(string originalDataFolder, string writeFolder, string dateStr, string fullCarIDsTxtPath) // 这个不用了 { CreateDictionary(fullCarIDsTxtPath, dateStr); if (!Directory.Exists(writeFolder)) // G:\interpolation_data\01AllCar\ { Directory.CreateDirectory(writeFolder); } string[] names = Directory.GetFiles(originalDataFolder); for (int pathIndex = 0; pathIndex < names.Length; pathIndex++) // 读取第二遍,写第一遍 { string path = names[pathIndex]; //Console.WriteLine("将原数据塞入carIDwithInfo,读取进度:{0}/{1} ", pathIndex, names.Length); StreamReader sr = new StreamReader(path, Encoding.Default); string line; //读取第二遍原始数据 while ((line = sr.ReadLine()) != null) { CarState carState; string[] oneRecord = line.Split('|'); string objectID = oneRecord[0]; string GPStime = oneRecord[9]; double longitude = Convert.ToDouble(oneRecord[10]); double latitude = Convert.ToDouble(oneRecord[11]); int state = Convert.ToInt32(oneRecord[3]); //是否空车 if (state == 1) { carState = CarState.Vacant; } else { carState = CarState.InUse; } if (GPStime.Length < "2016-08-01 11:00:00".Length) { continue; } if (DateTime.Parse(GPStime) >= DateTime.Parse("2016-08-" + dateStr + " 00:00:00") && DateTime.Parse(GPStime) < DateTime.Parse("2016-08-" + dateStr + " 23:59:59")) { OnePoint oneCarInfo = new OnePoint(new GISVertex(longitude, latitude), carState, objectID, GPStime); TrackPoints oneCarTrack = carIDwithInfo[objectID]; //key:carID -> ListofCarID oneCarTrack.addTrackPoint(oneCarInfo); //调用ListOfCarID的方法,将文件中一行的carInfo加到Dictionaty的ListOfCarID中 } } } //int itemCount = 0; foreach (var item in carIDwithInfo) { // itemCount++; //遍历每一ID对应的item if (item.Value.ShowCarTrackNum() != 0) { List <OnePoint> thisCarTrack = item.Value.GetThisCarTrack(); string objectID = null; List <double> newLatList = new List <double>(); List <double> newLngList = new List <double>(); List <string> newGPStimeList = new List <string>(); List <string> newStateList = new List <string>(); double last_longitude = thisCarTrack[0].CarLocation.x; double last_latitude = thisCarTrack[0].CarLocation.y; DateTime last_GPStime = DateTime.Parse(thisCarTrack[0].GPSTime); string last_state = thisCarTrack[0].State.ToString(); //OneJourney oneJourney = null; for (int i = 1; i < thisCarTrack.Count; i++) { objectID = thisCarTrack[i].CarID; double current_longitude = thisCarTrack[i].CarLocation.x; double current_latitude = thisCarTrack[i].CarLocation.y; DateTime current_GPStime = DateTime.Parse(thisCarTrack[i].GPSTime); string current_state = thisCarTrack[i].State.ToString(); if (current_GPStime.Minute - last_GPStime.Minute > 1) { int step_num = current_GPStime.Minute - last_GPStime.Minute; for (int j = 0; j < step_num; j++) { //插值,3s-6s 插入4s 5s 6s DateTime interpolate_time = last_GPStime.AddMinutes(j + 1); string time = interpolate_time.ToShortDateString() + " " + interpolate_time.Hour.ToString() + ":" + interpolate_time.Minute + ":" + "00"; //time格式化为:2016/8/3 11:2:18 double latitude = current_latitude + (DateTime.Parse(time).TimeOfDay.TotalSeconds - current_GPStime.TimeOfDay.TotalSeconds) * (current_latitude - last_latitude) / (current_GPStime - last_GPStime).TotalSeconds; //线性插值公式进行插值 double longitude = current_longitude + (DateTime.Parse(time).TimeOfDay.TotalSeconds - current_GPStime.TimeOfDay.TotalSeconds) * (current_longitude - last_longitude) / (current_GPStime - last_GPStime).TotalSeconds; //线性插值公式进行插值 newLatList.Add(latitude); newLngList.Add(longitude); newStateList.Add(current_state); newGPStimeList.Add(DateTime.Parse(time).ToString()); //time格式化为:2016/8/3 11:02:18 } } if (current_GPStime.Minute - last_GPStime.Minute == 1 || current_GPStime.Minute - last_GPStime.Minute == -59) { string time = current_GPStime.ToShortDateString() + " " + current_GPStime.Hour.ToString() + ":" + current_GPStime.Minute + ":" + "00"; //time格式化为:2016/8/3 11:2:18 double latitude = current_latitude + (DateTime.Parse(time).TimeOfDay.TotalSeconds - current_GPStime.TimeOfDay.TotalSeconds) * (current_latitude - last_latitude) / (current_GPStime - last_GPStime).TotalSeconds; //线性插值公式进行插值 double longitude = current_longitude + (DateTime.Parse(time).TimeOfDay.TotalSeconds - current_GPStime.TimeOfDay.TotalSeconds) * (current_longitude - last_longitude) / (current_GPStime - last_GPStime).TotalSeconds; //线性插值公式进行插值 newLatList.Add(latitude); newLngList.Add(longitude); newStateList.Add(current_state); newGPStimeList.Add(DateTime.Parse(time).ToString()); //time格式化为:2016/8/3 11:02:18 } last_GPStime = current_GPStime; last_latitude = current_latitude; last_longitude = current_longitude; last_state = current_state; //写第一遍原始数据 //sw.WriteLine(thisCarTrack[i].CarID + "," + thisCarTrack[i].CarLocation.x + "," + thisCarTrack[i].CarLocation.y + "," + thisCarTrack[i].GPSTime + "," + thisCarTrack[i].State); } //Console.WriteLine("将原数据塞入各自id的txt,写入进度:{0}/{1} ",itemCount,carIDwithInfo.Count); string WritePath = writeFolder + item.Key + "_Intepolation.txt"; FileStream fs = new FileStream(WritePath, FileMode.Create); StreamWriter sw = new StreamWriter(fs); for (int i = 0; i < newGPStimeList.Count; i++) { //写第二遍插值后的原数据 sw.WriteLine(objectID + "," + newLngList[i] + "," + newLatList[i] + "," + newGPStimeList[i] + "," + newStateList[i]); } //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } }
public void addTrackPoint(OnePoint oneCar) { carTrack.Add(oneCar); }
static void updatedRecordsIntoCarIDTxts(string originalDataFolder, string writeFolder, string dateStr, string fullCarIDsTxtPath) { CreateDictionary(fullCarIDsTxtPath, dateStr); if (!Directory.Exists(writeFolder)) // G:\interpolation_data\01AllCar\ { Directory.CreateDirectory(writeFolder); } string[] names = Directory.GetFiles(originalDataFolder); for (int pathIndex = 0; pathIndex < names.Length; pathIndex++) // 读取第二遍,写第一遍 { string path = names[pathIndex]; //Console.WriteLine("将原数据塞入各自id的txt,读取进度: " + path); StreamReader sr = new StreamReader(path, Encoding.Default); string line; //读取第二遍原始数据 while ((line = sr.ReadLine()) != null) { CarState carState; string[] oneRecord = line.Split('|'); string objectID = oneRecord[0]; string GPStime = oneRecord[9]; double longitude = Convert.ToDouble(oneRecord[10]); double latitude = Convert.ToDouble(oneRecord[11]); int state = Convert.ToInt32(oneRecord[3]); //是否空车 if (state == 1) { carState = CarState.Vacant; } else { carState = CarState.InUse; } if (GPStime.Length < "2016-08-01 11:00:00".Length) { continue; } if (DateTime.Parse(GPStime) >= DateTime.Parse("2016-08-" + dateStr + " 00:00:00") && DateTime.Parse(GPStime) < DateTime.Parse("2016-08-" + dateStr + " 23:59:59")) { OnePoint oneCarInfo = new OnePoint(new GISVertex(longitude, latitude), carState, objectID, GPStime); TrackPoints oneCarTrack = carIDwithInfo[objectID]; //key:carID -> ListofCarID oneCarTrack.addTrackPoint(oneCarInfo); //调用ListOfCarID的方法,将文件中一行的carInfo加到Dictionaty的ListOfCarID中 } } } //将Dictionary中的数据导出 foreach (var item in carIDwithInfo) { if (item.Value.ShowCarTrackNum() != 0) { string writePath = writeFolder + item.Key + ".txt"; FileStream fs = new FileStream(writePath, FileMode.Append); StreamWriter sw = new StreamWriter(fs); List <OnePoint> thisCarTrack = item.Value.GetThisCarTrack(); for (int i = 0; i < thisCarTrack.Count; i++) { //写第一遍原始数据 sw.WriteLine(thisCarTrack[i].CarID + "," + thisCarTrack[i].CarLocation.x + "," + thisCarTrack[i].CarLocation.y + "," + thisCarTrack[i].GPSTime + "," + thisCarTrack[i].State); } sw.Flush(); sw.Close(); fs.Close(); } //Console.WriteLine("将原数据塞入各自id的txt,写入进度: " + item.Key); } //Console.WriteLine("time duration: {0}", TimeDiff(start, end)); }