Libosmium  2.17.1
Fast and flexible C++ library for working with OpenStreetMap data
check_order.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_HANDLER_CHECK_ORDER_HPP
2 #define OSMIUM_HANDLER_CHECK_ORDER_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <osmium/handler.hpp>
37 #include <osmium/osm/node.hpp>
39 #include <osmium/osm/relation.hpp>
40 #include <osmium/osm/types.hpp>
41 #include <osmium/osm/way.hpp>
43 
44 #include <limits>
45 #include <stdexcept>
46 #include <string>
47 
48 namespace osmium {
49 
54  struct OSMIUM_EXPORT out_of_order_error : public std::runtime_error {
55 
57 
58  explicit out_of_order_error(const std::string& what, osmium::object_id_type id) :
59  std::runtime_error(what),
60  object_id(id) {
61  }
62 
63  explicit out_of_order_error(const char* what, osmium::object_id_type id) :
64  std::runtime_error(what),
65  object_id(id) {
66  }
67 
68  }; // struct out_of_order_error
69 
70  namespace handler {
71 
89 
90  osmium::object_id_type m_max_node_id = std::numeric_limits<osmium::object_id_type>::min();
91  osmium::object_id_type m_max_way_id = std::numeric_limits<osmium::object_id_type>::min();
92  osmium::object_id_type m_max_relation_id = std::numeric_limits<osmium::object_id_type>::min();
93 
94  public:
95 
96  void node(const osmium::Node& node) {
97  if (m_max_way_id > std::numeric_limits<osmium::object_id_type>::min()) {
98  throw out_of_order_error{"Found a node after a way.", node.id()};
99  }
100  if (m_max_relation_id > std::numeric_limits<osmium::object_id_type>::min()) {
101  throw out_of_order_error{"Found a node after a relation.", node.id()};
102  }
103 
104  if (m_max_node_id == node.id()) {
105  throw out_of_order_error{"Node ID twice in input. Maybe you are using a history or change file?", node.id()};
106  }
107  if (id_order{}(node.id(), m_max_node_id)) {
108  throw out_of_order_error{"Node IDs out of order.", node.id()};
109  }
110  m_max_node_id = node.id();
111  }
112 
113  void way(const osmium::Way& way) {
114  if (m_max_relation_id > std::numeric_limits<osmium::object_id_type>::min()) {
115  throw out_of_order_error{"Found a way after a relation.", way.id()};
116  }
117 
118  if (m_max_way_id == way.id()) {
119  throw out_of_order_error{"Way ID twice in input. Maybe you are using a history or change file?", way.id()};
120  }
121  if (id_order{}(way.id(), m_max_way_id)) {
122  throw out_of_order_error{"Way IDs out of order.", way.id()};
123  }
124  m_max_way_id = way.id();
125  }
126 
128  if (m_max_relation_id == relation.id()) {
129  throw out_of_order_error{"Relation ID twice in input. Maybe you are using a history or change file?", relation.id()};
130  }
131  if (id_order{}(relation.id(), m_max_relation_id)) {
132  throw out_of_order_error{"Relation IDs out of order.", relation.id()};
133  }
135  }
136 
138  return m_max_node_id;
139  }
140 
142  return m_max_way_id;
143  }
144 
146  return m_max_relation_id;
147  }
148 
149  }; // class CheckOrder
150 
151  } // namespace handler
152 
153 } // namespace osmium
154 
155 #endif // OSMIUM_HANDLER_CHECK_ORDER_HPP
Definition: node.hpp:48
Definition: relation.hpp:168
Definition: way.hpp:72
Definition: check_order.hpp:88
void node(const osmium::Node &node)
Definition: check_order.hpp:96
osmium::object_id_type m_max_relation_id
Definition: check_order.hpp:92
void relation(const osmium::Relation &relation)
Definition: check_order.hpp:127
osmium::object_id_type m_max_node_id
Definition: check_order.hpp:90
osmium::object_id_type max_node_id() const noexcept
Definition: check_order.hpp:137
osmium::object_id_type max_way_id() const noexcept
Definition: check_order.hpp:141
osmium::object_id_type max_relation_id() const noexcept
Definition: check_order.hpp:145
void way(const osmium::Way &way)
Definition: check_order.hpp:113
osmium::object_id_type m_max_way_id
Definition: check_order.hpp:91
Definition: handler.hpp:71
#define OSMIUM_EXPORT
Definition: compatibility.hpp:63
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
Definition: location.hpp:551
Definition: object_comparisons.hpp:87
Definition: check_order.hpp:54
osmium::object_id_type object_id
Definition: check_order.hpp:56
out_of_order_error(const std::string &what, osmium::object_id_type id)
Definition: check_order.hpp:58
out_of_order_error(const char *what, osmium::object_id_type id)
Definition: check_order.hpp:63