Example #1
0
        public void ReadGpxData(GpxFile gpxFile, Stream stream)
        {
            XmlSerializer serializer = new XmlSerializer (typeof (gpx));
            gpx gpx = (gpx)serializer.Deserialize (stream);

            if (gpx.trk != null)
            {
                foreach (gpxTrk trkType in gpx.trk)
                {
                    Track track = new Track ();

                    foreach (gpxTrkTrkseg trkseg in trkType.trkseg)
                    {
                        if (trkseg.trkpt.Length > 0)
                        {
                            TrackSegment segment = new TrackSegment ();
                            foreach (gpxTrkTrksegTrkpt trkpt in trkseg.trkpt)
                            {
                                TrackPoint waypoint = ReadTrackPoint (trkpt);
                                segment.AddPoint (waypoint);
                            }

                            track.AddSegment (segment);
                        }
                    }

                    gpxFile.AddTrack (track);
                }
            }

            if (gpx.wpt != null)
            {
                foreach (gpxWpt wptType in gpx.wpt)
                {
                    TrackPoint waypoint = ReadWayoint(wptType);
                    gpxFile.AddWaypoint (waypoint);
                }
            }

            if (gpx.rte != null)
            {
                foreach (gpxRte rteType in gpx.rte)
                {
                    Track route = new Track ();

                    if (rteType.rtept != null)
                    {
                        TrackSegment segment = new TrackSegment ();
                        foreach (gpxRteRtept wptType in rteType.rtept)
                        {
                            TrackPoint waypoint = ReadRoutePoint (wptType);
                            segment.AddPoint (waypoint);
                        }
                    }

                    gpxFile.AddRoute (route);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Calculates statistics for a <see cref="IGpsTrack"/> object.
        /// </summary>
        /// <param name="gpsTrack"><see cref="IGpsTrack"/> object to calculate statistics for.</param>
        /// <returns>GpsTrackStatistics object containing the statistics data.</returns>
        public static GpsTrackStatistics CalculateTrackStatistics(Track track)
        {
            GpsTrackStatistics stat = new GpsTrackStatistics ();

            TrackPoint lastPoint = null;
            TrackPoint lastPointWithElevation = null;

            foreach (TrackSegment segment in track.Segments)
            {
                foreach (TrackPoint point in segment.Points)
                {
                    stat.Bounds.ExtendToCover(point.Location);
                    if (point.Elevation.HasValue)
                    {
                        stat.MinElevation = Math.Min(stat.MinElevation, point.Elevation.Value);
                        stat.MaxElevation = Math.Max(stat.MaxElevation, point.Elevation.Value);
                    }

                    if (point.Time.HasValue)
                    {
                        if (false == stat.StartTime.HasValue || point.Time < stat.StartTime)
                            stat.StartTime = point.Time.Value;
                        if (false == stat.EndTime.HasValue || point.Time > stat.EndTime)
                            stat.EndTime = point.Time.Value;
                    }

                    if (lastPoint != null)
                    {
                        double horizontalDistance = GpsUtilities.CalculateHorizontalDistance(lastPoint, point);
                        stat.HorizontalDistance += horizontalDistance;
                    }

                    if (lastPointWithElevation != null)
                    {
                        // calculate ascent/descent
                        if (point.Elevation.HasValue)
                        {
                            double elevationDifference = point.Elevation.Value - lastPointWithElevation.Elevation.Value;
                            if (elevationDifference < 0)
                                stat.TotalDescent -= elevationDifference;
                            else
                                stat.TotalAscent += elevationDifference;
                        }
                    }

                    lastPoint = point;

                    if (point.Elevation.HasValue)
                        lastPointWithElevation = point;
                }
            }

            return stat;
        }