|
| 1 | +#include <osmium/osm.hpp> |
| 2 | + |
| 3 | +#include "format.hpp" |
| 4 | +#include "input-handler.hpp" |
| 5 | +#include "osmdata.hpp" |
| 6 | + |
| 7 | +input_handler_t::input_handler_t(osmium::Box const &bbox, bool append, |
| 8 | + osmdata_t const *osmdata) |
| 9 | +: m_data(osmdata), m_bbox(bbox), m_append(append) |
| 10 | +{} |
| 11 | + |
| 12 | +void input_handler_t::node(osmium::Node const &node) |
| 13 | +{ |
| 14 | + if (m_type != osmium::item_type::node) { |
| 15 | + m_type = osmium::item_type::node; |
| 16 | + m_data->flush(); |
| 17 | + } |
| 18 | + |
| 19 | + if (node.deleted()) { |
| 20 | + if (!m_append) { |
| 21 | + throw std::runtime_error{"Input file contains deleted objects but " |
| 22 | + "you are not in append mode."}; |
| 23 | + } |
| 24 | + m_data->node_delete(node.id()); |
| 25 | + } else { |
| 26 | + // if the node is not valid, then node.location.lat/lon() can throw. |
| 27 | + // we probably ought to treat invalid locations as if they were |
| 28 | + // deleted and ignore them. |
| 29 | + if (!node.location().valid()) { |
| 30 | + fmt::print( |
| 31 | + stderr, |
| 32 | + "WARNING: Node {} (version {}) has an invalid location and has " |
| 33 | + "been ignored. This is not expected to happen with recent " |
| 34 | + "planet files, so please check that your input is correct.\n", |
| 35 | + node.id(), node.version()); |
| 36 | + |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (!m_bbox.valid() || m_bbox.contains(node.location())) { |
| 41 | + if (m_append) { |
| 42 | + m_data->node_modify(node); |
| 43 | + } else { |
| 44 | + m_data->node_add(node); |
| 45 | + } |
| 46 | + m_progress.add_node(node.id()); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void input_handler_t::way(osmium::Way &way) |
| 52 | +{ |
| 53 | + if (m_type != osmium::item_type::way) { |
| 54 | + m_type = osmium::item_type::way; |
| 55 | + m_data->flush(); |
| 56 | + } |
| 57 | + |
| 58 | + if (way.deleted()) { |
| 59 | + if (!m_append) { |
| 60 | + throw std::runtime_error{"Input file contains deleted objects but " |
| 61 | + "you are not in append mode."}; |
| 62 | + } |
| 63 | + m_data->way_delete(way.id()); |
| 64 | + } else { |
| 65 | + if (m_append) { |
| 66 | + m_data->way_modify(&way); |
| 67 | + } else { |
| 68 | + m_data->way_add(&way); |
| 69 | + } |
| 70 | + } |
| 71 | + m_progress.add_way(way.id()); |
| 72 | +} |
| 73 | + |
| 74 | +void input_handler_t::relation(osmium::Relation const &rel) |
| 75 | +{ |
| 76 | + if (m_type != osmium::item_type::relation) { |
| 77 | + m_type = osmium::item_type::relation; |
| 78 | + m_data->flush(); |
| 79 | + } |
| 80 | + |
| 81 | + if (rel.deleted()) { |
| 82 | + if (!m_append) { |
| 83 | + throw std::runtime_error{"Input file contains deleted objects but " |
| 84 | + "you are not in append mode."}; |
| 85 | + } |
| 86 | + m_data->relation_delete(rel.id()); |
| 87 | + } else { |
| 88 | + if (rel.members().size() > 32767) { |
| 89 | + return; |
| 90 | + } |
| 91 | + if (m_append) { |
| 92 | + m_data->relation_modify(rel); |
| 93 | + } else { |
| 94 | + m_data->relation_add(rel); |
| 95 | + } |
| 96 | + } |
| 97 | + m_progress.add_rel(rel.id()); |
| 98 | +} |
0 commit comments