1 #ifndef OSMIUM_AREA_MULTIPOLYGON_MANAGER_LEGACY_HPP 2 #define OSMIUM_AREA_MULTIPOLYGON_MANAGER_LEGACY_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/area/stats.hpp> 37 #include <osmium/handler.hpp> 38 #include <osmium/handler/check_order.hpp> 39 #include <osmium/memory/buffer.hpp> 40 #include <osmium/memory/callback_buffer.hpp> 41 #include <osmium/osm/item_type.hpp> 42 #include <osmium/osm/relation.hpp> 43 #include <osmium/osm/tag.hpp> 44 #include <osmium/osm/way.hpp> 45 #include <osmium/relations/manager_util.hpp> 46 #include <osmium/relations/members_database.hpp> 47 #include <osmium/relations/relations_database.hpp> 48 #include <osmium/relations/relations_manager.hpp> 49 #include <osmium/storage/item_stash.hpp> 50 51 #include <algorithm> 52 #include <cassert> 53 #include <cstddef> 54 #include <cstdint> 55 #include <cstring> 56 #include <vector> 57 58 namespace osmium { 59 60 /** 61 * @brief Code related to the building of areas (multipolygons) from relations. 62 */ 63 namespace area { 64 65 /** 66 * This class collects all data needed for creating areas from 67 * relations tagged with type=multipolygon or type=boundary. 68 * Most of its functionality is derived from the parent class 69 * osmium::relations::Collector. 70 * 71 * The actual assembling of the areas is done by the assembler 72 * class given as template argument. 73 * 74 * @tparam TAssembler Multipolygon Assembler class. 75 * @pre The Ids of all objects must be unique in the input data. 76 */ 77 template <typename TAssembler> 78 class MultipolygonManagerLegacy : public osmium::relations::RelationsManager<MultipolygonManagerLegacy<TAssembler>, false, true, false> { 79 80 using assembler_config_type = typename TAssembler::config_type; 81 const assembler_config_type m_assembler_config; 82 83 area_stats m_stats; 84 85 public: 86 87 /** 88 * Construct a MultipolygonManagerLegacy. 89 * 90 * @param assembler_config The configuration that will be given to 91 * any newly constructed area assembler. 92 */ MultipolygonManagerLegacy(const assembler_config_type & assembler_config)93 explicit MultipolygonManagerLegacy(const assembler_config_type& assembler_config) : 94 m_assembler_config(assembler_config) { 95 } 96 97 /** 98 * Access the aggregated statistics generated by the assemblers 99 * called from the manager. 100 */ stats() const101 const area_stats& stats() const noexcept { 102 return m_stats; 103 } 104 105 /** 106 * We are interested in all relations tagged with type=multipolygon 107 * or type=boundary with at least one way member. 108 */ new_relation(const osmium::Relation & relation) const109 bool new_relation(const osmium::Relation& relation) const { 110 const char* type = relation.tags().get_value_by_key("type"); 111 112 // ignore relations without "type" tag 113 if (!type) { 114 return false; 115 } 116 117 if ((!std::strcmp(type, "multipolygon")) || (!std::strcmp(type, "boundary"))) { 118 return std::any_of(relation.members().cbegin(), relation.members().cend(), [](const RelationMember& member) { 119 return member.type() == osmium::item_type::way; 120 }); 121 } 122 123 return false; 124 } 125 126 /** 127 * This is called when a relation is complete, ie. all members 128 * were found in the input. It will build the area using the 129 * assembler. 130 */ complete_relation(const osmium::Relation & relation)131 void complete_relation(const osmium::Relation& relation) { 132 std::vector<const osmium::Way*> ways; 133 ways.reserve(relation.members().size()); 134 for (const auto& member : relation.members()) { 135 if (member.ref() != 0) { 136 ways.push_back(this->get_member_way(member.ref())); 137 assert(ways.back() != nullptr); 138 } 139 } 140 141 try { 142 TAssembler assembler{m_assembler_config}; 143 assembler(relation, ways, this->buffer()); 144 m_stats += assembler.stats(); 145 } catch (const osmium::invalid_location&) { 146 // XXX ignore 147 } 148 } 149 150 /** 151 * This is called when a way is not in any multipolygon 152 * relation. 153 */ way_not_in_any_relation(const osmium::Way & way)154 void way_not_in_any_relation(const osmium::Way& way) { 155 // you need at least 4 nodes to make up a polygon 156 if (way.nodes().size() <= 3) { 157 return; 158 } 159 try { 160 if (!way.nodes().front().location() || !way.nodes().back().location()) { 161 throw osmium::invalid_location{"invalid location"}; 162 } 163 if (way.ends_have_same_location()) { 164 // way is closed and has enough nodes, build simple multipolygon 165 TAssembler assembler{m_assembler_config}; 166 assembler(way, this->buffer()); 167 m_stats += assembler.stats(); 168 } 169 } catch (const osmium::invalid_location&) { 170 // XXX ignore 171 } 172 } 173 174 }; // class MultipolygonManagerLegacy 175 176 } // namespace area 177 178 } // namespace osmium 179 180 #endif // OSMIUM_AREA_MULTIPOLYGON_MANAGER_LEGACY_HPP 181