Exemplo n.º 1
0
        public Result <Route> TryCalculate(Profile profile, RouterPoint source, RouterPoint target)
        {
            if (!this._db.Supports(profile))
            {
                return(new Result <Route>("Routing profile is not supported.", (Func <string, Exception>)(message => new Exception(message))));
            }
            Func <ushort, Factor> getFactor = this.GetGetFactor(profile);
            DirectedMetaGraph     contracted;
            float       weight;
            List <uint> path;

            if (this._db.TryGetContracted(profile, out contracted))
            {
                OsmSharp.Routing.Algorithms.Contracted.BidirectionalDykstra bidirectionalDykstra = new OsmSharp.Routing.Algorithms.Contracted.BidirectionalDykstra(contracted, (IEnumerable <Path>)source.ToPaths(this._db, getFactor, true), (IEnumerable <Path>)target.ToPaths(this._db, getFactor, false));
                bidirectionalDykstra.Run();
                if (!bidirectionalDykstra.HasSucceeded)
                {
                    return(new Result <Route>(bidirectionalDykstra.ErrorMessage, (Func <string, Exception>)(message => (Exception) new RouteNotFoundException(message))));
                }
                path = bidirectionalDykstra.GetPath(out weight);
            }
            else
            {
                OsmSharp.Routing.Algorithms.Default.BidirectionalDykstra bidirectionalDykstra = new OsmSharp.Routing.Algorithms.Default.BidirectionalDykstra(new OsmSharp.Routing.Algorithms.Default.Dykstra(this._db.Network.GeometricGraph.Graph, getFactor, (IEnumerable <Path>)source.ToPaths(this._db, getFactor, true), float.MaxValue, false), new OsmSharp.Routing.Algorithms.Default.Dykstra(this._db.Network.GeometricGraph.Graph, getFactor, (IEnumerable <Path>)target.ToPaths(this._db, profile, false), float.MaxValue, true));
                bidirectionalDykstra.Run();
                if (!bidirectionalDykstra.HasSucceeded)
                {
                    return(new Result <Route>(bidirectionalDykstra.ErrorMessage, (Func <string, Exception>)(message => (Exception) new RouteNotFoundException(message))));
                }
                path = bidirectionalDykstra.GetPath(out weight);
            }
            if ((int)source.EdgeId == (int)target.EdgeId)
            {
                Path from = source.PathTo(this._db, profile, target);
                if (from != null && (double)from.Weight < (double)weight)
                {
                    path = new List <uint>(2);
                    for (; from != null; from = from.From)
                    {
                        path.Insert(0, from.Vertex);
                    }
                }
            }
            return(this.BuildRoute(profile, source, target, path));
        }
Exemplo n.º 2
0
 public static Path PathTo(this RouterPoint point, RouterDb db, Profile profile, RouterPoint target)
 {
     return(point.PathTo(db, (Func <ushort, Factor>)(p => profile.Factor(db.EdgeProfiles.Get((uint)p))), target));
 }