private List <GPS> ConvertBinary(string file) { List <GPS> GpsArray = new List <GPS>(); try { System.IO.FileStream FS = new System.IO.FileStream(file, FileMode.Open, FileAccess.Read); BinaryReader BR = new BinaryReader(FS); Byte[] r = BR.ReadBytes(Convert.ToInt32(FS.Length)); FileInfo fi = new FileInfo(file); string id = fi.Name.Substring(0, fi.Name.Length - 4); for (int i = 0; i < FS.Length / 39; i++) { Byte[] longitude = new Byte[4]; Byte[] latitude = new Byte[4]; Byte[] GPStime = new Byte[8]; Byte[] state = new Byte[1]; Byte[] speed = new Byte[2]; Byte[] direction = new Byte[2]; Byte[] km = new Byte[4]; Byte[] temp = new Byte[2]; Byte[] oil = new Byte[2]; for (int j = 0; j < 4; j++) { longitude[j] = r[39 * i + 3 - j]; } for (int j = 0; j < 4; j++) { latitude[j] = r[39 * i + 7 - j]; } for (int j = 0; j < 8; j++) { GPStime[j] = r[39 * i + 15 - j]; //GPStime[j] = r[39 * i + j]; } state[0] = r[39 * i + 16]; //char[] ch = Encoding.UTF8.GetChars(state); for (int j = 0; j < 2; j++) { speed[j] = r[39 * i + 18 - j]; } for (int j = 0; j < 2; j++) { direction[j] = r[39 * i + 20 - j]; } for (int j = 0; j < 4; j++) { km[j] = r[39 * i + 24 - j]; } for (int j = 0; j < 2; j++) { temp[j] = r[39 * i + 26 - j]; } for (int j = 0; j < 2; j++) { oil[j] = r[39 * i + 38 - j]; } float a = BitConverter.ToSingle(longitude, 0); float b = BitConverter.ToSingle(latitude, 0); long c = BitConverter.ToInt64(GPStime, 0); DateTime dt = GPS.ConvertIntDatetime(c); string dts = dt.ToString("yyyy-MM-dd HH:mm:ss"); //int hour = System.Int32.Parse(dt.ToString("HH")); //int min = System.Int32.Parse(dt.ToString("mm")); //int sec = System.Int32.Parse(dt.ToString("ss")); //long second = hour * 3600 + min * 60 + sec; Int16 f = BitConverter.ToInt16(speed, 0); Int16 h = (Int16)(BitConverter.ToInt16(direction, 0) * 45); float k = BitConverter.ToSingle(km, 0); Int16 t = BitConverter.ToInt16(temp, 0); Int16 o = BitConverter.ToInt16(oil, 0); //float k = BitConverter.ToSingle(km, 0); GPS gpsData = new GPS() { ID = id, Longitude = a, Latitude = b, Time = dt, Speed = f, State = state[0], Direction = h, KM = k, TEMP = t, Oil = o }; if (dt.Year == 2016 && gpsData.Longitude <= 180 && gpsData.Longitude >= -180 && gpsData.Latitude <= 90 && gpsData.Latitude >= -90) { GpsArray.Add(gpsData); } } GpsArray = GpsArray.OrderBy(a => a.Time).ToList(); //按照GPS时间排序 FS.Close(); BR.Close(); return(GpsArray); } catch (Exception ex) { logger.Error("Error: File format incorrect! "); return(null); } }
//[STAThread()] private List <GPS> CalculateStayPoint(List <GPS> GPSDataCol) { int i = 0; int pointNum = GPSDataCol.Count; List <GPS> vSP = new List <GPS>(); GPS pCurrentPt = null; GPS pNextPt = null; int ptrStart = 0; int ptrEnd = 0; double interval = 0.0; string stationName = ""; if (pointNum < 2) { return(GPSDataCol); } //先剔除位置错误的点 for (int j = 0; j < GPSDataCol.Count; j++) { if (j < GPSDataCol.Count - 1) { pCurrentPt = GPSDataCol[j]; pNextPt = GPSDataCol[j + 1]; //if (pCurrentPt.Time.ToString() == "3/7/2016 12:03:14 AM") //{ // Console.WriteLine("test"); //} double distance = 0.0; using (Geodesic geod = new Geodesic()) { geod.Inverse(pCurrentPt.Latitude, pCurrentPt.Longitude, pNextPt.Latitude, pNextPt.Longitude, out distance); } double deltaTime = (pNextPt.Time - pCurrentPt.Time).TotalHours; //速度不可能达到的极限,说明存在错误点 if (distance / (1000 * deltaTime) > speedError) { GPSDataCol.RemoveAt(j + 1); j = j - 1; } } } pointNum = GPSDataCol.Count; if (pointNum < 2) { return(GPSDataCol); } ptrStart = ptrEnd = 0; while (ptrEnd < pointNum) { if (ptrEnd == pointNum - 1) { if (ptrStart != ptrEnd) { stationName = ""; Point Position = ComputeMeanCoord(GPSDataCol, ptrStart, ptrEnd, ref stationName); pCurrentPt.Longitude = Position.x; pCurrentPt.Latitude = Position.y; pCurrentPt.Stay = 1; vSP.Add(pCurrentPt); pNextPt.Longitude = Position.x; pNextPt.Latitude = Position.y; pNextPt.Stay = 2; vSP.Add(pNextPt); } } if (ptrStart > pointNum - 1 || ptrEnd > pointNum - 1) { return(vSP); } if (ptrStart == 0) { pCurrentPt = GPSDataCol[0]; pNextPt = GPSDataCol[1]; ptrStart++; } else { if (ptrStart == ptrEnd && ptrStart < pointNum - 1) { ptrEnd = ptrStart + 1; } pCurrentPt = GPSDataCol[ptrStart]; pNextPt = GPSDataCol[ptrEnd]; } double distance = 0.0; using (Geodesic geod = new Geodesic()) { geod.Inverse(pCurrentPt.Latitude, pCurrentPt.Longitude, pNextPt.Latitude, pNextPt.Longitude, out distance); } //if (double.IsNaN(distance)) distance = 0; if (distance >= disError) { vSP.Add(pCurrentPt); ptrStart = ++ptrEnd; continue; } if (distance < disThreh) { ptrEnd++; continue; } else { interval = (pNextPt.Time - pCurrentPt.Time).TotalSeconds; if (interval >= timeThreh) { stationName = ""; Point Position = ComputeMeanCoord(GPSDataCol, ptrStart, ptrEnd, ref stationName); pCurrentPt.Longitude = Position.x; pCurrentPt.Latitude = Position.y; pCurrentPt.Stay = 1; vSP.Add(pCurrentPt); pNextPt.Longitude = Position.x; pNextPt.Latitude = Position.y; pNextPt.Stay = 2; vSP.Add(pNextPt); ptrStart = ++ptrEnd; continue; } else { if (ptrStart == 1) { pCurrentPt.Stay = 0; vSP.Add(pCurrentPt); } pNextPt.Stay = 0; vSP.Add(pNextPt); ptrStart = ++ptrEnd; continue; } } } return(vSP); }