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+
214297void 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
523606progress_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
543625progress_display_t
544626osmdata_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
588668void osmdata_t::process_dependents () const
0 commit comments