Exemple #1
0
        public void KmlWriteTestv2_0()
        {
            // instantiate and load the gpx test document.
            XmlStreamSource source = new XmlStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test.v2.0.kml"));
            KmlDocument document = new KmlDocument(source);
            object      kml      = document.Kml;

            if (kml is OsmSharp.Xml.Kml.v2_0.kml)
            { // all ok here!
                MemoryStream write_file = new MemoryStream();

                // create a new xml source.
                XmlStreamSource write_source = new XmlStreamSource(write_file);
                KmlDocument     kml_target   = new KmlDocument(write_source);

                // set the target data the same as the source document.
                kml_target.Kml = kml;

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

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

                // check to see if the data was writter correctly.
                // instantiate and load the osm test document.
                source   = new XmlStreamSource(write_file);
                document = new KmlDocument(source);
                kml      = document.Kml;

                // check the result that was written and then read again.
                if (kml is OsmSharp.Xml.Kml.v2_0.kml)
                { // all ok here!
                    OsmSharp.Xml.Kml.v2_0.kml kml_type = (kml as OsmSharp.Xml.Kml.v2_0.kml);

                    // test the gpx test file content.
                    Assert.IsNotNull(kml_type.Item, "No item was found!");
                    Assert.IsInstanceOf <OsmSharp.Xml.Kml.v2_0.Placemark>(kml_type.Item, "Incorrect item type!");

                    OsmSharp.Xml.Kml.v2_0.Placemark type = (kml_type.Item as OsmSharp.Xml.Kml.v2_0.Placemark);
                    Assert.AreEqual(type.Items.Length, 3, "Incorrect number of items in folder!");
                }
                else
                {
                    Assert.Fail("No kml data was read, or data was of the incorrect type!");
                }

                document.Close();
                source.Close();
            }
            else
            {
                Assert.Fail("No kml data was read, or data was of the incorrect type!");
            }

            document.Close();
            source.Close();
        }
Exemple #2
0
        public void KmlReadTestv2_0()
        {
            // instantiate and load the gpx test document.
            XmlStreamSource source = new XmlStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test.v2.0.kml"));
            KmlDocument document = new KmlDocument(source);
            object      kml      = document.Kml;

            if (kml is OsmSharp.Xml.Kml.v2_0.kml)
            { // all ok here!
                OsmSharp.Xml.Kml.v2_0.kml kml_type = (kml as OsmSharp.Xml.Kml.v2_0.kml);

                // test the gpx test file content.
                Assert.IsNotNull(kml_type.Item, "No item was found!");
                Assert.IsInstanceOf <OsmSharp.Xml.Kml.v2_0.Placemark>(kml_type.Item, "Incorrect item type!");

                OsmSharp.Xml.Kml.v2_0.Placemark type = (kml_type.Item as OsmSharp.Xml.Kml.v2_0.Placemark);
                Assert.AreEqual(type.Items.Length, 3, "Incorrect number of items in folder!");
            }
            else
            {
                Assert.Fail("No kml data was read, or data was of the incorrect type!");
            }

            document.Close();
            source.Close();
        }
Exemple #3
0
        /// <summary>
        /// Reads the actual Kml.
        /// </summary>
        private void DoReadKml()
        {
            // 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);
            KmlDocument     document = new KmlDocument(source);
            object          kml      = document.Kml;

            switch (document.Version)
            {
            case KmlVersion.Kmlv2_1:
                this.ConvertKml(kml as OsmSharp.Xml.Kml.v2_1.KmlType);
                break;

            case KmlVersion.Kmlv2_0_response:
                this.ConvertKml(kml as OsmSharp.Xml.Kml.v2_0_response.kml);
                break;

            case KmlVersion.Kmlv2_0:
                this.ConvertKml(kml as OsmSharp.Xml.Kml.v2_0.kml);
                break;
            }
        }
Exemple #4
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();
        }
Exemple #5
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();
        }
        /// <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;
            }
        }
Exemple #7
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();
        }
Exemple #8
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();
        }
Exemple #9
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();
            }
        }