]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/doc/index/src/examples/rtree/variants_map.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / geometry / doc / index / src / examples / rtree / variants_map.cpp
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_variants_map
12
13 #include <boost/geometry.hpp>
14 #include <boost/geometry/geometries/point.hpp>
15 #include <boost/geometry/geometries/box.hpp>
16 #include <boost/geometry/geometries/polygon.hpp>
17 #include <boost/geometry/geometries/ring.hpp>
18 #include <boost/geometry/geometries/linestring.hpp>
19
20 #include <boost/geometry/index/rtree.hpp>
21
22 #include <cmath>
23 #include <vector>
24 #include <map>
25 #include <iostream>
26 #include <boost/foreach.hpp>
27 #include <boost/variant.hpp>
28
29 namespace bg = boost::geometry;
30 namespace bgi = boost::geometry::index;
31
32 typedef bg::model::point<float, 2, bg::cs::cartesian> point;
33 typedef bg::model::box<point> box;
34 typedef bg::model::polygon<point, false, false> polygon; // ccw, open polygon
35 typedef bg::model::ring<point, false, false> ring; // ccw, open ring
36 typedef bg::model::linestring<point> linestring;
37 typedef boost::variant<polygon, ring, linestring> geometry;
38
39 typedef std::map<unsigned, geometry> map;
40 typedef std::pair<box, map::iterator> value;
41
42 template <class Container>
43 void fill(unsigned i, Container & container)
44 {
45 for ( float a = 0 ; a < 6.28316f ; a += 1.04720f )
46 {
47 float x = i + int(10*::cos(a))*0.1f;
48 float y = i + int(10*::sin(a))*0.1f;
49 container.push_back(point(x, y));
50 }
51 }
52
53 struct print_visitor : public boost::static_visitor<>
54 {
55 void operator()(polygon const& g) const { std::cout << bg::wkt<polygon>(g) << std::endl; }
56 void operator()(ring const& g) const { std::cout << bg::wkt<ring>(g) << std::endl; }
57 void operator()(linestring const& g) const { std::cout << bg::wkt<linestring>(g) << std::endl; }
58 };
59
60 struct envelope_visitor : public boost::static_visitor<box>
61 {
62 box operator()(polygon const& g) const { return bg::return_envelope<box>(g); }
63 box operator()(ring const& g) const { return bg::return_envelope<box>(g); }
64 box operator()(linestring const& g) const { return bg::return_envelope<box>(g); }
65 };
66
67
68 int main()
69 {
70 // geometries container
71 map geometries;
72
73 // create some geometries
74 for ( unsigned i = 0 ; i < 10 ; ++i )
75 {
76 unsigned c = rand() % 3;
77
78 if ( 0 == c )
79 {
80 // create polygon
81 polygon p;
82 fill(i, p.outer());
83 geometries.insert(std::make_pair(i, geometry(p)));
84 }
85 else if ( 1 == c )
86 {
87 // create ring
88 ring r;
89 fill(i, r);
90 geometries.insert(std::make_pair(i, geometry(r)));
91 }
92 else if ( 2 == c )
93 {
94 // create linestring
95 linestring l;
96 fill(i, l);
97 geometries.insert(std::make_pair(i, geometry(l)));
98 }
99 }
100
101 // display geometries
102 std::cout << "generated geometries:" << std::endl;
103 BOOST_FOREACH(map::value_type const& p, geometries)
104 boost::apply_visitor(print_visitor(), p.second);
105
106 // create the rtree using default constructor
107 bgi::rtree< value, bgi::quadratic<16, 4> > rtree;
108
109 // fill the spatial index
110 for ( map::iterator it = geometries.begin() ; it != geometries.end() ; ++it )
111 {
112 // calculate polygon bounding box
113 box b = boost::apply_visitor(envelope_visitor(), it->second);
114 // insert new value
115 rtree.insert(std::make_pair(b, it));
116 }
117
118 // find values intersecting some area defined by a box
119 box query_box(point(0, 0), point(5, 5));
120 std::vector<value> result_s;
121 rtree.query(bgi::intersects(query_box), std::back_inserter(result_s));
122
123 // find 5 nearest values to a point
124 std::vector<value> result_n;
125 rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n));
126
127 // note: in Boost.Geometry the WKT representation of a box is polygon
128
129 // note: the values store the bounding boxes of geometries
130 // the geometries aren't used for querying but are printed
131
132 // display results
133 std::cout << "spatial query box:" << std::endl;
134 std::cout << bg::wkt<box>(query_box) << std::endl;
135 std::cout << "spatial query result:" << std::endl;
136 BOOST_FOREACH(value const& v, result_s)
137 boost::apply_visitor(print_visitor(), v.second->second);
138
139 std::cout << "knn query point:" << std::endl;
140 std::cout << bg::wkt<point>(point(0, 0)) << std::endl;
141 std::cout << "knn query result:" << std::endl;
142 BOOST_FOREACH(value const& v, result_n)
143 boost::apply_visitor(print_visitor(), v.second->second);
144
145 return 0;
146 }
147
148 //]