Libosmium: include/osmium/area/multipolygon_manager.hpp Source File

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

Loading...
Searching...
No Matches
multipolygon_manager.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_AREA_MULTIPOLYGON_MANAGER_HPP
2#define OSMIUM_AREA_MULTIPOLYGON_MANAGER_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/area/stats.hpp>
37#include <osmium/osm/item_type.hpp>
38#include <osmium/osm/relation.hpp>
39#include <osmium/osm/tag.hpp>
40#include <osmium/osm/way.hpp>
41#include <osmium/relations/manager_util.hpp>
42#include <osmium/relations/members_database.hpp>
43#include <osmium/relations/relations_database.hpp>
44#include <osmium/relations/relations_manager.hpp>
45#include <osmium/storage/item_stash.hpp>
46#include <osmium/tags/taglist.hpp>
47#include <osmium/tags/tags_filter.hpp>
48
49#include <algorithm>
50#include <cassert>
51#include <cstddef>
52#include <cstdint>
53#include <cstring>
54#include <vector>
55
56namespace osmium {
57
61 namespace area {
62
75 template <typename TAssembler>
76 class MultipolygonManager : public osmium::relations::RelationsManager<MultipolygonManager<TAssembler>, false, true, false> {
77
78 using assembler_config_type = typename TAssembler::config_type;
79 assembler_config_type m_assembler_config;
80
81 area_stats m_stats;
82
83 osmium::TagsFilter m_filter;
84
85 public:
86
96 explicit MultipolygonManager(assembler_config_type assembler_config, osmium::TagsFilter filter = osmium::TagsFilter{true}) :
97 m_assembler_config(std::move(assembler_config)),
98 m_filter(std::move(filter)) {
99 }
100
105 const area_stats& stats() const noexcept {
106 return m_stats;
107 }
108
113 bool new_relation(const osmium::Relation& relation) const {
114 const char* type = relation.tags().get_value_by_key("type");
115
116 // ignore relations without "type" tag
117 if (type == nullptr) {
118 return false;
119 }
120
121 if (((!std::strcmp(type, "multipolygon")) || (!std::strcmp(type, "boundary"))) && osmium::tags::match_any_of(relation.tags(), m_filter)) {
122 return std::any_of(relation.members().cbegin(), relation.members().cend(), [](const RelationMember& member) {
123 return member.type() == osmium::item_type::way;
124 });
125 }
126
127 return false;
128 }
129
135 void complete_relation(const osmium::Relation& relation) {
136 std::vector<const osmium::Way*> ways;
137 ways.reserve(relation.members().size());
138 for (const auto& member : relation.members()) {
139 if (member.ref() != 0) {
140 ways.push_back(this->get_member_way(member.ref()));
141 assert(ways.back() != nullptr);
142 }
143 }
144
145 try {
146 TAssembler assembler{m_assembler_config};
147 assembler(relation, ways, this->buffer());
148 m_stats += assembler.stats();
149 } catch (const osmium::invalid_location&) {
150 // XXX ignore
151 }
152 }
153
154 void after_way(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
160 try {
161 if (!way.nodes().front().location() || !way.nodes().back().location()) {
162 throw osmium::invalid_location{"invalid location"};
163 }
164 if (way.ends_have_same_location()) {
165 if (way.tags().has_tag("area", "no")) {
166 return;
167 }
168
169 if (osmium::tags::match_none_of(way.tags(), m_filter)) {
170 return;
171 }
172
173 TAssembler assembler{m_assembler_config};
174 assembler(way, this->buffer());
175 m_stats += assembler.stats();
176 this->possibly_flush();
177 }
178 } catch (const osmium::invalid_location&) {
179 // XXX ignore
180 }
181 }
182
183 }; // class MultipolygonManager
184
185 } // namespace area
186
187} // namespace osmium
188
189#endif // OSMIUM_AREA_MULTIPOLYGON_MANAGER_HPP
osmium::RelationMember
Definition relation.hpp:56
osmium::Relation
Definition relation.hpp:161
osmium::Way
Definition way.hpp:72
osmium::area::MultipolygonManager::stats
const area_stats & stats() const noexcept
Definition multipolygon_manager.hpp:105
osmium::area::MultipolygonManager::new_relation
bool new_relation(const osmium::Relation &relation) const
Definition multipolygon_manager.hpp:113
osmium::area::MultipolygonManager::m_stats
area_stats m_stats
Definition multipolygon_manager.hpp:81
osmium::area::MultipolygonManager::assembler_config_type
typename TAssembler::config_type assembler_config_type
Definition multipolygon_manager.hpp:78
osmium::area::MultipolygonManager::complete_relation
void complete_relation(const osmium::Relation &relation)
Definition multipolygon_manager.hpp:135
osmium::area::MultipolygonManager::m_assembler_config
assembler_config_type m_assembler_config
Definition multipolygon_manager.hpp:79
osmium::area::MultipolygonManager::MultipolygonManager
MultipolygonManager(assembler_config_type assembler_config, osmium::TagsFilter filter=osmium::TagsFilter{true})
Definition multipolygon_manager.hpp:96
osmium::area::MultipolygonManager::m_filter
osmium::TagsFilter m_filter
Definition multipolygon_manager.hpp:83
osmium::area::MultipolygonManager::after_way
void after_way(const osmium::Way &way)
Definition multipolygon_manager.hpp:154
osmium::handler::Handler::way
void way(const osmium::Way &) const noexcept
Definition handler.hpp:81
osmium::relations::RelationsManagerBase::possibly_flush
void possibly_flush()
Flush the output buffer if it is full.
Definition relations_manager.hpp:274
osmium::relations::RelationsManagerBase::buffer
osmium::memory::Buffer & buffer() noexcept
Access the output buffer.
Definition relations_manager.hpp:259
osmium::relations::RelationsManagerBase::get_member_way
const osmium::Way * get_member_way(osmium::object_id_type id) const noexcept
Definition relations_manager.hpp:213
osmium::relations::RelationsManager
Definition relations_manager.hpp:305
osmium::relations::RelationsManager< MultipolygonManager< TAssembler >, false, true, false >::relation
void relation(const osmium::Relation &relation)
Definition relations_manager.hpp:488
item_stash.hpp
item_type.hpp
manager_util.hpp
members_database.hpp
osmium::area
Code related to the building of areas (multipolygons) from relations.
Definition assembler.hpp:55
osmium::tags::match_none_of
bool match_none_of(const osmium::TagList &tag_list, const TFilter &filter)
Definition taglist.hpp:60
osmium::tags::match_any_of
bool match_any_of(const osmium::TagList &tag_list, const TFilter &filter)
Definition taglist.hpp:50
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::TagsFilter
TagsFilterBase< bool > TagsFilter
Definition tags_filter.hpp:155
std
Definition location.hpp:654
relation.hpp
relations_database.hpp
relations_manager.hpp
stats.hpp
osmium::area::area_stats
Definition stats.hpp:49
osmium::invalid_location
Definition location.hpp:54
tag.hpp
taglist.hpp
tags_filter.hpp
way.hpp