/// <summary> /// Deserializes from the given stream, returns an optimized index. /// </summary> public static MappedAttributesIndex Deserialize(Stream stream, MappedAttributesIndexProfile profile) { var version = stream.ReadByte(); if (version > 1) { throw new Exception(string.Format("Cannot deserialize mapped attributes index: Invalid version #: {0}, upgrade Itinero.", version)); } var bytes = new byte[4]; stream.Read(bytes, 0, 4); var length = BitConverter.ToUInt32(bytes, 0); ArrayBase <uint> data; if (profile == null || profile.DataProfile == null) { data = Context.ArrayFactory.CreateMemoryBackedArray <uint>(length); data.CopyFrom(stream); } else { var position = stream.Position; var map = new MemoryMapStream(new CappedStream(stream, position, length * 4)); data = new Array <uint>(map.CreateUInt32(length), profile.DataProfile); stream.Seek(length * 4, SeekOrigin.Current); } var attributes = AttributesIndex.Deserialize(new LimitedStream(stream, stream.Position), true); return(new MappedAttributesIndex(data, attributes)); }
/// <summary> /// Deserializes a graph from the given stream. /// </summary> /// <returns></returns> public static Graph Deserialize(System.IO.Stream stream, GraphProfile profile) { var initialPosition = stream.Position; // read sizes. long size = 1; var version = stream.ReadByte(); if (version != 1) { throw new Exception(string.Format("Cannot deserialize graph: Invalid version #: {0}.", version)); } var bytes = new byte[8]; stream.Read(bytes, 0, 8); size = size + 8; var vertexLength = BitConverter.ToInt64(bytes, 0); stream.Read(bytes, 0, 8); size = size + 8; var edgeLength = BitConverter.ToInt64(bytes, 0); stream.Read(bytes, 0, 4); size = size + 4; var vertexSize = BitConverter.ToInt32(bytes, 0); stream.Read(bytes, 0, 4); size = size + 4; var edgeSize = BitConverter.ToInt32(bytes, 0); ArrayBase <uint> vertices; ArrayBase <uint> edges; if (profile == null) { // just create arrays and read the data. vertices = new MemoryArray <uint>(vertexLength * vertexSize); vertices.CopyFrom(stream); size += vertexLength * vertexSize * 4; edges = new MemoryArray <uint>(edgeLength * edgeSize); edges.CopyFrom(stream); size += edgeLength * edgeSize * 4; } else { // create accessors over the exact part of the stream that represents vertices/edges. var position = stream.Position; var map1 = new MemoryMapStream(new CappedStream(stream, position, vertexLength * vertexSize * 4)); vertices = new Array <uint>(map1.CreateUInt32(vertexLength * vertexSize), profile.VertexProfile); size += vertexLength * vertexSize * 4; var map2 = new MemoryMapStream(new CappedStream(stream, position + vertexLength * vertexSize * 4, edgeLength * edgeSize * 4)); edges = new Array <uint>(map2.CreateUInt32(edgeLength * edgeSize), profile.EdgeProfile); size += edgeLength * edgeSize * 4; } // make sure stream is positioned at the correct location. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new Graph(edgeSize - MINIMUM_EDGE_SIZE, vertices, edges)); }
/// <summary> /// Deserializes from a stream. /// </summary> public static RoutingNetwork Deserialize(Stream stream, RoutingNetworkProfile profile) { var version = stream.ReadByte(); if (version > 2) { throw new Exception(string.Format("Cannot deserialize routing network: Invalid version #: {0}, upgrade Itinero.", version)); } var position = stream.Position; var initialPosition = stream.Position; // read maxEdgeDistance if version # = 2. var maxEdgeDistance = Constants.DefaultMaxEdgeDistance; if (version == 2) { var bytes = new byte[4]; stream.Read(bytes, 0, 4); maxEdgeDistance = BitConverter.ToSingle(bytes, 0); } // deserialize graph. var graph = GeometricGraph.Deserialize(stream, profile == null ? null : profile.GeometricGraphProfile); var size = stream.Position - position; var edgeLength = graph.EdgeCount; var edgeSize = 1; ArrayBase <uint> edgeData; if (profile == null) { // just create arrays and read the data. edgeData = Context.ArrayFactory.CreateMemoryBackedArray <uint>(edgeLength * edgeSize); edgeData.CopyFrom(stream); size += edgeLength * edgeSize * 4; } else { // create accessors over the exact part of the stream that represents vertices/edges. position = stream.Position; var map = new MemoryMapStream(new CappedStream(stream, position, edgeLength * edgeSize * 4)); edgeData = new Array <uint>(map.CreateUInt32(edgeLength * edgeSize), profile.EdgeDataProfile); size += edgeLength * edgeSize * 4; } // make stream is positioned correctly. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new RoutingNetwork(graph, edgeData, maxEdgeDistance)); }
/// <summary> /// Deserializes from a stream. /// </summary> /// <returns></returns> public static DirectedMetaGraph Deserialize(System.IO.Stream stream, DirectedMetaGraphProfile profile) { var version = stream.ReadByte(); if (version != 1) { throw new Exception(string.Format("Cannot deserialize directed meta graph: Invalid version #: {0}.", version)); } var graph = DirectedGraph.Deserialize(stream, profile == null ? null : profile.DirectedGraphProfile); var initialPosition = stream.Position; long size = 0; var bytes = new byte[4]; stream.Read(bytes, 0, 4); size = size + 4; var vertexSize = BitConverter.ToInt32(bytes, 0); stream.Read(bytes, 0, 4); size = size + 4; var edgeSize = BitConverter.ToInt32(bytes, 0); var edgeLength = graph.EdgeCount; ArrayBase <uint> edges; if (profile == null) { // just create arrays and read the data. edges = Context.ArrayFactory.CreateMemoryBackedArray <uint>(edgeLength * edgeSize); edges.CopyFrom(stream); size += edgeLength * edgeSize * 4; } else { // create accessors over the exact part of the stream that represents vertices/edges. var position = stream.Position; var map1 = new MemoryMapStream(new CappedStream(stream, position, edgeLength * edgeSize * 4)); edges = new Array <uint>(map1.CreateUInt32(edgeLength * edgeSize), profile.EdgeMetaProfile); size += edgeLength * edgeSize * 4; } // make sure stream is positioned at the correct location. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new DirectedMetaGraph(graph, edgeSize, edges)); }
/// <summary> /// Deserializes a restrictions db starting at the current position in the stream. /// </summary> public static RestrictionsDb Deserialize(Stream stream, RestrictionsDbProfile profile) { var version = stream.ReadByte(); if (version > 1) { throw new Exception(string.Format("Cannot deserialize restrictions db: Invalid version #: {0}, upgrade Itinero.", version)); } // read hascomplexrestrictions. var hasComplexRestrictionsByte = stream.ReadByte(); var hasComplexRestrictions = hasComplexRestrictionsByte == 1; // read sizes. var bytes = new byte[16]; stream.Read(bytes, 0, 16); var count = BitConverter.ToUInt32(bytes, 0); var hashSize = BitConverter.ToUInt32(bytes, 4); var indexSize = BitConverter.ToUInt32(bytes, 8); var restrictionSize = BitConverter.ToUInt32(bytes, 12); ArrayBase <uint> hashes; if (profile == null || profile.HashesProfile == null) { hashes = Context.ArrayFactory.CreateMemoryBackedArray <uint>(hashSize); hashes.CopyFrom(stream); } else { var position = stream.Position; var map = new MemoryMapStream(new CappedStream(stream, position, hashSize * 4)); hashes = new Array <uint>(map.CreateUInt32(hashSize), profile.HashesProfile); stream.Seek(hashSize * 4, SeekOrigin.Current); } ArrayBase <uint> index; if (profile == null || profile.IndexProfile == null) { index = Context.ArrayFactory.CreateMemoryBackedArray <uint>(indexSize); index.CopyFrom(stream); } else { var position = stream.Position; var map = new MemoryMapStream(new CappedStream(stream, position, indexSize * 4)); index = new Array <uint>(map.CreateUInt32(indexSize), profile.IndexProfile); stream.Seek(indexSize * 4, SeekOrigin.Current); } ArrayBase <uint> restrictions; if (profile == null || profile.RestrictionsProfile == null) { restrictions = Context.ArrayFactory.CreateMemoryBackedArray <uint>(restrictionSize); restrictions.CopyFrom(stream); } else { var position = stream.Position; var map = new MemoryMapStream(new CappedStream(stream, position, restrictionSize * 4)); restrictions = new Array <uint>(map.CreateUInt32(restrictionSize), profile.RestrictionsProfile); stream.Seek(restrictionSize * 4, SeekOrigin.Current); } return(new RestrictionsDb(count, hasComplexRestrictions, hashes, index, restrictions)); }