public GhostPlane parseTrackFile(string file, MathClass _mathClass, double allowedRecordLength) { GhostPlane ghostPlane = new GhostPlane(); Console.WriteLine("Parsing " + file); XElement trackpointsXml = XElement.Load(file); if (trackpointsXml != null) { ghostPlane.TrackPoints = new List <TrackPoint>(); ghostPlane.ID = TARGETMAX; if (trackpointsXml.Descendants("name").First() != null) { string[] title = trackpointsXml.Descendants("name").First().Value.Split(new string[] { " - " }, StringSplitOptions.None); ghostPlane.Name = title[0].Trim(); ghostPlane.Username = title.Length > 1 ? title[1] : ""; } else { ghostPlane.Name = "DA40-NG Asobo"; ghostPlane.Username = ""; } double counter = 0; DateTime lastTimer = new DateTime(); double latitudeOffset = 0; double longitudeOffset = 0; double headingOffset = 0; double altitudeOffset = 0; GeoLocation root = new GeoLocation(0, 0); foreach (var trackPoint in trackpointsXml.Descendants("trkseg").First().Elements("trkpt")) { try { // FREE VERSION if (counter > allowedRecordLength) { break; } double.TryParse(trackPoint.Attribute("lat").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out double lat); double.TryParse(trackPoint.Attribute("lon").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out double lon); lat *= Math.PI / 180; lon *= Math.PI / 180; if (counter == 0) { root = new GeoLocation(lat, lon); } double ele = 1000; double.TryParse(trackPoint.Element("ele").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out ele); DateTime time = DateTime.Parse(trackPoint.Element("time").Value); int agl = 10; if (trackPoint.Element("agl") != null) { int.TryParse(trackPoint.Element("agl").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out agl); } double velocity = 0; if (trackPoint.Element("velocity") != null) { double.TryParse(trackPoint.Element("velocity").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out velocity); } double airspeed = 0; if (trackPoint.Element("airspeed") != null) { double.TryParse(trackPoint.Element("airspeed").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out velocity); } short heading = 0; if (trackPoint.Element("heading") != null) { short.TryParse(trackPoint.Element("heading").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out heading); } short pitch = 0; if (trackPoint.Element("pitch") != null) { short.TryParse(trackPoint.Element("pitch").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out pitch); } short roll = 0; if (trackPoint.Element("roll") != null) { short.TryParse(trackPoint.Element("roll").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out roll); } int lights = 0; if (trackPoint.Element("lights") != null) { int.TryParse(trackPoint.Element("lights").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out lights); } int avionics = 0; if (trackPoint.Element("avionics") != null) { int.TryParse(trackPoint.Element("avionics").Value, NumberStyles.Any, CultureInfo.InvariantCulture, out avionics); } if (counter != 0) { counter += (time - lastTimer).TotalSeconds; } GeoLocation loc = new GeoLocation(lat, lon); // START OFFSETS if (ghostTeleport.alt != 0 && altitudeOffset == 0) { //Console.WriteLine(JsonConvert.SerializeObject(ghostTeleport, Formatting.Indented)); latitudeOffset = ghostTeleport.location.Latitude - loc.Latitude; longitudeOffset = ghostTeleport.location.Longitude - loc.Longitude; headingOffset = (ghostTeleport.radius * 180 / Math.PI) - heading; altitudeOffset = ghostTeleport.alt - ele; Console.WriteLine("Offsets: lat" + latitudeOffset + " lon" + longitudeOffset + " head" + headingOffset + " alt" + altitudeOffset); } // CONTINUE OFFSETS if (ghostTeleport.alt != 0 && ghostPlane.TrackPoints.Count > 0 && _mathClass.findDistanceBetweenPoints(ghostTeleport.location.Latitude, ghostTeleport.location.Longitude, root.Latitude, root.Longitude) > 100) { loc = _mathClass.RotatePointFrom(root, headingOffset * Math.PI / 180, loc); } loc.Latitude += latitudeOffset; loc.Longitude += longitudeOffset; heading += (short)headingOffset; ele += altitudeOffset; ghostPlane.TrackPoints.Add(new TrackPoint(loc, ele, agl, velocity, airspeed, (short)(heading > 180 ? heading - 360 : heading), (short)(pitch > 180 ? pitch - 360 : pitch), (short)(roll > 180 ? roll - 360 : roll), lights, avionics, time, counter, trackPoint.Element("message") != null ? trackPoint.Element("message").Value : "")); if (counter == 0) { counter += 0.0001; } lastTimer = time; } catch { } } ghostPlane.Length = counter; if (ghostPlane.TrackPoints.Count > 0) { Console.WriteLine(ghostPlane.TrackPoints.Count + " track points loaded"); ghostPlanes.Add(ghostPlane); } } else { MessageBox.Show("GPX file is invalid"); } //trackPlaying = null; return(ghostPlane); }
public GeoLocation RotatePointFrom(GeoLocation startPoint, double initialBearingRadians, GeoLocation endPoint) { double dist = findDistanceBetweenPoints(startPoint.Latitude, startPoint.Longitude, endPoint.Latitude, endPoint.Longitude); double bearing = findBearingToPoint(startPoint.Latitude, startPoint.Longitude, endPoint.Latitude, endPoint.Longitude); return(FindPointAtDistanceFrom(startPoint, bearing + initialBearingRadians, dist / 1000)); }