/// <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); }
/// <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); }