/// <summary> /// Deserializes from the given stream. /// </summary> public static ShapesDb Deserialize(Stream stream) { var version = stream.ReadByte(); if (version != 1) { throw new Exception("Cannot deserialize stops db, version # doesn't match."); } // read size. var sizeBytes = new byte[8]; stream.Read(sizeBytes, 0, 8); var size = BitConverter.ToInt64(sizeBytes, 0); // read index. var index = new MemoryArray <uint>(size); index.CopyFrom(stream); // read shapes. var shapes = ShapesArray.CreateFrom(stream, true); return(new ShapesDb(size, index, shapes)); }
/// <summary> /// Deserializes a graph from the stream. /// </summary> public static GeometricGraph Deserialize(System.IO.Stream stream, GeometricGraphProfile profile) { var version = stream.ReadByte(); if (version != 1) { throw new Exception(string.Format("Cannot deserialize geometric graph: Invalid version #: {0}.", version)); } var graph = Graph.Deserialize(stream, profile == null ? null : profile.GraphProfile); var initialPosition = stream.Position; var size = 0L; ArrayBase <float> coordinates; ShapesArray shapes; if (profile == null) { // don't use the stream, the read from it. coordinates = Context.ArrayFactory.CreateMemoryBackedArray <float>(graph.VertexCount * 2); coordinates.CopyFrom(stream); size += graph.VertexCount * 2 * 4; long shapeSize; shapes = ShapesArray.CreateFrom(stream, true, out shapeSize); size += shapeSize; } else { // use the stream as a map. var position = stream.Position; var map = new MemoryMapStream(new CappedStream(stream, position, graph.VertexCount * 4 * 2)); coordinates = new Array <float>(map.CreateSingle(graph.VertexCount * 2), profile.CoordinatesProfile); size += graph.VertexCount * 2 * 4; stream.Seek(position + graph.VertexCount * 4 * 2, System.IO.SeekOrigin.Begin); long shapeSize; shapes = ShapesArray.CreateFrom(stream, false, out shapeSize); size += shapeSize; } // make stream is positioned correctly. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new GeometricGraph(graph, coordinates, shapes)); }
internal static Graph ReadFrom(Stream stream) { // read & verify header. var header = stream.ReadWithSizeString(); var version = stream.ReadByte(); if (header != nameof(Graph)) { throw new InvalidDataException($"Cannot read {nameof(Graph)}: Header invalid."); } if (version != 1) { throw new InvalidDataException($"Cannot read {nameof(Graph)}: Version # invalid."); } // read zoom and edge data size. var zoom = stream.ReadByte(); var edgeDataSize = stream.ReadByte(); // read tile index. var tileSizeIndex = stream.ReadByte(); var tiles = SparseMemoryArray <byte> .CopyFromWithHeader(stream); // read vertices. var coordinateSizeInBytes = stream.ReadByte(); var vertexPointer = stream.ReadInt64(); var vertices = MemoryArray <byte> .CopyFromWithSize(stream); var edgePointers = MemoryArray <uint> .CopyFromWithSize(stream); // read edges. var edgePointer = stream.ReadInt64(); var edges = MemoryArray <byte> .CopyFromWithSize(stream); // read shapes. var shapes = ShapesArray.CreateFrom(stream, true, false); return(new Graph(zoom, edgeDataSize, tileSizeIndex, tiles, coordinateSizeInBytes, vertices, (uint)vertexPointer, edgePointers, (uint)edgePointer, edges, shapes)); }
public static GeometricGraph Deserialize(Stream stream, GeometricGraphProfile profile) { int num1 = stream.ReadByte(); if (num1 != 1) { throw new Exception(string.Format("Cannot deserialize geometric graph: Invalid version #: {0}.", (object)num1)); } Graph graph = Graph.Deserialize(stream, profile == null ? (GraphProfile)null : profile.GraphProfile); long position1 = stream.Position; long num2 = 0; ArrayBase <float> coordinates; ShapesArray from; long num3; if (profile == null) { coordinates = (ArrayBase <float>) new MemoryArray <float>((long)(graph.VertexCount * 2U)); coordinates.CopyFrom(stream); long num4 = num2 + (long)(uint)((int)graph.VertexCount * 2 * 4); long size; from = ShapesArray.CreateFrom(stream, true, out size); num3 = num4 + size; } else { long position2 = stream.Position; coordinates = (ArrayBase <float>) new Array <float>(((MemoryMap) new MemoryMapStream((Stream) new CappedStream(stream, position2, (long)(uint)((int)graph.VertexCount * 4 * 2)))).CreateSingle((long)(graph.VertexCount * 2U)), profile.CoordinatesProfile); long num4 = num2 + (long)(uint)((int)graph.VertexCount * 2 * 4); stream.Seek(position2 + (long)(uint)((int)graph.VertexCount * 4 * 2), SeekOrigin.Begin); long size; from = ShapesArray.CreateFrom(stream, false, out size); num3 = num4 + size; } stream.Seek(position1 + num3, SeekOrigin.Begin); return(new GeometricGraph(graph, coordinates, from)); }
public void TestCopyToCreateFrom() { var box = new Box( new Coordinate(-90, -180), new Coordinate(90, 180)); var refArray = new ShapesArray(1024); var rand = new System.Random(46541577); var totalCoordinateCount = 0; for (var i = 0; i < 1024; i++) { var count = rand.Next(10); totalCoordinateCount += count; var newShape = new List <Coordinate>(count); for (var j = 0; j < count; j++) { newShape.Add(box.GenerateRandomIn()); } var shape = new ShapeEnumerable(newShape); refArray[i] = shape; } using (var stream = new MemoryStream()) { Assert.AreEqual(16 + (1024 * 8) + (totalCoordinateCount * 8), refArray.CopyTo(stream)); stream.Seek(0, SeekOrigin.Begin); var array = ShapesArray.CreateFrom(stream, true); for (var i = 0; i < refArray.Length; i++) { var refShape = refArray[i]; if (refShape.Count == 0) { continue; } var shape = array[i]; Assert.IsNotNull(shape); for (var j = 0; j < shape.Count; j++) { Assert.AreEqual(refShape[j].Latitude, shape[j].Latitude); Assert.AreEqual(refShape[j].Longitude, shape[j].Longitude); } } stream.Seek(0, SeekOrigin.Begin); array = ShapesArray.CreateFrom(stream, false); for (var i = 0; i < refArray.Length; i++) { var refShape = refArray[i]; if (refShape.Count == 0) { continue; } var shape = array[i]; Assert.IsNotNull(shape); for (var j = 0; j < shape.Count; j++) { Assert.AreEqual(refShape[j].Latitude, shape[j].Latitude); Assert.AreEqual(refShape[j].Longitude, shape[j].Longitude); } } } }