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-2021 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> 38 #include <osmium/handler/check_order.hpp> 39 #include <osmium/io/file.hpp> 40 #include <osmium/io/reader.hpp> 41 #include <osmium/memory/buffer.hpp> 42 #include <osmium/osm/entity_bits.hpp> 43 #include <osmium/util/progress_bar.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 55 /** 56 * This is a handler class used for the second pass of relation 57 * managers. An object of this class is instantiated as a member 58 * of the Manager and used to re-direct all calls to the handler 59 * to the "parent" manager. 60 * 61 * @tparam TManager The manager we want to call functions on. 62 */ 63 template <typename TManager> 64 class SecondPassHandler : public osmium::handler::Handler { 65 66 TManager& m_manager; 67 68 public: 69 SecondPassHandler(TManager & manager)70 explicit SecondPassHandler(TManager& manager) noexcept : 71 m_manager(manager) { 72 } 73 74 /** 75 * Overwrites the function in the handler parent class. 76 */ node(const osmium::Node & node)77 void node(const osmium::Node& node) { 78 m_manager.handle_node(node); 79 } 80 81 /** 82 * Overwrites the function in the handler parent class. 83 */ way(const osmium::Way & way)84 void way(const osmium::Way& way) { 85 m_manager.handle_way(way); 86 } 87 88 /** 89 * Overwrites the function in the handler parent class. 90 */ relation(const osmium::Relation & relation)91 void relation(const osmium::Relation& relation) { 92 m_manager.handle_relation(relation); 93 } 94 95 /** 96 * Overwrites the function in the handler parent class. 97 * 98 * Calls the flush_output() function on the manager. 99 */ flush()100 void flush() { 101 m_manager.flush_output(); 102 } 103 104 }; // class SecondPassHandler 105 106 /** 107 * Read relations from file and feed them into all the managers 108 * specified as parameters. Opens an osmium::io::Reader internally 109 * with the file parameter. 110 * 111 * After the file is read, the prepare_for_lookup() function is called 112 * on all the managers making them ready for querying the data they 113 * have stored. 114 * 115 * @tparam TManager Any number of relation manager types. 116 * @param file The file that should be opened with an osmium::io::Reader. 117 * @param managers Relation managers we want the relations to be sent 118 * to. 119 */ 120 template <typename ...TManager> read_relations(const osmium::io::File & file,TManager &...managers)121 void read_relations(const osmium::io::File& file, TManager& ...managers) { 122 static_assert(sizeof...(TManager) > 0, "Need at least one manager as parameter."); 123 osmium::io::Reader reader{file, osmium::osm_entity_bits::relation}; 124 osmium::apply(reader, managers...); 125 reader.close(); 126 (void)std::initializer_list<int>{(managers.prepare_for_lookup(), 0)...}; 127 } 128 129 /** 130 * Read relations from file and feed them into all the managers 131 * specified as parameters. Opens an osmium::io::Reader internally 132 * with the file parameter. 133 * 134 * After the file is read, the prepare_for_lookup() function is called 135 * on all the managers making them ready for querying the data they 136 * have stored. 137 * 138 * @tparam TManager Any number of relation manager types. 139 * @param progress_bar Reference to osmium::ProgressBar object that 140 * will be updated while reading the data. 141 * @param file The file that should be opened with an osmium::io::Reader. 142 * @param managers Relation managers we want the relations to be sent 143 * to. 144 */ 145 template <typename ...TManager> read_relations(osmium::ProgressBar & progress_bar,const osmium::io::File & file,TManager &...managers)146 void read_relations(osmium::ProgressBar& progress_bar, const osmium::io::File& file, TManager& ...managers) { 147 static_assert(sizeof...(TManager) > 0, "Need at least one manager as parameter."); 148 osmium::io::Reader reader{file, osmium::osm_entity_bits::relation}; 149 while (auto buffer = reader.read()) { 150 progress_bar.update(reader.offset()); 151 osmium::apply(buffer, managers...); 152 } 153 reader.close(); 154 (void)std::initializer_list<int>{(managers.prepare_for_lookup(), 0)...}; 155 progress_bar.file_done(file.size()); 156 } 157 158 /** 159 * Struct for memory usage numbers returned by various relations 160 * managers from the used_memory() function. 161 */ 162 struct relations_manager_memory_usage { 163 std::size_t relations_db; 164 std::size_t members_db; 165 std::size_t stash; 166 }; 167 168 /** 169 * Prints relations managers memory usage numbers to the specified 170 * stream. 171 * 172 * @tparam TStream Output stream type (like std::cout, std::cerr, or 173 * osmium::VerboseOutput). 174 * @param stream Reference to stream where the output should go. 175 * @param mu Memory usage data as returned by the used_memory() 176 * functions of various relations managers. 177 */ 178 template <typename TStream> print_used_memory(TStream & stream,const relations_manager_memory_usage & mu)179 void print_used_memory(TStream& stream, const relations_manager_memory_usage& mu) { 180 const auto total = mu.relations_db + mu.members_db + mu.stash; 181 182 stream << " relations: " << std::setw(8) << (mu.relations_db / 1024) << " kB\n" 183 << " members: " << std::setw(8) << (mu.members_db / 1024) << " kB\n" 184 << " stash: " << std::setw(8) << (mu.stash / 1024) << " kB\n" 185 << " total: " << std::setw(8) << (total / 1024) << " kB\n" 186 << " ======================\n"; 187 } 188 189 } // namespace relations 190 191 } // namespace osmium 192 193 #endif // OSMIUM_RELATIONS_MANAGER_UTIL_HPP 194