public void TestGetStop() { var db = new ShortcutsDb(Vehicle.Bicycle.Fastest()); db.AddStop(10, new AttributeCollection(new Attribute() { Key = "some_key", Value = "some_value" })); db.AddStop(11, new AttributeCollection(new Attribute() { Key = "some_key", Value = "some_value" })); db.AddStop(12, new AttributeCollection(new Attribute() { Key = "some_key1", Value = "some_value1" })); db.AddStop(13, new AttributeCollection(new Attribute() { Key = "some_key1", Value = "some_value1" })); var a = db.GetStop(10); Assert.IsNotNull(a); Assert.AreEqual(1, a.Count); }
public void TestAddStops() { var db = new ShortcutsDb(Vehicle.Bicycle.Fastest()); db.AddStop(10, new AttributeCollection(new Attribute() { Key = "some_key", Value = "some_value" })); db.AddStop(11, new AttributeCollection(new Attribute() { Key = "some_key", Value = "some_value" })); db.AddStop(12, new AttributeCollection(new Attribute() { Key = "some_key1", Value = "some_value1" })); db.AddStop(13, new AttributeCollection(new Attribute() { Key = "some_key1", Value = "some_value1" })); }
public void TestGetShortcutByVertices() { var db = new ShortcutsDb(Vehicle.Bicycle.Fastest()); db.AddStop(10, null); db.AddStop(11, null); db.AddStop(12, null); db.AddStop(13, null); var s1 = db.Add(new uint[] { 10, 100, 101, 102, 103, 11 }, new AttributeCollection(new Attribute() { Key = "some_key1", Value = "some_value1" })); var s2 = db.Add(new uint[] { 12, 110, 111, 112, 113, 13 }, new AttributeCollection(new Attribute() { Key = "some_key1", Value = "some_value1" })); IAttributeCollection meta; var s = db.Get(10, 11, out meta); Assert.IsNotNull(s); Assert.AreEqual(6, s.Length); Assert.IsNotNull(meta); Assert.AreEqual(1, meta.Count); s = db.Get(12, 13, out meta); Assert.IsNotNull(s); Assert.AreEqual(6, s.Length); Assert.IsNotNull(meta); Assert.AreEqual(1, meta.Count); }
/// <summary> /// Deserializes a database from the given stream. /// </summary> public static RouterDb Deserialize(Stream stream, RouterDbProfile profile) { // deserialize all basic data. // version1: OsmSharp.Routing state of layout. // version2: Added ShortcutsDbs. var version = stream.ReadByte(); if (version != 1 && version != 2) { throw new Exception(string.Format("Cannot deserialize routing db: Invalid version #: {0}.", version)); } var guidBytes = new byte[16]; stream.Read(guidBytes, 0, 16); var guid = new Guid(guidBytes); var supportedProfiles = stream.ReadWithSizeStringArray(); var metaDb = stream.ReadWithSizeAttributesCollection(); var shorcutsCount = (int)0; if (version == 2) { // when version < 1 there are no shortcuts and thus no shortcut count. shorcutsCount = stream.ReadByte(); } var contractedCount = stream.ReadByte(); var profiles = AttributesIndex.Deserialize(new LimitedStream(stream), true); var meta = AttributesIndex.Deserialize(new LimitedStream(stream), true); var network = RoutingNetwork.Deserialize(stream, profile == null ? null : profile.RoutingNetworkProfile); // create router db. var routerDb = new RouterDb(guid, network, profiles, meta, metaDb, supportedProfiles); // read all shortcut dbs. for (var i = 0; i < shorcutsCount; i++) { var shortcutsName = stream.ReadWithSizeString(); var shorcutsDb = ShortcutsDb.Deserialize(stream); routerDb._shortcutsDbs[shortcutsName] = shorcutsDb; } // read all contracted versions. for (var i = 0; i < contractedCount; i++) { var profileName = stream.ReadWithSizeString(); var contracted = ContractedDb.Deserialize(stream, profile == null ? null : profile.ContractedDbProfile); routerDb._contracted[profileName] = contracted; } return(routerDb); }
public void TestAddShortcuts() { var db = new ShortcutsDb(Vehicle.Bicycle.Fastest()); db.AddStop(10, null); db.AddStop(11, null); db.AddStop(12, null); db.AddStop(13, null); db.Add(new uint[] { 10, 100, 101, 102, 103, 11 }, new AttributeCollection(new Attribute() { Key = "some_key1", Value = "some_value1" })); db.Add(new uint[] { 12, 110, 111, 112, 113, 13 }, new AttributeCollection(new Attribute() { Key = "some_key1", Value = "some_value1" })); }
/// <summary> /// Executes the actual run. /// </summary> protected override void DoRun() { var router = new Router(_db); router.VerifyAllStoppable = true; // resolve locations as vertices. // WARNING: use RouterPointEmbedder first. var points = new RouterPoint[_locations.Length]; for (var i = 0; i < points.Length; i++) { var resolver = new Algorithms.Search.ResolveVertexAlgorithm(_db.Network.GeometricGraph, _locations[i].Latitude, _locations[i].Longitude, 500, 3000, router.GetIsAcceptable(_profile)); resolver.Run(); if (!resolver.HasSucceeded) { throw new System.Exception(string.Format("Could not resolve shortcut location at index {1}: {0}.", resolver.ErrorMessage, i)); } points[i] = resolver.Result; } // use non-contracted calculation. var weightHandler = _profile.AugmentedWeightHandlerCached(_db); var algorithm = new Itinero.Algorithms.Default.ManyToMany <Weight>(_db, weightHandler, points, points, new Weight() { Distance = float.MaxValue, Time = _maxShortcutDuration, Value = _maxShortcutDuration }); algorithm.Run(); if (!algorithm.HasSucceeded) { this.ErrorMessage = "Many to many calculation failed: " + algorithm.ErrorMessage; } // build shortcuts db. _shortcutsDb = new ShortcutsDb(_profile); for (var i = 0; i < points.Length; i++) { _shortcutsDb.AddStop(points[i].VertexId(_db), _locationsMeta[i]); } var routes = new EdgePath <float> [_locations.Length][]; _shortcutIds = new uint[_locations.Length][]; var pathList = new List <uint>(); var shortcutProfile = _db.EdgeProfiles.Add(new AttributeCollection( new Attribute(ShortcutExtensions.SHORTCUT_KEY, _name))); for (var i = 0; i < _locations.Length; i++) { _shortcutIds[i] = new uint[_locations.Length]; for (var j = 0; j < _locations.Length; j++) { _shortcutIds[i][j] = uint.MaxValue; if (i == j) { continue; } EdgePath <Weight> path; if (algorithm.TryGetPath(i, j, out path)) { pathList.Clear(); path.AddToListAsVertices(pathList); if (path.Weight.Time < _minShortcutSize) { // don't add very short shortcuts. continue; } if (pathList.Count < 2) { Itinero.Logging.Logger.Log("ShortcutBuilder", TraceEventType.Warning, "Shortcut consists of only one vertex from {0}@{1} -> {2}@{3}!", i, _locations[i], j, _locations[j]); continue; } if (pathList[0] == pathList[pathList.Count - 1]) { Itinero.Logging.Logger.Log("ShortcutBuilder", TraceEventType.Warning, "Shortcut has the same start and end vertex from {0}@{1} -> {2}@{3}!", i, _locations[i], j, _locations[j]); continue; } // add the shortcut and keep the id. var shortcutId = _shortcutsDb.Add(pathList.ToArray(), null); _shortcutIds[i][j] = shortcutId; // add the shortcut as an edge. _db.Network.AddEdge(pathList[0], pathList[pathList.Count - 1], new Data.Network.Edges.EdgeData() { Distance = path.Weight.Time + _switchPentaly, MetaId = 0, Profile = (ushort)shortcutProfile }, null); } else { Itinero.Logging.Logger.Log("ShortcutBuilder", TraceEventType.Warning, "Shortcut not found from {0}@{1} -> {2}@{3}!", i, _locations[i], j, _locations[j]); } } } }
/// <summary> /// Deserializes a database from the given stream. /// </summary> public static RouterDb Deserialize(Stream stream, RouterDbProfile profile) { // deserialize all basic data. // version1: OsmSharp.Routing state of layout. // version2: Added ShortcutsDbs. // version3: Add advanced profile serialization. // version4: Added missing restriction dbs. var version = stream.ReadByte(); if (version != 1 && version != 2 && version != 3 && version != 4) { throw new Exception(string.Format("Cannot deserialize routing db: Invalid version #: {0}.", version)); } var guidBytes = new byte[16]; stream.Read(guidBytes, 0, 16); var guid = new Guid(guidBytes); var supportedVehicleInstances = new List <Vehicle>(); if (version <= 2) { // just contains vehicle names. var supportedVehicles = stream.ReadWithSizeStringArray(); foreach (var vehicleName in supportedVehicles) { Profile vehicleProfile; if (Profile.TryGet(vehicleName, out vehicleProfile)) { supportedVehicleInstances.Add(vehicleProfile.Parent); } else { Itinero.Logging.Logger.Log("RouterDb", Logging.TraceEventType.Warning, "Vehicle with name {0} was not found, register all vehicle profiles before deserializing the router db.", vehicleName); } } } else { // contains the full vehicles. var lengthBytes = new byte[4]; stream.Read(lengthBytes, 0, 4); var size = BitConverter.ToInt32(lengthBytes, 0); for (var i = 0; i < size; i++) { var vehicle = Vehicle.Deserialize(stream); supportedVehicleInstances.Add(vehicle); vehicle.Register(); } } var metaDb = stream.ReadWithSizeAttributesCollection(); var shorcutsCount = (int)0; if (version >= 2) { // when version < 1 there are no shortcuts and thus no shortcut count. shorcutsCount = stream.ReadByte(); } var contractedCount = stream.ReadByte(); var restrictionDbCount = 0; if (version >= 4) { restrictionDbCount = stream.ReadByte(); } var profiles = AttributesIndex.Deserialize(new LimitedStream(stream), true); var meta = AttributesIndex.Deserialize(new LimitedStream(stream), true); var network = RoutingNetwork.Deserialize(stream, profile == null ? null : profile.RoutingNetworkProfile); // create router db. var routerDb = new RouterDb(guid, network, profiles, meta, metaDb, supportedVehicleInstances.ToArray()); // read all shortcut dbs. for (var i = 0; i < shorcutsCount; i++) { var shortcutsName = stream.ReadWithSizeString(); var shorcutsDb = ShortcutsDb.Deserialize(stream); routerDb._shortcutsDbs[shortcutsName] = shorcutsDb; } // read all contracted versions. for (var i = 0; i < contractedCount; i++) { var profileName = stream.ReadWithSizeString(); var contracted = ContractedDb.Deserialize(stream, profile == null ? null : profile.ContractedDbProfile); routerDb._contracted[profileName] = contracted; } // read all restriction dbs. for (var i = 0; i < restrictionDbCount; i++) { var restrictionDbName = stream.ReadWithSizeString(); var restrictionDb = RestrictionsDb.Deserialize(stream, profile == null ? null : profile.RestrictionDbProfile); routerDb._restrictionDbs[restrictionDbName] = restrictionDb; } return(routerDb); }
/// <summary> /// Tries to get a shortcuts db. /// </summary> public bool TryGetShortcuts(string name, out ShortcutsDb shortcutsDb) { return(_shortcutsDbs.TryGetValue(name, out shortcutsDb)); }
/// <summary> /// Adds a shortcuts db. /// </summary> public void AddShortcuts(string name, ShortcutsDb shortcutsDb) { _shortcutsDbs[name] = shortcutsDb; }