internal long WriteTo(Stream stream) { var p = stream.Position; // write header and version. stream.WriteWithSize($"{nameof(Graph)}"); stream.WriteByte(1); // writes zoom and edge data size. stream.WriteByte((byte)_zoom); stream.WriteByte((byte)_edgeDataSize); // write tile index. stream.WriteByte((byte)TileSizeInIndex); _tiles.CopyToWithHeader(stream); // write vertices. stream.WriteByte((byte)CoordinateSizeInBytes); stream.Write(BitConverter.GetBytes((long)_vertexPointer), 0, 8); _vertices.CopyToWithSize(stream); _edgePointers.CopyToWithSize(stream); // write edges. stream.Write(BitConverter.GetBytes((long)_edgePointer), 0, 8); _edges.CopyToWithSize(stream); // write shapes. _shapes.CopyTo(stream); return(stream.Position - p); }
public void TestCopyTo() { using (var map = new MemoryMapStream()) { var array = new ShapesArray(map, 1); array.Set(0, new Coordinate(0, 0.1f), new Coordinate(1, 1.1f)); using (var stream = new MemoryStream()) { Assert.AreEqual(16 + 8 + (4 * 4), array.CopyTo(stream)); } } }
/// <summary> /// Serializes this graph to disk. /// </summary> public long Serialize(System.IO.Stream stream) { // compress first. this.Compress(); // serialize the base graph & make sure to seek to right after. long size = 1; stream.WriteByte(1); size += _graph.Serialize(stream); // serialize the coordinates. size += _coordinates.CopyTo(stream); // and serialize the shapes. size += _shapes.CopyTo(stream); return(size); }
/// <summary> /// Serializes this graph to disk. /// </summary> public long Serialize(System.IO.Stream stream) { // VERSION1: default. // VERSION2: including elevation. // compress first. this.Compress(); // serialize the base graph & make sure to seek to right after. long size = 1; if (_elevation == null) { // keep things compatible with the previous format. stream.WriteByte(1); } else { // only break compatibility when there is elevation. stream.WriteByte(2); size++; stream.WriteByte(1); // extra flag for future version upgrades, indicating if elevation is there. } // write graph. size += _graph.Serialize(stream); // serialize the coordinates. size += _coordinates.CopyTo(stream); if (_elevation != null) { // write elevation. size += _elevation.CopyTo(stream); } // and serialize the shapes. size += _shapes.CopyTo(stream); return(size); }
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); } } } }