public void TestSerialize()
        {
            OsmSharp.Math.Random.StaticRandomGenerator.Set(116542346);

            var box = new GeoCoordinateBox(
                new GeoCoordinate(90, 180),
                new GeoCoordinate(-90, -180));
            var size = 5;
            var maxCollectionSize   = 4;
            var referenceDictionary = new Dictionary <long, ICoordinateCollection>();
            var coordinates         = new HugeCoordinateCollectionIndex(100);

            for (int idx = 0; idx < size; idx++)
            {
                var currentSize      = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(maxCollectionSize) + 1;
                var coordinatesArray = new GeoCoordinate[currentSize];
                while (currentSize > 0)
                {
                    coordinatesArray[currentSize - 1] = box.GenerateRandomIn(OsmSharp.Math.Random.StaticRandomGenerator.Get());
                    currentSize--;
                }
                var coordinatesCollection = new CoordinateArrayCollection <GeoCoordinate>(coordinatesArray);
                referenceDictionary[idx] = coordinatesCollection;
                coordinates[idx]         = coordinatesCollection;
            }

            coordinates.Trim();
            coordinates.Compress();

            byte[] data = null;
            using (var stream = new MemoryStream())
            {
                long length = coordinates.Serialize(stream);
                data = stream.ToArray();

                Assert.AreEqual(168, length);
                Assert.AreEqual(data.Length, length);
            }

            var result = HugeCoordinateCollectionIndex.Deserialize(new MemoryStream(data));

            // check result.
            for (int idx = 0; idx < size; idx++)
            {
                var referenceCollection = referenceDictionary[idx];
                var collection          = result[idx];

                referenceCollection.Reset();
                collection.Reset();

                while (referenceCollection.MoveNext())
                {
                    Assert.IsTrue(collection.MoveNext());
                    Assert.AreEqual(referenceCollection.Latitude, collection.Latitude);
                    Assert.AreEqual(referenceCollection.Longitude, collection.Longitude);
                }
                Assert.IsFalse(collection.MoveNext());
            }

            result = HugeCoordinateCollectionIndex.Deserialize(new MemoryStream(data), true);

            // check result.
            for (int idx = 0; idx < size; idx++)
            {
                var referenceCollection = referenceDictionary[idx];
                var collection          = result[idx];

                referenceCollection.Reset();
                collection.Reset();

                while (referenceCollection.MoveNext())
                {
                    Assert.IsTrue(collection.MoveNext());
                    Assert.AreEqual(referenceCollection.Latitude, collection.Latitude);
                    Assert.AreEqual(referenceCollection.Longitude, collection.Longitude);
                }
                Assert.IsFalse(collection.MoveNext());
            }
        }
Ejemplo n.º 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));
        }