Esempio n. 1
0
        public static void ReadGPX(string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                using (GpxReader reader = new GpxReader(fs))
                {
                    while (reader.Read())
                    {
                        switch (reader.ObjectType)
                        {
                        case GpxObjectType.Metadata:
                            GpxMetadata metadata = reader.Metadata;
                            Logger.Info("Metadata");
                            break;

                        case GpxObjectType.WayPoint:
                            GpxWayPoint waypoint = reader.WayPoint;
                            Logger.Info("WayPoint");
                            break;

                        case GpxObjectType.Route:
                            GpxRoute route = reader.Route;
                            Logger.Info("Route");
                            break;

                        case GpxObjectType.Track:
                            GpxTrack track = reader.Track;
                            Logger.Info("Track");
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Read gpx file segments as enumerable GeoPoints
        /// </summary>
        /// <param name="gpxFileStream">GPX file stream</param>
        /// <returns></returns>
        public static IEnumerable <IEnumerable <T> > ReadGPX_Segments <T>(Stream gpxFileStream
                                                                          , Func <GpxTrackPoint, T> conversionFunc
                                                                          , Func <GpxRoutePoint, T> routeConversionFunc)
        {
            IEnumerable <IEnumerable <T> > segments = Enumerable.Empty <IEnumerable <T> >();

            using (GpxReader reader = new GpxReader(gpxFileStream))
            {
                while (reader.Read())
                {
                    switch (reader.ObjectType)
                    {
                    case GpxObjectType.Track:
                        GpxTrack track = reader.Track;
                        segments = segments.Concat(ConvertTrack(track, conversionFunc));
                        break;

                    case GpxObjectType.Route:
                        GpxRoute route = reader.Route;
                        segments = segments.Concat(ConvertRoute(route, routeConversionFunc));
                        break;
                    }
                }
            }
            return(segments);
        }
Esempio n. 3
0
        public void WriteRoute(GpxRoute route)
        {
            Writer_.WriteStartElement("rte");
            WriteTrackOrRoute(route);

            foreach (GpxRoutePoint routePoint in route.RoutePoints)
            {
                WriteRoutePoint("rtept", routePoint);
            }

            Writer_.WriteEndElement();
        }
Esempio n. 4
0
 public ActionResult Edit([Bind(Include = "Id,UserId,Name,Distance,Time,Elevation,SentDate,FilePath,MapUrl")] GpxRoute gpxRoute)
 {
     if (ModelState.IsValid)
     {
         db.Entry(gpxRoute).State = EntityState.Modified;
         db.Entry(gpxRoute).Property("FilePath").IsModified = false;
         db.Entry(gpxRoute).Property("MapUrl").IsModified   = false;
         db.Entry(gpxRoute).Property("UserId").IsModified   = false;
         db.SaveChanges();
         return(RedirectToAction("Index", "GpxRoutes"));
     }
     return(View(gpxRoute));
 }
Esempio n. 5
0
        public void WriteRoute(GpxRoute route)
        {
            Writer_.WriteStartElement("rte");

            WriteTrackOrRoute(route);

            foreach (GpxRoutePoint routePoint in route.RoutePoints)
            {
                WriteRoutePoint("rtept", routePoint);
            }

            Writer_.WriteEndElement();
        }
Esempio n. 6
0
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            GpxRoute gpxRoute = db.GpxRoutes.Find(id);

            if (gpxRoute == null)
            {
                return(HttpNotFound());
            }
            return(View(gpxRoute));
        }
Esempio n. 7
0
        /// <summary>
        /// Writes the given route to the output stream
        /// </summary>
        /// <param name="route">The route to write</param>
        public void Write(GpxRoute route)
        {
            _xmlWriter.WriteStartElement("rte");

            for (int i = 0; i < route.Points.Count; i++)
            {
                WritePoint(route.Points[i], "rtept");
            }
            if (this.Settings.WriteMetadata)
            {
                this.WriteTrackMetadata(route.Metadata);
            }

            _xmlWriter.WriteEndElement();
        }
        public LoadedGpxFileTourInfo(LoadedGpxFile file, GpxRoute rawRouteData)
        {
            this.File            = file;
            this.RawRouteData    = rawRouteData;
            this.RawTrackOrRoute = rawRouteData;

            rawRouteData.Extensions ??= new GpxExtensions();
            this.RawTourExtensionData = rawRouteData.Extensions.GetOrCreateExtension <RouteExtension>();

            this.Segments = new List <LoadedGpxFileTourSegmentInfo>(1);
            this.Segments.Add(new LoadedGpxFileTourSegmentInfo(rawRouteData));

            this.Waypoints = file.Waypoints;

            this.CalculateTourMetrics();
        }
Esempio n. 9
0
        public void Constructor_IEnumerablePoints_CreatesRouteWithPoints()
        {
            List <GpxPoint> points = new List <GpxPoint> {
                new GpxPoint(16.5, 45.9, 100, new DateTime(2011, 2, 24, 20, 00, 00)),
                new GpxPoint(16.6, 46.0, 110, new DateTime(2011, 2, 24, 20, 00, 10)),
                new GpxPoint(16.5, 46.1, 200, new DateTime(2011, 2, 24, 20, 00, 20))
            };

            GpxRoute target = new GpxRoute(points);

            Assert.Equal(points.Count, target.Points.Count);
            for (int i = 0; i < target.Points.Count; i++)
            {
                Assert.Same(points[i], target.Points[i]);
            }
        }
Esempio n. 10
0
        private void ReadRouteExtensions(GpxRoute route)
        {
            if (reader.IsEmptyElement)
            {
                return;
            }

            string elementName = reader.Name;

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:

                    if (reader.NamespaceURI == GpxNamespaces.GARMIN_EXTENSIONS_NAMESPACE)
                    {
                        switch (reader.LocalName)
                        {
                        case "RouteExtension":
                            ReadGarminTrackOrRouteExtensions(route);
                            break;

                        default:
                            reader.SkipElement();
                            break;
                        }

                        break;
                    }

                    reader.SkipElement();
                    break;

                case XmlNodeType.EndElement:
                    if (reader.Name != elementName)
                    {
                        throw new FormatException(reader.Name);
                    }
                    return;
                }
            }

            throw new FormatException(elementName);
        }
Esempio n. 11
0
        private IEnumerable <GpxRoute> SerializeRoutes(GpsData data)
        {
            foreach (var route in data.Routes)
            {
                var rte = new GpxRoute();

                SerializeRouteMetadata(route, rte, x => x.Name, (gpx, s) => gpx.name        = s);
                SerializeRouteMetadata(route, rte, x => x.Description, (gpx, s) => gpx.desc = s);
                SerializeRouteMetadata(route, rte, x => x.Comment, (gpx, s) => gpx.cmt      = s);

                rte.rtept = new GpxPoint[route.Waypoints.Count];
                for (var j = 0; j < route.Waypoints.Count; j++)
                {
                    rte.rtept[j] = ConvertToGpxPoint(route.Waypoints[j]);
                }
                yield return(rte);
            }
        }
Esempio n. 12
0
        private static IEnumerable <IEnumerable <T> > ConvertRoute <T>(GpxRoute route, Func <GpxRoutePoint, T> conversionFunc)
        {
            IEnumerable <IEnumerable <T> > segments = null;

            if (route == null || route.RoutePoints == null)
            {
                throw new ArgumentNullException("route", "Route is empty.");
            }

            try
            {
                segments = Enumerable.Range(0, 1).Select(_ => route.RoutePoints.Select(pt => conversionFunc(pt)));
            }
            catch (Exception)
            {
                throw;
            }
            return(segments);
        }
Esempio n. 13
0
        public ActionResult DeleteConfirmed(int id)
        {
            GpxRoute gpxRoute = db.GpxRoutes.Find(id);

            if (System.IO.File.Exists(gpxRoute.FilePath))
            {
                try
                {
                    System.IO.File.Delete(gpxRoute.FilePath);
                }
                catch (Exception e)
                {
                    Log.Error(e.Message);
                }
            }
            db.GpxRoutes.Remove(gpxRoute);
            db.SaveChanges();
            return(RedirectToAction("Index", "GpxRoutes"));
        }
Esempio n. 14
0
        public void Save_IGpxWriter_WritesDataToWriter()
        {
            var waypoint = new GpxPoint();
            var route    = new GpxRoute();
            var track    = new GpxTrack();

            Mock <IGpxWriter> writerM = new Mock <IGpxWriter>();

            writerM.Setup(w => w.Write(waypoint)).Verifiable();
            writerM.Setup(w => w.Write(route)).Verifiable();
            writerM.Setup(w => w.Write(track)).Verifiable();

            var target = new GpxDocument(new[] { waypoint }, new[] { route }, new[] { track });

            target.Save(writerM.Object);

            writerM.Verify(w => w.Write(waypoint), Times.Once());
            writerM.Verify(w => w.Write(route), Times.Once());
            writerM.Verify(w => w.Write(track), Times.Once());
        }
Esempio n. 15
0
        /// <summary>
        /// Reads a GPX route from the internal XmlReader
        /// </summary>
        /// <returns>the route parsed from the XmlReader</returns>
        private GpxRoute ReadRoute()
        {
            GpxRoute result = new GpxRoute();

            if (_xmlReader.IsEmptyElement == false)
            {
                _xmlReader.Read();

                GpxTrackMetadata metadata = null;
                if (this.Settings.ReadMetadata)
                {
                    metadata = new GpxTrackMetadata();
                }

                while ((_xmlReader.NodeType == XmlNodeType.EndElement && _xmlReader.Name == "rte") == false)
                {
                    bool elementParsed = false;

                    if (_xmlReader.Name == "rtept")
                    {
                        result.Points.Add(ReadPoint("rtept"));
                        elementParsed = true;
                    }

                    if (this.Settings.ReadMetadata)
                    {
                        elementParsed = elementParsed || this.TryReadTrackMetadata(metadata);
                    }

                    if (!elementParsed)
                    {
                        _xmlReader.Skip();
                    }
                }

                result.Metadata = metadata;
            }
            _xmlReader.Skip();

            return(result);
        }
Esempio n. 16
0
        private IEnumerable <GpxRoute> SerializeRoutes(GpsData data)
        {
            foreach (var route in data.Routes)
            {
                var rte = new GpxRoute();

                SerializeRouteMetadata(route, rte, x => x.Name, (gpx, s) => gpx.name        = s);
                SerializeRouteMetadata(route, rte, x => x.Description, (gpx, s) => gpx.desc = s);
                SerializeRouteMetadata(route, rte, x => x.Comment, (gpx, s) => gpx.cmt      = s);

                rte.rtept = new GpxWaypoint[route.Coordinates.Count];
                for (var j = 0; j < route.Coordinates.Count; j++)
                {
                    rte.rtept[j] = new GpxWaypoint
                    {
                        lat = (decimal)route.Coordinates[j].Latitude,
                        lon = (decimal)route.Coordinates[j].Longitude,
                        ele = route.Coordinates[j].Is3D ? (decimal)((Is3D)route.Coordinates[j]).Elevation : 0m
                    };
                }
                yield return(rte);
            }
        }
Esempio n. 17
0
        // GET: GpxRoutes/Details/5
        public ActionResult Details(int?id)
        {
            MapListService mapListService = new MapListService();

            ViewBag.maps = mapListService.GetMapList();

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            GpxRoute gpxRoute = db.GpxRoutes.Find(id);

            ViewBag.id  = gpxRoute.Id;
            ViewBag.key = config.BING_KEY;
            GpxOperationsService gpxOperationsService = new GpxOperationsService(gpxRoute.FilePath, gpxRoute.Name);
            GpxData gpxData = gpxOperationsService.GpxData;

            if (gpxRoute == null)
            {
                return(HttpNotFound());
            }
            return(View(gpxData));
        }
Esempio n. 18
0
        private GpxRoute ReadGpxRoute(XmlReader reader)
        {
            GpxRoute route = new GpxRoute();
            if (reader.IsEmptyElement) return route;

            string elementName = reader.Name;

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:

                        switch (reader.Name)
                        {
                            case "name":
                                route.Name = ReadContentAsString(reader);
                                break;
                            case "cmt":
                                route.Comment = ReadContentAsString(reader);
                                break;
                            case "desc":
                                route.Description = ReadContentAsString(reader);
                                break;
                            case "src":
                                route.Source = ReadContentAsString(reader);
                                break;
                            case "link":
                                route.Links.Add(ReadGpxLink(reader));
                                break;
                            case "number":
                                route.Number = int.Parse(ReadContentAsString(reader));
                                break;
                            case "type":
                                route.Type = ReadContentAsString(reader);
                                break;
                            case "extensions":
                                ReadRouteExtensions(reader, route);
                                break;
                            case "rtept":
                                route.RoutePoints.Add(ReadGpxRoutePoint(reader));
                                break;
                            default:
                                throw new FormatException(reader.Name);
                        }

                        break;

                    case XmlNodeType.EndElement:
                        if (reader.Name != elementName) throw new FormatException(reader.Name);
                        return route;
                }
            }

            throw new FormatException(elementName);
        }
Esempio n. 19
0
        private void ReadRouteExtensions(XmlReader reader, GpxRoute route)
        {
            if (reader.IsEmptyElement) return;

            string elementName = reader.Name;

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (reader.LocalName == "RouteExtension" && reader.NamespaceURI == GARMIN_EXTENSIONS)
                        {
                            ReadGarminTrackOrRouteExtensions(reader, route);
                            break;
                        }

                        SkipElement(reader);
                        break;

                    case XmlNodeType.EndElement:
                        if (reader.Name != elementName) throw new FormatException(reader.Name);
                        return;
                }
            }

            throw new FormatException(elementName);
        }
Esempio n. 20
0
        public void Constructor_Parameterless_CreatesEmptyRoute()
        {
            GpxRoute target = new GpxRoute();

            Assert.Empty(target.Points);
        }
Esempio n. 21
0
 /// <summary>
 /// Invoked when the reader has moved past a GPX rte element.
 /// </summary>
 /// <param name="route">
 /// The <see cref="GpxRoute"/> instance that represents what we read, guaranteed non-
 /// <see langword="null"/>.
 /// </param>
 /// <remarks>
 /// This is not guaranteed to be called for every GPX file.
 /// </remarks>
 public virtual void VisitRoute(GpxRoute route)
 {
 }
 public LoadedGpxFileTourSegmentInfo(GpxRoute route)
 {
     this.Points = route.RoutePoints;
 }
Esempio n. 23
0
        private void ReadRouteExtensions(GpxRoute route)
        {
            if (Reader_.IsEmptyElement) return;

            string elementName = Reader_.Name;

            while (Reader_.Read())
            {
                switch (Reader_.NodeType)
                {
                    case XmlNodeType.Element:

                        if (Reader_.NamespaceURI == GpxNamespaces.GARMIN_EXTENSIONS_NAMESPACE)
                        {
                            switch (Reader_.LocalName)
                            {
                                case "RouteExtension":
                                    ReadGarminTrackOrRouteExtensions(route);
                                    break;
                                default:
                                    SkipElement();
                                    break;
                            }

                            break;
                        }

                        SkipElement();
                        break;

                    case XmlNodeType.EndElement:
                        if (Reader_.Name != elementName) throw new FormatException(Reader_.Name);
                        return;
                }
            }

            throw new FormatException(elementName);
        }
Esempio n. 24
0
        private GpxRoute ReadGpxRoute()
        {
            GpxRoute route = new GpxRoute();

            if (reader.IsEmptyElement)
            {
                return(route);
            }

            string elementName = reader.Name;

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:

                    switch (reader.Name)
                    {
                    case GpxSymbol.Name:
                        route.Name = reader.ReadElementContentAsString();
                        break;

                    case GpxSymbol.Comment:
                        route.Comment = reader.ReadElementContentAsString();
                        break;

                    case "desc":
                        route.Description = reader.ReadElementContentAsString();
                        break;

                    case "src":
                        route.Source = reader.ReadElementContentAsString();
                        break;

                    case "link":
                        route.Links.Add(ReadGpxLink());
                        break;

                    case "number":
                        route.Number = reader.ReadElementContentAsInt();
                        break;

                    case "type":
                        route.Type = reader.ReadElementContentAsString();
                        break;

                    case "rtept":
                        route.Add(ReadGpxRoutePoint());
                        break;

                    case "extensions":
                        ReadRouteExtensions(route);
                        break;

                    default:
                        reader.SkipElement();
                        break;
                    }

                    break;

                case XmlNodeType.EndElement:
                    if (reader.Name != elementName)
                    {
                        throw new FormatException(reader.Name);
                    }
                    return(route);
                }
            }

            throw new FormatException(elementName);
        }
        private void ReadGpx(string filePath, string fileName)
        {
            List <double>   lat        = new List <double>();
            List <double>   lng        = new List <double>();
            List <double>   elevations = new List <double>();
            List <DateTime> timeList   = new List <DateTime>();

            NumberFormatInfo numberFormatInfo = new CultureInfo("en-US", false).NumberFormat;

            numberFormatInfo.NumberDecimalSeparator = ".";

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(XmlSerializeGpx.Gpx), "http://www.topografix.com/GPX/1/1");

            try
            {
                FileStream          fileStream = new FileStream(filePath, FileMode.Open);
                XmlReader           reader     = XmlReader.Create(fileStream);
                XmlSerializeGpx.Gpx gpxObj     = (XmlSerializeGpx.Gpx)xmlSerializer.Deserialize(reader);
                foreach (XmlSerializeGpx.Trkpt track in gpxObj.trk.trkseg.trkpt)
                {
                    lat.Add(track.lat);
                    lng.Add(track.lon);
                    elevations.Add(track.ele);
                    timeList.Add(track.time);
                }

                GpxData = new GpxData
                {
                    Lat        = lat,
                    Lng        = lng,
                    Distances  = CalculateDistances(lat, lng),
                    Elevations = CalculateElevations(elevations),
                    Name       = fileName,
                    Distance   = CalculateDistance(lat, lng).ToString("N", numberFormatInfo),
                    Elevation  = CalculateElevation(elevations).ToString("N", numberFormatInfo),
                    Time       = CalculateTime(timeList).ToString(@"hh\:mm\:ss"),
                    SentDate   = DateTime.Now.ToString("d", CultureInfo.CreateSpecificCulture("pl"))
                };

                var userId  = int.Parse(HttpContext.Current.Session["UserId"].ToString());
                var context = new GpxContext();
                var route   = new GpxRoute
                {
                    Name      = GpxData.Name,
                    Distance  = GpxData.Distance,
                    Time      = GpxData.Time,
                    Elevation = GpxData.Elevation,
                    SentDate  = GpxData.SentDate,
                    FilePath  = filePath,
                    MapUrl    = PrepareUrl(lat, lng),
                    UserId    = userId
                };

                if (!context.GpxRoutes.Any(r => r.Name == fileName &&
                                           r.UserId == userId &&
                                           r.Elevation == GpxData.Elevation &&
                                           r.Time == GpxData.Time &&
                                           r.Distance == GpxData.Distance))
                {
                    context.GpxRoutes.Add(route);
                }
                context.SaveChanges();

                fileStream.Close();
            }
            catch (Exception e)
            {
                var defaultGpxDataService = new DefaultGpxDataService();
                GpxData = defaultGpxDataService.GetDefaultGpxData();
                Log.Error(e.Message);
            }
        }
Esempio n. 26
0
        public void GeometryType_ReturnsRoute()
        {
            GpxRoute target = new GpxRoute();

            Assert.Equal(GpxGeometryType.Route, target.GeometryType);
        }