]>
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_shared_ptr | |
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 | #include <boost/shared_ptr.hpp> | |
24 | ||
25 | namespace bg = boost::geometry; | |
26 | namespace bgi = boost::geometry::index; | |
27 | ||
28 | namespace boost { namespace geometry { namespace index { | |
29 | ||
30 | template <typename Box> | |
31 | struct indexable< boost::shared_ptr<Box> > | |
32 | { | |
33 | typedef boost::shared_ptr<Box> V; | |
34 | ||
35 | typedef Box const& result_type; | |
36 | result_type operator()(V const& v) const { return *v; } | |
37 | }; | |
38 | ||
39 | }}} // namespace boost::geometry::index | |
40 | ||
41 | int main() | |
42 | { | |
43 | typedef bg::model::point<float, 2, bg::cs::cartesian> point; | |
44 | typedef bg::model::box<point> box; | |
45 | typedef boost::shared_ptr<box> shp; | |
46 | typedef shp value; | |
47 | ||
48 | // create the rtree using default constructor | |
49 | bgi::rtree< value, bgi::linear<16, 4> > rtree; | |
50 | ||
51 | std::cout << "filling index with boxes shared pointers:" << std::endl; | |
52 | ||
53 | // fill the spatial index | |
54 | for ( unsigned i = 0 ; i < 10 ; ++i ) | |
55 | { | |
56 | // create a box | |
57 | shp b(new box(point(i+0.0f, i+0.0f), point(i+0.5f, i+0.5f))); | |
58 | ||
59 | // display new box | |
60 | std::cout << bg::wkt<box>(*b) << std::endl; | |
61 | ||
62 | // insert new value | |
63 | rtree.insert(b); | |
64 | } | |
65 | ||
66 | // find values intersecting some area defined by a box | |
67 | box query_box(point(0, 0), point(5, 5)); | |
68 | std::vector<value> result_s; | |
69 | rtree.query(bgi::intersects(query_box), std::back_inserter(result_s)); | |
70 | ||
71 | // find 5 nearest values to a point | |
72 | std::vector<value> result_n; | |
73 | rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n)); | |
74 | ||
75 | // note: in Boost.Geometry the WKT representation of a box is polygon | |
76 | ||
77 | // display results | |
78 | std::cout << "spatial query box:" << std::endl; | |
79 | std::cout << bg::wkt<box>(query_box) << std::endl; | |
80 | std::cout << "spatial query result:" << std::endl; | |
81 | BOOST_FOREACH(value const& v, result_s) | |
82 | std::cout << bg::wkt<box>(*v) << std::endl; | |
83 | ||
84 | std::cout << "knn query point:" << std::endl; | |
85 | std::cout << bg::wkt<point>(point(0, 0)) << std::endl; | |
86 | std::cout << "knn query result:" << std::endl; | |
87 | BOOST_FOREACH(value const& v, result_n) | |
88 | std::cout << bg::wkt<box>(*v) << std::endl; | |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | //] |