Exemplo n.º 1
0
        /// <summary>
        /// Creates a new router.
        /// </summary>
        /// <param name="flatFile">The flatfile containing the graph.</param>
        /// <param name="interpreter">The routing interpreter.</param>
        /// <returns></returns>
        public static Router CreateLiveFrom(Stream flatFile, IOsmRoutingInterpreter interpreter)
        {
            var serializer = new LiveEdgeFlatfileSerializer();
            var source     = serializer.Deserialize(flatFile, false) as DynamicGraphRouterDataSource <LiveEdge>;

            // creates the live edge router.
            var liveEdgeRouter = new TypedRouterLiveEdge(
                source, interpreter, new Dykstra());

            return(new Router(liveEdgeRouter)); // create the actual router.
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // make sure the output is logged to console.
            OsmSharp.Logging.Log.Enable();
            OsmSharp.Logging.Log.RegisterListener(new OsmSharp.WinForms.UI.Logging.ConsoleTraceListener());

            var nwbDump = @"C:\work\via\data\NWB\nwb.dump";

            // load data.
            OsmSharp.Logging.Log.TraceEvent("Nederlands.Test", OsmSharp.Logging.TraceEventType.Information,
                                            string.Format("Loading {0}...", nwbDump));
            var serializer = new LiveEdgeFlatfileSerializer();
            IBasicRouterDataSource <LiveEdge> nwbGraph = null;

            using (var stream = new FileInfo(nwbDump).OpenRead())
            {
                nwbGraph = serializer.Deserialize(stream);
            }

            NWBMapping.BAANSUBSRT  = "BST_CODE";
            BasicRouter.MaxSettles = 65536;

            // create test case.
            var testCase = new GeoCoordinate[]
            {
                new GeoCoordinate(51.95833631000295f, 4.946079254150391f),
                new GeoCoordinate(51.94288923910573f, 4.954748153686523f)
            };

            // create encoder.
            var encoder = ReferencedNWBEncoder.CreateBinary(nwbGraph);

            // build line location from shortest path.
            OsmSharp.Routing.Route route;
            var line     = encoder.BuildLineLocationFromShortestPath(testCase[0], testCase[1], out route);
            var lineJson = line.ToFeatures().ToGeoJson(); // create geojson to view output.

            // encode the line location.
            var encoded = encoder.Encode(line);

            // create decoder.
            var decoder = ReferencedNWBDecoder.CreateBinary(nwbGraph);

            // decode line location.
            var decodedLine     = decoder.Decode(encoded);
            var decodedLineJson = line.ToFeatures().ToGeoJson(); // create geojson to view output.
        }
        /// <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 progress = new OsmStreamFilterProgress();

            progress.RegisterSource(source);

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

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

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

            var performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 5000);

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

            // read from the OSM-stream.
            var memoryData = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex, 100000000);
            var targetData = new LiveGraphOsmStreamTarget(memoryData, new OsmRoutingInterpreter(), tagsIndex);

            targetData.RegisterSource(progress);
            targetData.Pull();

            var metaData = new TagsCollection();

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

            routingSerializer.Serialize(writeStream, memoryData, 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();
        }
        /// <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 performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 100000);
            performanceInfo.Start();
            performanceInfo.Report("Pulling from {0}...", testFile.Name);

            var stream = testFile.OpenRead();
            var source = new PBFOsmStreamSource(stream);
            var progress = new OsmStreamFilterProgress();
            progress.RegisterSource(source);

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

            var tagsIndex = new TagsTableCollectionIndex();
            var interpreter = new OsmRoutingInterpreter();
            var graph = new DynamicGraphRouterDataSource<LiveEdge>(tagsIndex);
            var routingSerializer = new LiveEdgeFlatfileSerializer();

            // read from the OSM-stream.
            using (var fileFactory = new MemoryMappedFileFactory(@"d:\temp\"))
            {
                using (var memoryMappedGraph = new MemoryMappedGraph<LiveEdge>(10000, fileFactory))
                {
                    using (var coordinates = new HugeCoordinateIndex(fileFactory, 10000))
                    {
                        var memoryData = new DynamicGraphRouterDataSource<LiveEdge>(memoryMappedGraph, tagsIndex);
                        var targetData = new LiveGraphOsmStreamTarget(memoryData, new OsmRoutingInterpreter(), tagsIndex, coordinates);
                        targetData.RegisterSource(progress);
                        targetData.Pull();

                        performanceInfo.Stop();

                        performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 100000);
                        performanceInfo.Start();
                        performanceInfo.Report("Writing file for {0}...", testFile.Name);

                        var metaData = new TagsCollection();
                        metaData.Add("some_key", "some_value");
                        routingSerializer.Serialize(writeStream, memoryData, metaData);
                    }
                }
            }
            stream.Dispose();
            writeStream.Dispose();

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

            performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 100000);
            performanceInfo.Start();
            performanceInfo.Report("Reading file for {0}...", testFile.Name);

            var testInputFile = new FileInfo(@"europe-latest.osm.pbf.routing");
            Stream readStream = testInputFile.OpenRead();

            var deserializedGraph = routingSerializer.Deserialize(readStream, false);

            readStream.Dispose();

            OsmSharp.Logging.Log.TraceEvent("LiveSerializerFlatFile", OsmSharp.Logging.TraceEventType.Information,
                string.Format("Read: {0}KB", testInputFile.Length / 1024));

            OsmSharp.Logging.Log.TraceEvent("LiveSerializerFlatFile", Logging.TraceEventType.Information, deserializedGraph.ToInvariantString());

            performanceInfo.Stop();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new router.
        /// </summary>
        /// <param name="flatFile">The flatfile containing the graph.</param>
        /// <param name="interpreter">The routing interpreter.</param>
        /// <returns></returns>
        public static Router CreateLiveFrom(Stream flatFile, IOsmRoutingInterpreter interpreter)
        {
            var serializer = new LiveEdgeFlatfileSerializer();
            var source = serializer.Deserialize(flatFile, false) as DynamicGraphRouterDataSource<LiveEdge>;

            // creates the live edge router.
            var liveEdgeRouter = new TypedRouterLiveEdge(
                source, interpreter, new Dykstra());

            return new Router(liveEdgeRouter); // create the actual router.
        }
        public void RoutingSerializationFlatfileLiveEdge()
        {
            const string embeddedString = "OsmSharp.Test.Unittests.test_network_real1.osm";

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

            // serialize network.
            var routingSerializer = new LiveEdgeFlatfileSerializer();
            TagsCollectionBase metaData = new TagsCollection();
            metaData.Add("some_key", "some_value");
            DynamicGraphRouterDataSource<LiveEdge> 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<LiveEdge>;
            }

            // 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.Distance, arc.Value.Distance);
                    Assert.AreEqual(referenceArc.Value.Forward, arc.Value.Forward);
                    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));
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        public void RoutingSerializationFlatfileLiveEdge()
        {
            const string embeddedString = "OsmSharp.Test.Unittests.test_network_real1.osm";

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

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

            metaData.Add("some_key", "some_value");
            DynamicGraphRouterDataSource <LiveEdge> 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 <LiveEdge>;
            }

            // 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.Distance, arc.Value.Distance);
                    Assert.AreEqual(referenceArc.Value.Forward, arc.Value.Forward);
                    Assert.AreEqual(referenceArc.Value.RepresentsNeighbourRelations, arc.Value.RepresentsNeighbourRelations);
                    Assert.AreEqual(referenceArc.Value.Tags, arc.Value.Tags);
                    GeoCoordinateSimple[] referenceArcValueCoordinates;
                    GeoCoordinateSimple[] arcValueCoordinates;
                    Assert.AreEqual(network.GetEdgeShape(vertex, arc.Key, out arcValueCoordinates),
                                    referenceNetwork.GetEdgeShape(vertex, referenceArc.Key, out referenceArcValueCoordinates));
                    if (referenceArcValueCoordinates == null)
                    { // other arc coordinates also null?
                        Assert.IsNull(arcValueCoordinates);
                    }
                    else
                    { // compare coordinates.
                        for (int coordIdx = 0; coordIdx < referenceArcValueCoordinates.Length; coordIdx++)
                        {
                            Assert.AreEqual(referenceArcValueCoordinates[coordIdx].Latitude,
                                            arcValueCoordinates[coordIdx].Latitude);
                            Assert.AreEqual(referenceArcValueCoordinates[coordIdx].Longitude,
                                            arcValueCoordinates[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));
                        }
                    }
                }
            }
        }
        /// <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 performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 100000);

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

            var stream   = testFile.OpenRead();
            var source   = new PBFOsmStreamSource(stream);
            var progress = new OsmStreamFilterProgress();

            progress.RegisterSource(source);

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

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

            var tagsIndex         = new TagsTableCollectionIndex();
            var interpreter       = new OsmRoutingInterpreter();
            var graph             = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
            var routingSerializer = new LiveEdgeFlatfileSerializer();

            // read from the OSM-stream.
            using (var fileFactory = new MemoryMappedFileFactory(@"d:\temp\"))
            {
                using (var memoryMappedGraph = new MemoryMappedGraph <LiveEdge>(10000, fileFactory))
                {
                    using (var coordinates = new HugeCoordinateIndex(fileFactory, 10000))
                    {
                        var memoryData = new DynamicGraphRouterDataSource <LiveEdge>(memoryMappedGraph, tagsIndex);
                        var targetData = new LiveGraphOsmStreamTarget(memoryData, new OsmRoutingInterpreter(), tagsIndex, coordinates);
                        targetData.RegisterSource(progress);
                        targetData.Pull();

                        performanceInfo.Stop();

                        performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 100000);
                        performanceInfo.Start();
                        performanceInfo.Report("Writing file for {0}...", testFile.Name);

                        var metaData = new TagsCollection();
                        metaData.Add("some_key", "some_value");
                        routingSerializer.Serialize(writeStream, memoryData, metaData);
                    }
                }
            }
            stream.Dispose();
            writeStream.Dispose();

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

            performanceInfo = new PerformanceInfoConsumer("LiveSerializerFlatFile.Serialize", 100000);
            performanceInfo.Start();
            performanceInfo.Report("Reading file for {0}...", testFile.Name);

            var    testInputFile = new FileInfo(@"europe-latest.osm.pbf.routing");
            Stream readStream    = testInputFile.OpenRead();

            var deserializedGraph = routingSerializer.Deserialize(readStream, false);

            readStream.Dispose();

            OsmSharp.Logging.Log.TraceEvent("LiveSerializerFlatFile", OsmSharp.Logging.TraceEventType.Information,
                                            string.Format("Read: {0}KB", testInputFile.Length / 1024));

            OsmSharp.Logging.Log.TraceEvent("LiveSerializerFlatFile", Logging.TraceEventType.Information, deserializedGraph.ToInvariantString());

            performanceInfo.Stop();
        }