Libosmium  2.18.0
Fast and flexible C++ library for working with OpenStreetMap data
manager_util.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_RELATIONS_MANAGER_UTIL_HPP
2 #define OSMIUM_RELATIONS_MANAGER_UTIL_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2022 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <osmium/fwd.hpp>
37 #include <osmium/handler.hpp>
39 #include <osmium/io/file.hpp>
40 #include <osmium/io/reader.hpp>
41 #include <osmium/memory/buffer.hpp>
44 #include <osmium/visitor.hpp>
45 
46 #include <cstddef>
47 #include <initializer_list>
48 #include <iomanip>
49 #include <utility>
50 
51 namespace osmium {
52 
53  namespace relations {
54 
63  template <typename TManager>
65 
66  TManager& m_manager;
67 
68  public:
69 
70  explicit SecondPassHandler(TManager& manager) noexcept :
71  m_manager(manager) {
72  }
73 
77  void node(const osmium::Node& node) {
78  m_manager.handle_node(node);
79  }
80 
84  void way(const osmium::Way& way) {
85  m_manager.handle_way(way);
86  }
87 
92  m_manager.handle_relation(relation);
93  }
94 
100  void flush() {
101  m_manager.flush_output();
102  }
103 
104  }; // class SecondPassHandler
105 
120  template <typename ...TManager>
121  void read_relations(const osmium::io::File& file, TManager& ...managers) {
123  osmium::apply(reader, managers...);
124  reader.close();
125  (void)std::initializer_list<int>{(managers.prepare_for_lookup(), 0)...};
126  }
127 
144  template <typename ...TManager>
145  void read_relations(osmium::ProgressBar& progress_bar, const osmium::io::File& file, TManager& ...managers) {
147  while (auto buffer = reader.read()) {
148  progress_bar.update(reader.offset());
149  osmium::apply(buffer, managers...);
150  }
151  reader.close();
152  (void)std::initializer_list<int>{(managers.prepare_for_lookup(), 0)...};
153  progress_bar.file_done(file.size());
154  }
155 
161  std::size_t relations_db;
162  std::size_t members_db;
163  std::size_t stash;
164  };
165 
176  template <typename TStream>
177  void print_used_memory(TStream& stream, const relations_manager_memory_usage& mu) {
178  const auto total = mu.relations_db + mu.members_db + mu.stash;
179 
180  stream << " relations: " << std::setw(8) << (mu.relations_db / 1024) << " kB\n"
181  << " members: " << std::setw(8) << (mu.members_db / 1024) << " kB\n"
182  << " stash: " << std::setw(8) << (mu.stash / 1024) << " kB\n"
183  << " total: " << std::setw(8) << (total / 1024) << " kB\n"
184  << " ======================\n";
185  }
186 
187  } // namespace relations
188 
189 } // namespace osmium
190 
191 #endif // OSMIUM_RELATIONS_MANAGER_UTIL_HPP
Definition: node.hpp:48
Definition: progress_bar.hpp:46
void file_done(std::size_t file_size)
Definition: progress_bar.hpp:166
void update(std::size_t current_size)
Definition: progress_bar.hpp:150
Definition: relation.hpp:161
Definition: way.hpp:72
Definition: handler.hpp:71
Definition: file.hpp:72
Definition: reader.hpp:90
Definition: manager_util.hpp:64
void relation(const osmium::Relation &relation)
Definition: manager_util.hpp:91
void node(const osmium::Node &node)
Definition: manager_util.hpp:77
void way(const osmium::Way &way)
Definition: manager_util.hpp:84
SecondPassHandler(TManager &manager) noexcept
Definition: manager_util.hpp:70
TManager & m_manager
Definition: manager_util.hpp:66
void flush()
Definition: manager_util.hpp:100
@ relation
Definition: entity_bits.hpp:70
void print_used_memory(TStream &stream, const relations_manager_memory_usage &mu)
Definition: manager_util.hpp:177
void read_relations(const osmium::io::File &file, TManager &...managers)
Definition: manager_util.hpp:121
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
void apply(TIterator it, TIterator end, THandlers &&... handlers)
Definition: visitor.hpp:326
std::size_t members_db
Definition: manager_util.hpp:162
std::size_t relations_db
Definition: manager_util.hpp:161
std::size_t stash
Definition: manager_util.hpp:163