/// <summary> /// Does the v1 deserialization. /// </summary> /// <param name="stream"></param> /// <param name="lazy"></param> /// <param name="vehicles"></param> /// <returns></returns> protected override IBasicRouterDataSource <TEdgeData> DoDeserialize( LimitedStream stream, bool lazy, IEnumerable <string> vehicles) { ITagsCollectionIndex tagsCollectionIndex = this.CreateTagsCollectionIndex(); DynamicGraphRouterDataSource <TEdgeData> graph = this.CreateGraph(tagsCollectionIndex); // deserialize vertices. var sizeBytes = new byte[8]; stream.Read(sizeBytes, 0, 8); var position = stream.Position; var size = BitConverter.ToInt64(sizeBytes, 0); this.DeserializeVertices(stream, size, graph); stream.Seek(position + size, System.IO.SeekOrigin.Begin); // deserialize edges. stream.Read(sizeBytes, 0, 8); position = stream.Position; size = BitConverter.ToInt64(sizeBytes, 0); this.DeserializeEdges(stream, size, graph); stream.Seek(position + size, System.IO.SeekOrigin.Begin); // deserialize tags. stream.Read(sizeBytes, 0, 8); position = stream.Position; size = BitConverter.ToInt64(sizeBytes, 0); this.DeserializeTags(stream, size, tagsCollectionIndex); stream.Seek(position + size, System.IO.SeekOrigin.Begin); return(graph); }
/// <summary> /// Does the v2 deserialization. /// </summary> /// <param name="stream"></param> /// <param name="lazy"></param> /// <returns></returns> protected override IBasicRouterDataSource <CHEdgeData> DoDeserialize( LimitedStream stream, bool lazy) { var intBytes = new byte[4]; stream.Read(intBytes, 0, 4); int startOfBlocks = BitConverter.ToInt32(intBytes, 0); // deserialize regions index. stream.Read(intBytes, 0, 4); int sizeRegionIndex = BitConverter.ToInt32(intBytes, 0); var chVertexRegionIndex = (CHVertexRegionIndex)_runtimeTypeModel.Deserialize( new CappedStream(stream, stream.Position, sizeRegionIndex), null, typeof(CHVertexRegionIndex)); // deserialize blocks index. stream.Seek(startOfBlocks, SeekOrigin.Begin); stream.Read(intBytes, 0, 4); int sizeBlockIndex = BitConverter.ToInt32(intBytes, 0); var chBlockIndex = (CHBlockIndex)_runtimeTypeModel.Deserialize( new CappedStream(stream, stream.Position, sizeBlockIndex), null, typeof(CHBlockIndex)); return(new CHEdgeDataDataSource(stream, this, sizeRegionIndex + 8, chVertexRegionIndex, RegionZoom, startOfBlocks + sizeBlockIndex + 4, chBlockIndex, BlockVertexSize)); }
/// <summary> /// Does the v2 deserialization. /// </summary> /// <param name="stream"></param> /// <param name="lazy"></param> /// <param name="vehicles"></param> /// <returns></returns> protected override IBasicRouterDataSource <CHEdgeData> DoDeserialize( LimitedStream stream, bool lazy, IEnumerable <string> vehicles) { var intBytes = new byte[4]; stream.Read(intBytes, 0, 4); int startOfBlocks = BitConverter.ToInt32(intBytes, 0); stream.Read(intBytes, 0, 4); int startOfTags = BitConverter.ToInt32(intBytes, 0); stream.Read(intBytes, 0, 4); int sizeRegionIndex = BitConverter.ToInt32(intBytes, 0); // deserialize regions index. var chVertexRegionIndex = (CHVertexRegionIndex)_runtimeTypeModel.Deserialize( new CappedStream(stream, stream.Position, sizeRegionIndex), null, typeof(CHVertexRegionIndex)); // deserialize blocks index. stream.Seek(startOfBlocks, SeekOrigin.Begin); stream.Read(intBytes, 0, 4); int sizeBlockIndex = BitConverter.ToInt32(intBytes, 0); var chBlockIndex = (CHBlockIndex)_runtimeTypeModel.Deserialize( new CappedStream(stream, stream.Position, sizeBlockIndex), null, typeof(CHBlockIndex)); // deserialize tags. stream.Seek(startOfTags, SeekOrigin.Begin); ITagsCollectionIndexReadonly tagsIndex = TagIndexSerializer.DeserializeBlocks(stream); return(new v2.CHEdgeDataDataSource(stream, this, vehicles, sizeRegionIndex + 12, chVertexRegionIndex, _regionZoom, startOfBlocks + sizeBlockIndex + 4, chBlockIndex, (uint)_blockVertexSize, tagsIndex)); }
/// <summary> /// Does the v1 deserialization. /// </summary> /// <param name="stream"></param> /// <param name="lazy"></param> /// <param name="vehicles"></param> /// <returns></returns> protected override IBasicRouterDataSource <CHEdgeData> DoDeserialize( LimitedStream stream, bool lazy, IEnumerable <string> vehicles) { // serialize all tile meta data. stream.Seek(0, SeekOrigin.Begin); var compressionFlag = new byte[1]; stream.Read(compressionFlag, 0, 1); bool decompress = (compressionFlag[0] == (byte)1); var tileCountBytes = new byte[4]; stream.Read(tileCountBytes, 0, tileCountBytes.Length); // var tileCount = BitConverter.ToInt32(tileCountBytes, 0); var tileMetaEndBytes = new byte[8]; stream.Read(tileMetaEndBytes, 0, tileMetaEndBytes.Length); var tileMetaEnd = BitConverter.ToInt64(tileMetaEndBytes, 0); // deserialize meta data. var metas = (SerializableGraphTileMetas)_runtimeTypeModel.Deserialize( new CappedStream(stream, stream.Position, tileMetaEnd - stream.Position), null, typeof(SerializableGraphTileMetas)); // create the datasource. var routerDataSource = new CHEdgeDataDataSource(stream, decompress, vehicles, metas, Zoom, this, 1000); if (!lazy) { // pre-load everything. for (int tileIdx = 0; tileIdx < metas.TileX.Length; tileIdx++) { routerDataSource.LoadMissingTile( new Tile(metas.TileX[tileIdx], metas.TileY[tileIdx], Zoom)); } } // return router datasource. return(routerDataSource); }
/// <summary> /// Deserializes the meta-data. /// </summary> /// <param name="stream"></param> /// <param name="tagsCollectionIndex"></param> /// <param name="size"></param> protected virtual void DeserializeTags(LimitedStream stream, long size, ITagsCollectionIndex tagsCollectionIndex) { RuntimeTypeModel typeModel = RuntimeTypeModel.Create(); typeModel.Add(typeof(SerializableTag), true); // read tags collection-count. var countBytes = new byte[4]; stream.Read(countBytes, 0, 4); int max = BitConverter.ToInt32(countBytes, 0); // serialize tags collections one-by-one. var currentTags = new List <SerializableTag>(); if (max > 0) { // keep reading until max. currentTags.AddRange(typeModel.DeserializeWithSize(stream, null, typeof(SerializableTag[])) as SerializableTag[]); // detect an empty collection at zero. if (currentTags[0].CollectionId == 1) { // no zero, there is an empty collection. tagsCollectionIndex.Add(new TagsCollection()); } while (tagsCollectionIndex.Max < max) { // try and detect an id change. bool idChanged = true; while (currentTags.Count > 0 && idChanged) { // there are tags. idChanged = false; var tagsCollection = new TagsCollection(); int index = 0; uint currentId = currentTags[index].CollectionId; // detect an empty collection in between. if (currentId > tagsCollectionIndex.Max + 1) { // it's possible there was an empty tag collection in there somewhere. tagsCollectionIndex.Add(new TagsCollection()); } tagsCollection.Add(new Tag(currentTags[index].Key, currentTags[index].Value)); index++; while (currentTags.Count > index) { // test for a different id. if (currentTags[index].CollectionId != currentId || currentTags[index].CollectionId == uint.MaxValue) { // yes! an id change was detected. idChanged = true; currentTags.RemoveRange(0, index); if (tagsCollection.Count > 0) { // there are tags. tagsCollectionIndex.AddObject(tagsCollection); } break; } tagsCollection.Add(new Tag(currentTags[index].Key, currentTags[index].Value)); index++; } } // read next block. currentTags.AddRange(typeModel.DeserializeWithSize(stream, null, typeof(SerializableTag[])) as SerializableTag[]); } } }