/// <summary>
        /// Tests preprocessing data from a PBF file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pbfFile"></param>
        public static void TestSerialization(string name, string pbfFile)
        {
            var testFile = new FileInfo(string.Format(@".\TestFiles\{0}", pbfFile));
            var stream   = testFile.OpenRead();
            var source   = new PBFOsmStreamSource(stream);

            var testOutputFile = new FileInfo(@"test.routing");

            testOutputFile.Delete();
            Stream writeStream = testOutputFile.OpenWrite();

            var tagsIndex   = new TagsTableCollectionIndex();
            var interpreter = new OsmRoutingInterpreter();
            var graph       = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex);

            var performanceInfo = new PerformanceInfoConsumer("CHSerializerFlatFile.Serialize");

            performanceInfo.Start();
            performanceInfo.Report("Pulling from {0}...", testFile.Name);

            var data = CHEdgeGraphOsmStreamTarget.Preprocess(
                source, new OsmRoutingInterpreter(), Vehicle.Car);

            var metaData = new TagsCollection();

            metaData.Add("some_key", "some_value");
            var routingSerializer = new CHEdgeFlatfileSerializer();

            routingSerializer.Serialize(writeStream, data, metaData);

            stream.Dispose();
            writeStream.Dispose();

            OsmSharp.Logging.Log.TraceEvent("CHSerializerFlatFile", OsmSharp.Logging.TraceEventType.Information,
                                            string.Format("Serialized file: {0}KB", testOutputFile.Length / 1024));

            performanceInfo.Stop();

            performanceInfo = new PerformanceInfoConsumer("CHSerializerFlatFile.Deserialize");
            performanceInfo.Start();
            performanceInfo.Report("Deserializing again...");

            // open file again and read.
            writeStream = testOutputFile.OpenRead();
            var deserializedGraph = routingSerializer.Deserialize(writeStream);

            performanceInfo.Stop();
        }
        /// <summary>
        /// Tests preprocessing data from a PBF file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pbfFile"></param>
        public static void TestSerialization(string name, string pbfFile)
        {
            var testFile = new FileInfo(string.Format(@".\TestFiles\{0}", pbfFile));
            var stream = testFile.OpenRead();
            var source = new OsmSharp.Osm.Streams.Filters.OsmStreamFilterProgress();
            source.RegisterSource(new PBFOsmStreamSource(stream));

            var testOutputFile = new FileInfo(@"test.pedestrian.routing");
            testOutputFile.Delete();
            Stream writeStream = testOutputFile.OpenWrite();

            var tagsIndex = new TagsTableCollectionIndex();
            var interpreter = new OsmRoutingInterpreter();
            var graph = new DynamicGraphRouterDataSource<CHEdgeData>(tagsIndex);

            var performanceInfo = new PerformanceInfoConsumer("CHSerializerFlatFile.Serialize");
            performanceInfo.Start();
            performanceInfo.Report("Pulling from {0}...", testFile.Name);

            var data = CHEdgeGraphOsmStreamTarget.Preprocess(
                source, new OsmRoutingInterpreter(), Vehicle.Car);

            var metaData = new TagsCollection();
            metaData.Add("some_key", "some_value");
            var routingSerializer = new CHEdgeFlatfileSerializer();
            routingSerializer.Serialize(writeStream, data, metaData);

            stream.Dispose();
            writeStream.Dispose();

            OsmSharp.Logging.Log.TraceEvent("CHSerializerFlatFile", OsmSharp.Logging.TraceEventType.Information,
                string.Format("Serialized file: {0}KB", testOutputFile.Length / 1024));

            performanceInfo.Stop();

            performanceInfo = new PerformanceInfoConsumer("CHSerializerFlatFile.Deserialize");
            performanceInfo.Start();
            performanceInfo.Report("Deserializing again...");

            // open file again and read.
            writeStream = testOutputFile.OpenRead();
            var deserializedGraph = routingSerializer.Deserialize(writeStream);

            performanceInfo.Stop();
        }
        public void RoutingSerializationFlatfileCHEdgeData()
        {
            const string embeddedString = "OsmSharp.Test.Unittests.test_network_real1.osm";

            // load the network.
            var referenceNetwork = CHEdgeGraphOsmStreamTarget.Preprocess(new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString)), new OsmRoutingInterpreter(), Vehicle.Car);

            // serialize network.
            var routingSerializer = new CHEdgeFlatfileSerializer();
            TagsCollectionBase metaData = new TagsCollection();
            metaData.Add("some_key", "some_value");
            DynamicGraphRouterDataSource<CHEdgeData> network;
            byte[] byteArray;
            using (var stream = new MemoryStream())
            {
                routingSerializer.Serialize(stream, referenceNetwork, metaData);
                byteArray = stream.ToArray();
            }
            using (var stream = new MemoryStream(byteArray))
            {
                network = routingSerializer.Deserialize(stream, out metaData, false) as DynamicGraphRouterDataSource<CHEdgeData>;
            }

            // compare networks.
            Assert.IsNotNull(network);
            Assert.AreEqual(referenceNetwork.VertexCount, network.VertexCount);
            for (uint vertex = 0; vertex < network.VertexCount; vertex++)
            {
                float referenceLatitude, referenceLongitude, latitude, longitude;
                Assert.IsTrue(referenceNetwork.GetVertex(vertex, out referenceLatitude, out referenceLongitude));
                Assert.IsTrue(network.GetVertex(vertex, out latitude, out longitude));
                Assert.AreEqual(referenceLatitude, latitude);
                Assert.AreEqual(referenceLongitude, longitude);

                var referenceArcs = referenceNetwork.GetEdges(vertex).ToKeyValuePairs();
                var arcs = network.GetEdges(vertex).ToKeyValuePairs();
                Assert.AreEqual(referenceArcs.Length, arcs.Length);
                for (int idx = 0; idx < referenceArcs.Length; idx++)
                {
                    var referenceArc = referenceArcs[idx];
                    // find the same edge in the new arcs.
                    var arc = arcs.First((x) => { return x.Key == referenceArcs[idx].Key; });

                    Assert.AreEqual(referenceArc.Key, arc.Key);
                    Assert.AreEqual(referenceArc.Value.Forward, arc.Value.Forward);
                    Assert.AreEqual(referenceArc.Value.ForwardWeight, arc.Value.ForwardWeight);
                    Assert.AreEqual(referenceArc.Value.ForwardContractedId, arc.Value.ForwardContractedId);
                    Assert.AreEqual(referenceArc.Value.Backward, arc.Value.Backward);
                    Assert.AreEqual(referenceArc.Value.BackwardWeight, arc.Value.BackwardWeight);
                    Assert.AreEqual(referenceArc.Value.BackwardContractedId, arc.Value.BackwardContractedId);
                    Assert.AreEqual(referenceArc.Value.RepresentsNeighbourRelations, arc.Value.RepresentsNeighbourRelations);
                    Assert.AreEqual(referenceArc.Value.Tags, arc.Value.Tags);
                    ICoordinateCollection referenceCoordinates;
                    ICoordinateCollection coordinates;
                    if(referenceNetwork.GetEdgeShape(vertex, referenceArc.Key, out referenceCoordinates))
                    { // there is a shape.
                        Assert.IsTrue(network.GetEdgeShape(vertex, arc.Key, out coordinates));
                        if(referenceCoordinates == null)
                        { // reference shape is null, shape is null.
                            Assert.IsNull(coordinates);
                        }
                        else
                        { // reference shape is not null compare them.
                            Assert.IsNotNull(coordinates);
                            referenceCoordinates.Reset();
                            coordinates.Reset();
                            while(referenceCoordinates.MoveNext())
                            {
                                Assert.IsTrue(coordinates.MoveNext());

                                Assert.AreEqual(referenceCoordinates.Latitude, coordinates.Latitude);
                                Assert.AreEqual(referenceCoordinates.Longitude, coordinates.Longitude);
                            }
                            Assert.IsFalse(coordinates.MoveNext());
                        }
                    }
                    else
                    { // there is no shape.
                        Assert.IsFalse(network.GetEdgeShape(vertex, arc.Key, out coordinates));
                    }

                    // check tags.
                    var referenceTags = referenceNetwork.TagsIndex.Get(referenceArc.Value.Tags);
                    var tags = network.TagsIndex.Get(arc.Value.Tags);
                    if (referenceTags == null)
                    { // other tags also have to be null.
                        Assert.IsNull(tags);
                    }
                    else
                    { // contents need to be the same.
                        Assert.AreEqual(referenceTags.Count, tags.Count);
                        foreach (var referenceTag in referenceTags)
                        {
                            Assert.IsTrue(tags.ContainsKeyValue(referenceTag.Key, referenceTag.Value));
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public void RoutingSerializationFlatfileCHEdgeData()
        {
            const string embeddedString = "OsmSharp.Test.Unittests.test_network_real1.osm";

            // load the network.
            var referenceNetwork = CHEdgeGraphOsmStreamTarget.Preprocess(new XmlOsmStreamSource(
                                                                             Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString)), new OsmRoutingInterpreter(), Vehicle.Car);

            // serialize network.
            var routingSerializer       = new CHEdgeFlatfileSerializer();
            TagsCollectionBase metaData = new TagsCollection();

            metaData.Add("some_key", "some_value");
            DynamicGraphRouterDataSource <CHEdgeData> network;

            byte[] byteArray;
            using (var stream = new MemoryStream())
            {
                routingSerializer.Serialize(stream, referenceNetwork, metaData);
                byteArray = stream.ToArray();
            }
            using (var stream = new MemoryStream(byteArray))
            {
                network = routingSerializer.Deserialize(stream, out metaData, false) as DynamicGraphRouterDataSource <CHEdgeData>;
            }

            // compare networks.
            Assert.IsNotNull(network);
            Assert.AreEqual(referenceNetwork.VertexCount, network.VertexCount);
            for (uint vertex = 0; vertex < network.VertexCount; vertex++)
            {
                float referenceLatitude, referenceLongitude, latitude, longitude;
                Assert.IsTrue(referenceNetwork.GetVertex(vertex, out referenceLatitude, out referenceLongitude));
                Assert.IsTrue(network.GetVertex(vertex, out latitude, out longitude));
                Assert.AreEqual(referenceLatitude, latitude);
                Assert.AreEqual(referenceLongitude, longitude);

                var referenceArcs = referenceNetwork.GetEdges(vertex);
                var arcs          = network.GetEdges(vertex);
                Assert.AreEqual(referenceArcs.Length, arcs.Length);
                for (int idx = 0; idx < referenceArcs.Length; idx++)
                {
                    var referenceArc = referenceArcs[idx];
                    // find the same edge in the new arcs.
                    var arc = arcs.First((x) => { return(x.Key == referenceArcs[idx].Key); });

                    Assert.AreEqual(referenceArc.Key, arc.Key);
                    Assert.AreEqual(referenceArc.Value.Weight, arc.Value.Weight);
                    Assert.AreEqual(referenceArc.Value.Forward, arc.Value.Forward);
                    Assert.AreEqual(referenceArc.Value.RepresentsNeighbourRelations, arc.Value.RepresentsNeighbourRelations);
                    Assert.AreEqual(referenceArc.Value.Tags, arc.Value.Tags);
                    if (referenceArc.Value.Coordinates == null)
                    { // other arc coordinates also null?
                        Assert.IsNull(arc.Value.Coordinates);
                    }
                    else
                    { // compare coordinates.
                        for (int coordIdx = 0; coordIdx < referenceArc.Value.Coordinates.Length; coordIdx++)
                        {
                            Assert.AreEqual(referenceArc.Value.Coordinates[coordIdx].Latitude,
                                            arc.Value.Coordinates[coordIdx].Latitude);
                            Assert.AreEqual(referenceArc.Value.Coordinates[coordIdx].Longitude,
                                            arc.Value.Coordinates[coordIdx].Longitude);
                        }
                    }

                    // check tags.
                    var referenceTags = referenceNetwork.TagsIndex.Get(referenceArc.Value.Tags);
                    var tags          = network.TagsIndex.Get(arc.Value.Tags);
                    if (referenceTags == null)
                    { // other tags also have to be null.
                        Assert.IsNull(tags);
                    }
                    else
                    { // contents need to be the same.
                        Assert.AreEqual(referenceTags.Count, tags.Count);
                        foreach (var referenceTag in referenceTags)
                        {
                            Assert.IsTrue(tags.ContainsKeyValue(referenceTag.Key, referenceTag.Value));
                        }
                    }
                }
            }
        }