|
| 1 | + |
| 2 | +#include <memory> |
| 3 | +#include <queue> |
| 4 | +#include <stdexcept> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include <osmium/io/any_input.hpp> |
| 8 | +#include <osmium/visitor.hpp> |
| 9 | + |
| 10 | +#include "format.hpp" |
| 11 | +#include "input.hpp" |
| 12 | +#include "osmdata.hpp" |
| 13 | +#include "progress-display.hpp" |
| 14 | + |
| 15 | +type_id_version check_input(type_id_version const &last, type_id_version curr) |
| 16 | +{ |
| 17 | + if (curr.id < 0) { |
| 18 | + throw std::runtime_error{ |
| 19 | + "Negative OSM object ids are not allowed: {} id {}."_format( |
| 20 | + osmium::item_type_to_name(curr.type), curr.id)}; |
| 21 | + } |
| 22 | + |
| 23 | + if (last.type == curr.type) { |
| 24 | + if (last.id < curr.id) { |
| 25 | + return curr; |
| 26 | + } |
| 27 | + |
| 28 | + if (last.id > curr.id) { |
| 29 | + throw std::runtime_error{ |
| 30 | + "Input data is not ordered: {} id {} after {}."_format( |
| 31 | + osmium::item_type_to_name(last.type), curr.id, last.id)}; |
| 32 | + } |
| 33 | + |
| 34 | + if (last.version < curr.version) { |
| 35 | + return curr; |
| 36 | + } |
| 37 | + |
| 38 | + throw std::runtime_error{ |
| 39 | + "Input data is not ordered: {} id {} version {} after {}."_format( |
| 40 | + osmium::item_type_to_name(last.type), curr.id, curr.version, |
| 41 | + last.version)}; |
| 42 | + } |
| 43 | + |
| 44 | + if (item_type_to_nwr_index(last.type) <= |
| 45 | + item_type_to_nwr_index(curr.type)) { |
| 46 | + return curr; |
| 47 | + } |
| 48 | + |
| 49 | + throw std::runtime_error{"Input data is not ordered: {} after {}."_format( |
| 50 | + osmium::item_type_to_name(curr.type), |
| 51 | + osmium::item_type_to_name(last.type))}; |
| 52 | +} |
| 53 | + |
| 54 | +type_id_version check_input(type_id_version const &last, |
| 55 | + osmium::OSMObject const &object) |
| 56 | +{ |
| 57 | + return check_input(last, {object.type(), object.id(), object.version()}); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * A data source is where we get the OSM objects from, one at a time. It |
| 62 | + * wraps the osmium::io::Reader. |
| 63 | + */ |
| 64 | +class data_source_t |
| 65 | +{ |
| 66 | +public: |
| 67 | + explicit data_source_t(osmium::io::File const &file) |
| 68 | + : m_reader(new osmium::io::Reader{file}) |
| 69 | + { |
| 70 | + get_next_nonempty_buffer(); |
| 71 | + m_last = check_input(m_last, *m_it); |
| 72 | + } |
| 73 | + |
| 74 | + bool empty() const noexcept { return !m_buffer; } |
| 75 | + |
| 76 | + bool next() |
| 77 | + { |
| 78 | + assert(!empty()); |
| 79 | + ++m_it; |
| 80 | + |
| 81 | + while (m_it == m_end) { |
| 82 | + if (!get_next_nonempty_buffer()) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + m_last = check_input(m_last, *m_it); |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + osmium::OSMObject *get() noexcept |
| 92 | + { |
| 93 | + assert(!empty()); |
| 94 | + return &*m_it; |
| 95 | + } |
| 96 | + |
| 97 | + std::size_t offset() const noexcept { return m_reader->offset(); } |
| 98 | + |
| 99 | + void close() |
| 100 | + { |
| 101 | + m_reader->close(); |
| 102 | + m_reader.reset(); |
| 103 | + } |
| 104 | + |
| 105 | +private: |
| 106 | + bool get_next_nonempty_buffer() |
| 107 | + { |
| 108 | + while ((m_buffer = m_reader->read())) { |
| 109 | + m_it = m_buffer.begin<osmium::OSMObject>(); |
| 110 | + m_end = m_buffer.end<osmium::OSMObject>(); |
| 111 | + if (m_it != m_end) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + using iterator = osmium::memory::Buffer::t_iterator<osmium::OSMObject>; |
| 119 | + |
| 120 | + std::unique_ptr<osmium::io::Reader> m_reader; |
| 121 | + osmium::memory::Buffer m_buffer{}; |
| 122 | + iterator m_it{}; |
| 123 | + iterator m_end{}; |
| 124 | + type_id_version m_last = {osmium::item_type::node, 0, 0}; |
| 125 | + |
| 126 | +}; // class data_source_t |
| 127 | + |
| 128 | +/** |
| 129 | + * A element in a priority queue of OSM objects. Holds a pointer to the OSM |
| 130 | + * object as well as a pointer to the source the OSM object came from. |
| 131 | + */ |
| 132 | +class queue_element_t |
| 133 | +{ |
| 134 | +public: |
| 135 | + queue_element_t(osmium::OSMObject *object, data_source_t *source) noexcept |
| 136 | + : m_object(object), m_source(source) |
| 137 | + {} |
| 138 | + |
| 139 | + osmium::OSMObject const &object() const noexcept { return *m_object; } |
| 140 | + |
| 141 | + osmium::OSMObject &object() noexcept { return *m_object; } |
| 142 | + |
| 143 | + data_source_t *data_source() const noexcept { return m_source; } |
| 144 | + |
| 145 | + friend bool operator<(queue_element_t const &lhs, |
| 146 | + queue_element_t const &rhs) noexcept |
| 147 | + { |
| 148 | + // This is needed for the priority queue. We want objects with smaller |
| 149 | + // id (and earlier versions of the same object) to come first, but |
| 150 | + // the priority queue expects largest first. So we need to reverse the |
| 151 | + // comparison here. |
| 152 | + return lhs.object() > rhs.object(); |
| 153 | + } |
| 154 | + |
| 155 | + friend bool operator==(queue_element_t const &lhs, |
| 156 | + queue_element_t const &rhs) noexcept |
| 157 | + { |
| 158 | + return lhs.object().type() == rhs.object().type() && |
| 159 | + lhs.object().id() == rhs.object().id(); |
| 160 | + } |
| 161 | + |
| 162 | + friend bool operator!=(queue_element_t const &lhs, |
| 163 | + queue_element_t const &rhs) noexcept |
| 164 | + { |
| 165 | + return !(lhs == rhs); |
| 166 | + } |
| 167 | + |
| 168 | +private: |
| 169 | + osmium::OSMObject *m_object; |
| 170 | + data_source_t *m_source; |
| 171 | + |
| 172 | +}; // class queue_element_t |
| 173 | + |
| 174 | +void process_file(osmium::io::File const &file, osmdata_t &osmdata, |
| 175 | + progress_display_t &progress, bool append) |
| 176 | +{ |
| 177 | + osmium::io::Reader reader{file}; |
| 178 | + type_id_version last{osmium::item_type::node, 0, 0}; |
| 179 | + |
| 180 | + while (osmium::memory::Buffer buffer = reader.read()) { |
| 181 | + for (auto &object : buffer.select<osmium::OSMObject>()) { |
| 182 | + last = check_input(last, object); |
| 183 | + if (!append && object.deleted()) { |
| 184 | + throw std::runtime_error{ |
| 185 | + "Input file contains deleted objects but " |
| 186 | + "you are not in append mode."}; |
| 187 | + } |
| 188 | + osmium::apply_item(object, osmdata, progress); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + osmdata.flush(); |
| 193 | + |
| 194 | + reader.close(); |
| 195 | +} |
| 196 | + |
| 197 | +void process_files(std::vector<osmium::io::File> const &files, |
| 198 | + osmdata_t &osmdata, progress_display_t &progress, |
| 199 | + bool append) |
| 200 | +{ |
| 201 | + if (files.size() == 1) { |
| 202 | + process_file(files.front(), osmdata, progress, append); |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + std::vector<data_source_t> data_sources; |
| 207 | + data_sources.reserve(files.size()); |
| 208 | + |
| 209 | + std::priority_queue<queue_element_t> queue; |
| 210 | + |
| 211 | + for (osmium::io::File const &file : files) { |
| 212 | + data_sources.emplace_back(file); |
| 213 | + |
| 214 | + if (!data_sources.back().empty()) { |
| 215 | + queue.emplace(data_sources.back().get(), &data_sources.back()); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + while (!queue.empty()) { |
| 220 | + auto element = queue.top(); |
| 221 | + queue.pop(); |
| 222 | + if (queue.empty() || element != queue.top()) { |
| 223 | + if (!append && element.object().deleted()) { |
| 224 | + throw std::runtime_error{ |
| 225 | + "Input file contains deleted objects but " |
| 226 | + "you are not in append mode."}; |
| 227 | + } |
| 228 | + osmium::apply_item(element.object(), osmdata, progress); |
| 229 | + } |
| 230 | + |
| 231 | + auto *source = element.data_source(); |
| 232 | + if (source->next()) { |
| 233 | + queue.emplace(source->get(), source); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + osmdata.flush(); |
| 238 | + |
| 239 | + for (auto &data_source : data_sources) { |
| 240 | + data_source.close(); |
| 241 | + } |
| 242 | +} |
0 commit comments