Пример #1
0
        public void RTreeStreamSerializeSmallTest()
        {
            var rect1 = new BoxF2D(0, 0, 2, 2);
            var rect2 = new BoxF2D(4, 0, 6, 2);
            var rect3 = new BoxF2D(0, 4, 2, 6);
            var rect4 = new BoxF2D(4, 4, 6, 6);

            // create the index and reference index.
            var index = new RTreeMemoryIndex <string>();

            // add data.
            index.Add(rect1, rect1.ToString() + "1");
            index.Add(rect1, rect1.ToString() + "2");
            index.Add(rect1, rect1.ToString() + "3");
            index.Add(rect1, rect1.ToString() + "4");

            index.Add(rect2, rect2.ToString() + "1");
            index.Add(rect2, rect2.ToString() + "2");
            index.Add(rect2, rect2.ToString() + "3");
            index.Add(rect2, rect2.ToString() + "4");

            index.Add(rect3, rect3.ToString() + "1");
            index.Add(rect3, rect3.ToString() + "2");
            index.Add(rect3, rect3.ToString() + "3");
            index.Add(rect3, rect3.ToString() + "4");

            index.Add(rect4, rect4.ToString() + "1");
            index.Add(rect4, rect4.ToString() + "2");
            index.Add(rect4, rect4.ToString() + "3");
            index.Add(rect4, rect4.ToString() + "4");

            var stream     = new MemoryStream();
            var serializer = new DataTestClassSerializer();

            serializer.Serialize(stream, index);

            ISpatialIndexReadonly <string> deserialized =
                serializer.Deserialize(stream, true);

            // some simple queries.
            var result = new HashSet <string>(
                deserialized.Get(rect4));

            Assert.AreEqual(4, result.Count);
            Assert.IsTrue(result.Contains(rect4.ToString() + "1"));
            Assert.IsTrue(result.Contains(rect4.ToString() + "2"));
            Assert.IsTrue(result.Contains(rect4.ToString() + "3"));
            Assert.IsTrue(result.Contains(rect4.ToString() + "4"));

            result = new HashSet <string>(
                deserialized.Get(rect3));
            Assert.AreEqual(4, result.Count);
            Assert.IsTrue(result.Contains(rect3.ToString() + "1"));
            Assert.IsTrue(result.Contains(rect3.ToString() + "2"));
            Assert.IsTrue(result.Contains(rect3.ToString() + "3"));
            Assert.IsTrue(result.Contains(rect3.ToString() + "4"));

            result = new HashSet <string>(
                deserialized.Get(rect2));
            Assert.AreEqual(4, result.Count);
            Assert.IsTrue(result.Contains(rect2.ToString() + "1"));
            Assert.IsTrue(result.Contains(rect2.ToString() + "2"));
            Assert.IsTrue(result.Contains(rect2.ToString() + "3"));
            Assert.IsTrue(result.Contains(rect2.ToString() + "4"));

            result = new HashSet <string>(
                deserialized.Get(rect1));
            Assert.AreEqual(4, result.Count);
            Assert.IsTrue(result.Contains(rect1.ToString() + "1"));
            Assert.IsTrue(result.Contains(rect1.ToString() + "2"));
            Assert.IsTrue(result.Contains(rect1.ToString() + "3"));
            Assert.IsTrue(result.Contains(rect1.ToString() + "4"));
        }
Пример #2
0
        public void RTreeStreamSerializeIndexAddTests()
        {
            // build test-data.
            var       testDataList    = new List <KeyValuePair <BoxF2D, string> >();
            const int count           = 1000;
            var       randomGenerator = new RandomGenerator(66707770); // make this deterministic

            for (int idx = 0; idx < count; idx++)
            {
                double x1 = randomGenerator.Generate(1.0);
                double x2 = randomGenerator.Generate(1.0);
                double y1 = randomGenerator.Generate(1.0);
                double y2 = randomGenerator.Generate(1.0);

                var box = new BoxF2D(new PointF2D(x1, y1), new PointF2D(x2, y2));

                testDataList.Add(new KeyValuePair <BoxF2D, string>(
                                     box, idx.ToString(System.Globalization.CultureInfo.InvariantCulture)));
            }

            // create the index and reference index.
            var index     = new RTreeMemoryIndex <string>();
            var reference = new ReferenceImplementation <string>();

            // add all the data.
            for (int idx = 0; idx < count; idx++)
            {
                var keyValuePair = testDataList[idx];
                index.Add(keyValuePair.Key, keyValuePair.Value);
                reference.Add(keyValuePair.Key, keyValuePair.Value);

                //Assert.AreEqual(reference.Count(), index.Count());
            }

            //Assert.AreEqual(count, index.Count());

            var stream     = new MemoryStream();
            var serializer = new DataTestClassSerializer();

            serializer.Serialize(stream, index);

            ISpatialIndexReadonly <string> deserialized =
                serializer.Deserialize(stream, true);

            // query all.
            var totalBox        = new BoxF2D(0, 0, 1, 1);
            var resultIndex     = new HashSet <string>(deserialized.Get(totalBox));
            var resultReference = new HashSet <string>(reference.Get(totalBox));

            foreach (var data in resultIndex)
            {
                Assert.IsTrue(resultReference.Contains(data));
            }
            foreach (var data in resultReference)
            {
                Assert.IsTrue(resultIndex.Contains(data));
            }

            // generate random boxes and compare results.
            for (int idx = 0; idx < 20; idx++)
            {
                double x1 = randomGenerator.Generate(1.0);
                double x2 = randomGenerator.Generate(1.0);
                double y1 = randomGenerator.Generate(1.0);
                double y2 = randomGenerator.Generate(1.0);

                var box = new BoxF2D(new PointF2D(x1, y1), new PointF2D(x2, y2));

                resultIndex     = new HashSet <string>(deserialized.Get(box));
                resultReference = new HashSet <string>(reference.Get(box));

                foreach (var data in resultIndex)
                {
                    Assert.IsTrue(resultReference.Contains(data));
                }
                foreach (var data in resultReference)
                {
                    Assert.IsTrue(resultIndex.Contains(data));
                }
            }
        }