]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/doc/index/src/examples/rtree/quick_start.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 //[rtree_quickstart_include
15 #include <boost/geometry.hpp>
16 #include <boost/geometry/geometries/point.hpp>
17 #include <boost/geometry/geometries/box.hpp>
19 #include <boost/geometry/index/rtree.hpp>
21 // to store queries results
26 #include <boost/foreach.hpp>
28 namespace bg
= boost::geometry
;
29 namespace bgi
= boost::geometry::index
;
34 //[rtree_quickstart_valuetype
35 typedef bg::model::point
<float, 2, bg::cs::cartesian
> point
;
36 typedef bg::model::box
<point
> box
;
37 typedef std::pair
<box
, unsigned> value
;
40 //[rtree_quickstart_create
41 // create the rtree using default constructor
42 bgi::rtree
< value
, bgi::quadratic
<16> > rtree
;
45 //[rtree_quickstart_insert
47 for ( unsigned i
= 0 ; i
< 10 ; ++i
)
50 box
b(point(i
+ 0.0f
, i
+ 0.0f
), point(i
+ 0.5f
, i
+ 0.5f
));
52 rtree
.insert(std::make_pair(b
, i
));
56 //[rtree_quickstart_spatial_query
57 // find values intersecting some area defined by a box
58 box
query_box(point(0, 0), point(5, 5));
59 std::vector
<value
> result_s
;
60 rtree
.query(bgi::intersects(query_box
), std::back_inserter(result_s
));
63 //[rtree_quickstart_nearest_query
64 // find 5 nearest values to a point
65 std::vector
<value
> result_n
;
66 rtree
.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n
));
69 // note: in Boost.Geometry WKT representation of a box is polygon
71 //[rtree_quickstart_output
73 std::cout
<< "spatial query box:" << std::endl
;
74 std::cout
<< bg::wkt
<box
>(query_box
) << std::endl
;
75 std::cout
<< "spatial query result:" << std::endl
;
76 BOOST_FOREACH(value
const& v
, result_s
)
77 std::cout
<< bg::wkt
<box
>(v
.first
) << " - " << v
.second
<< std::endl
;
79 std::cout
<< "knn query point:" << std::endl
;
80 std::cout
<< bg::wkt
<point
>(point(0, 0)) << std::endl
;
81 std::cout
<< "knn query result:" << std::endl
;
82 BOOST_FOREACH(value
const& v
, result_n
)
83 std::cout
<< bg::wkt
<box
>(v
.first
) << " - " << v
.second
<< std::endl
;