Пример #1
0
        /// <summary>
        /// Deserializes a huge collection index from the given stream.
        /// </summary>
        /// <param name="stream">The source stream.</param>
        /// <param name="copy">The copy flag. When true all data is copied into memory, otherwise the source-stream is used as a memorymapped file.</param>
        /// <returns></returns>
        public static HugeCoordinateCollectionIndex Deserialize(Stream stream, bool copy = false)
        {
            // read sizes.
            long position  = 0;
            var  longBytes = new byte[8];

            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var indexLength = BitConverter.ToInt64(longBytes, 0);

            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var coordinateLength = BitConverter.ToInt64(longBytes, 0);

            var file            = new MemoryMappedStream(new LimitedStream(stream));
            var indexArray      = new MemoryMappedHugeArrayUInt64(file, indexLength, indexLength, 1024);
            var coordinateArray = new MemoryMappedHugeArraySingle(file, coordinateLength * 2, coordinateLength * 2, 1024);

            if (copy)
            { // copy the data.
                var indexArrayCopy = new HugeArray <ulong>(indexLength);
                indexArrayCopy.CopyFrom(indexArray);

                var coordinateArrayCopy = new HugeArray <float>(coordinateLength * 2);
                coordinateArrayCopy.CopyFrom(coordinateArray);

                file.Dispose();

                return(new HugeCoordinateCollectionIndex(indexLength, indexArrayCopy, coordinateArrayCopy));
            }

            return(new HugeCoordinateCollectionIndex(indexLength, indexArray, coordinateArray));
        }
Пример #2
0
        /// <summary>
        /// Deserializes a graph from the given stream.
        /// </summary>
        /// <param name="stream">The stream to read from. Reading will start at position 0.</param>
        /// <param name="edgeDataSize">The edge data size.</param>
        /// <param name="mapFrom">The map from for the edge data.</param>
        /// <param name="mapTo">The map to for the edge data.</param>
        /// <param name="copy">Flag to make an in-memory copy.</param>
        /// <returns></returns>
        public new static DirectedGraph <TEdgeData> Deserialize(System.IO.Stream stream, int edgeDataSize, MappedHugeArray <TEdgeData, uint> .MapFrom mapFrom,
                                                                MappedHugeArray <TEdgeData, uint> .MapTo mapTo, bool copy)
        {
            // read sizes.
            long position = 0;

            stream.Seek(4, System.IO.SeekOrigin.Begin);
            position = position + 4;
            var longBytes = new byte[8];

            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var vertexLength = BitConverter.ToInt64(longBytes, 0);

            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var edgeLength = BitConverter.ToInt64(longBytes, 0);

            var bufferSize  = 32;
            var cacheSize   = MemoryMappedHugeArrayUInt32.DefaultCacheSize;
            var file        = new MemoryMappedStream(new OsmSharp.IO.LimitedStream(stream));
            var vertexArray = new MemoryMappedHugeArrayUInt32(file, (vertexLength + 1) * VERTEX_SIZE, (vertexLength + 1) * VERTEX_SIZE, bufferSize / 4, cacheSize * 4);

            position = position + ((vertexLength + 1) * VERTEX_SIZE * 4);
            var vertexCoordinateArray = new MappedHugeArray <GeoCoordinateSimple, float>(
                new MemoryMappedHugeArraySingle(file, (vertexLength + 1) * 2, (vertexLength + 1) * 2, bufferSize / 4, cacheSize * 4),
                2, (array, idx, coordinate) =>
            {
                array[idx]     = coordinate.Latitude;
                array[idx + 1] = coordinate.Longitude;
            },
                (Array, idx) =>
            {
                return(new GeoCoordinateSimple()
                {
                    Latitude = Array[idx],
                    Longitude = Array[idx + 1]
                });
            });

            position = position + ((vertexLength + 1) * 2 * 4);
            var edgeArray = new MemoryMappedHugeArrayUInt32(file, edgeLength * EDGE_SIZE, edgeLength * EDGE_SIZE, bufferSize / 2, cacheSize * 4);

            position = position + (edgeLength * EDGE_SIZE * 4);
            var edgeDataArray = new MappedHugeArray <TEdgeData, uint>(
                new MemoryMappedHugeArrayUInt32(file, edgeLength * edgeDataSize, edgeLength * edgeDataSize, bufferSize * 2, cacheSize * 2), edgeDataSize, mapTo, mapFrom);

            position = position + (edgeLength * edgeDataSize * 4);

            // deserialize shapes.
            stream.Seek(position, System.IO.SeekOrigin.Begin);
            var cappedStream = new OsmSharp.IO.LimitedStream(stream);

            var shapes = HugeCoordinateCollectionIndex.Deserialize(cappedStream, copy);

            if (copy)
            { // copy the data.
                var vertexArrayCopy = new HugeArray <uint>(vertexArray.Length);
                vertexArrayCopy.CopyFrom(vertexArray);
                var vertexCoordinateArrayCopy = new HugeArray <GeoCoordinateSimple>(vertexCoordinateArray.Length);
                vertexCoordinateArrayCopy.CopyFrom(vertexCoordinateArray);
                var edgeArrayCopy = new HugeArray <uint>(edgeArray.Length);
                edgeArrayCopy.CopyFrom(edgeArray);
                var edgeDataArrayCopy = new HugeArray <TEdgeData>(edgeDataArray.Length);
                edgeDataArrayCopy.CopyFrom(edgeDataArray);

                file.Dispose();

                return(new DirectedGraph <TEdgeData>(vertexCoordinateArrayCopy, vertexArrayCopy, edgeArrayCopy, edgeDataArrayCopy, shapes));
            }

            return(new DirectedGraph <TEdgeData>(vertexCoordinateArray, vertexArray, edgeArray, edgeDataArray, shapes));
        }
        /// <summary>
        /// Deserializes a huge collection index from the given stream.
        /// </summary>
        /// <param name="stream">The source stream.</param>
        /// <param name="copy">The copy flag. When true all data is copied into memory, otherwise the source-stream is used as a memorymapped file.</param>
        /// <returns></returns>
        public static HugeCoordinateCollectionIndex Deserialize(Stream stream, bool copy = false)
        {
            // read sizes.
            long position = 0;
            var longBytes = new byte[8];
            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var indexLength = BitConverter.ToInt64(longBytes, 0);
            stream.Read(longBytes, 0, 8);
            position = position + 8;
            var coordinateLength = BitConverter.ToInt64(longBytes, 0);

            var file = new MemoryMappedStream(new LimitedStream(stream));
            var indexArray = new MemoryMappedHugeArrayUInt64(file, indexLength, indexLength, 1024);
            var coordinateArray = new MemoryMappedHugeArraySingle(file, coordinateLength * 2, coordinateLength * 2, 1024);

            if (copy)
            { // copy the data.
                var indexArrayCopy = new HugeArray<ulong>(indexLength);
                indexArrayCopy.CopyFrom(indexArray);

                var coordinateArrayCopy = new HugeArray<float>(coordinateLength * 2);
                coordinateArrayCopy.CopyFrom(coordinateArray);

                file.Dispose();

                return new HugeCoordinateCollectionIndex(indexLength, indexArrayCopy, coordinateArrayCopy);
            }

            return new HugeCoordinateCollectionIndex(indexLength, indexArray, coordinateArray);
        }