Skip to content

Commit 7f782ea

Browse files
committed
Fold functionality of input_handler_t into osmdata_t
1 parent c7ab81c commit 7f782ea

File tree

5 files changed

+117
-169
lines changed

5 files changed

+117
-169
lines changed

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ set(osm2pgsql_lib_SOURCES
66
expire-tiles.cpp
77
gazetteer-style.cpp
88
geometry-processor.cpp
9-
input-handler.cpp
109
logging.cpp
1110
middle-pgsql.cpp
1211
middle-ram.cpp

src/input-handler.cpp

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/input-handler.hpp

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/osmdata.cpp

Lines changed: 94 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "db-copy.hpp"
1616
#include "format.hpp"
17-
#include "input-handler.hpp"
1817
#include "logging.hpp"
1918
#include "middle.hpp"
2019
#include "options.hpp"
@@ -188,8 +187,9 @@ osmdata_t::osmdata_t(std::unique_ptr<dependency_manager_t> dependency_manager,
188187
options_t const &options)
189188
: m_dependency_manager(std::move(dependency_manager)), m_mid(std::move(mid)),
190189
m_outs(std::move(outs)), m_conninfo(options.database_options.conninfo()),
191-
m_num_procs(options.num_procs), m_append(options.append),
192-
m_droptemp(options.droptemp), m_parallel_indexing(options.parallel_indexing),
190+
m_bbox(options.bbox), m_num_procs(options.num_procs),
191+
m_append(options.append), m_droptemp(options.droptemp),
192+
m_parallel_indexing(options.parallel_indexing),
193193
m_with_extra_attrs(options.extra_attributes),
194194
m_with_forward_dependencies(options.with_forward_dependencies)
195195
{
@@ -211,6 +211,89 @@ slim_middle_t &osmdata_t::slim_middle() const noexcept
211211
return *slim;
212212
}
213213

214+
void osmdata_t::node(osmium::Node const &node)
215+
{
216+
if (m_type != osmium::item_type::node) {
217+
m_type = osmium::item_type::node;
218+
flush();
219+
}
220+
221+
if (node.deleted()) {
222+
if (!m_append) {
223+
throw std::runtime_error{"Input file contains deleted objects but "
224+
"you are not in append mode."};
225+
}
226+
node_delete(node.id());
227+
} else {
228+
// if the node is not valid, then node.location.lat/lon() can throw.
229+
// we probably ought to treat invalid locations as if they were
230+
// deleted and ignore them.
231+
if (!node.location().valid()) {
232+
log_warn("Ignored invalid location on node {} (version {})",
233+
node.id(), node.version());
234+
return;
235+
}
236+
237+
if (!m_bbox.valid() || m_bbox.contains(node.location())) {
238+
if (m_append) {
239+
node_modify(node);
240+
} else {
241+
node_add(node);
242+
}
243+
m_progress.add_node(node.id());
244+
}
245+
}
246+
}
247+
248+
void osmdata_t::way(osmium::Way &way)
249+
{
250+
if (m_type != osmium::item_type::way) {
251+
m_type = osmium::item_type::way;
252+
flush();
253+
}
254+
255+
if (way.deleted()) {
256+
if (!m_append) {
257+
throw std::runtime_error{"Input file contains deleted objects but "
258+
"you are not in append mode."};
259+
}
260+
way_delete(way.id());
261+
} else {
262+
if (m_append) {
263+
way_modify(&way);
264+
} else {
265+
way_add(&way);
266+
}
267+
}
268+
m_progress.add_way(way.id());
269+
}
270+
271+
void osmdata_t::relation(osmium::Relation const &rel)
272+
{
273+
if (m_type != osmium::item_type::relation) {
274+
m_type = osmium::item_type::relation;
275+
flush();
276+
}
277+
278+
if (rel.deleted()) {
279+
if (!m_append) {
280+
throw std::runtime_error{"Input file contains deleted objects but "
281+
"you are not in append mode."};
282+
}
283+
relation_delete(rel.id());
284+
} else {
285+
if (rel.members().size() > 32767) {
286+
return;
287+
}
288+
if (m_append) {
289+
relation_modify(rel);
290+
} else {
291+
relation_add(rel);
292+
}
293+
}
294+
m_progress.add_rel(rel.id());
295+
}
296+
214297
void osmdata_t::node_add(osmium::Node const &node) const
215298
{
216299
m_mid->node_set(node);
@@ -521,28 +604,27 @@ class multithreaded_processor
521604
} // anonymous namespace
522605

523606
progress_display_t osmdata_t::process_file(osmium::io::File const &file,
524-
osmium::Box const &bbox) const
607+
osmium::Box const &bbox)
525608
{
526-
input_handler_t handler{bbox, m_append, this};
527609
osmium::io::Reader reader{file};
528610
type_id_version last{osmium::item_type::node, 0, 0};
529611

530612
while (osmium::memory::Buffer buffer = reader.read()) {
531613
for (auto &object : buffer.select<osmium::OSMObject>()) {
532614
last = check_input(last, object);
533-
osmium::apply_item(object, handler);
615+
osmium::apply_item(object, *this);
534616
}
535617
}
536-
osmium::apply_flush(handler);
618+
flush();
537619

538620
reader.close();
539621

540-
return handler.progress();
622+
return m_progress;
541623
}
542624

543625
progress_display_t
544626
osmdata_t::process_files(std::vector<osmium::io::File> const &files,
545-
osmium::Box const &bbox) const
627+
osmium::Box const &bbox)
546628
{
547629
if (files.size() == 1) {
548630
return process_file(files.front(), bbox);
@@ -561,13 +643,11 @@ osmdata_t::process_files(std::vector<osmium::io::File> const &files,
561643
}
562644
}
563645

564-
input_handler_t handler{bbox, m_append, this};
565-
566646
while (!queue.empty()) {
567647
auto element = queue.top();
568648
queue.pop();
569649
if (queue.empty() || element != queue.top()) {
570-
osmium::apply_item(element.object(), handler);
650+
osmium::apply_item(element.object(), *this);
571651
}
572652

573653
auto *source = element.data_source();
@@ -576,13 +656,13 @@ osmdata_t::process_files(std::vector<osmium::io::File> const &files,
576656
}
577657
}
578658

579-
osmium::apply_flush(handler);
659+
flush();
580660

581661
for (auto &data_source : data_sources) {
582662
data_source.close();
583663
}
584664

585-
return handler.progress();
665+
return m_progress;
586666
}
587667

588668
void osmdata_t::process_dependents() const

0 commit comments

Comments
 (0)