Exemplo n.º 1
0
        public IEnumerable <GpsTrack> GetTracks(string gpxFile)
        {
            var xdoc = XDocument.Load(gpxFile);

            if (String.Equals(xdoc.Root.Name.LocalName, "gpx", StringComparison.InvariantCultureIgnoreCase))
            {
                foreach (var trk in
                         from el in xdoc.Root.Elements()
                         where String.Equals(el.Name.LocalName, "trk", StringComparison.InvariantCultureIgnoreCase)
                         select el)
                {
                    var ret = new GpsTrack()
                    {
                        Name   = String.Empty,
                        Points = new List <GpsPoint>()
                    };

                    var name = trk.Elements().FirstOrDefault(xe => String.Equals(xe.Name.LocalName, "name", StringComparison.InvariantCultureIgnoreCase));
                    if (name != null)
                    {
                        ret.Name = name.Value;
                    }

                    var points = from seg in trk.Elements()
                                 where String.Equals(seg.Name.LocalName, "trkseg", StringComparison.InvariantCultureIgnoreCase)
                                 from point in seg.Elements()
                                 where String.Equals(point.Name.LocalName, "trkpt", StringComparison.InvariantCultureIgnoreCase)
                                 let gps = GetPointFromXml(point)
                                           orderby gps.Time
                                           select gps;


                    ret.Points = points.ToList();

                    yield return(ret);
                }
            }
        }
Exemplo n.º 2
0
        public PlotGraph GetGraphFromTrack(GpsTrack trk, bool useImperial)
        {
            var ret = new PlotGraph()
            {
                Name               = trk.Name,
                MaxElevation       = 0.0d,
                MinElevation       = 0.0d,
                TotalDistance      = 0.0d,
                TotalElevationGain = 0.0d,
                TotalElevationLoss = 0.0d,
                Points             = new List <PlotPoint>(trk.Points.Count)
            };

            if (trk.Points.Count < 1)
            {
                return(ret);
            }

            var last  = trk.Points[0];
            var first = new PlotPoint()
            {
                Distance  = 0.0d,
                Elevation = IfMetersToFeet(useImperial, last.Elevation)
            };

            ret.MaxElevation = IfMetersToFeet(useImperial, last.Elevation);
            ret.MinElevation = IfMetersToFeet(useImperial, last.Elevation);

            ret.Points.Add(first);

            foreach (var point in trk.Points.Skip(1))
            {
                ret.TotalDistance += IfKilometersToMiles(useImperial, DistanceBetween(last, point));

                var eleChange = point.Elevation - last.Elevation;
                if (eleChange > 0)
                {
                    ret.TotalElevationGain += IfMetersToFeet(useImperial, eleChange);
                }
                else
                {
                    ret.TotalElevationLoss += IfMetersToFeet(useImperial, eleChange * -1);
                }

                var useElev = IfMetersToFeet(useImperial, point.Elevation);
                if (useElev > ret.MaxElevation)
                {
                    ret.MaxElevation = useElev;
                }
                if (useElev < ret.MinElevation)
                {
                    ret.MinElevation = useElev;
                }

                var newPoint = new PlotPoint()
                {
                    Distance  = ret.TotalDistance,
                    Elevation = useElev
                };
                ret.Points.Add(newPoint);

                last = point;
            }

            return(ret);
        }