Example #1
0
        /// <summary>
        /// Gets the target router point for the given stop.
        /// </summary>
        public RouterPoint GetTargetPoint(uint stop)
        {
            RouterPoint best           = null;
            var         bestWeight     = float.MaxValue;
            var         stopEnumerator = _multimodalDb.TransitDb.GetStopsEnumerator();

            if (stopEnumerator.MoveTo(stop))
            {
                _stopLinksDbEnumerator.MoveTo(stop);
                while (_stopLinksDbEnumerator.MoveNext())
                {
                    var point = new RouterPoint(stopEnumerator.Latitude, stopEnumerator.Longitude, _stopLinksDbEnumerator.EdgeId,
                                                _stopLinksDbEnumerator.Offset);
                    if (point.EdgeId == _routerPoint.EdgeId)
                    { // on the same edge.
                        EdgePath <float> path;
                        if (_backward)
                        { // from stop -> source.
                            path = point.EdgePathTo(_multimodalDb.RouterDb, new DefaultWeightHandler(_getFactor), _routerPoint);
                        }
                        else
                        { // from source -> stop.
                            path = _routerPoint.EdgePathTo(_multimodalDb.RouterDb, new DefaultWeightHandler(_getFactor), point);
                        }
                        if (path.Weight < bestWeight)
                        { // set as best because improvement.
                            best       = point;
                            bestWeight = path.Weight;
                        }
                    }
                    else
                    { // on different edge, to the usual.
                        var paths = point.ToEdgePaths(_multimodalDb.RouterDb, new DefaultWeightHandler(_getFactor), _backward);
                        EdgePath <float> visit;
                        if (_dykstra.TryGetVisit(paths[0].Vertex, out visit))
                        {         // check if this one is better.
                            if (visit.Weight + paths[0].Weight < bestWeight)
                            {     // concatenate paths and set best.
                                if (paths[0].Weight == 0)
                                { // just use the visit.
                                    best       = point;
                                    bestWeight = visit.Weight;
                                }
                                else
                                { // there is a distance/weight.
                                    best       = point;
                                    bestWeight = paths[0].Weight + visit.Weight;
                                }
                            }
                        }
                        if (paths.Length > 1 && _dykstra.TryGetVisit(paths[1].Vertex, out visit))
                        {         // check if this one is better.
                            if (visit.Weight + paths[1].Weight < bestWeight)
                            {     // concatenate paths and set best.
                                if (paths[1].Weight == 0)
                                { // just use the visit.
                                    best       = point;
                                    bestWeight = visit.Weight;
                                }
                                else
                                { // there is a distance/weight.
                                    best       = point;
                                    bestWeight = paths[1].Weight + visit.Weight;
                                }
                            }
                        }
                    }
                }
            }
            return(best);
        }
Example #2
0
        public void TestEdgePathTo()
        {
            // build router db.
            var routerDb = new RouterDb();

            routerDb.Network.AddVertex(0, 0, 0);
            routerDb.Network.AddVertex(1, .1f, .1f);
            routerDb.Network.AddEdge(0, 1, new EdgeData()
            {
                Distance = 1000,
                MetaId   = routerDb.EdgeProfiles.Add(new AttributeCollection(
                                                         new Attribute("name", "Abelshausen Blvd."))),
                Profile = (ushort)routerDb.EdgeProfiles.Add(new AttributeCollection(
                                                                new Attribute("highway", "residential")))
            }, new Coordinate(0.025f, 0.025f),
                                     new Coordinate(0.050f, 0.050f),
                                     new Coordinate(0.075f, 0.075f));

            // mock profile.
            var profile = MockProfile.CarMock();

            var point1 = new RouterPoint(0.01f, 0.01f, 0,
                                         (ushort)(0.1 * ushort.MaxValue));
            var point2 = new RouterPoint(0.09f, 0.09f, 0,
                                         (ushort)(0.9 * ushort.MaxValue));

            var path = point1.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point2);

            Assert.IsNotNull(path);
            Assert.AreEqual(800 * profile.Factor(new AttributeCollection(
                                                     new Attribute("highway", "residential"))).Value, path.Weight, 0.001f);

            path = point2.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point1);
            Assert.IsNotNull(path);
            Assert.AreEqual(800 * profile.Factor(new AttributeCollection(
                                                     new Attribute("highway", "residential"))).Value, path.Weight, 0.001f);

            path = point1.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point1);
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.001f);

            path = point2.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point2);
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.001f);

            // mock profile and force oneway forward.
            profile = MockProfile.CarMock(x => new Speed()
            {
                Value     = 50f / 3.6f,
                Direction = 1
            });

            path = point1.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point2);
            Assert.IsNotNull(path);
            Assert.AreEqual(800 * profile.Factor(new AttributeCollection(
                                                     new Attribute("highway", "residential"))).Value, path.Weight, 0.001f);

            path = point2.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point1);
            Assert.IsNull(path);

            path = point1.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point1);
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.001f);

            path = point2.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point2);
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.001f);

            // mock profile and force oneway backward.
            profile = MockProfile.CarMock(x => new Speed()
            {
                Value     = 50f / 3.6f,
                Direction = 2
            });

            path = point1.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point2);
            Assert.IsNull(path);

            path = point2.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point1);
            Assert.IsNotNull(path);
            Assert.AreEqual(800 * profile.Factor(new AttributeCollection(
                                                     new Attribute("highway", "residential"))).Value, path.Weight, 0.001f);

            path = point1.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point1);
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.001f);

            path = point2.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point2);
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.001f);

            // test the full edge.
            profile = MockProfile.CarMock();
            point1  = new RouterPoint(0f, 0f, 0, 0);
            point2  = new RouterPoint(0.1f, 0.1f, 0, ushort.MaxValue);

            path = point1.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point2);
            Assert.IsNotNull(path);
            Assert.AreEqual(1000 * profile.Factor(null).Value, path.Weight, 0.001f);
            Assert.AreEqual(1, path.Vertex);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.001f);
            Assert.AreEqual(0, path.Vertex);
            path = path.From;
            Assert.IsNull(path);

            path = point2.EdgePathTo(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), point1);
            Assert.IsNotNull(path);
            Assert.AreEqual(1000 * profile.Factor(null).Value, path.Weight, 0.001f);
            Assert.AreEqual(0, path.Vertex);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.001f);
            Assert.AreEqual(1, path.Vertex);
            path = path.From;
            Assert.IsNull(path);
        }
Example #3
0
        /// <summary>
        /// Gets the weight to the given stop.
        /// </summary>
        public float GetWeight(uint stop)
        {
            var bestWeight = float.MaxValue;

            _stopLinksDbEnumerator.MoveTo(stop);
            while (_stopLinksDbEnumerator.MoveNext())
            {
                var point = new RouterPoint(0, 0, _stopLinksDbEnumerator.EdgeId,
                                            _stopLinksDbEnumerator.Offset);
                if (point.EdgeId == _routerPoint.EdgeId)
                { // on the same edge.
                    EdgePath <float> path;
                    if (_backward)
                    { // from stop -> source.
                        path = point.EdgePathTo(_multimodalDb.RouterDb, new DefaultWeightHandler(_getFactor), _routerPoint);
                    }
                    else
                    { // from source -> stop.
                        path = _routerPoint.EdgePathTo(_multimodalDb.RouterDb, new DefaultWeightHandler(_getFactor), point);
                    }
                    if (path.Weight < bestWeight)
                    { // set as best because improvement.
                        bestWeight = path.Weight;
                    }
                }
                else
                { // on different edge, to the usual.
                    var paths = point.ToEdgePaths(_multimodalDb.RouterDb, new DefaultWeightHandler(_profile.GetGetFactor(_multimodalDb.RouterDb)), _backward);
                    EdgePath <float> visit;
                    if (_dykstra.TryGetVisit(paths[0].Vertex, out visit))
                    {     // check if this one is better.
                        if (visit.Weight + paths[0].Weight < bestWeight)
                        { // concatenate paths and set best.
                            EdgePath <float> best;
                            if (paths[0].Weight == 0)
                            { // just use the visit.
                                best = visit;
                            }
                            else
                            { // there is a distance/weight.
                                best = new EdgePath <float>(Itinero.Constants.NO_VERTEX,
                                                            paths[0].Weight + visit.Weight, visit);
                            }
                            bestWeight = best.Weight;
                        }
                    }
                    if (paths.Length > 1 && _dykstra.TryGetVisit(paths[1].Vertex, out visit))
                    {     // check if this one is better.
                        if (visit.Weight + paths[1].Weight < bestWeight)
                        { // concatenate paths and set best.
                            EdgePath <float> best;
                            if (paths[1].Weight == 0)
                            { // just use the visit.
                                best = visit;
                            }
                            else
                            { // there is a distance/weight.
                                best = new EdgePath <float>(Itinero.Constants.NO_VERTEX,
                                                            paths[1].Weight + visit.Weight, visit);
                            }
                            bestWeight = best.Weight;
                        }
                    }
                }
            }
            return(bestWeight);
        }