Example #1
0
        /// <summary>
        /// Tries to calculate a tree starting at the given location.
        /// </summary>
        public static Result <Tree> TryCalculateTree(this RouterBase router, Profile profile, RouterPoint origin, float max)
        {
            if (!router.SupportsAll(profile))
            {
                return(new Result <Tree>(string.Format("Profile {0} not supported.",
                                                       profile.Name)));
            }

            if (profile.Metric != ProfileMetric.TimeInSeconds)
            {
                return(new Result <Tree>(string.Format("Profile {0} not supported, only profiles with metric TimeInSeconds are supported.",
                                                       profile.Name)));
            }

            // get the weight handler.
            var weightHandler = router.GetDefaultWeightHandler(profile);
            var getFactor     = router.GetDefaultGetFactor(profile);

            // calculate isochrones.
            var treeBuilder = new TreeBuilder(
                new DykstraEdgeVisitor(router.Db.Network.GeometricGraph,
                                       getFactor, origin.ToEdgePaths <float>(router.Db, weightHandler, true), max));

            treeBuilder.Run();

            return(new Result <Tree>(treeBuilder.Tree));
        }
Example #2
0
        /// <summary>
        /// Calculates heatmap for the given profile.
        /// </summary>
        public static HeatmapResult CalculateHeatmap(this RouterBase router, Profile profile, RouterPoint origin, int limitInSeconds, int zoom = 16)
        {
            if (!router.SupportsAll(profile))
            {
                throw new ArgumentException(string.Format("Profile {0} not supported.",
                                                          profile.Name));
            }

            if (profile.Metric != ProfileMetric.TimeInSeconds)
            {
                throw new ArgumentException(string.Format("Profile {0} not supported, only profiles with metric TimeInSeconds are supported.",
                                                          profile.Name));
            }

            // get the weight handler.
            var weightHandler = router.GetDefaultWeightHandler(profile);
            var getFactor     = router.GetDefaultGetFactor(profile);

            // calculate isochrones.
            var isochrone = new TileBasedHeatmapBuilder(
                new DykstraEdgeVisitor(router.Db.Network.GeometricGraph,
                                       getFactor, origin.ToEdgePaths <float>(router.Db, weightHandler, true), limitInSeconds), zoom);

            isochrone.Run();

            var result = isochrone.Result;

            result.MaxMetric = profile.Name;
            return(result);
        }
Example #3
0
        /// <summary>
        /// Creates a new search helper.
        /// </summary>
        public SearchHelper(RouterDb routerDb, Dykstra search, Profile profile, RouterPoint target)
        {
            _search = search;
            _target = target;

            _targets     = new HashSet <uint>();
            _targetPaths = target.ToEdgePaths(routerDb, new DefaultWeightHandler(profile.GetGetFactor(routerDb)), false);
            for (var i = 0; i < _targetPaths.Length; i++)
            {
                _targets.Add(_targetPaths[i].Vertex);
            }
        }
Example #4
0
        /// <summary>
        /// Creates a new closest stop search.
        /// </summary>
        public ClosestStopsSearch(MultimodalDb multimodalDb, Profile profile, Func <ushort, Factor> getFactor,
                                  RouterPoint routerPoint, float maxSeconds, bool backward)
        {
            if (profile.Metric != ProfileMetric.TimeInSeconds)
            { // oeps, this profile is not time-based!
                throw new ArgumentOutOfRangeException(
                          "The default closest stop search can only be used with profiles that use time in seconds as a metric.");
            }

            _multimodalDb = multimodalDb;
            _profile      = profile;
            _getFactor    = getFactor;
            _routerPoint  = routerPoint;
            _maxSeconds   = maxSeconds;
            _backward     = backward;

            // build search.
            _dykstra = new Dykstra(_multimodalDb.RouterDb.Network.GeometricGraph.Graph, new DefaultWeightHandler(_getFactor), x => Itinero.Constants.NO_VERTEX,
                                   _routerPoint.ToEdgePaths(_multimodalDb.RouterDb, new DefaultWeightHandler(_getFactor), !_backward), _maxSeconds, _backward);
        }
Example #5
0
        /// <summary>
        /// Calculates isochrones for the given profile based on the given limits.
        /// </summary>
        public static List <LocalGeo.Polygon> CalculateIsochrones(this RouterBase router, Profile profile, RouterPoint origin, List <float> limits, int zoom = 16)
        {
            if (profile.Metric != ProfileMetric.TimeInSeconds)
            {
                throw new ArgumentException(string.Format("Profile {0} not supported, only profiles with metric TimeInSeconds are supported.",
                                                          profile.Name));
            }

            // get the weight handler.
            var weightHandler = router.GetDefaultWeightHandler(profile);
            var getFactor     = router.GetDefaultGetFactor(profile);

            // calculate isochrones.
            var isochrone = new TileBasedIsochroneBuilder(
                new DykstraEdgeVisitor(router.Db.Network.GeometricGraph,
                                       getFactor, origin.ToEdgePaths <float>(router.Db, weightHandler, true),
                                       limits.Max() * 1.1f), limits, zoom);

            isochrone.Run();

            return(isochrone.Polygons);
        }
Example #6
0
        public void TestToEdgePaths()
        {
            var distance = Coordinate.DistanceEstimateInMeter(new Coordinate(0, 0),
                                                              new Coordinate(0.1f, 0.1f));

            // build router db.
            var routerDb = new RouterDb(Itinero.Data.Edges.EdgeDataSerializer.MAX_DISTANCE);

            routerDb.Network.AddVertex(0, 0, 0);
            routerDb.Network.AddVertex(1, .1f, .1f);
            routerDb.Network.AddEdge(0, 1, new EdgeData()
            {
                Distance = distance,
                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 point = new RouterPoint(0.04f, 0.04f, 0, (ushort)(0.4 * ushort.MaxValue));
            var paths = point.ToEdgePaths(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), false);

            var factor = profile.Factor(new AttributeCollection(
                                            new Attribute("highway", "residential")));
            var weight0 = Coordinate.DistanceEstimateInMeter(new Coordinate(0, 0),
                                                             new Coordinate(0.04f, 0.04f)) * factor.Value;
            var weight1 = Coordinate.DistanceEstimateInMeter(new Coordinate(.1f, .1f),
                                                             new Coordinate(0.04f, 0.04f)) * factor.Value;

            Assert.IsNotNull(paths);
            Assert.AreEqual(2, paths.Length);

            Assert.IsNotNull(paths.First(x => x.Vertex == 0));
            Assert.AreEqual(weight0, paths.First(x => x.Vertex == 0).Weight, 0.01);
            Assert.IsNotNull(paths.First(x => x.Vertex == 0).From);
            Assert.AreEqual(Constants.NO_VERTEX, paths.First(x => x.Vertex == 0).From.Vertex);

            Assert.IsNotNull(paths.First(x => x.Vertex == 1));
            Assert.AreEqual(weight1, paths.First(x => x.Vertex == 1).Weight, 0.01);
            Assert.IsNotNull(paths.First(x => x.Vertex == 1).From);
            Assert.AreEqual(Constants.NO_VERTEX, paths.First(x => x.Vertex == 1).From.Vertex);

            point = new RouterPoint(0, 0, 0, 0);
            paths = point.ToEdgePaths(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), true);

            Assert.IsNotNull(paths.First(x => x.Vertex == 0));
            Assert.AreEqual(0, paths.First(x => x.Vertex == 0).Weight, 0.01);
            Assert.IsNull(paths.First(x => x.Vertex == 0).From);

            Assert.IsNotNull(paths.First(x => x.Vertex == 1));
            Assert.AreEqual(distance * profile.Factor(null).Value, paths.First(x => x.Vertex == 1).Weight, 0.01);
            Assert.IsNotNull(paths.First(x => x.Vertex == 1).From);
            Assert.AreEqual(0, paths.First(x => x.Vertex == 1).From.Vertex);

            point = new RouterPoint(.1f, .1f, 0, ushort.MaxValue);
            paths = point.ToEdgePaths(routerDb, profile.DefaultWeightHandler(new Router(routerDb)), true);

            Assert.IsNotNull(paths.First(x => x.Vertex == 0));
            Assert.AreEqual(distance * profile.Factor(null).Value, paths.First(x => x.Vertex == 0).Weight, 0.01);
            Assert.IsNotNull(paths.First(x => x.Vertex == 0).From);
            Assert.AreEqual(1, paths.First(x => x.Vertex == 0).From.Vertex);

            Assert.IsNotNull(paths.First(x => x.Vertex == 1));
            Assert.AreEqual(0, paths.First(x => x.Vertex == 1).Weight, 0.01);
            Assert.IsNull(paths.First(x => x.Vertex == 1).From);
        }
Example #7
0
        /// <summary>
        /// Tries to calculate a tree starting at the given location.
        /// </summary>
        public static Result <Tree> TryCalculateTree(this RouterBase router, Profile profile, RouterPoint origin, float max)
        {
            if (!router.SupportsAll(profile))
            {
                return(new Result <Tree>(string.Format("Profile {0} not supported.",
                                                       profile.FullName)));
            }

            if (profile.Metric != ProfileMetric.TimeInSeconds)
            {
                return(new Result <Tree>(string.Format("Profile {0} not supported, only profiles with metric TimeInSeconds are supported.",
                                                       profile.FullName)));
            }

            // get the weight handler.
            var weightHandler = router.GetDefaultWeightHandler(profile);
            var getFactor     = router.GetDefaultGetFactor(profile);

            // calculate isochrones.
            TreeBuilder treeBuilder = null;

            if (!router.Db.HasComplexRestrictions(profile))
            {
                treeBuilder = new TreeBuilder(router.Db.Network.GeometricGraph,
                                              new Algorithms.Default.Dykstra(router.Db.Network.GeometricGraph.Graph,
                                                                             weightHandler, null, origin.ToEdgePaths <float>(router.Db, weightHandler, true), max, router.Closures, false));
            }
            else
            {
                treeBuilder = new TreeBuilder(router.Db.Network.GeometricGraph,
                                              new Algorithms.Default.EdgeBased.Dykstra(router.Db.Network.GeometricGraph.Graph,
                                                                                       weightHandler, router.Db.GetGetRestrictions(profile, true), origin.ToEdgePaths <float>(router.Db, weightHandler, true), max, router.Closures, false));
            }
            treeBuilder.Run();

            return(new Result <Tree>(treeBuilder.Tree));
        }
Example #8
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 #9
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);
        }