/// <summary> /// Gets a feature collection with features representing the stop links. /// </summary> /// <returns></returns> public static FeatureCollection GetStopLinks(this MultimodalDb db, Profile profile, uint stopId) { var features = new FeatureCollection(); var stopEnumerator = db.TransitDb.GetStopsEnumerator(); if (stopEnumerator.MoveTo(stopId)) { var stopLocation = new GeoAPI.Geometries.Coordinate(stopEnumerator.Longitude, stopEnumerator.Latitude); var attributes = new AttributesTable(); attributes.AddAttribute("stop_id", stopId); features.Add(new Feature(new Point(stopLocation), attributes)); var stopLinksDb = db.GetStopLinksDb(profile).GetEnumerator(); stopLinksDb.MoveTo(stopId); while (stopLinksDb.MoveNext()) { var routerPoint = new RouterPoint(0, 0, stopLinksDb.EdgeId, stopLinksDb.Offset); var linkLocation = routerPoint.LocationOnNetwork(db.RouterDb).ToCoordinate(); attributes = new AttributesTable(); attributes.AddAttribute("edge_id", stopLinksDb.EdgeId.ToInvariantString()); attributes.AddAttribute("offset", stopLinksDb.Offset.ToInvariantString()); features.Add(new Feature(new Point(linkLocation), attributes)); features.Add(new Feature(new LineString(new GeoAPI.Geometries.Coordinate[] { stopLocation, linkLocation }), new AttributesTable())); } } return(features); }
/// <summary> /// Creates a multimodal router. /// </summary> public MultimodalRouter(MultimodalDb db, Profile transferProfile) { _db = db; _transferProfile = transferProfile; _router = new Router(db.RouterDb); this.PreferTransit = true; this.PreferTransitThreshold = 60 * 5; }
/// <summary> /// Tests building a multimodaldb. /// </summary> public static Func <MultimodalDb> GetTestBuildMultimopdalDb(RouterDb routerDb, TransitDb transitDb) { return(() => { var multimodalDb = new MultimodalDb(routerDb, transitDb); multimodalDb.TransitDb.AddTransfersDb(Itinero.Osm.Vehicles.Vehicle.Pedestrian.Fastest(), 100); multimodalDb.AddStopLinksDb(Itinero.Osm.Vehicles.Vehicle.Pedestrian.Fastest(), maxDistance: 100); return multimodalDb; }); }
public void TestAddStopLinksDb() { // build a simple network and connections db. var routerDb = new RouterDb(); routerDb.LoadTestNetwork( System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( "Itinero.Transit.Test.test_data.networks.network1.geojson")); var transitDb = new TransitDb(); var feed = DummyGTFSFeedBuilder.OneConnection( TimeOfDay.FromTotalSeconds(0), TimeOfDay.FromTotalSeconds(3600)); feed.Stops.Get(0).Latitude = 51.22965768754021f; feed.Stops.Get(0).Longitude = 4.460974931716918f; feed.Stops.Get(1).Latitude = 51.229617377118906f; feed.Stops.Get(1).Longitude = 4.463152885437011f; transitDb.LoadFrom(feed); var db = new MultimodalDb(routerDb, transitDb); // add stop links. var profile = VehicleMock.Car().Fastest(); db.AddStopLinksDb(profile); // check result. var stopLinksDb = db.GetStopLinksDb(profile); Assert.IsNotNull(stopLinksDb); var stop0 = db.TransitDb.SearchFirstStopsWithTags((t) => { return(t.Contains("id", "0")); }); var stop1 = db.TransitDb.SearchFirstStopsWithTags((t) => { return(t.Contains("id", "1")); }); var stopLinksDbEnumerator = stopLinksDb.GetEnumerator(); stopLinksDbEnumerator.MoveTo(stop0); Assert.AreEqual(1, stopLinksDbEnumerator.Count); Assert.IsTrue(stopLinksDbEnumerator.MoveNext()); Assert.AreEqual(0, stopLinksDbEnumerator.EdgeId); Assert.AreEqual(0, stopLinksDbEnumerator.Offset); stopLinksDbEnumerator.MoveTo(stop1); Assert.AreEqual(1, stopLinksDbEnumerator.Count); Assert.IsTrue(stopLinksDbEnumerator.MoveNext()); Assert.AreEqual(0, stopLinksDbEnumerator.EdgeId); Assert.AreEqual(ushort.MaxValue, stopLinksDbEnumerator.Offset); }
/// <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); }
public static void Main(string[] args) { Itinero.Logging.Logger.LogAction = (o, level, message, parameters) => { Log.Information(string.Format("[{0}] {1} - {2}", o, level, message)); }; // attach logger. Log.Logger = new LoggerConfiguration() .WriteTo.ColoredConsole() .CreateLogger(); // load multimodal db and extract tiles. var multimodalDb = MultimodalDb.Deserialize(File.OpenRead(@"C:\work\data\routing\belgium.multimodaldb")); var tile = Tiles.Tile.CreateAroundLocation(51.267966846313556f, 4.801913201808929f, 9); var tileRange = tile.GetSubTiles(14); var func = new Action(() => { foreach (var t in tileRange) { var config = new VectorTileConfig() { SegmentLayerConfig = new SegmentLayerConfig() { Name = "transportation" }, StopLayerConfig = new StopLayerConfig() { Name = "stops" } }; //Log.Information("Extracting tile: {0}", t.ToInvariantString()); var vectorTile = multimodalDb.ExtractTile(t.Id, config); //Log.Information("Writing tile: {0}", t.ToInvariantString()); using (var stream = File.Open(t.Id.ToInvariantString() + ".mvt", FileMode.Create)) { Itinero.VectorTiles.Mapbox.MapboxTileWriter.Write(vectorTile, stream); } } }); func.TestPerf(string.Format("Extracted and written {0} tiles.", tileRange.Count)); Console.ReadLine(); }
/// <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); }
/// <summary> /// Extract data for the given tile. /// </summary> public static VectorTile ExtractTile(this MultimodalDb db, ulong tileId, VectorTileConfig config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } var layers = new List <Layer>(); if (config.SegmentLayerConfig != null) { layers.Add(db.RouterDb.ExtractSegmentLayer(tileId, config.SegmentLayerConfig)); } if (config.StopLayerConfig != null) { layers.Add(db.TransitDb.ExtractPointLayerForStops(tileId, config.StopLayerConfig)); } return(new VectorTile() { Layers = layers, TileId = tileId }); }
/// <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); }
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); }
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); }
/// <summary> /// Creates a new closest stop search. /// </summary> public ClosestStopsSearch(MultimodalDb multimodalDb, Profile profile, RouterPoint routerPoint, float maxSeconds, bool backward) : this(multimodalDb, profile, (p) => profile.Factor(multimodalDb.RouterDb.EdgeProfiles.Get(p)), routerPoint, maxSeconds, backward) { }