1 | #include <iostream>
|
---|
2 | #include <boost/foreach.hpp>
|
---|
3 | #include <boost/geometry.hpp>
|
---|
4 | #include <boost/geometry/geometries/point.hpp>
|
---|
5 | #include <boost/geometry/index/rtree.hpp>
|
---|
6 |
|
---|
7 | namespace bg = boost::geometry;
|
---|
8 | namespace bgm = boost::geometry::model;
|
---|
9 | namespace bgi = boost::geometry::index;
|
---|
10 |
|
---|
11 | typedef bgm::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > point;
|
---|
12 | typedef bgi::rtree<point, bgi::rstar<32, 9, 9, 32> > rtree;
|
---|
13 |
|
---|
14 | double distance(point const& p1, point const& p2)
|
---|
15 | {
|
---|
16 | return bg::distance(p1, p2) * 6371000.0;
|
---|
17 | }
|
---|
18 |
|
---|
19 | struct less
|
---|
20 | {
|
---|
21 | less(point const& qq) : q(qq) {}
|
---|
22 | bool operator()(point const& l, point const& r)
|
---|
23 | {
|
---|
24 | return bg::comparable_distance(l, q) < bg::comparable_distance(r, q);
|
---|
25 | }
|
---|
26 | point q;
|
---|
27 | };
|
---|
28 |
|
---|
29 | template <typename It>
|
---|
30 | void sort(It first, It last, point const& q)
|
---|
31 | {
|
---|
32 | std::sort(first, last, less(q));
|
---|
33 | }
|
---|
34 |
|
---|
35 | int main()
|
---|
36 | {
|
---|
37 | std::vector<point> pts;
|
---|
38 | pts.push_back(point(10.9325, 59.9226));
|
---|
39 | pts.push_back(point(10.9352, 59.9285));
|
---|
40 | pts.push_back(point(11.0821, 59.9182));
|
---|
41 | pts.push_back(point(11.0890, 59.9097));
|
---|
42 | pts.push_back(point(11.0971, 59.8969));
|
---|
43 | pts.push_back(point(11.0999, 59.8924));
|
---|
44 | pts.push_back(point(11.0995, 59.8895));
|
---|
45 | pts.push_back(point(11.1550, 59.9251));
|
---|
46 | pts.push_back(point(11.1571, 59.9247));
|
---|
47 |
|
---|
48 | pts.push_back(point(10.9402, 59.9343));
|
---|
49 | pts.push_back(point(10.9351, 59.9364));
|
---|
50 | pts.push_back(point(10.9466, 59.9316));
|
---|
51 | pts.push_back(point(10.9204, 59.9402));
|
---|
52 | pts.push_back(point(10.9157, 59.9400));
|
---|
53 | pts.push_back(point(10.9428, 59.9455));
|
---|
54 | pts.push_back(point(10.9479, 59.9477));
|
---|
55 | pts.push_back(point(10.9469, 59.9492));
|
---|
56 |
|
---|
57 | // Randomize points
|
---|
58 | for ( int i = 0 ; i < 30000 ; ++i )
|
---|
59 | {
|
---|
60 | double x = rand() / double(RAND_MAX);
|
---|
61 | double y = rand() / double(RAND_MAX);
|
---|
62 | point p(4 + x * 27, 50 + y * 21);
|
---|
63 | pts.push_back(p);
|
---|
64 | }
|
---|
65 |
|
---|
66 | // Create using pack create
|
---|
67 | //rtree rt (pts);
|
---|
68 |
|
---|
69 | // Create using insert
|
---|
70 | rtree rt;
|
---|
71 | BOOST_FOREACH(point const& p, pts)
|
---|
72 | {
|
---|
73 | rt.insert(p);
|
---|
74 | }
|
---|
75 |
|
---|
76 | point q(10.9325, 59.9226);
|
---|
77 |
|
---|
78 | sort(pts.begin(), pts.end(), q);
|
---|
79 |
|
---|
80 | std::cout << "Points:\n";
|
---|
81 | int i = 0;
|
---|
82 | BOOST_FOREACH(point const& p, pts)
|
---|
83 | {
|
---|
84 | std::cout << bg::wkt(p) << " - " << distance(p, q) << std::endl;
|
---|
85 | if ( i++ > 20 )
|
---|
86 | break;
|
---|
87 | }
|
---|
88 |
|
---|
89 | std::cout << "Bounds:\n";
|
---|
90 | std::cout << bg::wkt(rt.bounds()) << std::endl;
|
---|
91 | std::cout << "Size:\n";
|
---|
92 | std::cout << rt.size() << std::endl;
|
---|
93 |
|
---|
94 | std::vector<point> res;
|
---|
95 | rt.query(bgi::nearest(q, 8), std::back_inserter(res));
|
---|
96 |
|
---|
97 | sort(res.begin(), res.end(), q);
|
---|
98 |
|
---|
99 | std::cout << "Nearest:\n";
|
---|
100 | BOOST_FOREACH(point const& p, res)
|
---|
101 | {
|
---|
102 | std::cout << bg::wkt(p) << " - " << distance(p, q) << std::endl;
|
---|
103 | }
|
---|
104 | }
|
---|