]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/doc/index/src/examples/rtree/polygons_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_polygons_shared_ptr
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>
18 #include <boost/geometry/index/rtree.hpp>
23 #include <boost/foreach.hpp>
24 #include <boost/shared_ptr.hpp>
26 namespace bg
= boost::geometry
;
27 namespace bgi
= boost::geometry::index
;
31 typedef bg::model::point
<float, 2, bg::cs::cartesian
> point
;
32 typedef bg::model::box
<point
> box
;
33 typedef bg::model::polygon
<point
, false, false> polygon
; // ccw, open polygon
34 typedef boost::shared_ptr
<polygon
> shp
;
35 typedef std::pair
<box
, shp
> value
;
37 // create the rtree using default constructor
38 bgi::rtree
< value
, bgi::linear
<16, 4> > rtree
;
40 std::cout
<< "filling index with polygons shared pointers:" << std::endl
;
42 // create some polygons and fill the spatial index
43 for ( unsigned i
= 0 ; i
< 10 ; ++i
)
47 for ( float a
= 0 ; a
< 6.28316f
; a
+= 1.04720f
)
49 float x
= i
+ int(10*::cos(a
))*0.1f
;
50 float y
= i
+ int(10*::sin(a
))*0.1f
;
51 p
->outer().push_back(point(x
, y
));
54 // display new polygon
55 std::cout
<< bg::wkt
<polygon
>(*p
) << std::endl
;
57 // calculate polygon bounding box
58 box b
= bg::return_envelope
<box
>(*p
);
60 rtree
.insert(std::make_pair(b
, p
));
63 // find values intersecting some area defined by a box
64 box
query_box(point(0, 0), point(5, 5));
65 std::vector
<value
> result_s
;
66 rtree
.query(bgi::intersects(query_box
), std::back_inserter(result_s
));
68 // find 5 nearest values to a point
69 std::vector
<value
> result_n
;
70 rtree
.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n
));
72 // note: in Boost.Geometry the WKT representation of a box is polygon
74 // note: the values store the bounding boxes of polygons
75 // the polygons aren't used for querying but are printed
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
<polygon
>(*v
.second
) << 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
<polygon
>(*v
.second
) << std::endl
;