Libosmium: include/osmium/handler/check_order.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/handler.hpp>
37#include <osmium/osm/node.hpp>
38#include <osmium/osm/object_comparisons.hpp>
39#include <osmium/osm/relation.hpp>
40#include <osmium/osm/types.hpp>
41#include <osmium/osm/way.hpp>
42#include <osmium/util/compatibility.hpp>
43
44#include <stdexcept>
45#include <string>
46
47namespace osmium {
48
53 struct OSMIUM_EXPORT out_of_order_error : public std::runtime_error {
54
55 osmium::object_id_type object_id;
56
57 explicit out_of_order_error(const std::string& what, osmium::object_id_type id) :
58 std::runtime_error(what),
59 object_id(id) {
60 }
61
62 explicit out_of_order_error(const char* what, osmium::object_id_type id) :
63 std::runtime_error(what),
64 object_id(id) {
65 }
66
67 }; // struct out_of_order_error
68
69 namespace handler {
70
90 class CheckOrder : public osmium::handler::Handler {
91
92 osmium::object_id_type m_max_node_id = 0;
93 osmium::object_id_type m_max_way_id = 0;
94 osmium::object_id_type m_max_relation_id = 0;
95 osmium::object_version_type m_last_version = 0;
96 bool m_has_node = false;
97 bool m_has_way = false;
98 bool m_has_relation = false;
99 bool m_with_history;
100
101 public:
102
103 CheckOrder(bool with_history = false) : m_with_history(with_history) {
104 }
105
106 void node(const osmium::Node& node) {
107 if (m_has_way) {
108 throw out_of_order_error{"Found a node after a way.", node.id()};
109 }
110 if (m_has_relation) {
111 throw out_of_order_error{"Found a node after a relation.", node.id()};
112 }
113
114 if (m_has_node) {
115 if (m_max_node_id == node.id()) {
116 if (m_with_history) {
117 if (node.version() <= m_last_version) {
118 throw out_of_order_error{"Versions out of order", node.id()};
119 }
120 } else {
121 throw out_of_order_error{"Node ID twice in input. Maybe you are using a history or change file?", node.id()};
122 }
123 }
124 if (id_order{}(node.id(), m_max_node_id)) {
125 throw out_of_order_error{"Node IDs out of order: " + std::to_string(node.id()), node.id()};
126 }
127 m_max_node_id = node.id();
128 } else {
129 m_max_node_id = node.id();
130 m_has_node = true;
131 }
132
133 if (m_with_history) {
134 m_last_version = node.version();
135 }
136 }
137
138 void way(const osmium::Way& way) {
139 if (m_has_relation) {
140 throw out_of_order_error{"Found a way after a relation.", way.id()};
141 }
142
143 if (m_has_way) {
144 if (m_max_way_id == way.id()) {
145 if (m_with_history) {
146 if (way.version() <= m_last_version) {
147 throw out_of_order_error{"Versions out of order", way.id()};
148 }
149 } else {
150 throw out_of_order_error{"Way ID twice in input. Maybe you are using a history or change file?", way.id()};
151 }
152 }
153 if (id_order{}(way.id(), m_max_way_id)) {
154 throw out_of_order_error{"Way IDs out of order: " + std::to_string(way.id()), way.id()};
155 }
156 m_max_way_id = way.id();
157 } else {
158 m_max_way_id = way.id();
159 m_has_way = true;
160 }
161
162 if (m_with_history) {
163 m_last_version = way.version();
164 }
165 }
166
167 void relation(const osmium::Relation& relation) {
168 if (m_has_relation) {
169 if (m_max_relation_id == relation.id()) {
170 if (m_with_history) {
171 if (relation.version() <= m_last_version) {
172 throw out_of_order_error{"Versions out of order", relation.id()};
173 }
174 } else {
175 throw out_of_order_error{"Relation ID twice in input. Maybe you are using a history or change file?", relation.id()};
176 }
177 }
178 if (id_order{}(relation.id(), m_max_relation_id)) {
179 throw out_of_order_error{"Relation IDs out of order: " + std::to_string(relation.id()), relation.id()};
180 }
181 m_max_relation_id = relation.id();
182 } else {
183 m_max_relation_id = relation.id();
184 m_has_relation = true;
185 }
186
187 if (m_with_history) {
188 m_last_version = relation.version();
189 }
190 }
191
192 osmium::object_id_type max_node_id() const noexcept {
193 return m_max_node_id;
194 }
195
196 osmium::object_id_type max_way_id() const noexcept {
197 return m_max_way_id;
198 }
199
200 osmium::object_id_type max_relation_id() const noexcept {
201 return m_max_relation_id;
202 }
203
204 }; // class CheckOrder
205
206 } // namespace handler
207
208} // namespace osmium
209
210#endif // OSMIUM_HANDLER_CHECK_ORDER_HPP
osmium::Node
Definition node.hpp:48
osmium::Relation
Definition relation.hpp:161
osmium::Way
Definition way.hpp:72
osmium::handler::CheckOrder::m_has_node
bool m_has_node
Definition check_order.hpp:96
osmium::handler::CheckOrder::node
void node(const osmium::Node &node)
Definition check_order.hpp:106
osmium::handler::CheckOrder::m_max_relation_id
osmium::object_id_type m_max_relation_id
Definition check_order.hpp:94
osmium::handler::CheckOrder::relation
void relation(const osmium::Relation &relation)
Definition check_order.hpp:167
osmium::handler::CheckOrder::m_max_node_id
osmium::object_id_type m_max_node_id
Definition check_order.hpp:92
osmium::handler::CheckOrder::m_with_history
bool m_with_history
Definition check_order.hpp:99
osmium::handler::CheckOrder::m_has_relation
bool m_has_relation
Definition check_order.hpp:98
osmium::handler::CheckOrder::m_has_way
bool m_has_way
Definition check_order.hpp:97
osmium::handler::CheckOrder::max_node_id
osmium::object_id_type max_node_id() const noexcept
Definition check_order.hpp:192
osmium::handler::CheckOrder::m_last_version
osmium::object_version_type m_last_version
Definition check_order.hpp:95
osmium::handler::CheckOrder::max_way_id
osmium::object_id_type max_way_id() const noexcept
Definition check_order.hpp:196
osmium::handler::CheckOrder::max_relation_id
osmium::object_id_type max_relation_id() const noexcept
Definition check_order.hpp:200
osmium::handler::CheckOrder::CheckOrder
CheckOrder(bool with_history=false)
Definition check_order.hpp:103
osmium::handler::CheckOrder::way
void way(const osmium::Way &way)
Definition check_order.hpp:138
osmium::handler::CheckOrder::m_max_way_id
osmium::object_id_type m_max_way_id
Definition check_order.hpp:93
osmium::handler::Handler
Definition handler.hpp:71
compatibility.hpp
OSMIUM_EXPORT
#define OSMIUM_EXPORT
Definition compatibility.hpp:45
handler.hpp
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition types.hpp:45
osmium::object_version_type
uint32_t object_version_type
Type for OSM object version number.
Definition types.hpp:47
std
Definition location.hpp:654
node.hpp
object_comparisons.hpp
relation.hpp
osmium::id_order
Definition object_comparisons.hpp:87
osmium::out_of_order_error
Definition check_order.hpp:53
osmium::out_of_order_error::object_id
osmium::object_id_type object_id
Definition check_order.hpp:55
osmium::out_of_order_error::out_of_order_error
out_of_order_error(const std::string &what, osmium::object_id_type id)
Definition check_order.hpp:57
osmium::out_of_order_error::out_of_order_error
out_of_order_error(const char *what, osmium::object_id_type id)
Definition check_order.hpp:62
types.hpp
way.hpp