/// <summary>
        /// Executes the CH contractions while verifying each step.
        /// </summary>
        /// <param name="stream"></param>
        public void DoTestCHSparseVerifiedContraction(Stream stream)
        {
            _interpreter = new OsmRoutingInterpreter();

            var tagsIndex = new SimpleTagsIndex();

            // do the data processing.
            _data = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex);
            var targetData = new CHEdgeGraphOsmStreamWriter(
                _data, _interpreter, _data.TagsIndex, Vehicle.Car);
            var dataProcessorSource = new XmlOsmStreamSource(stream);
            var sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(dataProcessorSource);
            targetData.RegisterSource(sorter);
            targetData.Pull();

            // do the pre-processing part.
            //INodeWitnessCalculator witness_calculator = new CHRouterWitnessCalculator(_data);
            INodeWitnessCalculator witnessCalculator = new DykstraWitnessCalculator(_data);
            var preProcessor = new CHPreProcessor(_data,
                                                  new EdgeDifferenceContractedSearchSpace(_data, witnessCalculator), witnessCalculator);

            preProcessor.OnBeforeContractionEvent += new CHPreProcessor.VertexDelegate(pre_processor_OnBeforeContractionEvent);
            preProcessor.OnAfterContractionEvent  += new CHPreProcessor.VertexDelegate(pre_processor_OnAfterContractionEvent);
            preProcessor.Start();
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a router.
        /// </summary>
        /// <param name="interpreter"></param>
        /// <param name="manifestResourceName"></param>
        /// <returns></returns>
        protected override Router CreateRouter(IOsmRoutingInterpreter interpreter, string manifestResourceName)
        {
            SimpleTagsIndex tagsIndex = new SimpleTagsIndex();

            // do the data processing.
            var data       = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex);
            var targetData = new CHEdgeGraphOsmStreamTarget(
                data, interpreter, tagsIndex, Vehicle.Car);
            var dataProcessorSource = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(manifestResourceName));
            var sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(dataProcessorSource);
            targetData.RegisterSource(sorter);
            targetData.Pull();

            // do the pre-processing part.
            var witnessCalculator = new DykstraWitnessCalculator();
            var preProcessor      = new CHPreProcessor(data,
                                                       new EdgeDifference(data, witnessCalculator), witnessCalculator);

            preProcessor.Start();

            //IBasicRouter<LiveEdge> basicRouter = new DykstraRoutingLive(memoryData.TagsIndex);
            return(Router.CreateCHFrom(data, new CHRouter(), interpreter));
        }
Esempio n. 3
0
        /// <summary>
        /// Builds the data.
        /// </summary>
        /// <param name="interpreter"></param>
        /// <param name="embeddedString"></param>
        /// <returns></returns>
        public override IBasicRouterDataSource <CHEdgeData> BuildData(IRoutingInterpreter interpreter,
                                                                      string embeddedString)
        {
            string key = string.Format("CHEdgeDifference.Routing.IBasicRouterDataSource<CHEdgeData>.OSM.{0}",
                                       embeddedString);
            var data = StaticDictionary.Get <IBasicRouterDataSource <CHEdgeData> >(
                key);

            if (data == null)
            {
                var tagsIndex = new SimpleTagsIndex();

                // do the data processing.
                var memoryData = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex);
                var targetData = new CHEdgeGraphOsmStreamWriter(
                    memoryData, interpreter, memoryData.TagsIndex, Vehicle.Car);
                var dataProcessorSource = new XmlOsmStreamSource(
                    Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString));
                var sorter = new OsmStreamFilterSort();
                sorter.RegisterSource(dataProcessorSource);
                targetData.RegisterSource(sorter);
                targetData.Pull();

                // do the pre-processing part.
                var witnessCalculator = new DykstraWitnessCalculator(memoryData);
                var preProcessor      = new CHPreProcessor(memoryData,
                                                           new EdgeDifference(memoryData, witnessCalculator), witnessCalculator);
                preProcessor.Start();

                data = memoryData;
                StaticDictionary.Add <IBasicRouterDataSource <CHEdgeData> >(key, data);
            }
            return(data);
        }
Esempio n. 4
0
        public void TestRandomBlockTagSerializatonNonBeginPositionLimitedStreamRegression()
        {
            SimpleTagsIndex tagsIndex = new SimpleTagsIndex();

            SimpleTagsCollection tagsCollection = new SimpleTagsCollection();

            for (int i = 0; i < 101; i++)
            {
                int tagCollectionSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(10) + 1;
                for (int idx = 0; idx < tagCollectionSize; idx++)
                {
                    int tagValue = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(10);
                    tagsCollection.Add(
                        string.Format("key_{0}", tagValue),
                        string.Format("value_{0}", tagValue));
                }
                uint tagsId = tagsIndex.Add(tagsCollection);
            }

            ITagsIndexReadonly tagsIndexReadonly = this.SerializeDeserializeBlockLimitedStream(tagsIndex, 10, 123);

            Assert.AreEqual(tagsIndex.Max, tagsIndexReadonly.Max);
            for (uint idx = 0; idx < tagsIndex.Max; idx++)
            {
                this.CompareTagsCollections(tagsIndex.Get(idx),
                                            tagsIndexReadonly.Get(idx));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Builds data source.
        /// </summary>
        /// <param name="interpreter"></param>
        /// <param name="embeddedString"></param>
        /// <returns></returns>
        public override IBasicRouterDataSource <LiveEdge> BuildData(IRoutingInterpreter interpreter,
                                                                    string embeddedString)
        {
            string key = string.Format("Dykstra.Routing.IBasicRouterDataSource<SimpleWeighedEdge>.OSM.{0}",
                                       embeddedString);
            var data = StaticDictionary.Get <IBasicRouterDataSource <LiveEdge> >(
                key);

            if (data == null)
            {
                var tagsIndex = new SimpleTagsIndex();

                // do the data processing.
                var memoryData          = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
                var targetData          = new LiveGraphOsmStreamWriter(memoryData, interpreter, memoryData.TagsIndex);
                var dataProcessorSource = new XmlOsmStreamSource(
                    Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString));
                var sorter = new OsmStreamFilterSort();
                sorter.RegisterSource(dataProcessorSource);
                targetData.RegisterSource(sorter);
                targetData.Pull();

                data = memoryData;
                StaticDictionary.Add <IBasicRouterDataSource <LiveEdge> >(key,
                                                                          data);
            }
            return(data);
        }
Esempio n. 6
0
        /// <summary>
        /// Returns a new router.
        /// </summary>
        /// <param name="interpreter"></param>
        /// <param name="embeddedName"></param>
        /// <param name="contract"></param>
        /// <returns></returns>
        public override Router BuildRouter(IOsmRoutingInterpreter interpreter, string embeddedName, bool contract)
        {
            if (_data == null)
            {
                _data = new Dictionary <string, DynamicGraphRouterDataSource <CHEdgeData> >();
            }
            DynamicGraphRouterDataSource <CHEdgeData> data = null;

            if (!_data.TryGetValue(embeddedName, out data))
            {
                var tagsIndex = new SimpleTagsIndex();

                // do the data processing.
                data =
                    new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex);
                var targetData = new CHEdgeGraphOsmStreamTarget(
                    data, interpreter, tagsIndex, Vehicle.Car);
                var dataProcessorSource = new XmlOsmStreamSource(
                    Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format(
                                                                                  "OsmSharp.Test.Unittests.{0}", embeddedName)));
                var sorter = new OsmStreamFilterSort();
                sorter.RegisterSource(dataProcessorSource);
                targetData.RegisterSource(sorter);
                targetData.Pull();

                // do the pre-processing part.
                var preProcessor = new CHPreProcessor(data,
                                                      new SparseOrdering(data), new DykstraWitnessCalculator());
                preProcessor.Start();

                _data[embeddedName] = data;
            }
            return(Router.CreateCHFrom(data, new CHRouter(), interpreter));
        }
Esempio n. 7
0
        /// <summary>
        /// Builds the data.
        /// </summary>
        /// <param name="interpreter"></param>
        /// <param name="embeddedString"></param>
        /// <returns></returns>
        public override IBasicRouterDataSource <CHEdgeData> BuildData(IOsmRoutingInterpreter interpreter,
                                                                      string embeddedString)
        {
            var tagsIndex = new SimpleTagsIndex();

            // do the data processing.
            var memoryData = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex);
            var targetData = new CHEdgeGraphOsmStreamTarget(
                memoryData, interpreter, tagsIndex, Vehicle.Car);
            var dataProcessorSource = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString));
            var sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(dataProcessorSource);
            targetData.RegisterSource(sorter);
            targetData.Pull();

            // do the pre-processing part.
            var preProcessor = new CHPreProcessor(memoryData,
                                                  new SparseOrdering(memoryData), new DykstraWitnessCalculator());

            preProcessor.Start();

            return(memoryData);
        }
Esempio n. 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="vehicle"></param>
        public CHEdgeGraphFileStreamTarget(Stream stream, Vehicle vehicle)
        {
            _graphStream = stream;

            var tagsIndex   = new SimpleTagsIndex();
            var interpreter = new OsmRoutingInterpreter();

            _graph       = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex);
            _graphTarget = new CHEdgeGraphOsmStreamTarget(
                _graph, interpreter, tagsIndex, vehicle);
        }
Esempio n. 9
0
        /// <summary>
        /// Builds an in-memory data source from an xml data stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        private DynamicGraphRouterDataSource <CHEdgeData> BuildData(Stream stream)
        {
            var interpreter = new OsmRoutingInterpreter();
            var tagsIndex   = new SimpleTagsIndex();

            // do the data processing.
            var data       = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex);
            var targetData = new CHEdgeGraphOsmStreamTarget(
                data, interpreter, tagsIndex, Vehicle.Car);
            var dataProcessorSource = new XmlOsmStreamSource(stream);
            var sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(dataProcessorSource);
            targetData.RegisterSource(sorter);
            targetData.Pull();

            return(data);
        }
Esempio n. 10
0
        /// <summary>
        /// Creates a router using live interpreted edges.
        /// </summary>
        /// <param name="reader">The OSM-stream reader.</param>
        /// <param name="interpreter">The routing interpreter.</param>
        /// <returns></returns>
        public static Router CreateLiveFrom(OsmStreamSource reader, IRoutingInterpreter interpreter)
        {
            var tagsIndex = new SimpleTagsIndex(); // creates a tagged index.

            // read from the OSM-stream.
            var memoryData = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
            var targetData = new LiveGraphOsmStreamWriter(memoryData, interpreter, tagsIndex);
            var sorter     = new OsmStreamFilterSort();

            sorter.RegisterSource(reader);
            targetData.RegisterSource(sorter);
            targetData.Pull();

            // creates the live edge router.
            var liveEdgeRouter = new TypedRouterLiveEdge(
                memoryData, interpreter, new DykstraRoutingLive(tagsIndex));

            return(new Router(liveEdgeRouter)); // create the actual router.
        }
Esempio n. 11
0
        public void TestSimpleTagSerializatonNonBeginPosition()
        {
            SimpleTagsIndex tagsIndex = new SimpleTagsIndex();

            SimpleTagsCollection tagsCollection = new SimpleTagsCollection();

            tagsCollection.Add("key1", "value1");

            uint tagsId = tagsIndex.Add(tagsCollection);

            ITagsIndexReadonly tagsIndexReadonly = this.SerializeDeserialize(tagsIndex, 1201);

            Assert.AreEqual(tagsIndex.Max, tagsIndexReadonly.Max);
            for (uint idx = 0; idx < tagsIndex.Max; idx++)
            {
                this.CompareTagsCollections(tagsIndex.Get(idx),
                                            tagsIndexReadonly.Get(idx));
            }
        }
        /// <summary>
        /// Builds a raw router to compare against.
        /// </summary>
        /// <returns></returns>
        public void BuildDykstraRouter(string embeddedName,
                                       IOsmRoutingInterpreter interpreter)
        {
            var tagsIndex = new SimpleTagsIndex();

            // do the data processing.
            var data       = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
            var targetData = new LiveGraphOsmStreamTarget(
                data, interpreter, tagsIndex, new Vehicle[] { Vehicle.Car });
            var dataProcessorSource = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedName));
            var sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(dataProcessorSource);
            targetData.RegisterSource(sorter);
            targetData.Pull();

            // initialize the router.
            _referenceRouter = Router.CreateLiveFrom(data, new DykstraRoutingLive(), interpreter);
        }
        /// <summary>
        /// Builds a raw data source.
        /// </summary>
        /// <returns></returns>
        public DynamicGraphRouterDataSource <LiveEdge> BuildDykstraDataSource(
            IRoutingInterpreter interpreter, string embeddedName)
        {
            var tagsIndex = new SimpleTagsIndex();

            // do the data processing.
            var data       = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
            var targetData = new LiveGraphOsmStreamWriter(
                data, interpreter, data.TagsIndex);
            var dataProcessorSource = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format(
                                                                              "OsmSharp.UnitTests.{0}", embeddedName)));
            var sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(dataProcessorSource);
            targetData.RegisterSource(sorter);
            targetData.Pull();

            return(data);
        }
        /// <summary>
        /// Builds data source.
        /// </summary>
        /// <param name="interpreter"></param>
        /// <param name="embeddedString"></param>
        /// <param name="vehicle"></param>
        /// <returns></returns>
        public override IBasicRouterDataSource <LiveEdge> BuildData(IRoutingInterpreter interpreter,
                                                                    string embeddedString, Vehicle vehicle)
        {
            var tagsIndex = new SimpleTagsIndex();

            // do the data processing.
            var memoryData =
                new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
            var targetData = new LiveGraphOsmStreamWriter(
                memoryData, interpreter, memoryData.TagsIndex);
            var dataProcessorSource = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString));
            var sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(dataProcessorSource);
            targetData.RegisterSource(sorter);
            targetData.Pull();

            return(memoryData);
        }
Esempio n. 15
0
        public void RoutingRegressionTest1()
        {
            OsmRoutingInterpreter interpreter = new OsmRoutingInterpreter();

            SimpleTagsIndex tags_index = new SimpleTagsIndex();

            // do the data processing.
            DynamicGraphRouterDataSource <LiveEdge> memory_data =
                new DynamicGraphRouterDataSource <LiveEdge>(tags_index);
            LiveGraphOsmStreamWriter target_data = new LiveGraphOsmStreamWriter(
                memory_data, interpreter, memory_data.TagsIndex);
            XmlOsmStreamSource data_processor_source = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test_routing_regression1.osm"));
            OsmStreamFilterSort sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(data_processor_source);
            target_data.RegisterSource(sorter);
            target_data.Pull();

            IBasicRouter <LiveEdge> basic_router = new DykstraRoutingLive(memory_data.TagsIndex);
            Router router = Router.CreateLiveFrom(memory_data, basic_router, interpreter);

            // resolve the three points in question.
            GeoCoordinate point35         = new GeoCoordinate(51.01257, 4.000753);
            RouterPoint   point35resolved = router.Resolve(Vehicle.Car, point35);
            GeoCoordinate point40         = new GeoCoordinate(51.01250, 4.000013);
            RouterPoint   point40resolved = router.Resolve(Vehicle.Car, point40);
            GeoCoordinate point45         = new GeoCoordinate(51.01315, 3.999588);
            RouterPoint   point45resolved = router.Resolve(Vehicle.Car, point45);

            // route between 35 and 45.
            OsmSharpRoute routebefore = router.Calculate(Vehicle.Car, point35resolved, point45resolved);

            GeoCoordinate point129         = new GeoCoordinate(51.01239, 3.999573);
            RouterPoint   point129resolved = router.Resolve(Vehicle.Car, point129);

            // route between 35 and 45.
            OsmSharpRoute routeafter = router.Calculate(Vehicle.Car, point35resolved, point45resolved);

            Assert.AreEqual(routebefore.TotalDistance, routeafter.TotalDistance);
        }
Esempio n. 16
0
        /// <summary>
        /// Builds the data source.
        /// </summary>
        /// <returns></returns>
        private DynamicGraphRouterDataSource <CHEdgeData> BuildData(IRoutingInterpreter interpreter)
        {
            DynamicGraphRouterDataSource <CHEdgeData> data = null;

            if (data == null)
            {
                var tagsIndex = new SimpleTagsIndex();

                // do the data processing.
                data = new DynamicGraphRouterDataSource <CHEdgeData>(tagsIndex);
                var targetData = new CHEdgeGraphOsmStreamWriter(
                    data, interpreter, data.TagsIndex, Vehicle.Car);
                var dataProcessorSource = new XmlOsmStreamSource(
                    Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test_network.osm"));
                var sorter = new OsmStreamFilterSort();
                sorter.RegisterSource(dataProcessorSource);
                targetData.RegisterSource(sorter);
                targetData.Pull();
            }
            return(data);
        }
        /// <summary>
        /// Creates a router based on the resource.
        /// </summary>
        /// <param name="interpreter"></param>
        /// <param name="manifestResourceName"></param>
        /// <returns></returns>
        protected Router CreateReferenceRouter(IOsmRoutingInterpreter interpreter, string manifestResourceName)
        {
            SimpleTagsIndex tagsIndex = new SimpleTagsIndex();

            // do the data processing.
            DynamicGraphRouterDataSource <LiveEdge> memoryData =
                new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
            LiveGraphOsmStreamTarget target_data = new LiveGraphOsmStreamTarget(
                memoryData, interpreter, tagsIndex);
            XmlOsmStreamSource dataProcessorSource = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(manifestResourceName));
            OsmStreamFilterSort sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(dataProcessorSource);
            target_data.RegisterSource(sorter);
            target_data.Pull();

            IBasicRouter <LiveEdge> basicRouter = new DykstraRoutingLive();

            return(Router.CreateLiveFrom(memoryData, basicRouter, interpreter));
        }
Esempio n. 18
0
        /// <summary>
        /// Tests the edge matcher in combination with dykstra routing.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="highway"></param>
        /// <param name="vehicle"></param>
        /// <param name="matcher"></param>
        /// <param name="pointName"></param>
        /// <param name="notFound"></param>
        private void TestResolveOnEdgeSingle(string name, string highway,
                                             Vehicle vehicle, IEdgeMatcher matcher,
                                             string pointName, bool notFound)
        {
            var fromName = new GeoCoordinate(51.0003, 4.0007);
            var toName   = new GeoCoordinate(51.0003, 4.0008);

            var fromNoname = new GeoCoordinate(51.0, 4.0007);
            var toNoname   = new GeoCoordinate(51.0, 4.0008);

            TagsCollection pointTags = new SimpleTagsCollection();

            pointTags["name"] = pointName;

            TagsCollection tags = new SimpleTagsCollection();

            tags["highway"] = highway;
            //tags["name"] = name;

            var tagsIndex = new SimpleTagsIndex();

            // do the data processing.
            var  data          = new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
            uint vertexNoname1 = data.AddVertex((float)fromNoname.Latitude, (float)fromNoname.Longitude);
            uint vertexNoname2 = data.AddVertex((float)toNoname.Latitude, (float)toNoname.Longitude);

            data.AddArc(vertexNoname1, vertexNoname2, new LiveEdge()
            {
                Forward = true,
                Tags    = tagsIndex.Add(tags)
            }, null);
            tags            = new SimpleTagsCollection();
            tags["highway"] = highway;
            tags["name"]    = name;
            uint vertexName1 = data.AddVertex((float)fromName.Latitude, (float)fromName.Longitude);
            uint vertexName2 = data.AddVertex((float)toName.Latitude, (float)toName.Longitude);

            data.AddArc(vertexName1, vertexName2, new LiveEdge()
            {
                Forward = true,
                Tags    = tagsIndex.Add(tags)
            }, null);

            IRoutingInterpreter interpreter = new OsmRoutingInterpreter();

            // creates the data.
            IBasicRouter <LiveEdge> router = new DykstraRoutingLive(
                data.TagsIndex);

            var nonameLocation = new GeoCoordinate(
                (fromNoname.Latitude + toNoname.Latitude) / 2.0,
                (fromNoname.Longitude + toNoname.Longitude) / 2.0);
            var nameLocation = new GeoCoordinate(
                (fromName.Latitude + toName.Latitude) / 2.0,
                (fromName.Longitude + toName.Longitude) / 2.0);

            const float         delta  = 0.01f;
            SearchClosestResult result = router.SearchClosest(data, interpreter, vehicle, nonameLocation, delta, matcher, pointTags);

            if (result.Distance < double.MaxValue)
            { // there is a result.
                Assert.IsFalse(notFound, "A result was found but was supposed not to  be found!");

                if (name == pointName)
                { // the name location was supposed to be found!
                    Assert.IsTrue(result.Vertex1 == vertexName1 || result.Vertex1 == vertexName2);
                    Assert.IsTrue(result.Vertex2 == vertexName1 || result.Vertex2 == vertexName2);
                }
                else
                { // the noname location was supposed to be found!
                    Assert.IsTrue(result.Vertex1 == vertexNoname1 || result.Vertex1 == vertexNoname2);
                    Assert.IsTrue(result.Vertex2 == vertexNoname1 || result.Vertex2 == vertexNoname2);
                }
                return;
            }
            Assert.IsTrue(notFound, "A result was not found but was supposed to be found!");
        }
Esempio n. 19
0
        public void TestRandomPartialTagSerializaton()
        {
            SimpleTagsIndex tagsIndex = new SimpleTagsIndex();

            SimpleTagsCollection tagsCollection = new SimpleTagsCollection();

            for (int i = 0; i < 100; i++)
            {
                int tagCollectionSize = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(10) + 1;
                for (int idx = 0; idx < tagCollectionSize; idx++)
                {
                    int tagValue = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(10);
                    tagsCollection.Add(
                        string.Format("key_{0}", tagValue),
                        string.Format("value_{0}", tagValue));
                }
                uint tagsId = tagsIndex.Add(tagsCollection);
            }

            uint from = 40;
            uint to   = 50;

            ITagsIndexReadonly tagsIndexReadonly = this.SerializeDeserialize(tagsIndex, from, to);

            Assert.AreEqual(System.Math.Min(to, tagsIndex.Max), tagsIndexReadonly.Max);
            for (uint idx = 0; idx < tagsIndex.Max; idx++)
            {
                if (idx >= from && idx < to)
                {
                    this.CompareTagsCollections(tagsIndex.Get(idx),
                                                tagsIndexReadonly.Get(idx));
                }
                else
                {
                    Assert.IsNull(tagsIndexReadonly.Get(idx));
                }
            }

            from = 0;
            to   = 100;

            tagsIndexReadonly = this.SerializeDeserialize(tagsIndex, from, to);
            Assert.AreEqual(System.Math.Min(to, tagsIndex.Max), tagsIndexReadonly.Max);
            for (uint idx = 0; idx < tagsIndex.Max; idx++)
            {
                if (idx >= from && idx < to)
                {
                    this.CompareTagsCollections(tagsIndex.Get(idx),
                                                tagsIndexReadonly.Get(idx));
                }
                else
                {
                    Assert.IsNull(tagsIndexReadonly.Get(idx));
                }
            }

            from = 10;
            to   = 1000;

            tagsIndexReadonly = this.SerializeDeserialize(tagsIndex, from, to);
            Assert.AreEqual(System.Math.Min(to, tagsIndex.Max), tagsIndexReadonly.Max);
            for (uint idx = 0; idx < tagsIndex.Max; idx++)
            {
                if (idx >= from && idx < to)
                {
                    this.CompareTagsCollections(tagsIndex.Get(idx),
                                                tagsIndexReadonly.Get(idx));
                }
                else
                {
                    Assert.IsNull(tagsIndexReadonly.Get(idx));
                }
            }
        }
        public void RoutingSerializationV2CompressedRoutingTest()
        {
            const string embeddedString = "OsmSharp.Test.Unittests.test_network.osm";

            // create the tags index.
            var tagsIndex = new SimpleTagsIndex();

            // creates a new interpreter.
            var interpreter = new OsmRoutingInterpreter();

            // do the data processing.
            var original =
                new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
            var targetData = new LiveGraphOsmStreamTarget(
                original, interpreter, tagsIndex);
            var dataProcessorSource = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString));

            targetData.RegisterSource(dataProcessorSource);
            targetData.Pull();

            // create serializer.
            var routingSerializer = new V2RoutingDataSourceLiveEdgeSerializer(true);

            // serialize/deserialize.
            byte[] byteArray;
            using (var stream = new MemoryStream())
            {
                try
                {
                    routingSerializer.Serialize(stream, original);
                    byteArray = stream.ToArray();
                }
                catch (Exception)
                {
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }
                    throw;
                }
            }

            IBasicRouterDataSource <LiveEdge> deserializedVersion =
                routingSerializer.Deserialize(new MemoryStream(byteArray));

            Assert.AreEqual(original.TagsIndex.Get(0), deserializedVersion.TagsIndex.Get(0));

            // try to do some routing on the deserialized version.
            var         basicRouter = new DykstraRoutingLive();
            Router      router      = Router.CreateLiveFrom(deserializedVersion, basicRouter, interpreter);
            RouterPoint source      = router.Resolve(Vehicle.Car,
                                                     new GeoCoordinate(51.0578532, 3.7192229));
            RouterPoint target = router.Resolve(Vehicle.Car,
                                                new GeoCoordinate(51.0576193, 3.7191801));

            // calculate the route.
            Route route = router.Calculate(Vehicle.Car, source, target);

            Assert.IsNotNull(route);
            Assert.AreEqual(5, route.Entries.Length);

            float latitude, longitude;

            deserializedVersion.GetVertex(20, out latitude, out longitude);
            Assert.AreEqual(latitude, route.Entries[0].Latitude, 0.00001);
            Assert.AreEqual(longitude, route.Entries[0].Longitude, 0.00001);
            Assert.AreEqual(RoutePointEntryType.Start, route.Entries[0].Type);

            deserializedVersion.GetVertex(21, out latitude, out longitude);
            Assert.AreEqual(latitude, route.Entries[1].Latitude, 0.00001);
            Assert.AreEqual(longitude, route.Entries[1].Longitude, 0.00001);
            Assert.AreEqual(RoutePointEntryType.Along, route.Entries[1].Type);

            deserializedVersion.GetVertex(16, out latitude, out longitude);
            Assert.AreEqual(latitude, route.Entries[2].Latitude, 0.00001);
            Assert.AreEqual(longitude, route.Entries[2].Longitude, 0.00001);
            Assert.AreEqual(RoutePointEntryType.Along, route.Entries[2].Type);

            deserializedVersion.GetVertex(22, out latitude, out longitude);
            Assert.AreEqual(latitude, route.Entries[3].Latitude, 0.00001);
            Assert.AreEqual(longitude, route.Entries[3].Longitude, 0.00001);
            Assert.AreEqual(RoutePointEntryType.Along, route.Entries[3].Type);

            deserializedVersion.GetVertex(23, out latitude, out longitude);
            Assert.AreEqual(latitude, route.Entries[4].Latitude, 0.00001);
            Assert.AreEqual(longitude, route.Entries[4].Longitude, 0.00001);
            Assert.AreEqual(RoutePointEntryType.Stop, route.Entries[4].Type);
        }
Esempio n. 21
0
        public void RoutingRegressionTest2()
        {
            OsmRoutingInterpreter interpreter = new OsmRoutingInterpreter();

            SimpleTagsIndex tags_index = new SimpleTagsIndex();

            // do the data processing.
            DynamicGraphRouterDataSource <LiveEdge> memory_data =
                new DynamicGraphRouterDataSource <LiveEdge>(tags_index);
            LiveGraphOsmStreamWriter target_data = new LiveGraphOsmStreamWriter(
                memory_data, interpreter, memory_data.TagsIndex);
            XmlOsmStreamSource data_processor_source = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test_routing_regression1.osm"));
            OsmStreamFilterSort sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(data_processor_source);
            target_data.RegisterSource(sorter);
            target_data.Pull();

            IBasicRouter <LiveEdge> basic_router = new DykstraRoutingLive(memory_data.TagsIndex);
            Router router = Router.CreateLiveFrom(memory_data, basic_router, interpreter);

            // resolve the three points in question.
            GeoCoordinate point35         = new GeoCoordinate(51.01257, 4.000753);
            RouterPoint   point35resolved = router.Resolve(Vehicle.Car, point35);
            GeoCoordinate point45         = new GeoCoordinate(51.01315, 3.999588);
            RouterPoint   point45resolved = router.Resolve(Vehicle.Car, point45);
            GeoCoordinate point40         = new GeoCoordinate(51.01250, 4.000013);
            RouterPoint   point40resolved = router.Resolve(Vehicle.Car, point40);

            // calculate two smaller routes.
            OsmSharpRoute route3545             = router.Calculate(Vehicle.Car, point35resolved, point45resolved);
            OsmSharpRoute route4540             = router.Calculate(Vehicle.Car, point45resolved, point40resolved);
            OsmSharpRoute route3540concatenated = OsmSharpRoute.Concatenate(route3545, route4540);

            OsmSharpRoute route3540 = router.Calculate(Vehicle.Car, point35resolved, point40resolved);

            // check if both routes are equal.
            Assert.AreEqual(route3540.Entries.Length, route3540concatenated.Entries.Length);
            for (int idx = 0; idx < route3540.Entries.Length; idx++)
            {
                Assert.AreEqual(route3540.Entries[idx].Distance, route3540concatenated.Entries[idx].Distance);
                Assert.AreEqual(route3540.Entries[idx].Latitude, route3540concatenated.Entries[idx].Latitude);
                Assert.AreEqual(route3540.Entries[idx].Longitude, route3540concatenated.Entries[idx].Longitude);
                Assert.AreEqual(route3540.Entries[idx].Time, route3540concatenated.Entries[idx].Time);
                Assert.AreEqual(route3540.Entries[idx].Type, route3540concatenated.Entries[idx].Type);
                Assert.AreEqual(route3540.Entries[idx].WayFromName, route3540concatenated.Entries[idx].WayFromName);

                // something that is allowed to be different in this case!
                // route3540.Entries[idx].Points != null

                // check sidestreets.
                if (route3540.Entries[idx].SideStreets != null &&
                    route3540.Entries[idx].SideStreets.Length > 0)
                { // check if the sidestreets represent the same information.
                    for (int metricIdx = 0; metricIdx < route3540concatenated.Entries[idx].SideStreets.Length; metricIdx++)
                    {
                        Assert.AreEqual(route3540.Entries[idx].SideStreets[metricIdx].WayName,
                                        route3540concatenated.Entries[idx].SideStreets[metricIdx].WayName);
                        Assert.AreEqual(route3540.Entries[idx].SideStreets[metricIdx].Latitude,
                                        route3540concatenated.Entries[idx].SideStreets[metricIdx].Latitude);
                        Assert.AreEqual(route3540.Entries[idx].SideStreets[metricIdx].Longitude,
                                        route3540concatenated.Entries[idx].SideStreets[metricIdx].Longitude);
                    }
                }
                else
                {
                    Assert.IsTrue(route3540concatenated.Entries[idx].SideStreets == null ||
                                  route3540concatenated.Entries[idx].SideStreets.Length == 0);
                }


                if (route3540.Entries[idx].Tags != null &&
                    route3540.Entries[idx].Tags.Length > 0)
                { // check if the Tags represent the same information.
                    for (int metricIdx = 0; metricIdx < route3540concatenated.Entries[idx].Tags.Length; metricIdx++)
                    {
                        Assert.AreEqual(route3540.Entries[idx].Tags[metricIdx].Key,
                                        route3540concatenated.Entries[idx].Tags[metricIdx].Key);
                        Assert.AreEqual(route3540.Entries[idx].Tags[metricIdx].Value,
                                        route3540concatenated.Entries[idx].Tags[metricIdx].Value);
                    }
                }
                else
                {
                    Assert.IsTrue(route3540concatenated.Entries[idx].Tags == null ||
                                  route3540concatenated.Entries[idx].Tags.Length == 0);
                }

                Assert.AreEqual(route3540.Entries[idx].Distance, route3540concatenated.Entries[idx].Distance);
            }
            if (route3540.Tags != null &&
                route3540.Tags.Length > 0)
            {
                for (int tagIdx = 0; tagIdx < route3540.Tags.Length; tagIdx++)
                {
                    if (route3540.Tags[tagIdx].Key != "debug_route")
                    {
                        Assert.AreEqual(route3540.Tags[tagIdx].Key, route3540concatenated.Tags[tagIdx].Key);
                        Assert.AreEqual(route3540.Tags[tagIdx].Value, route3540concatenated.Tags[tagIdx].Value);
                    }
                }
            }
            else
            {
                Assert.IsTrue(route3540concatenated.Tags == null ||
                              route3540concatenated.Tags.Length == 0);
            }
            if (route3540.Metrics != null)
            {
                for (int metricIdx = 0; metricIdx < route3540concatenated.Entries.Length; metricIdx++)
                {
                    Assert.AreEqual(route3540.Metrics[metricIdx].Key, route3540concatenated.Metrics[metricIdx].Key);
                    Assert.AreEqual(route3540.Metrics[metricIdx].Value, route3540concatenated.Metrics[metricIdx].Value);
                }
            }
            else
            {
                Assert.IsNull(route3540concatenated.Metrics);
            }

            // remove the point in between, the only difference between the regular and the concatenated route.
            route3540concatenated.Entries[7].Points = null;

            // create the language generator.
            var languageGenerator = new LanguageTestGenerator();

            // generate the instructions.
            var instructionGenerator        = new InstructionGenerator();
            List <Instruction> instructions =
                instructionGenerator.Generate(route3540, interpreter, languageGenerator);
            List <Instruction> instructionsConcatenated =
                instructionGenerator.Generate(route3540concatenated, interpreter, languageGenerator);

            Assert.AreEqual(instructions.Count, instructionsConcatenated.Count);
            for (int idx = 0; idx < instructions.Count; idx++)
            {
                Assert.AreEqual(instructions[idx].Location.Center,
                                instructionsConcatenated[idx].Location.Center);
                Assert.AreEqual(instructions[idx].Text,
                                instructionsConcatenated[idx].Text);
            }
        }
Esempio n. 22
0
        public void RoutingRegressionTest4()
        {
            OsmRoutingInterpreter interpreter = new OsmRoutingInterpreter();

            SimpleTagsIndex tags_index = new SimpleTagsIndex();

            // do the data processing.
            DynamicGraphRouterDataSource <LiveEdge> memory_data =
                new DynamicGraphRouterDataSource <LiveEdge>(tags_index);
            LiveGraphOsmStreamWriter target_data = new LiveGraphOsmStreamWriter(
                memory_data, interpreter, memory_data.TagsIndex);
            XmlOsmStreamSource data_processor_source = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test_network.osm"));
            OsmStreamFilterSort sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(data_processor_source);
            target_data.RegisterSource(sorter);
            target_data.Pull();

            IBasicRouter <LiveEdge> basic_router = new DykstraRoutingLive(memory_data.TagsIndex);
            Router router = Router.CreateLiveFrom(memory_data, basic_router, interpreter);

            // build coordinates list of resolved points.
            List <GeoCoordinate> testPoints = new List <GeoCoordinate>();

            testPoints.Add(new GeoCoordinate(51.0581719, 3.7201622));
            testPoints.Add(new GeoCoordinate(51.0580439, 3.7202134));
            testPoints.Add(new GeoCoordinate(51.0580573, 3.7204378));
            testPoints.Add(new GeoCoordinate(51.0581862, 3.7203758));

            // build a matrix of routes between all points.
            OsmSharpRoute[][] referenceRoutes = new OsmSharpRoute[testPoints.Count][];
            int[]             permuationArray = new int[testPoints.Count];
            for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++)
            {
                permuationArray[fromIdx] = fromIdx;
                referenceRoutes[fromIdx] = new OsmSharpRoute[testPoints.Count];
                for (int toIdx = 0; toIdx < testPoints.Count; toIdx++)
                {
                    // create router from scratch.
                    router = Router.CreateLiveFrom(
                        memory_data, basic_router, interpreter);

                    // resolve points.
                    RouterPoint from = router.Resolve(Vehicle.Car, testPoints[fromIdx]);
                    RouterPoint to   = router.Resolve(Vehicle.Car, testPoints[toIdx]);

                    // calculate route.
                    referenceRoutes[fromIdx][toIdx] = router.Calculate(Vehicle.Car, from, to);
                }
            }

            // resolve points in some order and compare the resulting routes.
            // they should be identical in length except for some numerical rounding errors.
            PermutationEnumerable <int> enumerator = new PermutationEnumerable <int>(
                permuationArray);

            foreach (int[] permutation in enumerator)
            {
                // create router from scratch.
                router = Router.CreateLiveFrom(memory_data, basic_router, interpreter);

                // resolve in the order of the permutation.
                RouterPoint[] resolvedPoints = new RouterPoint[permutation.Length];
                for (int idx = 0; idx < permutation.Length; idx++)
                {
                    resolvedPoints[permutation[idx]] = router.Resolve(Vehicle.Car, testPoints[permutation[idx]]);
                }

                for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++)
                {
                    for (int toIdx = 0; toIdx < testPoints.Count; toIdx++)
                    {
                        // calculate route.
                        OsmSharpRoute route = router.Calculate(Vehicle.Car, resolvedPoints[fromIdx], resolvedPoints[toIdx]);

                        Assert.AreEqual(referenceRoutes[fromIdx][toIdx].TotalDistance, route.TotalDistance, 0.1);
                    }
                }
            }
        }
        public void RoutingSerializationV2DataSourceTest()
        {
            const string embeddedString = "OsmSharp.Test.Unittests.test_network.osm";

            // create the tags index.
            var tagsIndex = new SimpleTagsIndex();

            // creates a new interpreter.
            var interpreter = new OsmRoutingInterpreter();

            // do the data processing.
            var original =
                new DynamicGraphRouterDataSource <LiveEdge>(tagsIndex);
            var targetData = new LiveGraphOsmStreamTarget(
                original, interpreter, tagsIndex);
            var dataProcessorSource = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedString));

            targetData.RegisterSource(dataProcessorSource);
            targetData.Pull();

            // store some lat/lons.
            var verticesLocations = new List <GeoCoordinate>();

            for (uint vertex = 1; vertex <= 5; vertex++)
            {
                float latitude, longitude;
                if (original.GetVertex(vertex, out latitude, out longitude))
                {
                    verticesLocations.Add(
                        new GeoCoordinate(latitude, longitude));
                }
            }

            // create serializer.
            var routingSerializer = new V2RoutingDataSourceLiveEdgeSerializer(false);

            // serialize/deserialize.
            IBasicRouterDataSource <LiveEdge> deserializedVersion;

            byte[] byteArray;
            using (var stream = new MemoryStream())
            {
                try
                {
                    routingSerializer.Serialize(stream, original);
                    byteArray = stream.ToArray();
                }
                catch (Exception)
                {
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }
                    throw;
                }
            }
            using (var stream = new MemoryStream(byteArray))
            {
                try
                {
                    deserializedVersion = routingSerializer.Deserialize(stream, false);
                }
                catch (Exception)
                {
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }
                    throw;
                }
            }

            //Assert.AreEqual(original.VertexCount, deserializedVersion.VertexCount);
            Assert.AreEqual(original.TagsIndex.Get(0), deserializedVersion.TagsIndex.Get(0));

            for (uint vertex = 1; vertex <= 5; vertex++)
            {
                float latitude, longitude;
                if (deserializedVersion.GetVertex(vertex, out latitude, out longitude))
                {
                    Assert.AreEqual(verticesLocations[(int)vertex - 1].Latitude, latitude, 0.000001);
                    Assert.AreEqual(verticesLocations[(int)vertex - 1].Longitude, longitude, 0.000001);
                }
            }
        }
Esempio n. 24
0
        public void RoutingRegressionTest2()
        {
            OsmRoutingInterpreter interpreter = new OsmRoutingInterpreter();

            SimpleTagsIndex tags_index = new SimpleTagsIndex();

            // do the data processing.
            DynamicGraphRouterDataSource <LiveEdge> memory_data =
                new DynamicGraphRouterDataSource <LiveEdge>(tags_index);
            LiveGraphOsmStreamTarget target_data = new LiveGraphOsmStreamTarget(
                memory_data, interpreter, tags_index);
            XmlOsmStreamSource data_processor_source = new XmlOsmStreamSource(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.Test.Unittests.test_network.osm"));
            OsmStreamFilterSort sorter = new OsmStreamFilterSort();

            sorter.RegisterSource(data_processor_source);
            target_data.RegisterSource(sorter);
            target_data.Pull();

            IBasicRouter <LiveEdge> basic_router = new DykstraRoutingLive();
            Router router = Router.CreateLiveFrom(memory_data, basic_router, interpreter);

            // build coordinates list of resolved points.
            List <GeoCoordinate> testPoints = new List <GeoCoordinate>();

            testPoints.Add(new GeoCoordinate(51.0582204, 3.7193524));
            testPoints.Add(new GeoCoordinate(51.0582199, 3.7194002));
            //testPoints.Add(new GeoCoordinate(51.0582178, 3.7195025));

            testPoints.Add(new GeoCoordinate(51.0581727, 3.7195833));
            testPoints.Add(new GeoCoordinate(51.0581483, 3.7195553));
            //testPoints.Add(new GeoCoordinate(51.0581219, 3.7195231));
            //testPoints.Add(new GeoCoordinate(51.0580965, 3.7194918));

            testPoints.Add(new GeoCoordinate(51.0581883, 3.7196617));
            testPoints.Add(new GeoCoordinate(51.0581628, 3.7196889));

            // build a matrix of routes between all points.
            Route[][] referenceRoutes = new Route[testPoints.Count][];
            int[]     permuationArray = new int[testPoints.Count];
            for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++)
            {
                permuationArray[fromIdx] = fromIdx;
                referenceRoutes[fromIdx] = new Route[testPoints.Count];
                for (int toIdx = 0; toIdx < testPoints.Count; toIdx++)
                {
                    // create router from scratch.
                    router = Router.CreateLiveFrom(
                        memory_data, basic_router, interpreter);

                    // resolve points.
                    RouterPoint from = router.Resolve(Vehicle.Car, testPoints[fromIdx]);
                    RouterPoint to   = router.Resolve(Vehicle.Car, testPoints[toIdx]);

                    // calculate route.
                    referenceRoutes[fromIdx][toIdx] = router.Calculate(Vehicle.Car, from, to);
                }
            }

            // resolve points in some order and compare the resulting routes.
            // they should be identical in length except for some numerical rounding errors.
            PermutationEnumerable <int> enumerator = new PermutationEnumerable <int>(
                permuationArray);

            foreach (int[] permutation in enumerator)
            {
                //int[] permutation = new int[] { 5, 0, 1, 2, 4, 3, 6, 7, 8 };

                //// incrementally resolve points.
                //// create router from scratch.
                //router = new Router<SimpleWeighedEdge>(memory_data, interpreter, basic_router);

                //// resolve in the order of the permutation.
                //RouterPoint[] resolvedPoints = new RouterPoint[permutation.Length];
                //resolvedPoints[permutation[0]] = router.Resolve(VehicleEnum.Car, testPoints[permutation[0]]);
                //for (int idx = 1; idx < permutation.Length; idx++)
                //{
                //    resolvedPoints[permutation[idx]] = router.Resolve(VehicleEnum.Car, testPoints[permutation[idx]]);

                //    for (int fromIdx = 0; fromIdx <= idx; fromIdx++)
                //    {
                //        for (int toIdx = 0; toIdx <= idx; toIdx++)
                //        {
                //            // calculate route.
                //            OsmSharpRoute route = router.Calculate(VehicleEnum.Car,
                //                resolvedPoints[permutation[fromIdx]], resolvedPoints[permutation[toIdx]]);

                //            Assert.AreEqual(referenceRoutes[permutation[fromIdx]][permutation[toIdx]].TotalDistance,
                //                route.TotalDistance, 0.1);
                //        }
                //    }
                //}

                // create router from scratch.
                router = Router.CreateLiveFrom(memory_data, basic_router, interpreter);

                // resolve in the order of the permutation.
                RouterPoint[] resolvedPoints = new RouterPoint[permutation.Length];
                for (int idx = 0; idx < permutation.Length; idx++)
                {
                    resolvedPoints[permutation[idx]] = router.Resolve(Vehicle.Car, testPoints[permutation[idx]]);
                }

                for (int fromIdx = 0; fromIdx < testPoints.Count; fromIdx++)
                {
                    for (int toIdx = 0; toIdx < testPoints.Count; toIdx++)
                    {
                        // calculate route.
                        Route route = router.Calculate(Vehicle.Car, resolvedPoints[fromIdx], resolvedPoints[toIdx]);

                        Assert.AreEqual(referenceRoutes[fromIdx][toIdx].TotalDistance, route.TotalDistance, 0.1);
                    }
                }
            }
        }