serialize.cpp 5.2 KB

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