Ejemplo n.º 1
0
        public void Save_ThrowsExceptionIfPathIsNull()
        {
            string path   = null;
            var    target = new GpxDocument();

            Assert.Throws <ArgumentNullException>(() => target.Save(path));
        }
Ejemplo n.º 2
0
        public ActionResult MeshNodeTrack(string id, DateTime?start, DateTime?stop)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return(new ContentResult {
                    Content = "Invalid id"
                });
            }

            start = start ?? DateTime.Today.AddDays(-1);
            stop  = stop ?? DateTime.Now;

            GpxDocument gpx = new GpxDocument();

            gpx.StartTrack(id + " track");
            using (var mnc = new MeshNodeEntities())
            {
                foreach (var point in (from p in mnc.Locations where p.Name == id && p.Time >= start && p.Time <= stop select p))
                {
                    gpx.AppendToTrack(point.Location.Latitude.Value, point.Location.Longitude.Value, null, point.Time);
                }
            }
            gpx.FinishTrack();

            return(new FileContentResult(gpx.ToUtf8(), "text/xml")
            {
                FileDownloadName = id + ".gpx"
            });
        }
Ejemplo n.º 3
0
        public void Save_IGpxWriter_ThrowsExceptionIfWriterIsNull()
        {
            IGpxWriter writer = null;

            var target = new GpxDocument();

            Assert.Throws <ArgumentNullException>(() => target.Save(writer));
        }
Ejemplo n.º 4
0
        public void Constructor_CreatesEmptyDocument()
        {
            var target = new GpxDocument();

            Assert.Empty(target.Waypoints);
            Assert.Empty(target.Routes);
            Assert.Empty(target.Tracks);
        }
Ejemplo n.º 5
0
        public void Load_LoadsGpxEntitiesFromFile()
        {
            string path = "../../../Data/Gpx/gpx-real-file.gpx";

            var target = GpxDocument.Load(path);

            Assert.Equal(3, target.Waypoints.Count);
            Assert.Equal(2, target.Routes.Count);
            Assert.Single(target.Tracks);
        }
Ejemplo n.º 6
0
        public void GpxWritev1_0Test()
        {
            // instantiate and load the gpx test document.
            XmlStreamSource source = new XmlStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test.v1.0.gpx"));
            GpxDocument document = new GpxDocument(source);
            object      gpx      = document.Gpx;

            if (gpx is OsmSharp.Xml.Gpx.v1_0.gpx)
            { // all ok here!
                // get the target file.
                MemoryStream write_file = new MemoryStream();

                // create a new xml source.
                XmlStreamSource write_source = new XmlStreamSource(write_file);
                GpxDocument     gpx_target   = new GpxDocument(write_source);

                // set the target data the same as the source document.
                gpx_target.Gpx = gpx;

                // save the data.
                gpx_target.Save();

                // close the old document.
                document.Close();
                source.Close();

                // check to see if the data was written correctly.
                // instantiate and load the gpx test document.
                source   = new XmlStreamSource(write_file);
                document = new GpxDocument(source);
                gpx      = document.Gpx;

                if (gpx is OsmSharp.Xml.Gpx.v1_0.gpx)
                { // all ok here!
                    OsmSharp.Xml.Gpx.v1_0.gpx gpx_type = (gpx as OsmSharp.Xml.Gpx.v1_0.gpx);

                    // test the gpx test file content.
                    Assert.IsNotNull(gpx_type.trk, "Gpx has not track!");
                    Assert.AreEqual(gpx_type.trk[0].trkseg.Length, 424, "Not the correct number of track segments found!");
                }
                else
                {
                    Assert.Fail("No gpx data was read, or data was of the incorrect type!");
                }
            }
            else
            {
                Assert.Fail("No gpx data was read, or data was of the incorrect type!");
            }

            document.Close();
            source.Close();
        }
Ejemplo n.º 7
0
        public void Constructor_WaypointsRoutesTracks_CreatesDocumentWithGpxEntities()
        {
            IEnumerable <GpxPoint> waypoints = new[] { new GpxPoint() };
            IEnumerable <GpxRoute> routes    = new[] { new GpxRoute() };
            IEnumerable <GpxTrack> tracks    = new[] { new GpxTrack() };

            var target = new GpxDocument(waypoints, routes, tracks);

            Assert.Equal(waypoints, target.Waypoints);
            Assert.Equal(routes, target.Routes);
            Assert.Equal(tracks, target.Tracks);
        }
Ejemplo n.º 8
0
        public void Load_IGpxReader_LoadsEntitiesFromReader()
        {
            using (var reader = new GpxReader(TestDataReader.Open("gpx-real-file.gpx"), new GpxReaderSettings()
            {
                ReadMetadata = true
            })) {
                var target = GpxDocument.Load(reader);

                Assert.Equal(3, target.Waypoints.Count);
                Assert.Equal(2, target.Routes.Count);
                Assert.Single(target.Tracks);
            }
        }
Ejemplo n.º 9
0
        public void Save_SavesDataToFile()
        {
            string path = PathHelper.GetTempFilePath("gpxdocument-save-test.gpx");

            var target = GpxDocument.Load(PathHelper.RealGpxFilePath);

            target.Save(path);

            var original = XDocument.Load(PathHelper.RealGpxFilePath);
            var saved    = XDocument.Load(path);

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(original, saved));
        }
Ejemplo n.º 10
0
        public void Load_IGpxReader_LoadsEntitiesFromReader()
        {
            using (var reader = new GpxReader(new MemoryStream(GpxTestData.gpx_real_file), new GpxReaderSettings()
            {
                ReadMetadata = true
            })) {
                var target = GpxDocument.Load(reader);

                Assert.Equal(3, target.Waypoints.Count);
                Assert.Equal(2, target.Routes.Count);
                Assert.Equal(1, target.Tracks.Count);
            }
        }
Ejemplo n.º 11
0
        public static List <Tile> GetIntersectingTiles(GpxDocument doc, int level)
        {
            var result = new List <Tile>();
            var bounds = doc.Bounds();
            var tiles  = BboxToTiles(bounds, 14);

            foreach (var tile in tiles)
            {
                if (doc.Intersects(tile.Bounds()))
                {
                    result.Add(tile);
                }
            }
            return(result);
        }
Ejemplo n.º 12
0
        public void Save_SavesDataToFile()
        {
            string path = "TestFiles\\gpxdocument-save-test.gpx";

            File.Delete(path);

            var target = GpxDocument.Load("../../src/Tests.SpatialLite.Gps/Data/Gpx/gpx-real-file.gpx");

            target.Save(path);

            var original = XDocument.Load("../../src/Tests.SpatialLite.Gps/Data/Gpx/gpx-real-file.gpx");
            var saved    = XDocument.Load(path);

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(original, saved, null));
        }
Ejemplo n.º 13
0
        public void GpxReadRegression1Test()
        {
            // instantiate and load the gpx test document.
            var source = new XmlStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Test.Unittests.regression1.gpx"));
            var    document = new GpxDocument(source);
            object gpx      = document.Gpx;

            if (gpx is OsmSharp.IO.Xml.Gpx.v1_1.gpxType)
            { // all ok here!
            }
            else
            {
                Assert.Fail("No gpx data was read, or data was of the incorrect type!");
            }

            document.Close();
            source.Close();
        }
Ejemplo n.º 14
0
        internal static void Save(Stream stream, Route route)
        {
            GpxDocument gpxDocument = new GpxDocument((IXmlSource) new XmlStreamSource(stream));
            gpxType     gpxType     = new gpxType();

            gpxType.trk = new trkType[1];
            List <wptType> wptTypeList1 = new List <wptType>();
            trkType        trkType      = new trkType();
            List <wptType> wptTypeList2 = new List <wptType>();

            trkType.trkseg = new trksegType[1];
            trksegType trksegType = new trksegType();

            for (int index1 = 0; index1 < route.Segments.Count; ++index1)
            {
                RouteSegment segment = route.Segments[index1];
                if (segment.Points != null)
                {
                    for (int index2 = 0; index2 < segment.Points.Length; ++index2)
                    {
                        RouteStop point     = segment.Points[index2];
                        RouteTags routeTags = point.Tags == null ? (RouteTags)null : ((IEnumerable <RouteTags>)point.Tags).FirstOrDefault <RouteTags>((Func <RouteTags, bool>)(x => x.Value == "name"));
                        wptTypeList2.Add(new wptType()
                        {
                            lat  = (Decimal)point.Latitude,
                            lon  = (Decimal)point.Longitude,
                            name = routeTags == null ? string.Empty : routeTags.Value
                        });
                    }
                }
                wptTypeList1.Add(new wptType()
                {
                    lat = (Decimal)segment.Latitude,
                    lon = (Decimal)segment.Longitude
                });
            }
            trksegType.trkpt  = wptTypeList1.ToArray();
            trkType.trkseg[0] = trksegType;
            gpxType.trk[0]    = trkType;
            gpxType.wpt       = wptTypeList2.ToArray();
            gpxDocument.Gpx   = (object)gpxType;
            gpxDocument.Save();
        }
Ejemplo n.º 15
0
        private void DoReadGpx()
        {
            if (this._stream.CanSeek)
            {
                this._stream.Seek(0L, SeekOrigin.Begin);
            }
            GpxDocument gpxDocument = new GpxDocument((IXmlSource) new XmlStreamSource(this._stream));
            object      gpx         = gpxDocument.Gpx;

            switch (gpxDocument.Version)
            {
            case GpxVersion.Gpxv1_0:
                this.ReadGpxv1_0(gpx as gpx);
                break;

            case GpxVersion.Gpxv1_1:
                this.ReadGpxv1_1(gpx as gpxType);
                break;
            }
        }
Ejemplo n.º 16
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());
        }
Ejemplo n.º 17
0
        public static List <Tile> GetIntersectingTiles(string datadir)
        {
            var files             = Directory.GetFiles("data");
            var intersectingtiles = new List <Tile>();

            foreach (var file in files)
            {
                var gpxDocument = new GpxDocument(file);
                var tiles       = GetIntersectingTiles(gpxDocument, 14);

                foreach (var tile in tiles)
                {
                    if (!intersectingtiles.Contains(tile))
                    {
                        intersectingtiles.Add(tile);
                    }
                    ;
                }
            }
            return(intersectingtiles);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Reads the actual Gpx.
        /// </summary>
        private void DoReadGpx()
        {
            // seek to the beginning of the stream.
            if (_stream.CanSeek)
            {
                _stream.Seek(0, SeekOrigin.Begin);
            }

            // instantiate and load the gpx test document.
            XmlStreamSource source   = new XmlStreamSource(_stream);
            GpxDocument     document = new GpxDocument(source);
            object          gpx      = document.Gpx;

            switch (document.Version)
            {
            case GpxVersion.Gpxv1_0:
                this.ReadGpxv1_0(gpx as OsmSharp.Xml.Gpx.v1_0.gpx);
                break;

            case GpxVersion.Gpxv1_1:
                this.ReadGpxv1_1(gpx as Xml.Gpx.v1_1.gpxType);
                break;
            }
        }
Ejemplo n.º 19
0
        public void GpxReadv1_0Test()
        {
            // instantiate and load the gpx test document.
            XmlStreamSource source = new XmlStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test.v1.0.gpx"));
            GpxDocument document = new GpxDocument(source);
            object      gpx      = document.Gpx;

            if (gpx is OsmSharp.Xml.Gpx.v1_0.gpx)
            { // all ok here!
                OsmSharp.Xml.Gpx.v1_0.gpx gpx_type = (gpx as OsmSharp.Xml.Gpx.v1_0.gpx);

                // test the gpx test file content.
                Assert.IsNotNull(gpx_type.trk, "Gpx has no track!");
                Assert.AreEqual(gpx_type.trk[0].trkseg.Length, 424, "Not the correct number of track segments found!");
            }
            else
            {
                Assert.Fail("No gpx data was read, or data was of the incorrect type!");
            }

            document.Close();
            source.Close();
        }
Ejemplo n.º 20
0
        public void Load_string_ThrowsExceptionIfPathIsNull()
        {
            string path = null;

            Assert.Throws <ArgumentNullException>(() => GpxDocument.Load(path));
        }
Ejemplo n.º 21
0
        public void Load_IGpxReader_ThrowsExceptionIfReaderIsNull()
        {
            IGpxReader reader = null;

            Assert.Throws <ArgumentNullException>(() => GpxDocument.Load(reader));
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Saves the route to a gpx file.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="route"></param>
        internal static void Save(Stream stream, Route route)
        {
            XmlStreamSource source          = new XmlStreamSource(stream);
            GpxDocument     output_document = new GpxDocument(source);
            gpxType         output_gpx      = new gpxType();

            output_gpx.trk = new trkType[1];

            // initialize all objects.
            List <wptType> segments = new List <wptType>();
            trkType        track    = new trkType();
            List <wptType> poi_gpx  = new List <wptType>();

            track.trkseg = new trksegType[1];

            // ============= CONSTRUCT TRACK SEGMENT ==============
            trksegType track_segment = new trksegType();

            // loop over all points.
            for (int idx = 0; idx < route.Entries.Length; idx++)
            {
                // get the current entry.
                RoutePointEntry entry = route.Entries[idx];

                // ================== INITIALIZE A NEW SEGMENT IF NEEDED! ========
                wptType waypoint;
                if (entry.Points != null)
                { // loop over all points and create a waypoint for each.
                    for (int p_idx = 0; p_idx < entry.Points.Length; p_idx++)
                    {
                        RoutePoint point = entry.Points[p_idx];

                        waypoint      = new wptType();
                        waypoint.lat  = (decimal)point.Latitude;
                        waypoint.lon  = (decimal)point.Longitude;
                        waypoint.name = point.Name;
                        poi_gpx.Add(waypoint);
                    }
                }

                // insert poi's.
                double longitde = entry.Longitude;
                double latitude = entry.Latitude;

                waypoint     = new wptType();
                waypoint.lat = (decimal)entry.Latitude;
                waypoint.lon = (decimal)entry.Longitude;

                segments.Add(waypoint);
            }

            // put the segment in the track.
            track_segment.trkpt = segments.ToArray();
            track.trkseg[0]     = track_segment;

            // set the track to the output.
            output_gpx.trk[0] = track;
            output_gpx.wpt    = poi_gpx.ToArray();

            // save the ouput.
            output_document.Gpx = output_gpx;
            output_document.Save();
        }
Ejemplo n.º 23
0
        public void Load_string_ThrowsExceptionIfFileDoesntExists()
        {
            string path = "non-existing-file.gpx";

            Assert.Throws <FileNotFoundException>(() => GpxDocument.Load(path));
        }
Ejemplo n.º 24
0
        static void Main(string[] args)
        {
            var nodeIds = -1;
            var wayIds  = -1;

            var geos = new List <OsmGeo>();

            foreach (var gpxFile in Directory.EnumerateFiles("./data/", "*.gpx"))
            {
                using (var stream = File.OpenRead(gpxFile))
                {
                    // read gpx.
                    var xmlSource   = new XmlStreamSource(stream);
                    var gpxDocument = new GpxDocument(xmlSource);

                    var gpxType = gpxDocument.Gpx as OsmSharp.IO.Xml.Gpx.v1_1.gpxType;

                    if (gpxType.trk != null)
                    {
                        foreach (var trk in gpxType.trk)
                        {
                            if (trk == null ||
                                trk.trkseg == null)
                            {
                                continue;
                            }

                            foreach (var trkseg in trk.trkseg)
                            {
                                var nodeLocations = new List <Itinero.LocalGeo.Coordinate>();
                                if (trkseg.trkpt == null)
                                {
                                    continue;
                                }

                                foreach (var wpt in trkseg.trkpt)
                                {
                                    nodeLocations.Add(
                                        new Itinero.LocalGeo.Coordinate((float)wpt.lat,
                                                                        (float)wpt.lon));
                                }

                                var nodeLocationArray = Itinero.LocalGeo.Extensions.Simplify(
                                    nodeLocations.ToArray(), 1);

                                var nodes = new List <long>();
                                foreach (var nodeLocation in nodeLocationArray)
                                {
                                    var node = new Node()
                                    {
                                        Id          = nodeIds,
                                        Latitude    = (float)nodeLocation.Latitude,
                                        Longitude   = (float)nodeLocation.Longitude,
                                        UserId      = 0,
                                        UserName    = "******",
                                        ChangeSetId = -1
                                    };
                                    nodes.Add(nodeIds);
                                    nodeIds--;

                                    geos.Add(node);
                                }

                                if (nodes.Count > 0)
                                {
                                    var way = new Way()
                                    {
                                        Id          = wayIds,
                                        Nodes       = nodes.ToArray(),
                                        UserId      = 0,
                                        UserName    = "******",
                                        ChangeSetId = -1
                                    };
                                    wayIds--;

                                    geos.Add(way);
                                }
                            }
                        }
                    }

                    if (gpxType.wpt != null)
                    {
                        foreach (var wpt in gpxType.wpt)
                        {
                            var node = new Node()
                            {
                                Id        = nodeIds,
                                Latitude  = (float)wpt.lat,
                                Longitude = (float)wpt.lon,
                                Tags      = new TagsCollection(
                                    new Tag("name", wpt.name)
                                    ),
                                UserId      = 0,
                                UserName    = "******",
                                ChangeSetId = -1
                            };
                            nodeIds--;

                            geos.Add(node);
                        }
                    }
                }
            }

            geos.Sort((x, y) =>
            {
                if (x.Type == y.Type)
                {
                    return(x.Id.Value.CompareTo(y.Id.Value));
                }
                if (x.Type == OsmSharp.OsmGeoType.Node)
                {
                    return(-1);
                }
                else if (x.Type == OsmSharp.OsmGeoType.Way)
                {
                    if (y.Type == OsmSharp.OsmGeoType.Node)
                    {
                        return(1);
                    }
                    return(-1);
                }
                return(1);
            });

            using (var targetStream = File.Open("output.osm", FileMode.Create))
            {
                var target = new OsmSharp.Streams.XmlOsmStreamTarget(targetStream);
                target.RegisterSource(geos);
                target.Pull();
                target.Flush();
            }
        }