/// <summary> /// Creates a new router database. /// </summary> private RouterDb(Guid guid, RoutingNetwork network, AttributesIndex profiles, AttributesIndex meta, MappedAttributesIndex metaVertex, MetaCollectionDb vertexData, IAttributeCollection dbMeta, Profiles.Vehicle[] supportedVehicles) { _guid = guid; _network = network; _edgeProfiles = profiles; _meta = meta; _metaVertex = metaVertex; _vertexData = vertexData; _dbMeta = dbMeta; _supportedVehicles = new Dictionary <string, Vehicle>(); _supportedProfiles = new Dictionary <string, Profile>(); foreach (var vehicle in supportedVehicles) { _supportedVehicles[vehicle.Name.ToLowerInvariant()] = vehicle; foreach (var profile in vehicle.GetProfiles()) { _supportedProfiles[profile.FullName.ToLowerInvariant()] = profile; } } _contracted = new Dictionary <string, ContractedDb>(); _restrictionDbs = new Dictionary <string, RestrictionsDb>(); _shortcutsDbs = new Dictionary <string, ShortcutsDb>(); }
/// <summary> /// Creates a new router db. /// </summary> /// <param name="configuration">The configuration.</param> public RouterDb(RouterDbConfiguration configuration = null) { configuration = configuration ?? RouterDbConfiguration.Default; this.EdgeDataLayout = configuration.EdgeDataLayout ?? new EdgeDataLayout(); _network = new Graph(configuration.Zoom, this.EdgeDataLayout.Size); _edgesMeta = new MappedAttributesIndex(); }
public void TestSerializeDeserialize() { var map = new MappedAttributesIndex(); map[12345] = new AttributeCollection( new Attribute("highway", "primary")); map[123] = new AttributeCollection( new Attribute("highway", "residential")); map[1234] = new AttributeCollection( new Attribute("highway", "residential")); map[12] = new AttributeCollection( new Attribute("highway", "secondary")); map[1] = new AttributeCollection( new Attribute("highway", "secondary")); using (var stream = new MemoryStream()) { map.Serialize(stream); Assert.IsTrue(map.IsOptimized); Assert.AreEqual(map[1], new AttributeCollection( new Attribute("highway", "secondary"))); Assert.AreEqual(map[12], new AttributeCollection( new Attribute("highway", "secondary"))); Assert.AreEqual(map[123], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[1234], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[12345], new AttributeCollection( new Attribute("highway", "primary"))); stream.Seek(0, SeekOrigin.Begin); map = MappedAttributesIndex.Deserialize(stream, null); } Assert.AreEqual(map[1], new AttributeCollection( new Attribute("highway", "secondary"))); Assert.AreEqual(map[12], new AttributeCollection( new Attribute("highway", "secondary"))); Assert.AreEqual(map[123], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[1234], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[12345], new AttributeCollection( new Attribute("highway", "primary"))); }
public void TestSetGet() { var map = new MappedAttributesIndex(); map[123] = new AttributeCollection( new Attribute("highway", "residential")); map[1234] = new AttributeCollection( new Attribute("highway", "residential")); map[12345] = new AttributeCollection( new Attribute("highway", "primary")); Assert.AreEqual(map[123], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[1234], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[12345], new AttributeCollection( new Attribute("highway", "primary"))); }
/// <summary> /// Creates a new router database. /// </summary> public RouterDb(MemoryMap map, float maxEdgeDistance = Constants.DefaultMaxEdgeDistance) { _network = new RoutingNetwork(map, RoutingNetworkProfile.NoCache, maxEdgeDistance); _edgeProfiles = new AttributesIndex(AttributesIndexMode.IncreaseOne | AttributesIndexMode.ReverseAll); _meta = new AttributesIndex(map, AttributesIndexMode.ReverseStringIndexKeysOnly); _metaVertex = new MappedAttributesIndex(); _vertexData = new MetaCollectionDb(); _dbMeta = new AttributeCollection(); _supportedVehicles = new Dictionary <string, Vehicle>(); _supportedProfiles = new Dictionary <string, Profile>(); _contracted = new Dictionary <string, ContractedDb>(); _restrictionDbs = new Dictionary <string, RestrictionsDb>(); _shortcutsDbs = new Dictionary <string, ShortcutsDb>(); _guid = Guid.NewGuid(); }
public void TestOptimize() { var map = new MappedAttributesIndex(); map[12345] = new AttributeCollection( new Attribute("highway", "primary")); map[123] = new AttributeCollection( new Attribute("highway", "residential")); map[1234] = new AttributeCollection( new Attribute("highway", "residential")); map[12] = new AttributeCollection( new Attribute("highway", "secondary")); map[1] = new AttributeCollection( new Attribute("highway", "secondary")); Assert.AreEqual(map[1], new AttributeCollection( new Attribute("highway", "secondary"))); Assert.AreEqual(map[12], new AttributeCollection( new Attribute("highway", "secondary"))); Assert.AreEqual(map[123], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[1234], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[12345], new AttributeCollection( new Attribute("highway", "primary"))); map.Optimize(); Assert.IsTrue(map.IsOptimized); Assert.AreEqual(map[1], new AttributeCollection( new Attribute("highway", "secondary"))); Assert.AreEqual(map[12], new AttributeCollection( new Attribute("highway", "secondary"))); Assert.AreEqual(map[123], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[1234], new AttributeCollection( new Attribute("highway", "residential"))); Assert.AreEqual(map[12345], new AttributeCollection( new Attribute("highway", "primary"))); }
private RouterDb(EdgeDataLayout edgeDataLayout, Graph network, MappedAttributesIndex edgeMeta) { this.EdgeDataLayout = edgeDataLayout; _network = network; _edgesMeta = edgeMeta; }
/// <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. // version5: Added new dual edge-based contracted graph. // version6: Added vertex meta-data. var version = stream.ReadByte(); if (version != 1 && version != 2 && version != 3 && version != 4 && version != 5 && version != 6) { 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); MappedAttributesIndex metaVertex = null; MetaCollectionDb vertexData = null; if (version >= 6) { metaVertex = MappedAttributesIndex.Deserialize(new LimitedStream(stream), profile == null ? null : profile.VertexMetaProfile); vertexData = MetaCollectionDb.Deserialize(new LimitedStream(stream), profile == null ? null : profile.VertexDataProfile); } var network = RoutingNetwork.Deserialize(stream, profile == null ? null : profile.RoutingNetworkProfile); // create router db. var routerDb = new RouterDb(guid, network, profiles, meta, metaVertex, vertexData, 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); }