]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry Index |
2 | // | |
3 | // Quickbook Examples | |
4 | // | |
5 | // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland. | |
6 | // | |
7 | // Use, modification and distribution is subject to the Boost Software License, | |
8 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
9 | // http://www.boost.org/LICENSE_1_0.txt) | |
10 | ||
11 | //[rtree_value_index | |
12 | ||
13 | #include <boost/geometry.hpp> | |
14 | #include <boost/geometry/geometries/point.hpp> | |
15 | #include <boost/geometry/geometries/box.hpp> | |
16 | ||
17 | #include <boost/geometry/index/rtree.hpp> | |
18 | ||
19 | #include <cmath> | |
20 | #include <vector> | |
21 | #include <iostream> | |
22 | #include <boost/foreach.hpp> | |
23 | ||
24 | namespace bg = boost::geometry; | |
25 | namespace bgi = boost::geometry::index; | |
26 | ||
27 | template <typename Container> | |
28 | class my_indexable | |
29 | { | |
30 | typedef typename Container::size_type size_t; | |
31 | typedef typename Container::const_reference cref; | |
32 | Container const& container; | |
33 | ||
34 | public: | |
35 | typedef cref result_type; | |
36 | explicit my_indexable(Container const& c) : container(c) {} | |
37 | result_type operator()(size_t i) const { return container[i]; } | |
38 | }; | |
39 | ||
40 | int main() | |
41 | { | |
42 | typedef bg::model::point<float, 2, bg::cs::cartesian> point; | |
43 | typedef bg::model::box<point> box; | |
44 | typedef std::vector<box>::size_type value; | |
45 | typedef bgi::rstar<16, 4> parameters; | |
46 | typedef my_indexable< std::vector<box> > indexable_getter; | |
47 | ||
48 | // boxes | |
49 | std::vector<box> boxes; | |
50 | ||
51 | // create some boxes | |
52 | for ( unsigned i = 0 ; i < 10 ; ++i ) | |
53 | { | |
54 | // add a box | |
55 | boxes.push_back(box(point(i+0.0f, i+0.0f), point(i+0.5f, i+0.5f))); | |
56 | } | |
57 | ||
58 | // display boxes | |
59 | std::cout << "generated boxes:" << std::endl; | |
60 | BOOST_FOREACH(box const& b, boxes) | |
61 | std::cout << bg::wkt<box>(b) << std::endl; | |
62 | ||
63 | // create the rtree | |
64 | parameters params; | |
65 | indexable_getter ind(boxes); | |
66 | bgi::rtree<value, parameters, indexable_getter> rtree(params, ind); | |
67 | ||
68 | // fill the spatial index | |
69 | for ( size_t i = 0 ; i < boxes.size() ; ++i ) | |
70 | rtree.insert(i); | |
71 | ||
72 | // find values intersecting some area defined by a box | |
73 | box query_box(point(0, 0), point(5, 5)); | |
74 | std::vector<value> result_s; | |
75 | rtree.query(bgi::intersects(query_box), std::back_inserter(result_s)); | |
76 | ||
77 | // find 5 nearest values to a point | |
78 | std::vector<value> result_n; | |
79 | rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n)); | |
80 | ||
81 | // note: in Boost.Geometry the WKT representation of a box is polygon | |
82 | ||
83 | // display results | |
84 | std::cout << "spatial query box:" << std::endl; | |
85 | std::cout << bg::wkt<box>(query_box) << std::endl; | |
86 | std::cout << "spatial query result:" << std::endl; | |
87 | BOOST_FOREACH(value i, result_s) | |
88 | std::cout << bg::wkt<box>(boxes[i]) << std::endl; | |
89 | ||
90 | std::cout << "knn query point:" << std::endl; | |
91 | std::cout << bg::wkt<point>(point(0, 0)) << std::endl; | |
92 | std::cout << "knn query result:" << std::endl; | |
93 | BOOST_FOREACH(value i, result_n) | |
94 | std::cout << bg::wkt<box>(boxes[i]) << std::endl; | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | //] |