-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathinput.cpp
More file actions
350 lines (285 loc) · 9.5 KB
/
input.cpp
File metadata and controls
350 lines (285 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include <memory>
#include <queue>
#include <stdexcept>
#include <vector>
#include <osmium/io/any_input.hpp>
#include <osmium/visitor.hpp>
#include "format.hpp"
#include "input.hpp"
#include "logging.hpp"
#include "osmdata.hpp"
#include "progress-display.hpp"
type_id check_input(type_id const &last, type_id curr)
{
if (curr.id < 0) {
throw fmt_error("Negative OSM object ids are not allowed: {} id {}.",
osmium::item_type_to_name(curr.type), curr.id);
}
if (last.type == curr.type) {
if (last.id < curr.id) {
return curr;
}
if (last.id > curr.id) {
throw fmt_error("Input data is not ordered: {} id {} after {}.",
osmium::item_type_to_name(last.type), curr.id,
last.id);
}
throw fmt_error("Input data is not ordered:"
" {} id {} appears more than once.",
osmium::item_type_to_name(last.type), curr.id);
}
if (item_type_to_nwr_index(last.type) <=
item_type_to_nwr_index(curr.type)) {
return curr;
}
throw fmt_error("Input data is not ordered: {} after {}.",
osmium::item_type_to_name(curr.type),
osmium::item_type_to_name(last.type));
}
type_id check_input(type_id const &last, osmium::OSMObject const &object)
{
return check_input(last, {object.type(), object.id()});
}
namespace {
/**
* A data source is where we get the OSM objects from, one at a time. It
* wraps the osmium::io::Reader.
*/
class data_source_t
{
public:
explicit data_source_t(osmium::io::File const &file)
: m_reader(std::make_unique<osmium::io::Reader>(file))
{
get_next_nonempty_buffer();
m_last = check_input(m_last, *m_it);
}
bool empty() const noexcept { return !m_buffer; }
bool next()
{
assert(!empty());
++m_it;
while (m_it == m_end) {
if (!get_next_nonempty_buffer()) {
return false;
}
}
m_last = check_input(m_last, *m_it);
return true;
}
osmium::OSMObject *get() noexcept
{
assert(!empty());
return &*m_it;
}
std::size_t offset() const noexcept { return m_reader->offset(); }
void close()
{
m_reader->close();
m_reader.reset();
}
private:
bool get_next_nonempty_buffer()
{
while ((m_buffer = m_reader->read())) {
m_it = m_buffer.begin<osmium::OSMObject>();
m_end = m_buffer.end<osmium::OSMObject>();
if (m_it != m_end) {
return true;
}
}
return false;
}
using iterator = osmium::memory::Buffer::t_iterator<osmium::OSMObject>;
std::unique_ptr<osmium::io::Reader> m_reader;
osmium::memory::Buffer m_buffer;
iterator m_it;
iterator m_end;
type_id m_last = {osmium::item_type::node, 0};
}; // class data_source_t
/**
* A element in a priority queue of OSM objects. Holds a pointer to the OSM
* object as well as a pointer to the source the OSM object came from.
*/
class queue_element_t
{
public:
queue_element_t(osmium::OSMObject *object, data_source_t *source) noexcept
: m_object(object), m_source(source)
{}
osmium::OSMObject const &object() const noexcept { return *m_object; }
osmium::OSMObject &object() noexcept { return *m_object; }
data_source_t *data_source() const noexcept { return m_source; }
friend bool operator<(queue_element_t const &lhs,
queue_element_t const &rhs) noexcept
{
// This is needed for the priority queue. We want objects with smaller
// id (and earlier versions of the same object) to come first, but
// the priority queue expects largest first. So we need to reverse the
// comparison here.
return lhs.object() > rhs.object();
}
friend bool operator==(queue_element_t const &lhs,
queue_element_t const &rhs) noexcept
{
return lhs.object().type() == rhs.object().type() &&
lhs.object().id() == rhs.object().id();
}
friend bool operator!=(queue_element_t const &lhs,
queue_element_t const &rhs) noexcept
{
return !(lhs == rhs);
}
private:
osmium::OSMObject *m_object;
data_source_t *m_source;
}; // class queue_element_t
class input_context_t
{
public:
input_context_t(osmdata_t *osmdata, progress_display_t *progress,
bool append)
: m_osmdata(osmdata), m_progress(progress), m_append(append)
{
assert(osmdata);
assert(progress);
}
void apply(osmium::OSMObject *object)
{
if (!m_append && object->deleted()) {
throw std::runtime_error{"Input file contains deleted objects but "
"you are not in append mode."};
}
if (m_last_type != object->type()) {
if (m_last_type == osmium::item_type::node) {
m_osmdata->after_nodes();
m_progress->start_way_counter();
}
if (object->type() == osmium::item_type::relation) {
m_osmdata->after_ways();
m_progress->start_relation_counter();
}
m_last_type = object->type();
}
osmium::apply_item(*object, *m_osmdata, *m_progress);
}
void eof()
{
switch (m_last_type) {
case osmium::item_type::node:
m_osmdata->after_nodes();
// fallthrough
case osmium::item_type::way:
m_osmdata->after_ways();
break;
default:
break;
}
m_osmdata->after_relations();
m_progress->print_summary();
}
private:
osmdata_t *m_osmdata;
progress_display_t *m_progress;
osmium::item_type m_last_type = osmium::item_type::node;
bool m_append;
}; // class input_context_t
file_info process_single_file(osmium::io::File const &file, osmdata_t *osmdata,
progress_display_t *progress, bool append)
{
file_info finfo;
osmium::io::Reader reader{file};
finfo.header = reader.header();
type_id last{osmium::item_type::node, 0};
input_context_t ctx{osmdata, progress, append};
while (osmium::memory::Buffer buffer = reader.read()) {
for (auto &object : buffer.select<osmium::OSMObject>()) {
last = check_input(last, object);
ctx.apply(&object);
if (object.timestamp() > finfo.last_timestamp) {
finfo.last_timestamp = object.timestamp();
}
}
}
ctx.eof();
reader.close();
return finfo;
}
file_info process_multiple_files(std::vector<osmium::io::File> const &files,
osmdata_t *osmdata,
progress_display_t *progress, bool append)
{
file_info finfo;
std::vector<data_source_t> data_sources;
data_sources.reserve(files.size());
std::priority_queue<queue_element_t> queue;
for (osmium::io::File const &file : files) {
data_sources.emplace_back(file);
if (!data_sources.back().empty()) {
queue.emplace(data_sources.back().get(), &data_sources.back());
}
}
input_context_t ctx{osmdata, progress, append};
while (!queue.empty()) {
auto element = queue.top();
queue.pop();
if (queue.empty() || element != queue.top()) {
ctx.apply(&element.object());
if (element.object().timestamp() > finfo.last_timestamp) {
finfo.last_timestamp = element.object().timestamp();
}
}
auto *source = element.data_source();
if (source->next()) {
queue.emplace(source->get(), source);
}
}
ctx.eof();
for (auto &data_source : data_sources) {
data_source.close();
}
return finfo;
}
} // anonymous namespace
std::vector<osmium::io::File>
prepare_input_files(std::vector<std::string> const &input_files,
std::string const &input_format, bool append)
{
std::vector<osmium::io::File> files;
for (auto const &filename : input_files) {
osmium::io::File const file{filename, input_format};
if (file.format() == osmium::io::file_format::unknown) {
if (input_format.empty()) {
throw fmt_error("Cannot detect file format for '{}'."
" Try using -r.",
filename);
}
throw fmt_error("Unknown file format '{}'.", input_format);
}
if (!append && file.has_multiple_object_versions()) {
throw std::runtime_error{
"Reading an OSM change file only works in append mode."};
}
log_debug("Reading file: {}", filename);
files.emplace_back(file);
}
return files;
}
file_info process_files(std::vector<osmium::io::File> const &files,
osmdata_t *osmdata, bool append, bool show_progress)
{
assert(osmdata);
progress_display_t progress{show_progress};
if (files.size() == 1) {
return process_single_file(files.front(), osmdata, &progress, append);
}
return process_multiple_files(files, osmdata, &progress, append);
}