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> 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 43 #include <limits> 44 #include <stdexcept> 45 #include <string> 46 47 namespace osmium { 48 49 /** 50 * Exception thrown when a method in the CheckOrder class detects 51 * that the input is out of order. 52 */ 53 struct out_of_order_error : public std::runtime_error { 54 55 osmium::object_id_type object_id; 56 out_of_order_errorosmium::out_of_order_error57 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 out_of_order_errorosmium::out_of_order_error62 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 71 /** 72 * Handler that can be used to check that an OSM file is ordered 73 * correctly. Ordered in this case refers to the usual order in OSM 74 * files: First nodes in the order of their IDs, then ways in the order 75 * of their IDs, then relations in the order or their IDs. Negative 76 * IDs are ordered first then positive IDs, both ordered by absolute 77 * value. 78 * 79 * IDs have to be unique for each type. This check will fail for 80 * history files. 81 * 82 * To use this, add a CheckOrder member variable to your handler and 83 * call the node(), way(), and relation() methods from your node(), 84 * way(), and relations() handlers, respectively. An out_of_order_error 85 * exception will be thrown when the input is not in order. 86 */ 87 class CheckOrder : public osmium::handler::Handler { 88 89 osmium::object_id_type m_max_node_id = std::numeric_limits<osmium::object_id_type>::min(); 90 osmium::object_id_type m_max_way_id = std::numeric_limits<osmium::object_id_type>::min(); 91 osmium::object_id_type m_max_relation_id = std::numeric_limits<osmium::object_id_type>::min(); 92 93 public: 94 node(const osmium::Node & node)95 void node(const osmium::Node& node) { 96 if (m_max_way_id > std::numeric_limits<osmium::object_id_type>::min()) { 97 throw out_of_order_error{"Found a node after a way.", node.id()}; 98 } 99 if (m_max_relation_id > std::numeric_limits<osmium::object_id_type>::min()) { 100 throw out_of_order_error{"Found a node after a relation.", node.id()}; 101 } 102 103 if (m_max_node_id == node.id()) { 104 throw out_of_order_error{"Node ID twice in input. Maybe you are using a history or change file?", node.id()}; 105 } 106 if (id_order{}(node.id(), m_max_node_id)) { 107 throw out_of_order_error{"Node IDs out of order.", node.id()}; 108 } 109 m_max_node_id = node.id(); 110 } 111 way(const osmium::Way & way)112 void way(const osmium::Way& way) { 113 if (m_max_relation_id > std::numeric_limits<osmium::object_id_type>::min()) { 114 throw out_of_order_error{"Found a way after a relation.", way.id()}; 115 } 116 117 if (m_max_way_id == way.id()) { 118 throw out_of_order_error{"Way ID twice in input. Maybe you are using a history or change file?", way.id()}; 119 } 120 if (id_order{}(way.id(), m_max_way_id)) { 121 throw out_of_order_error{"Way IDs out of order.", way.id()}; 122 } 123 m_max_way_id = way.id(); 124 } 125 relation(const osmium::Relation & relation)126 void relation(const osmium::Relation& relation) { 127 if (m_max_relation_id == relation.id()) { 128 throw out_of_order_error{"Relation ID twice in input. Maybe you are using a history or change file?", relation.id()}; 129 } 130 if (id_order{}(relation.id(), m_max_relation_id)) { 131 throw out_of_order_error{"Relation IDs out of order.", relation.id()}; 132 } 133 m_max_relation_id = relation.id(); 134 } 135 max_node_id() const136 osmium::object_id_type max_node_id() const noexcept { 137 return m_max_node_id; 138 } 139 max_way_id() const140 osmium::object_id_type max_way_id() const noexcept { 141 return m_max_way_id; 142 } 143 max_relation_id() const144 osmium::object_id_type max_relation_id() const noexcept { 145 return m_max_relation_id; 146 } 147 148 }; // class CheckOrder 149 150 } // namespace handler 151 152 } // namespace osmium 153 154 #endif // OSMIUM_HANDLER_CHECK_ORDER_HPP 155