/// <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)); }
public static RoutingNetwork Deserialize(Stream stream, RoutingNetworkProfile profile) { int num1 = stream.ReadByte(); if (num1 > 2) { throw new Exception(string.Format("Cannot deserialize routing network: Invalid version #: {0}, upgrade OsmSharp.Routing.", (object)num1)); } long position1 = stream.Position; long position2 = stream.Position; float num2 = 5000f; if (num1 == 2) { byte[] buffer = new byte[4]; stream.Read(buffer, 0, 4); num2 = BitConverter.ToSingle(buffer, 0); } GeometricGraph graph = GeometricGraph.Deserialize(stream, profile == null ? (GeometricGraphProfile)null : profile.GeometricGraphProfile); long num3 = stream.Position - position1; long edgeCount = graph.EdgeCount; int num4 = 1; ArrayBase <uint> arrayBase; long num5; if (profile == null) { arrayBase = (ArrayBase <uint>) new MemoryArray <uint>(edgeCount * (long)num4); arrayBase.CopyFrom(stream); num5 = num3 + edgeCount * (long)num4 * 4L; } else { long position3 = stream.Position; arrayBase = (ArrayBase <uint>) new Array <uint>(((MemoryMap) new MemoryMapStream((Stream) new CappedStream(stream, position3, edgeCount * (long)num4 * 4L))).CreateUInt32(edgeCount * (long)num4), profile.EdgeDataProfile); num5 = num3 + edgeCount * (long)num4 * 4L; } stream.Seek(position2 + num5, SeekOrigin.Begin); ArrayBase <uint> edgeData = arrayBase; double num6 = (double)num2; return(new RoutingNetwork(graph, edgeData, (float)num6)); }
public void TestDeserialize() { var graph = new GeometricGraph(1, 100); // add one edge. graph.AddVertex(0, 0, 0); graph.AddVertex(1, 0, 0); graph.AddEdge(0, 1, new uint[] { 1 }, null); // serialize. using (var stream = new System.IO.MemoryStream()) { var size = graph.Serialize(stream); stream.Seek(0, System.IO.SeekOrigin.Begin); var deserializedGraph = GeometricGraph.Deserialize(stream, GeometricGraphProfile.Default); Assert.AreEqual(size, stream.Position); Assert.AreEqual(2, deserializedGraph.VertexCount); Assert.AreEqual(1, deserializedGraph.EdgeCount); // verify all edges. var edges = deserializedGraph.GetEdgeEnumerator(0); Assert.AreEqual(1, edges.Count()); Assert.AreEqual(1, edges.First().Data[0]); Assert.AreEqual(1, edges.First().To); edges = deserializedGraph.GetEdgeEnumerator(1); Assert.AreEqual(1, edges.Count()); Assert.AreEqual(true, edges.First().DataInverted); Assert.AreEqual(1, edges.First().Data[0]); Assert.AreEqual(0, edges.First().To); } graph = new GeometricGraph(1, 100); // add one edge. graph.AddVertex(0, 0, 0); graph.AddVertex(1, 0, 0); graph.AddVertex(2, 0, 0); graph.AddVertex(3, 0, 0); graph.AddVertex(4, 0, 0); graph.AddVertex(5, 0, 0); graph.AddEdge(0, 1, new uint[] { 1 }, null); graph.AddEdge(0, 2, new uint[] { 2 }, null); graph.AddEdge(0, 3, new uint[] { 3 }, null); graph.AddEdge(0, 4, new uint[] { 4 }, null); graph.AddEdge(5, 1, new uint[] { 5 }, null); graph.AddEdge(5, 2, new uint[] { 6 }, null); graph.AddEdge(5, 3, new uint[] { 7 }, null); graph.AddEdge(5, 4, new uint[] { 8 }, null); // serialize. using (var stream = new System.IO.MemoryStream()) { var size = graph.Serialize(stream); stream.Seek(0, System.IO.SeekOrigin.Begin); var deserializedGraph = GeometricGraph.Deserialize(stream, GeometricGraphProfile.Default); Assert.AreEqual(size, stream.Position); Assert.AreEqual(6, deserializedGraph.VertexCount); Assert.AreEqual(8, deserializedGraph.EdgeCount); } }