]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/doc/index/src/examples/rtree/value_shared_ptr.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)
11 //[rtree_value_shared_ptr
13 #include <boost/geometry.hpp>
14 #include <boost/geometry/geometries/point.hpp>
15 #include <boost/geometry/geometries/box.hpp>
17 #include <boost/geometry/index/rtree.hpp>
22 #include <boost/foreach.hpp>
23 #include <boost/shared_ptr.hpp>
25 namespace bg
= boost::geometry
;
26 namespace bgi
= boost::geometry::index
;
28 namespace boost
{ namespace geometry
{ namespace index
{
30 template <typename Box
>
31 struct indexable
< boost::shared_ptr
<Box
> >
33 typedef boost::shared_ptr
<Box
> V
;
35 typedef Box
const& result_type
;
36 result_type
operator()(V
const& v
) const { return *v
; }
39 }}} // namespace boost::geometry::index
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
;
48 // create the rtree using default constructor
49 bgi::rtree
< value
, bgi::linear
<16, 4> > rtree
;
51 std::cout
<< "filling index with boxes shared pointers:" << std::endl
;
53 // fill the spatial index
54 for ( unsigned i
= 0 ; i
< 10 ; ++i
)
57 shp
b(new box(point(i
+0.0f
, i
+0.0f
), point(i
+0.5f
, i
+0.5f
)));
60 std::cout
<< bg::wkt
<box
>(*b
) << std::endl
;
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
));
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
));
75 // note: in Boost.Geometry the WKT representation of a box is polygon
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
;
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
;