• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry Index
2 // Additional tests
3 
4 // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
5 
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 #include <iostream>
11 #include <fstream>
12 
13 #define BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
14 
15 #include <boost/geometry.hpp>
16 #include <boost/geometry/index/rtree.hpp>
17 #include <boost/geometry/index/detail/rtree/utilities/statistics.hpp>
18 
19 #include <boost/archive/binary_oarchive.hpp>
20 #include <boost/archive/binary_iarchive.hpp>
21 #include <boost/archive/xml_oarchive.hpp>
22 #include <boost/archive/xml_iarchive.hpp>
23 #include <boost/serialization/vector.hpp>
24 
25 #include <boost/foreach.hpp>
26 #include <boost/timer.hpp>
27 
28 template <typename T, size_t I = 0, size_t S = boost::tuples::length<T>::value>
29 struct print_tuple
30 {
31     template <typename Os>
applyprint_tuple32     static inline Os & apply(Os & os, T const& t)
33     {
34         os << boost::get<I>(t) << ", ";
35         return print_tuple<T, I+1>::apply(os, t);
36     }
37 };
38 
39 template <typename T, size_t S>
40 struct print_tuple<T, S, S>
41 {
42     template <typename Os>
applyprint_tuple43     static inline Os & apply(Os & os, T const&)
44     {
45         return os;
46     }
47 };
48 
main()49 int main()
50 {
51     namespace bg = boost::geometry;
52     namespace bgi = bg::index;
53 
54     typedef boost::tuple<std::size_t, std::size_t, std::size_t, std::size_t, std::size_t, std::size_t> S;
55 
56     typedef bg::model::point<double, 2, bg::cs::cartesian> P;
57     typedef bg::model::box<P> B;
58     typedef B V;
59     //typedef bgi::rtree<V, bgi::linear<16> > RT;
60     //typedef bgi::rtree<V, bgi::quadratic<8, 3> > RT;
61     //typedef bgi::rtree<V, bgi::rstar<8, 3> > RT;
62     typedef bgi::rtree<V, bgi::dynamic_linear > RT;
63 
64     //RT tree;
65     RT tree(bgi::dynamic_linear(16));
66     std::vector<V> vect;
67 
68     boost::timer t;
69 
70     //insert values
71     {
72         for ( double x = 0 ; x < 1000 ; x += 1 )
73             for ( double y = 0 ; y < 1000 ; y += 1 )
74                 vect.push_back(B(P(x, y), P(x+0.5, y+0.5)));
75         RT tmp(vect, tree.parameters());
76         tree = boost::move(tmp);
77     }
78     B q(P(5, 5), P(6, 6));
79     S s;
80 
81     std::cout << "vector and tree created in: " << t.elapsed() << std::endl;
82 
83     print_tuple<S>::apply(std::cout, bgi::detail::rtree::utilities::statistics(tree)) << std::endl;
84     std::cout << boost::get<0>(s) << std::endl;
85     BOOST_FOREACH(V const& v, tree | bgi::adaptors::queried(bgi::intersects(q)))
86         std::cout << bg::wkt<V>(v) << std::endl;
87 
88     // save
89     {
90         std::ofstream ofs("serialized_vector.bin", std::ios::binary | std::ios::trunc);
91         boost::archive::binary_oarchive oa(ofs);
92         t.restart();
93         oa << vect;
94         std::cout << "vector saved to bin in: " << t.elapsed() << std::endl;
95     }
96     {
97         std::ofstream ofs("serialized_tree.bin", std::ios::binary | std::ios::trunc);
98         boost::archive::binary_oarchive oa(ofs);
99         t.restart();
100         oa << tree;
101         std::cout << "tree saved to bin in: " << t.elapsed() << std::endl;
102     }
103     {
104         std::ofstream ofs("serialized_tree.xml", std::ios::trunc);
105         boost::archive::xml_oarchive oa(ofs);
106         t.restart();
107         oa << boost::serialization::make_nvp("rtree", tree);
108         std::cout << "tree saved to xml in: " << t.elapsed() << std::endl;
109     }
110 
111     t.restart();
112     vect.clear();
113     std::cout << "vector cleared in: " << t.elapsed() << std::endl;
114 
115     t.restart();
116     tree.clear();
117     std::cout << "tree cleared in: " << t.elapsed() << std::endl;
118 
119     // load
120 
121     {
122         std::ifstream ifs("serialized_vector.bin", std::ios::binary);
123         boost::archive::binary_iarchive ia(ifs);
124         t.restart();
125         ia >> vect;
126         std::cout << "vector loaded from bin in: " << t.elapsed() << std::endl;
127         t.restart();
128         RT tmp(vect, tree.parameters());
129         tree = boost::move(tmp);
130         std::cout << "tree rebuilt from vector in: " << t.elapsed() << std::endl;
131     }
132 
133     t.restart();
134     tree.clear();
135     std::cout << "tree cleared in: " << t.elapsed() << std::endl;
136 
137     {
138         std::ifstream ifs("serialized_tree.bin", std::ios::binary);
139         boost::archive::binary_iarchive ia(ifs);
140         t.restart();
141         ia >> tree;
142         std::cout << "tree loaded from bin in: " << t.elapsed() << std::endl;
143     }
144 
145     std::cout << "loaded from bin" << std::endl;
146     print_tuple<S>::apply(std::cout, bgi::detail::rtree::utilities::statistics(tree)) << std::endl;
147     BOOST_FOREACH(V const& v, tree | bgi::adaptors::queried(bgi::intersects(q)))
148         std::cout << bg::wkt<V>(v) << std::endl;
149 
150     t.restart();
151     tree.clear();
152     std::cout << "tree cleared in: " << t.elapsed() << std::endl;
153 
154     {
155         std::ifstream ifs("serialized_tree.xml");
156         boost::archive::xml_iarchive ia(ifs);
157         t.restart();
158         ia >> boost::serialization::make_nvp("rtree", tree);
159         std::cout << "tree loaded from xml in: " << t.elapsed() << std::endl;
160     }
161 
162     std::cout << "loaded from xml" << std::endl;
163     print_tuple<S>::apply(std::cout, bgi::detail::rtree::utilities::statistics(tree)) << std::endl;
164     BOOST_FOREACH(V const& v, tree | bgi::adaptors::queried(bgi::intersects(q)))
165         std::cout << bg::wkt<V>(v) << std::endl;
166 
167     return 0;
168 }
169