]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/doc/index/src/examples/rtree/variants_map.cpp
1 // Boost.Geometry Index
5 // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
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)
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>
20 #include <boost/geometry/index/rtree.hpp>
26 #include <boost/foreach.hpp>
27 #include <boost/variant.hpp>
29 namespace bg
= boost::geometry
;
30 namespace bgi
= boost::geometry::index
;
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
;
39 typedef std::map
<unsigned, geometry
> map
;
40 typedef std::pair
<box
, map::iterator
> value
;
42 template <class Container
>
43 void fill(unsigned i
, Container
& container
)
45 for ( float a
= 0 ; a
< 6.28316f
; a
+= 1.04720f
)
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
));
53 struct print_visitor
: public boost::static_visitor
<>
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
; }
60 struct envelope_visitor
: public boost::static_visitor
<box
>
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
); }
70 // geometries container
73 // create some geometries
74 for ( unsigned i
= 0 ; i
< 10 ; ++i
)
76 unsigned c
= rand() % 3;
83 geometries
.insert(std::make_pair(i
, geometry(p
)));
90 geometries
.insert(std::make_pair(i
, geometry(r
)));
97 geometries
.insert(std::make_pair(i
, geometry(l
)));
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
);
106 // create the rtree using default constructor
107 bgi::rtree
< value
, bgi::quadratic
<16, 4> > rtree
;
109 // fill the spatial index
110 for ( map::iterator it
= geometries
.begin() ; it
!= geometries
.end() ; ++it
)
112 // calculate polygon bounding box
113 box b
= boost::apply_visitor(envelope_visitor(), it
->second
);
115 rtree
.insert(std::make_pair(b
, it
));
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
));
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
));
127 // note: in Boost.Geometry the WKT representation of a box is polygon
129 // note: the values store the bounding boxes of geometries
130 // the geometries aren't used for querying but are printed
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
);
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
);