Ejemplo n.º 1
0
        public GeoKDBushTests()
        {
            string geoDataDir = Directory.GetCurrentDirectory() + "\\Data";
            string jsonCities = geoDataDir + "\\cities.json";
            string bz2Cities  = geoDataDir + "\\cities.bz2";

            if (!File.Exists(jsonCities))
            {
                if (!File.Exists(bz2Cities))
                {
                    throw new InvalidOperationException("No test data found, either as zip or json.");
                }

                BZip2.Decompress(File.OpenRead(bz2Cities), File.Create(jsonCities), true);
            }


            using (var file = File.OpenText(jsonCities))
            {
                var serializer = new JsonSerializer();
                cities = (List <City>)serializer.Deserialize(file, typeof(List <City>));
            }

            index     = new KDBush <City>(cities.ToArray(), p => p.Lon, p => p.Lat, nodeSize: 10);
            geoKdBush = new GeoKDBush <City>();
        }
Ejemplo n.º 2
0
        public void CreateKDBushIndex_RangeSearch_CorrectPointsReturned()
        {
            var index = new KDBush <double[]>(pointsData, nodeSize: 10);

            var result = index.Range(20, 30, 50, 70);

            Assert.Equal(new[] { 60, 20, 45, 3, 17, 71, 44, 19, 18, 15, 69, 90, 62, 96, 47, 8, 77, 72 },
                         result);
        }
Ejemplo n.º 3
0
        public void TestKDBush()
        {
            int max    = 1000000;
            var pts    = new Point[max];
            var random = new Random();

            for (var i = 0; i < max; i++)
            {
                var pt = new Point();
                pt.X   = random.Next(-180000, 180000) * 0.001;
                pt.Y   = random.Next(-90000, 90000) * 0.001;
                pts[i] = pt;
            }

            Stopwatch sp = new Stopwatch();

            sp.Start();
            var index = new KDBush(pts);

            sp.Stop();
            Console.WriteLine($"索引耗时:{sp.ElapsedMilliseconds}");

            sp.Start();
            for (int i = 0; i < 10000; i++)
            {
                var pt = new Point();
                pt.X = random.Next(-180000, 180000) * 0.001;
                pt.Y = random.Next(-90000, 90000) * 0.001;
                index.Range(pt.X - 1, pt.Y - 1, pt.X + 1, pt.Y + 1);
            }
            sp.Stop();
            Console.WriteLine($"小范围查询耗时:{sp.ElapsedMilliseconds}");

            sp.Start();
            for (int i = 0; i < 10000; i++)
            {
                var pt = new Point();
                pt.X = random.Next(-180000, 180000) * 0.001;
                pt.Y = random.Next(-90000, 90000) * 0.001;
                index.WithIn(pt.X, pt.Y, 1);
            }
            sp.Stop();
            Console.WriteLine($"半径查询耗时:{sp.ElapsedMilliseconds}");

            var pt1 = new Point();

            pt1.X = random.Next(-180000, 180000) * 0.001;
            pt1.Y = random.Next(-90000, 90000) * 0.001;
            var rr = index.Range(pt1.X - 10, pt1.Y - 10, pt1.X + 10, pt1.Y + 10);

            var pt2 = new Point();

            pt2.X = random.Next(-180000, 180000) * 0.001;
            pt2.Y = random.Next(-90000, 90000) * 0.001;
            var bb = index.WithIn(pt2.X, pt2.Y, 1);
        }
Ejemplo n.º 4
0
        public void CreateKDBushIndex_RadiusSearch_CorrectPointsReturned()
        {
            var index = new KDBush <double[]>(pointsData, nodeSize: 10);

            var qp = new double[] { 50, 50 };
            var r  = 20;

            var result = index.Within(qp[0], qp[1], r);

            Assert.Equal(new[] { 60, 6, 25, 92, 42, 20, 45, 3, 71, 44, 18, 96 }, result);
        }
Ejemplo n.º 5
0
        public void CreateKDBushIndex_RangeSearch_AllReturnedPointsInRange()
        {
            var index = new KDBush <double[]>(pointsData, nodeSize: 10);

            var result = index.Range(20, 30, 50, 70);

            foreach (int pIdx in result)
            {
                var p = pointsData[pIdx];
                Assert.False(p[0] < 20 || p[0] > 50 || p[1] < 30 || p[1] > 70);
            }
        }
Ejemplo n.º 6
0
        public void CreateKDBushIndex_RangeSearch_UnReturnedPointsAreOutsideRange()
        {
            var index = new KDBush <double[]>(pointsData, nodeSize: 10);

            var result = index.Range(20, 30, 50, 70);

            for (var i = 0; i < ids.Length; i++)
            {
                var p = pointsData[ids[i]];
                var pointIsNotInResult = result.IndexOf(ids[i]) < 0;
                Assert.False(pointIsNotInResult && p[0] >= 20 && p[0] <= 50 && p[1] >= 30 && p[1] <= 70);
            }
        }
Ejemplo n.º 7
0
        public void TestRadiusQuery()
        {
            // Verify that circular query works
            KDBush <int> kdbush = new KDBush <int>(10);

            kdbush.Index(points);

            var result = kdbush.Query(50, 50, 20);

            List <int> correctPoints = new List <int>(new int[] { 60, 6, 25, 92, 42, 20, 45, 3, 71, 44, 18, 96 });

            AssertPointsMatch(correctPoints, result);
        }
Ejemplo n.º 8
0
        public void TestRectQuery()
        {
            // Verify that rectangular query works
            KDBush <int> kdbush = new KDBush <int>(10);

            kdbush.Index(points);

            var result = kdbush.Query(20, 30, 50, 70);

            List <int> correctPoints = new List <int>(new int[] { 60, 20, 45, 3, 17, 71, 44, 19, 18, 15, 69, 90, 62, 96, 47, 8, 77, 72 });

            AssertPointsMatch(correctPoints, result);
        }
Ejemplo n.º 9
0
        public void CreateKDBushIndex_RadiusSearch_AllUnReturnedPointsOutsideRadius()
        {
            var index = new KDBush <double[]>(pointsData, nodeSize: 10);

            var qp = new double[] { 50, 50 };
            var r  = 20;
            var r2 = 20 * 20;

            var result = index.Within(qp[0], qp[1], r);

            for (var i = 0; i < ids.Length; i++)
            {
                var p = pointsData[ids[i]];
                Assert.False(result.IndexOf(ids[i]) < 0 && SqDist(p, qp) <= r2);
            }
        }
Ejemplo n.º 10
0
        public void CreateKDBushIndex_RadiusSearch_AllReturnedPointsInRadius()
        {
            var index = new KDBush <double[]>(pointsData, nodeSize: 10);

            var qp = new double[] { 50, 50 };
            var r  = 20;
            var r2 = 20 * 20;

            var result = index.Within(qp[0], qp[1], r);

            for (var i = 0; i < result.Count; i++)
            {
                var p = pointsData[result[i]];
                Assert.False(SqDist(p, qp) > r2);
            }
        }
Ejemplo n.º 11
0
        public void IndexIsSorted()
        {
            // Verify that indexing works
            int[] ids = new int[] { 97, 74, 95, 30, 77, 38, 76, 27, 80, 55, 72, 90, 88, 48, 43, 46, 65, 39, 62, 93, 9, 96, 47, 8, 3, 12, 15, 14, 21, 41, 36, 40, 69, 56, 85, 78, 17, 71, 44,
                                    19, 18, 13, 99, 24, 67, 33, 37, 49, 54, 57, 98, 45, 23, 31, 66, 68, 0, 32, 5, 51, 75, 73, 84, 35, 81, 22, 61, 89, 1, 11, 86, 52, 94, 16, 2, 6, 25, 92,
                                    42, 20, 60, 58, 83, 79, 64, 10, 59, 53, 26, 87, 4, 63, 50, 7, 28, 82, 70, 29, 34, 91 };

            KDBush <int> kdbush = new KDBush <int>(10);

            kdbush.Index(points);

            for (int i = 0; i < points.Count; i++)
            {
                Assert.AreEqual(ids[i], kdbush.points[i].UserData);
            }
        }
 internal void InitTree()
 {
     this.tree = new KDBush <GeoPoint>();
     this.tree.Index(this.points);
 }
Ejemplo n.º 13
0
        public void CreateKDBushIndex_TestsCoords()
        {
            var index = new KDBush <double[]>(pointsData, nodeSize: 10);

            Assert.Equal(coords, index.coords);
        }