Beispiel #1
0
        public void TestNetwork2()
        {
            var routerDb = new RouterDb();

            routerDb.LoadTestNetwork(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    "Itinero.Transit.Test.test_data.networks.network2.geojson"));

            var profile = VehicleMock.Car(x => new FactorAndSpeed()
            {
                Direction   = 0,
                Value       = 10f,
                SpeedFactor = 10f
            }).Fastest();

            var stopLocation = new Coordinate(51.229621576122774f, 4.464208334684372f);
            var stopLinksDb  = new StopLinksDb(1, routerDb, profile);

            stopLinksDb.Add(0, new RouterPoint((float)stopLocation.Latitude, (float)stopLocation.Longitude, 1,
                                               ushort.MaxValue / 2));
            var transitDb    = new TransitDb();
            var multimodalDb = new MultimodalDb(routerDb, transitDb);

            multimodalDb.AddStopLinksDb(stopLinksDb);
            multimodalDb.TransitDb.AddStop(51.229621576122774f, 4.464208334684372f, 0);

            var stopFound      = false;
            var distanceToStop = Coordinate.DistanceEstimateInMeter(routerDb.Network.GetVertex(0),
                                                                    routerDb.Network.GetVertex(1)) + Coordinate.DistanceEstimateInMeter(routerDb.Network.GetVertex(1),
                                                                                                                                        stopLocation);
            var closestStopSearch = new ClosestStopsSearch(multimodalDb,
                                                           profile, routerDb.Network.CreateRouterPointForVertex(0, 1), 3600, false);

            closestStopSearch.StopFound = (uint stopId, float seconds) =>
            {
                Assert.AreEqual(0, stopId);
                Assert.AreEqual(distanceToStop * 10, seconds, 1);
                stopFound = true;
                return(false);
            };
            closestStopSearch.Run();

            Assert.IsTrue(stopFound);

            var path = closestStopSearch.GetPath(0);

            Assert.IsNotNull(path);
            Assert.AreEqual(distanceToStop * 10, path.Weight, 1);
            Assert.AreEqual(Itinero.Constants.NO_VERTEX, path.Vertex);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(Coordinate.DistanceEstimateInMeter(routerDb.Network.GetVertex(0),
                                                               routerDb.Network.GetVertex(1)) * 10, path.Weight, 1);
            Assert.AreEqual(1, path.Vertex);
            path = path.From;
            Assert.IsNotNull(path);
            Assert.AreEqual(0, path.Weight, 0.01);
            Assert.AreEqual(0, path.Vertex);
            path = path.From;
            Assert.IsNull(path);
        }
Beispiel #2
0
        public void TestSourceEdgeHasStop()
        {
            var routerDb = new RouterDb();

            routerDb.Network.AddVertex(0, 51.27018537520318f, 4.799609184265137f);
            routerDb.Network.AddVertex(1, 51.2682252886248f, 4.793150424957275f);
            routerDb.Network.AddEdge(0, 1,
                                     new Itinero.Data.Network.Edges.EdgeData()
            {
                Profile  = 0,
                MetaId   = 0,
                Distance = 500
            }, null);
            routerDb.EdgeProfiles.Add(new AttributeCollection());
            routerDb.EdgeMeta.Add(new AttributeCollection());

            var profile = VehicleMock.Car(x => new FactorAndSpeed()
            {
                Direction   = 0,
                Value       = .1f,
                SpeedFactor = .1f
            }).Fastest();

            var stopLinksDb = new StopLinksDb(1, routerDb, profile);

            stopLinksDb.Add(0, new RouterPoint(51.269138216062984f, 4.796175956726074f, 0, ushort.MaxValue / 2));
            var transitDb    = new TransitDb();
            var multimodalDb = new MultimodalDb(routerDb, transitDb);

            multimodalDb.AddStopLinksDb(stopLinksDb);
            multimodalDb.TransitDb.AddStop(51.269138216062984f, 4.796175956726074f, 0);

            var stopFound         = false;
            var closestStopSearch = new ClosestStopsSearch(multimodalDb,
                                                           profile, new RouterPoint(51.269138216062984f, 4.796175956726074f, 0, ushort.MaxValue / 2), 1800, false);

            closestStopSearch.StopFound = (uint stopId, float seconds) =>
            {
                if (!stopFound)
                {
                    Assert.AreEqual(0, stopId);
                    Assert.AreEqual(0, seconds);
                    stopFound = true;
                }
                return(false);
            };
            closestStopSearch.Run();

            Assert.IsTrue(closestStopSearch.HasRun);
            Assert.IsTrue(closestStopSearch.HasSucceeded);
            Assert.IsTrue(stopFound);

            var path = closestStopSearch.GetPath(0);

            Assert.IsNotNull(path);
            Assert.AreEqual(Itinero.Constants.NO_VERTEX, path.Vertex);
            Assert.IsNull(path.From);
            Assert.AreEqual(0, path.Weight);
            var point = closestStopSearch.GetTargetPoint(0);

            Assert.IsNotNull(point);
            Assert.AreEqual(0, point.EdgeId);
            Assert.AreEqual(ushort.MaxValue / 2, point.Offset);

            stopFound         = false;
            closestStopSearch = new ClosestStopsSearch(multimodalDb,
                                                       profile, new RouterPoint(51.27018537520318f, 4.799609184265137f, 0, 0), 1800, false);
            closestStopSearch.StopFound = (uint stopId, float seconds) =>
            {
                Assert.AreEqual(0, stopId);
                Assert.AreEqual(profile.Factor(null).Value * 250, seconds, .1f);
                stopFound = true;
                return(false);
            };
            closestStopSearch.Run();

            Assert.IsTrue(stopFound);

            path = closestStopSearch.GetPath(0);
            Assert.IsNotNull(path);
            Assert.AreEqual(Itinero.Constants.NO_VERTEX, path.Vertex);
            Assert.IsNotNull(path.From);
            Assert.AreEqual(0, path.From.Vertex);
            Assert.AreEqual(profile.Factor(null).Value * 250, path.Weight, .1f);
            point = closestStopSearch.GetTargetPoint(0);
            Assert.IsNotNull(point);
            Assert.AreEqual(0, point.EdgeId);
            Assert.AreEqual(ushort.MaxValue / 2, point.Offset);
        }
Beispiel #3
0
        /// <summary>
        /// Tries to calculate an earliest arrival route from stop1 to stop2.
        /// </summary>
        public override Result <Route> TryEarliestArrival(DateTime departureTime,
                                                          RouterPoint sourcePoint, Profile sourceProfile, RouterPoint targetPoint, Profile targetProfile,
                                                          EarliestArrivalSettings settings)
        {
            // get the get factor function.
            var sourceGetFactor = _router.GetDefaultGetFactor(sourceProfile);
            var targetGetFactor = _router.GetDefaultGetFactor(targetProfile);

            // create the profile search.
            var tripEnumerator = _db.TransitDb.GetTripsEnumerator();
            var transfersDb    = _db.TransitDb.GetTransfersDb(_transferProfile);
            var profileSearch  = new ProfileSearch(_db.TransitDb, departureTime, transfersDb, _db.TransitDb.GetIsTripPossibleFunc());

            // search for sources.
            var departureTimeSeconds = (uint)(departureTime - departureTime.Date).TotalSeconds;
            var sourceSearch         = new ClosestStopsSearch(_db, sourceProfile, sourceGetFactor, sourcePoint,
                                                              settings.MaxSecondsSource, false);

            sourceSearch.StopFound = (s, t) =>
            {
                profileSearch.SetSourceStop(s, departureTimeSeconds + (uint)t);
                return(false);
            };

            // search for targets.
            var targetSearch = new ClosestStopsSearch(_db, targetProfile, targetGetFactor, targetPoint,
                                                      settings.MaxSecondsTarget, true);

            targetSearch.StopFound = (s, t) =>
            {
                profileSearch.SetTargetStop(s, (uint)t);
                return(false);
            };

            // create bidirectional helper if possible.
            SearchHelper helper = null;
            BidirectionalSearchHelper bidirectionalHelper = null;

            if (sourceProfile.Name == targetProfile.Name)
            { // profiles are the same.
                bidirectionalHelper = new BidirectionalSearchHelper(
                    sourceSearch.Search, targetSearch.Search);
                targetSearch.WasEdgeFound = bidirectionalHelper.TargetWasFound;
            }
            else
            { // profiles are different but the source can still reach the destination.
                helper = new SearchHelper(_router.Db, sourceSearch.Search, sourceProfile, targetPoint);
                sourceSearch.WasEdgeFound = helper.SourceWasFound;
            }

            // run source search.
            sourceSearch.Run();
            if (!sourceSearch.HasRun ||
                !sourceSearch.HasSucceeded)
            {
                return(new Result <Route>("Searching for source stops failed."));
            }

            // run target search.
            targetSearch.Run();
            if (!targetSearch.HasRun ||
                !targetSearch.HasSucceeded)
            {
                return(new Result <Route>("Searching for target stops failed."));
            }

            // run actual profile search.
            profileSearch.Run();
            if (!profileSearch.HasRun ||
                !profileSearch.HasSucceeded)
            {
                return(new Result <Route>("No route found."));
            }

            // build routes.
            var profileSearchRouteBuilder = new ProfileSearchRouteBuilder(profileSearch);

            profileSearchRouteBuilder.Run();
            if (!profileSearchRouteBuilder.HasRun ||
                !profileSearchRouteBuilder.HasSucceeded)
            {
                return(new Result <Route>(string.Format("Route could not be built: {0}.", profileSearchRouteBuilder.ErrorMessage)));
            }

            var sourceWeight  = sourceSearch.GetWeight(profileSearchRouteBuilder.Stops[0]);
            var targetWeight  = targetSearch.GetWeight(profileSearchRouteBuilder.Stops[profileSearchRouteBuilder.Stops.Count - 1]);
            var transitWeight = sourceWeight + profileSearchRouteBuilder.Duration + targetWeight;

            if (bidirectionalHelper != null &&
                bidirectionalHelper.HasSucceeded)
            { // there is a direct route to the target.
                if (transitWeight > bidirectionalHelper.BestWeight)
                {
                    if (!this.PreferTransit || bidirectionalHelper.BestWeight < this.PreferTransitThreshold)
                    { // transit it not preferred or belof threshold.
                        var path = bidirectionalHelper.GetPath();
                        return(new Result <Route>(
                                   CompleteRouteBuilder.Build(_router.Db, sourceProfile, sourcePoint, targetPoint, path)));
                    }
                }
            }
            else if (helper != null &&
                     helper.HasSucceeded)
            { // there is a direct route to the target.
                if (transitWeight > helper.BestWeight)
                {
                    if (!this.PreferTransit || bidirectionalHelper.BestWeight < this.PreferTransitThreshold)
                    { // transit it not preferred or belof threshold.
                        var path = helper.GetPath();
                        return(new Result <Route>(
                                   CompleteRouteBuilder.Build(_router.Db, sourceProfile, sourcePoint, targetPoint, path)));
                    }
                }
            }

            // build source/target routes.
            var sourceRoute = sourceSearch.GetRoute(profileSearchRouteBuilder.Stops[0]);
            var targetRoute = targetSearch.GetRoute(profileSearchRouteBuilder.Stops[profileSearchRouteBuilder.Stops.Count - 1]);

            // concatenate it all.
            var route = sourceRoute.Concatenate(profileSearchRouteBuilder.Route);

            route = route.Concatenate(targetRoute);

            return(new Result <Route>(route));
        }