Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / geometry / doc / index / src / examples / rtree / polygons_shared_ptr.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_polygons_shared_ptr
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
18 #include <boost/geometry/index/rtree.hpp>
19
20 #include <cmath>
21 #include <vector>
22 #include <iostream>
23 #include <boost/foreach.hpp>
24 #include <boost/shared_ptr.hpp>
25
26 namespace bg = boost::geometry;
27 namespace bgi = boost::geometry::index;
28
29 int main()
30 {
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;
36
37     // create the rtree using default constructor
38     bgi::rtree< value, bgi::linear<16, 4> > rtree;
39
40     std::cout << "filling index with polygons shared pointers:" << std::endl;
41
42     // create some polygons and fill the spatial index
43     for ( unsigned i = 0 ; i < 10 ; ++i )
44     {
45         // create a polygon
46         shp p(new polygon());
47         for ( float a = 0 ; a < 6.28316f ; a += 1.04720f )
48         {
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));
52         }
53
54         // display new polygon
55         std::cout << bg::wkt<polygon>(*p) << std::endl;
56
57         // calculate polygon bounding box
58         box b = bg::return_envelope<box>(*p);
59         // insert new value
60         rtree.insert(std::make_pair(b, p));
61     }
62
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));
67
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));
71
72     // note: in Boost.Geometry the WKT representation of a box is polygon
73
74     // note: the values store the bounding boxes of polygons
75     // the polygons aren't used for querying but are printed
76
77     // display results
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;
83
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;
89
90     return 0;
91 }
92
93 //]