• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_vector
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 
25 namespace bg = boost::geometry;
26 namespace bgi = boost::geometry::index;
27 
main()28 int main()
29 {
30     typedef bg::model::point<float, 2, bg::cs::cartesian> point;
31     typedef bg::model::box<point> box;
32     typedef bg::model::polygon<point, false, false> polygon; // ccw, open polygon
33     typedef std::pair<box, unsigned> value;
34 
35     // polygons
36     std::vector<polygon> polygons;
37 
38     // create some polygons
39     for ( unsigned i = 0 ; i < 10 ; ++i )
40     {
41         // create a polygon
42         polygon p;
43         for ( float a = 0 ; a < 6.28316f ; a += 1.04720f )
44         {
45             float x = i + int(10*::cos(a))*0.1f;
46             float y = i + int(10*::sin(a))*0.1f;
47             p.outer().push_back(point(x, y));
48         }
49 
50         // add polygon
51         polygons.push_back(p);
52     }
53 
54     // display polygons
55     std::cout << "generated polygons:" << std::endl;
56     BOOST_FOREACH(polygon const& p, polygons)
57         std::cout << bg::wkt<polygon>(p) << std::endl;
58 
59     // create the rtree using default constructor
60     bgi::rtree< value, bgi::rstar<16, 4> > rtree;
61 
62     // fill the spatial index
63     for ( unsigned i = 0 ; i < polygons.size() ; ++i )
64     {
65         // calculate polygon bounding box
66         box b = bg::return_envelope<box>(polygons[i]);
67         // insert new value
68         rtree.insert(std::make_pair(b, i));
69     }
70 
71     // find values intersecting some area defined by a box
72     box query_box(point(0, 0), point(5, 5));
73     std::vector<value> result_s;
74     rtree.query(bgi::intersects(query_box), std::back_inserter(result_s));
75 
76     // find 5 nearest values to a point
77     std::vector<value> result_n;
78     rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n));
79 
80     // note: in Boost.Geometry the WKT representation of a box is polygon
81 
82     // note: the values store the bounding boxes of polygons
83     // the polygons aren't used for querying but are printed
84 
85     // display results
86     std::cout << "spatial query box:" << std::endl;
87     std::cout << bg::wkt<box>(query_box) << std::endl;
88     std::cout << "spatial query result:" << std::endl;
89     BOOST_FOREACH(value const& v, result_s)
90         std::cout << bg::wkt<polygon>(polygons[v.second]) << std::endl;
91 
92     std::cout << "knn query point:" << std::endl;
93     std::cout << bg::wkt<point>(point(0, 0)) << std::endl;
94     std::cout << "knn query result:" << std::endl;
95     BOOST_FOREACH(value const& v, result_n)
96         std::cout << bg::wkt<polygon>(polygons[v.second]) << std::endl;
97 
98     return 0;
99 }
100 
101 //]
102