/// <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> /// Creates a new routing network. /// </summary> public RoutingNetwork(MemoryMap map, RoutingNetworkProfile profile, float maxEdgeDistance = Constants.DefaultMaxEdgeDistance) { _maxEdgeDistance = maxEdgeDistance; if (profile == null) { _graph = new GeometricGraph(map, 1); _edgeData = new Array <uint>(map, _edgeDataSize * _graph.EdgeCount); } else { _graph = new GeometricGraph(map, profile.GeometricGraphProfile, 1); _edgeData = new Array <uint>(map, _edgeDataSize * _graph.EdgeCount, profile.EdgeDataProfile); } }