/// <summary> /// Tries to calculate an earliest arrival route. /// </summary> public Result <Route> TryEarliestArrival(DateTime departureTime, string sourceProfileName, Coordinate sourceLocation, string targetProfileName, Coordinate targetLocation, Dictionary <string, object> parameters) { var sourceProfile = _router.Router.Db.GetSupportedProfile(sourceProfileName); var targetProfile = _router.Router.Db.GetSupportedProfile(targetProfileName); var sourcePoint = _router.Router.TryResolve(sourceProfile, sourceLocation, 1000); if (sourcePoint.IsError) { return(sourcePoint.ConvertError <Route>()); } var targetPoint = _router.Router.TryResolve(targetProfile, targetLocation, 1000); if (targetPoint.IsError) { return(targetPoint.ConvertError <Route>()); } int maxSecondsSource = 0; if (parameters.ContainsKey("sourceTime") && parameters["sourceTime"] is int && (int)parameters["sourceTime"] > 0) { // override the default source time. maxSecondsSource = (int)parameters["sourceTime"]; } else { // get the default source time. if (!_defaultSeconds.TryGetValue(sourceProfile.Name, out maxSecondsSource)) { maxSecondsSource = 30 * 60; } } int maxSecondsTarget = 0; if (parameters.ContainsKey("targetTime") && parameters["targetTime"] is int && (int)parameters["targetTime"] > 0) { // override the default target time. maxSecondsTarget = (int)parameters["targetTime"]; } else { // get the default target time. if (!_defaultSeconds.TryGetValue(targetProfile.Name, out maxSecondsTarget)) { maxSecondsTarget = 30 * 60; } } return(_router.TryEarliestArrival(departureTime, sourcePoint.Value, sourceProfile, targetPoint.Value, targetProfile, new EarliestArrivalSettings() { MaxSecondsSource = maxSecondsSource, MaxSecondsTarget = maxSecondsTarget })); }
/// <summary> /// Executes the given test on the given router. /// </summary> public static Route Test(MultimodalRouter router, FeatureCollection test) { var source = test.Features.FirstOrException(x => x.Attributes.Contains("type", "source"), "Invalid test data: no source found."); var target = test.Features.FirstOrException(x => x.Attributes.Contains("type", "target"), "Invalid test data: no target found"); var sourceLocation = (source.Geometry as Point).Coordinate; var targetLocation = (target.Geometry as Point).Coordinate; var sourceProfile = Itinero.Profiles.Profile.GetRegistered(source.Attributes.FirstOrException(x => x == "profile", "Invalid test data: no vehicle profile found on source.").ToInvariantString()); var targetProfile = Itinero.Profiles.Profile.GetRegistered(target.Attributes.FirstOrException(x => x == "profile", "Invalid test data: no vehicle profile found on target.").ToInvariantString()); var resolvedSource = router.Router.Resolve(sourceProfile, sourceLocation); var resolvedTarget = router.Router.Resolve(targetProfile, targetLocation); var result = test.Features.First(x => x.Attributes.Contains("type", "result")); var name = result.Attributes.FirstOrException(x => x == "name", "Name of test case not found, expected on result geometry.").ToInvariantString(); Contract.Requires(name != null, "Name of test case not set."); var performanceInfoConsumer = new PerformanceInfoConsumer(name, 5000); var time = DateTime.Now; if (result.Attributes.GetNames().Contains("departuretime") && DateTime.TryParseExact(result.Attributes.FirstOrException(x => x == "departuretime", string.Empty).ToInvariantString(), "yyyyMMddHHmm", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out time)) { performanceInfoConsumer.Start(); var route = router.TryEarliestArrival(time, resolvedSource, sourceProfile, resolvedTarget, targetProfile, EarliestArrivalSettings.Default); Assert.IsFalse(route.IsError, "Route was not found."); //object value; //if (result.Attributes.TryGetValue("time", out value)) //{ // var timeResult = (long)value; // Assert.AreEqual(timeResult, route.Value.TotalTime, Settings.MinimumTotalTimeDifference); //} //performanceInfoConsumer.Stop(); //File.WriteAllText("temp.geojson", route.Value.ToGeoJson()); return(route.Value); } else { throw new Exception("Invalid test data: no departure time set."); } }
static void Main(string[] args) { Logging.Logger.LogAction = (origin, level, message, parameters) => { Console.WriteLine(string.Format("[{0}] {1} - {2}", origin, level, message)); }; // download and extract test-data. Console.WriteLine("Downloading Belgium..."); Download.DownloadBelgiumAll(); Console.WriteLine("Downloading NMBS GTFS..."); var nmbsGtfs = Download.DownloadNMBS(); Console.WriteLine("Downloading Delijn GTFS..."); var delijnGfts = Download.DownloadDeLijn(); //Console.WriteLine("Downloading TEC GTFS..."); //var tecGtfs = Download.DownloadTEC(); //Console.WriteLine("Downloading MIVB GTFS..."); //var mivbGtfs = Download.DownloadMIVB(); // build routerdb and save the result. var routerDb = Staging.RouterDbBuilder.BuildBelgium(); // build transitdb's. var nmbs = TransitDbBuilder.RunOrLoad(nmbsGtfs); var delijn = TransitDbBuilder.RunOrLoad(delijnGfts); //var tec = TransitDbBuilder.RunOrLoad(tecGtfs); //var mivb = TransitDbBuilder.RunOrLoad(mivbGtfs); // merge transit db's. var transitDb = TransitDbBuilder.Merge(nmbs, delijn); // TODO: figure out why system is broken when loading multiple operators. //var tripId = kempen.SearchAllTrips((meta) => //{ // string name; // if (meta.TryGetValue("headsign", out name)) // { // if (name.ToLowerInvariant().Contains("wechelderzande")) // { // return true; // } // } // return false; //}).First(); //var features = kempen.GetTripFeatures(tripId); //var json = ToJson(features); // build multimodal db. var multimodalDb = MultimodalDbBuilder.Run(routerDb, transitDb); // create transit router. var transitRouter = new MultimodalRouter(multimodalDb, Vehicle.Pedestrian.Fastest()); var route = transitRouter.TryEarliestArrival(new DateTime(2017, 11, 08, 18, 00, 00), transitRouter.Router.Resolve(Vehicle.Pedestrian.Fastest(), Locations.GetLocation("antwerp-central")), Vehicle.Pedestrian.Fastest(), transitRouter.Router.Resolve(Vehicle.Pedestrian.Fastest(), Locations.GetLocation("turnhout")), Vehicle.Pedestrian.Fastest(), new EarliestArrivalSettings() { MaxSecondsSource = 1500, MaxSecondsTarget = 1500 }); route = transitRouter.TryEarliestArrival(new DateTime(2017, 11, 08, 18, 00, 00), transitRouter.Router.Resolve(Vehicle.Pedestrian.Fastest(), Locations.GetLocation("antwerp-central")), Vehicle.Pedestrian.Fastest(), transitRouter.Router.Resolve(Vehicle.Pedestrian.Fastest(), Locations.GetLocation("wechelderzande")), Vehicle.Pedestrian.Fastest(), new EarliestArrivalSettings() { MaxSecondsSource = 1500, MaxSecondsTarget = 1500 }); route = transitRouter.TryEarliestArrival(new DateTime(2017, 11, 18, 18, 00, 00), transitRouter.Router.Resolve(Vehicle.Pedestrian.Fastest(), Locations.GetLocation("antwerp-central")), Vehicle.Pedestrian.Fastest(), transitRouter.Router.Resolve(Vehicle.Pedestrian.Fastest(), Locations.GetLocation("brugge")), Vehicle.Pedestrian.Fastest(), new EarliestArrivalSettings() { MaxSecondsSource = 1500, MaxSecondsTarget = 1500 }); //// run tests. //var route = Runner.Test(transitRouter, "Itinero.Transit.Test.Functional.test_data.belgium.test1.geojson"); ////route = Runner.Test(transitRouter, "Itinero.Transit.Test.Functional.test_data.belgium.test2.geojson"); //route = Runner.Test(transitRouter, "Itinero.Transit.Test.Functional.test_data.belgium.test3.geojson"); Console.WriteLine("Done!"); Console.ReadLine(); }