• 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_quickstart
12 
13 //[rtree_quickstart_include
14 
15 #include <boost/geometry.hpp>
16 #include <boost/geometry/geometries/point.hpp>
17 #include <boost/geometry/geometries/box.hpp>
18 
19 #include <boost/geometry/index/rtree.hpp>
20 
21 // to store queries results
22 #include <vector>
23 
24 // just for output
25 #include <iostream>
26 #include <boost/foreach.hpp>
27 
28 namespace bg = boost::geometry;
29 namespace bgi = boost::geometry::index;
30 //]
31 
main()32 int main()
33 {
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;
38     //]
39 
40     //[rtree_quickstart_create
41     // create the rtree using default constructor
42     bgi::rtree< value, bgi::quadratic<16> > rtree;
43     //]
44 
45     //[rtree_quickstart_insert
46     // create some values
47     for ( unsigned i = 0 ; i < 10 ; ++i )
48     {
49         // create a box
50         box b(point(i + 0.0f, i + 0.0f), point(i + 0.5f, i + 0.5f));
51         // insert new value
52         rtree.insert(std::make_pair(b, i));
53     }
54     //]
55 
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));
61     //]
62 
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));
67     //]
68 
69     // note: in Boost.Geometry WKT representation of a box is polygon
70 
71     //[rtree_quickstart_output
72     // display results
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;
78 
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;
84     //]
85 
86     return 0;
87 }
88 
89 //]
90