/// <summary> /// Creates a new routing instances. /// </summary> public Instance(MultimodalRouter router, int carTime = 15 * 60, int pedestrianTime = 10 * 60, int bicycleTime = 5 * 60) { _router = router; _defaultSeconds = new Dictionary <string, int>(); _defaultSeconds.Add("car", carTime); _defaultSeconds.Add("pedestrian", pedestrianTime); _defaultSeconds.Add("bicycle", bicycleTime); }
/// <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."); } }
/// <summary> /// Loads an instance from a routing file. /// </summary> public static bool LoadInstance(FileInfo file) { try { if (!file.Exists) { return(false); } Logger.Log("Bootstrapper", TraceEventType.Information, "Loading instance {1} from: {0}", file.FullName, file.Name.GetNameUntilFirstDot()); if (file.Name.EndsWith("routerdb")) { RouterDb routerDb; using (var stream = File.OpenRead(file.FullName)) { routerDb = RouterDb.Deserialize(stream); } var multimodalDb = new MultimodalDb(routerDb, new TransitDb()); var multimodalRouter = new MultimodalRouter(multimodalDb, Itinero.Osm.Vehicles.Vehicle.Pedestrian.Fastest()); var instance = new Instances.Instance(multimodalRouter); InstanceManager.Register(file.Name.GetNameUntilFirstDot(), instance); } else if (file.Name.EndsWith("multimodaldb")) { MultimodalDb routerDb; using (var stream = File.OpenRead(file.FullName)) { routerDb = MultimodalDb.Deserialize(stream); } var multimodalRouter = new MultimodalRouter(routerDb, Itinero.Osm.Vehicles.Vehicle.Pedestrian.Fastest()); var instance = new Instances.Instance(multimodalRouter); InstanceManager.Register(file.Name.GetNameUntilFirstDot(), instance); } Logger.Log("Bootstrapper", TraceEventType.Information, "Loaded instance {1} from: {0}", file.FullName, file.Name.GetNameUntilFirstDot()); return(true); } catch (Exception ex) { Logger.Log("Bootstrapper", TraceEventType.Critical, "Failed to load file {0}: {1}", file, ex.ToInvariantString()); } return(false); }
/// <summary> /// Executes the given test on the given router. /// </summary> public static Route Test(MultimodalRouter router, string embeddedResourceId) { return(Test(router, Staging.Data.GetFeatureCollection(embeddedResourceId))); }
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(); }
/// <summary> /// Loads an instance from a folder. /// </summary> /// <returns></returns> public static bool LoadInstanceFromFolder(DirectoryInfo folder) { try { if (!folder.Exists) { return(false); } Logger.Log("Bootstrapper", TraceEventType.Information, "Loading instance {1} from: {0}", folder.FullName, folder.Name); var profiles = new List <Itinero.Profiles.Vehicle>(); foreach (var luaFile in folder.EnumerateFiles("*.lua")) { try { using (var stream = luaFile.OpenRead()) { profiles.Add(Itinero.Profiles.DynamicVehicle.LoadFromStream(stream)); } Logger.Log("Bootstrapper", TraceEventType.Information, "Loaded profile {0}.", luaFile.FullName); } catch (Exception ex) { Logger.Log("Bootstrapper", TraceEventType.Error, "Failed loading profile {0}:{1}", luaFile.FullName, ex.ToInvariantString()); } } if (profiles.Count == 0) { Logger.Log("Bootstrapper", TraceEventType.Information, "Loading instance {1} from: {0}, no vehicle profiles found or they could not be loaded.", folder.FullName, folder.Name); return(true); } var osmFile = folder.EnumerateFiles("*.osm").Concat( folder.EnumerateFiles("*.osm.pbf")).First(); var routerDb = new RouterDb(); using (var osmFileStream = osmFile.OpenRead()) { OsmStreamSource source; if (osmFile.FullName.EndsWith(".osm")) { source = new XmlOsmStreamSource(osmFileStream); } else { source = new PBFOsmStreamSource(osmFileStream); } routerDb.LoadOsmData(source, profiles.ToArray()); } var multimodalDb = new MultimodalDb(routerDb, new TransitDb()); var multimodalRouter = new MultimodalRouter(multimodalDb, Itinero.Osm.Vehicles.Vehicle.Pedestrian.Fastest()); var instance = new Instances.Instance(multimodalRouter); InstanceManager.Register(folder.Name, instance); Logger.Log("Bootstrapper", TraceEventType.Information, "Loaded instance {1} from: {0}", folder.FullName, folder.Name); return(true); } catch (Exception ex) { Logger.Log("Bootstrapper", TraceEventType.Critical, "Failed to load instance {1} from: {0}, {2}", folder.FullName, folder.Name, ex.ToInvariantString()); } return(false); }