Example #1
0
        public static RouterDb Deserialize(Stream stream, RouterDbProfile profile)
        {
            int num1 = stream.ReadByte();

            if (num1 != 1)
            {
                throw new Exception(string.Format("Cannot deserialize routing db: Invalid version #: {0}.", (object)num1));
            }
            byte[] numArray = new byte[16];
            stream.Read(numArray, 0, 16);
            Guid guid = new Guid(numArray);

            string[]           strArray           = stream.ReadWithSizeStringArray();
            TagsCollectionBase tagsCollectionBase = stream.ReadWithSizeTagsCollection();
            int                num2             = stream.ReadByte();
            AttributesIndex    attributesIndex1 = AttributesIndex.Deserialize((Stream) new LimitedStream(stream), true);
            AttributesIndex    attributesIndex2 = AttributesIndex.Deserialize((Stream) new LimitedStream(stream), true);
            RoutingNetwork     network          = RoutingNetwork.Deserialize(stream, profile == null ? (RoutingNetworkProfile)null : profile.RoutingNetworkProfile);
            AttributesIndex    profiles         = attributesIndex1;
            AttributesIndex    meta             = attributesIndex2;
            TagsCollectionBase dbMeta           = tagsCollectionBase;

            string[] supportedProfiles = strArray;
            RouterDb routerDb          = new RouterDb(guid, network, profiles, meta, dbMeta, supportedProfiles);

            for (int index1 = 0; index1 < num2; ++index1)
            {
                string            index2            = stream.ReadWithSizeString();
                DirectedMetaGraph directedMetaGraph = DirectedMetaGraph.Deserialize(stream, profile == null ? (DirectedMetaGraphProfile)null : profile.DirectedMetaGraphProfile);
                routerDb._contracted[index2] = directedMetaGraph;
            }
            return(routerDb);
        }
Example #2
0
        /// <summary>
        /// Deserializes from stream.
        /// </summary>
        public static TransitDb Deserialize(Stream stream)
        {
            var version = stream.ReadByte();

            if (version > 2)
            {
                throw new Exception("Cannot deserialize db, version # doesn't match.");
            }

            // read agencies attributes.
            var agencyAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true);

            // read connections db.
            var connectionsDb = ConnectionsDb.Deserialize(stream);

            // read schedules db.
            var schedulesDb = SchedulesDb.Deserialize(stream);

            // read stop attributes.
            var stopAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true);

            // read stop db.
            var stopsDb = StopsDb.Deserialize(stream);

            // read transfer db's.
            var transferDbsCount = stream.ReadByte();
            var transferDbs      = new Dictionary <string, TransfersDb>();

            for (var i = 0; i < transferDbsCount; i++)
            {
                var profileName = stream.ReadWithSizeString();
                var transferDb  = TransfersDb.Deserialize(stream);

                transferDbs.Add(profileName, transferDb);
            }

            // read trip attributes.
            var tripAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true);

            // write trips db.
            var tripsDb = TripsDb.Deserialize(stream);

            ShapesDb shapesDb = null;

            if (version != 1)
            { // write shapes db.
                shapesDb = ShapesDb.Deserialize(stream);
            }

            return(new TransitDb(agencyAttributes, connectionsDb, schedulesDb, stopAttributes, stopsDb, transferDbs,
                                 tripAttributes, tripsDb, shapesDb));
        }
Example #3
0
        /// <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);
        }
Example #4
0
        /// <summary>
        /// Deserializes a shortcuts db and leaves the stream position at the end of the shortcut db data.
        /// </summary>
        public static ShortcutsDb Deserialize(Stream stream)
        {
            var version = stream.ReadByte();

            if (version != 1)
            {
                throw new Exception(string.Format("Cannot deserialize shortcuts db: Invalid version #: {0}. Try upgrading Itinero or rebuild routing file with older version.", version));
            }

            // read profile name.
            var     profileName = stream.ReadWithSizeString();
            Profile profile;

            if (!Profile.TryGet(profileName, out profile))
            {
                throw new Exception(string.Format("Cannot deserialize shortcuts db: Profile not found: {0}. Make sure to register all vehicle profile before deserializing.", profileName));
            }

            // read meta-data.
            var metaDb = stream.ReadWithSizeAttributesCollection();

            // read stops and shortcuts data sizes.
            var bytes = new byte[4];

            stream.Read(bytes, 0, 4);
            var stopsPointer = BitConverter.ToUInt32(bytes, 0);

            stream.Read(bytes, 0, 4);
            var shortcutsPointer = BitConverter.ToUInt32(bytes, 0);

            // read stops meta and data.
            var stopsMeta = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var stops     = new MemoryArray <uint>(stopsPointer);

            stops.CopyFrom(stream);

            // read shortcuts meta and data.
            var shortcutsMeta = AttributesIndex.Deserialize(new LimitedStream(stream), true);
            var shortcuts     = new MemoryArray <uint>(shortcutsPointer);

            shortcuts.CopyFrom(stream);

            return(new ShortcutsDb(profile, metaDb, stopsMeta, stops, shortcutsMeta, shortcuts));
        }
Example #5
0
        public void TestWritableAfterDeserialization()
        {
            var refIndex = new AttributesIndex();

            var id1 = refIndex.Add(new Attribute("key1", "value1"));
            var id2 = refIndex.Add(new Attribute("key2", "value1"));
            var id3 = refIndex.Add(new Attribute("key2", "value2"));

            using (var memoryStream = new MemoryStream())
            {
                var size = refIndex.Serialize(memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
                var index = AttributesIndex.Deserialize(memoryStream, false);

                var id4 = index.Add(new Attribute("key3", "value4"));

                var attributes = index.Get(id1);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key1", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);

                attributes = index.Get(id2);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);

                attributes = index.Get(id3);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value2", attributes.First().Value);

                attributes = index.Get(id4);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key3", attributes.First().Key);
                Assert.AreEqual("value4", attributes.First().Value);
            }
        }
Example #6
0
        public void TestDeserialize()
        {
            var refIndex = new AttributesIndex();

            var id1 = refIndex.Add(new Attribute("key1", "value1"));
            var id2 = refIndex.Add(new Attribute("key2", "value1"));
            var id3 = refIndex.Add(new Attribute("key2", "value2"));

            using (var memoryStream = new MemoryStream())
            {
                var size = refIndex.Serialize(memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
                var index = AttributesIndex.Deserialize(memoryStream, false);
                Assert.AreEqual(size, memoryStream.Position);

                var attributes = index.Get(id1);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key1", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id2);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id3);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value2", attributes.First().Value);

                memoryStream.Seek(0, SeekOrigin.Begin);
                index = AttributesIndex.Deserialize(memoryStream, true);
                Assert.AreEqual(size, memoryStream.Position);

                attributes = index.Get(id1);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key1", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id2);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id3);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value2", attributes.First().Value);
            }

            var random = new System.Random(116542346);
            var keys   = 100;
            var values = 100000;

            refIndex = new AttributesIndex();
            var refIndexRef = new Dictionary <uint, IAttributeCollection>();

            for (var i = 0; i < 1000; i++)
            {
                var attributes = new AttributeCollection();
                for (var j = 0; j < random.Next(8); j++)
                {
                    attributes.AddOrReplace(new Attribute(
                                                string.Format("k{0}", random.Next(keys)),
                                                string.Format("v{0}", random.Next(values))));
                }

                var id = refIndex.Add(attributes);
                refIndexRef[id] = attributes;
            }

            using (var memoryStream = new MemoryStream())
            {
                var size = refIndex.Serialize(memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
                var index = AttributesIndex.Deserialize(memoryStream, false);
                Assert.AreEqual(size, memoryStream.Position);

                foreach (var refAttribute in refIndexRef)
                {
                    var attributes = index.Get(refAttribute.Key);
                    Assert.AreEqual(refAttribute.Value.Count, attributes.Count);

                    foreach (var attribute in refAttribute.Value)
                    {
                        Assert.IsTrue(attributes.Contains(attribute.Key, attribute.Value));
                    }

                    foreach (var attribute in attributes)
                    {
                        Assert.IsTrue(refAttribute.Value.Contains(attribute.Key, attribute.Value));
                    }
                }

                memoryStream.Seek(0, SeekOrigin.Begin);
                index = AttributesIndex.Deserialize(memoryStream, true);
                Assert.AreEqual(size, memoryStream.Position);

                foreach (var refAttribute in refIndexRef)
                {
                    var attributes = index.Get(refAttribute.Key);
                    Assert.AreEqual(refAttribute.Value.Count, attributes.Count);

                    foreach (var attribute in refAttribute.Value)
                    {
                        Assert.IsTrue(attributes.Contains(attribute.Key, attribute.Value));
                    }

                    foreach (var attribute in attributes)
                    {
                        Assert.IsTrue(refAttribute.Value.Contains(attribute.Key, attribute.Value));
                    }
                }
            }

            refIndex = new AttributesIndex(AttributesIndexMode.IncreaseOne);

            id1 = refIndex.Add(new Attribute("key1", "value1"));
            id2 = refIndex.Add(new Attribute("key2", "value1"));
            id3 = refIndex.Add(new Attribute("key2", "value2"));

            using (var memoryStream = new MemoryStream())
            {
                var size = refIndex.Serialize(memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
                var index = AttributesIndex.Deserialize(memoryStream, false);
                Assert.AreEqual(size, memoryStream.Position);

                var attributes = index.Get(id1);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key1", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id2);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id3);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value2", attributes.First().Value);

                memoryStream.Seek(0, SeekOrigin.Begin);
                index = AttributesIndex.Deserialize(memoryStream, true);
                Assert.AreEqual(size, memoryStream.Position);

                attributes = index.Get(id1);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key1", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id2);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value1", attributes.First().Value);
                attributes = index.Get(id3);
                Assert.IsNotNull(attributes);
                Assert.AreEqual(1, attributes.Count);
                Assert.AreEqual("key2", attributes.First().Key);
                Assert.AreEqual("value2", attributes.First().Value);
            }

            random      = new System.Random(116542346);
            keys        = 100;
            values      = 100000;
            refIndex    = new AttributesIndex(AttributesIndexMode.IncreaseOne);
            refIndexRef = new Dictionary <uint, IAttributeCollection>();
            for (var i = 0; i < 1000; i++)
            {
                var attributes = new AttributeCollection();
                for (var j = 0; j < random.Next(8); j++)
                {
                    attributes.AddOrReplace(new Attribute(
                                                string.Format("k{0}", random.Next(keys)),
                                                string.Format("v{0}", random.Next(values))));
                }

                var id = refIndex.Add(attributes);
                refIndexRef[id] = attributes;
            }

            using (var memoryStream = new MemoryStream())
            {
                var size = refIndex.Serialize(memoryStream);

                memoryStream.Seek(0, SeekOrigin.Begin);
                var index = AttributesIndex.Deserialize(memoryStream, false);
                Assert.AreEqual(size, memoryStream.Position);

                foreach (var refAttribute in refIndexRef)
                {
                    var attributes = index.Get(refAttribute.Key);
                    Assert.AreEqual(refAttribute.Value.Count, attributes.Count);

                    foreach (var attribute in refAttribute.Value)
                    {
                        Assert.IsTrue(attributes.Contains(attribute.Key, attribute.Value));
                    }

                    foreach (var attribute in attributes)
                    {
                        Assert.IsTrue(refAttribute.Value.Contains(attribute.Key, attribute.Value));
                    }
                }

                memoryStream.Seek(0, SeekOrigin.Begin);
                index = AttributesIndex.Deserialize(memoryStream, true);
                Assert.AreEqual(size, memoryStream.Position);

                foreach (var refAttribute in refIndexRef)
                {
                    var attributes = index.Get(refAttribute.Key);
                    Assert.AreEqual(refAttribute.Value.Count, attributes.Count);

                    foreach (var attribute in refAttribute.Value)
                    {
                        Assert.IsTrue(attributes.Contains(attribute.Key, attribute.Value));
                    }

                    foreach (var attribute in attributes)
                    {
                        Assert.IsTrue(refAttribute.Value.Contains(attribute.Key, attribute.Value));
                    }
                }
            }
        }
Example #7
0
        /// <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);
        }