Libosmium: include/osmium/relations/relations_manager.hpp Source File

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

Loading...
Searching...
No Matches
relations_manager.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_RELATIONS_RELATIONS_MANAGER_HPP
2#define OSMIUM_RELATIONS_RELATIONS_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/handler.hpp>
37#include <osmium/handler/check_order.hpp>
38#include <osmium/memory/buffer.hpp>
39#include <osmium/memory/callback_buffer.hpp>
40#include <osmium/osm/item_type.hpp>
41#include <osmium/osm/relation.hpp>
42#include <osmium/osm/tag.hpp>
43#include <osmium/osm/way.hpp>
44#include <osmium/relations/manager_util.hpp>
45#include <osmium/relations/members_database.hpp>
46#include <osmium/relations/relations_database.hpp>
47#include <osmium/storage/item_stash.hpp>
48#include <osmium/tags/taglist.hpp>
49#include <osmium/tags/tags_filter.hpp>
50
51#include <algorithm>
52#include <cassert>
53#include <cstddef>
54#include <cstdint>
55#include <cstring>
56#include <stdexcept>
57#include <type_traits>
58#include <vector>
59
60namespace osmium {
61
62 namespace relations {
63
74 class RelationsManagerBase : public osmium::handler::Handler {
75
76 // All relations and members we are interested in will be kept
77 // in here.
78 osmium::ItemStash m_stash;
79
81 relations::RelationsDatabase m_relations_db;
82
84 relations::MembersDatabase<osmium::Node> m_member_nodes_db;
85 relations::MembersDatabase<osmium::Way> m_member_ways_db;
86 relations::MembersDatabase<osmium::Relation> m_member_relations_db;
87
89 osmium::memory::CallbackBuffer m_output;
90
91 public:
92
93 RelationsManagerBase() :
94 m_relations_db(m_stash),
95 m_member_nodes_db(m_stash, m_relations_db),
96 m_member_ways_db(m_stash, m_relations_db),
97 m_member_relations_db(m_stash, m_relations_db) {
98 }
99
101 osmium::relations::RelationsDatabase& relations_database() noexcept {
102 return m_relations_db;
103 }
104
106 osmium::relations::MembersDatabase<osmium::Node>& member_nodes_database() noexcept {
107 return m_member_nodes_db;
108 }
109
111 const osmium::relations::MembersDatabase<osmium::Node>& member_nodes_database() const noexcept {
112 return m_member_nodes_db;
113 }
114
116 osmium::relations::MembersDatabase<osmium::Way>& member_ways_database() noexcept {
117 return m_member_ways_db;
118 }
119
121 const osmium::relations::MembersDatabase<osmium::Way>& member_ways_database() const noexcept {
122 return m_member_ways_db;
123 }
124
126 osmium::relations::MembersDatabase<osmium::Relation>& member_relations_database() noexcept {
127 return m_member_relations_db;
128 }
129
131 const osmium::relations::MembersDatabase<osmium::Relation>& member_relations_database() const noexcept {
132 return m_member_relations_db;
133 }
134
141 relations::MembersDatabaseCommon& member_database(osmium::item_type type) noexcept {
142 switch (type) {
143 case osmium::item_type::node:
144 return m_member_nodes_db;
145 case osmium::item_type::way:
146 return m_member_ways_db;
147 case osmium::item_type::relation:
148 return m_member_relations_db;
149 default:
150 break;
151 }
152
153 assert(false && "Should not be here");
154 return m_member_nodes_db;
155 }
156
163 const relations::MembersDatabaseCommon& member_database(osmium::item_type type) const noexcept {
164 switch (type) {
165 case osmium::item_type::node:
166 return m_member_nodes_db;
167 case osmium::item_type::way:
168 return m_member_ways_db;
169 case osmium::item_type::relation:
170 return m_member_relations_db;
171 default:
172 break;
173 }
174
175 assert(false && "Should not be here");
176 return m_member_nodes_db;
177 }
178
185 const osmium::OSMObject* get_member_object(const osmium::RelationMember& member) const noexcept {
186 if (member.ref() == 0) {
187 return nullptr;
188 }
189 return member_database(member.type()).get_object(member.ref());
190 }
191
199 const osmium::Node* get_member_node(osmium::object_id_type id) const noexcept {
200 if (id == 0) {
201 return nullptr;
202 }
203 return member_nodes_database().get(id);
204 }
205
213 const osmium::Way* get_member_way(osmium::object_id_type id) const noexcept {
214 if (id == 0) {
215 return nullptr;
216 }
217 return member_ways_database().get(id);
218 }
219
227 const osmium::Relation* get_member_relation(osmium::object_id_type id) const noexcept {
228 if (id == 0) {
229 return nullptr;
230 }
231 return member_relations_database().get(id);
232 }
233
239 void prepare_for_lookup() {
240 m_member_nodes_db.prepare_for_lookup();
241 m_member_ways_db.prepare_for_lookup();
242 m_member_relations_db.prepare_for_lookup();
243 }
244
248 relations_manager_memory_usage used_memory() const noexcept {
249 return {
250 m_relations_db.used_memory(),
251 m_member_nodes_db.used_memory()
252 + m_member_ways_db.used_memory()
253 + m_member_relations_db.used_memory(),
254 m_stash.used_memory()
255 };
256 }
257
259 osmium::memory::Buffer& buffer() noexcept {
260 return m_output.buffer();
261 }
262
264 void set_callback(const std::function<void(osmium::memory::Buffer&&)>& callback) {
265 m_output.set_callback(callback);
266 }
267
269 void flush_output() {
270 m_output.flush();
271 }
272
274 void possibly_flush() {
275 m_output.possibly_flush();
276 }
277
279 osmium::memory::Buffer read() {
280 return m_output.read();
281 }
282
283 }; // class RelationsManagerBase
284
304 template <typename TManager, bool TNodes, bool TWays, bool TRelations, bool TCheckOrder = true>
305 class RelationsManager : public RelationsManagerBase {
306
307 using check_order_handler = std::conditional_t<TCheckOrder, osmium::handler::CheckOrder, osmium::handler::Handler>;
308
309 check_order_handler m_check_order_handler;
310
311 SecondPassHandler<RelationsManager> m_handler_pass2;
312
313 static bool wanted_type(osmium::item_type type) noexcept {
314 return (TNodes && type == osmium::item_type::node) ||
315 (TWays && type == osmium::item_type::way) ||
316 (TRelations && type == osmium::item_type::relation);
317 }
318
328 bool new_relation(const osmium::Relation& /*relation*/) const noexcept {
329 return true;
330 }
331
342 bool new_member(const osmium::Relation& /*relation*/, const osmium::RelationMember& /*member*/, std::size_t /*n*/) const noexcept {
343 return true;
344 }
345
352 void complete_relation(const osmium::Relation& /*relation*/) const noexcept {
353 }
354
362 void before_node(const osmium::Node& /*node*/) const noexcept {
363 }
364
372 void node_not_in_any_relation(const osmium::Node& /*node*/) const noexcept {
373 }
374
382 void after_node(const osmium::Node& /*node*/) const noexcept {
383 }
384
392 void before_way(const osmium::Way& /*way*/) const noexcept {
393 }
394
402 void way_not_in_any_relation(const osmium::Way& /*way*/) const noexcept {
403 }
404
412 void after_way(const osmium::Way& /*way*/) const noexcept {
413 }
414
422 void before_relation(const osmium::Relation& /*relation*/) const noexcept {
423 }
424
432 void relation_not_in_any_relation(const osmium::Relation& /*relation*/) const noexcept {
433 }
434
442 void after_relation(const osmium::Relation& /*relation*/) const noexcept {
443 }
444
445 TManager& derived() noexcept {
446 return *static_cast<TManager*>(this);
447 }
448
449 void handle_complete_relation(RelationHandle& rel_handle) {
450 derived().complete_relation(*rel_handle);
451 possibly_flush();
452
453 for (const auto& member : rel_handle->members()) {
454 if (member.ref() != 0) {
455 member_database(member.type()).remove(member.ref(), rel_handle->id());
456 }
457 }
458
459 rel_handle.remove();
460 }
461
462 public:
463
464 RelationsManager() :
465 RelationsManagerBase(),
466 m_check_order_handler(),
467 m_handler_pass2(*this) {
468 }
469
473 SecondPassHandler<RelationsManager>& handler(const std::function<void(osmium::memory::Buffer&&)>& callback = nullptr) {
474 set_callback(callback);
475 return m_handler_pass2;
476 }
477
488 void relation(const osmium::Relation& relation) {
489 if (derived().new_relation(relation)) {
490 auto rel_handle = relations_database().add(relation);
491
492 std::size_t n = 0;
493 for (auto& member : rel_handle->members()) {
494 if (wanted_type(member.type()) &&
495 derived().new_member(relation, member, n)) {
496 member_database(member.type()).track(rel_handle, member.ref(), n);
497 } else {
498 member.set_ref(0); // set member id to zero to indicate we are not interested
499 }
500 ++n;
501 }
502 }
503 }
504
505 void handle_node(const osmium::Node& node) {
506 if (TNodes) {
507 m_check_order_handler.node(node);
508 derived().before_node(node);
509 const bool added = member_nodes_database().add(node, [this](RelationHandle& rel_handle) {
510 handle_complete_relation(rel_handle);
511 });
512 if (!added) {
513 derived().node_not_in_any_relation(node);
514 }
515 derived().after_node(node);
516 possibly_flush();
517 }
518 }
519
520 void handle_way(const osmium::Way& way) {
521 if (TWays) {
522 m_check_order_handler.way(way);
523 derived().before_way(way);
524 const bool added = member_ways_database().add(way, [this](RelationHandle& rel_handle) {
525 handle_complete_relation(rel_handle);
526 });
527 if (!added) {
528 derived().way_not_in_any_relation(way);
529 }
530 derived().after_way(way);
531 possibly_flush();
532 }
533 }
534
535 void handle_relation(const osmium::Relation& relation) {
536 if (TRelations) {
537 m_check_order_handler.relation(relation);
538 derived().before_relation(relation);
539 const bool added = member_relations_database().add(relation, [this](RelationHandle& rel_handle) {
540 handle_complete_relation(rel_handle);
541 });
542 if (!added) {
543 derived().relation_not_in_any_relation(relation);
544 }
545 derived().after_relation(relation);
546 possibly_flush();
547 }
548 }
549
558 template <typename TFunc>
559 void for_each_incomplete_relation(TFunc&& func) {
560 relations_database().for_each_relation(std::forward<TFunc>(func));
561 }
562
563 }; // class RelationsManager
564
565 } // namespace relations
566
567} // namespace osmium
568
569#endif // OSMIUM_RELATIONS_RELATIONS_MANAGER_HPP
buffer.hpp
callback_buffer.hpp
check_order.hpp
osmium::ItemStash
Definition item_stash.hpp:57
osmium::Node
Definition node.hpp:48
osmium::OSMObject
Definition object.hpp:64
osmium::OSMObject::id
object_id_type id() const noexcept
Get ID of this object.
Definition object.hpp:118
osmium::RelationMember
Definition relation.hpp:56
osmium::Relation
Definition relation.hpp:161
osmium::Relation::members
RelationMemberList & members()
Get a reference to the member list.
Definition relation.hpp:179
osmium::Way
Definition way.hpp:72
osmium::handler::Handler
Definition handler.hpp:71
osmium::handler::Handler::node
void node(const osmium::Node &) const noexcept
Definition handler.hpp:78
osmium::handler::Handler::way
void way(const osmium::Way &) const noexcept
Definition handler.hpp:81
osmium::memory::CallbackBuffer
Definition callback_buffer.hpp:70
osmium::relations::MembersDatabaseCommon
Definition members_database.hpp:63
osmium::relations::MembersDatabaseCommon::track
void track(RelationHandle &rel_handle, osmium::object_id_type member_id, std::size_t member_num)
Definition members_database.hpp:247
osmium::relations::MembersDatabaseCommon::get_object
const osmium::OSMObject * get_object(osmium::object_id_type id) const
Definition members_database.hpp:306
osmium::relations::MembersDatabaseCommon::remove
void remove(osmium::object_id_type member_id, osmium::object_id_type relation_id)
Definition members_database.hpp:274
osmium::relations::MembersDatabase
Definition members_database.hpp:334
osmium::relations::MembersDatabase::get
const TObject * get(osmium::object_id_type id) const
Definition members_database.hpp:404
osmium::relations::MembersDatabase::add
bool add(const TObject &object, TFunc &&func)
Definition members_database.hpp:365
osmium::relations::RelationHandle
Definition relations_database.hpp:208
osmium::relations::RelationHandle::remove
void remove()
Definition relations_database.hpp:274
osmium::relations::RelationsDatabase
Definition relations_database.hpp:82
osmium::relations::RelationsDatabase::for_each_relation
void for_each_relation(TFunc &&func)
Definition relations_database.hpp:323
osmium::relations::RelationsDatabase::add
RelationHandle add(const osmium::Relation &relation)
Definition relations_database.hpp:317
osmium::relations::RelationsManagerBase::member_database
relations::MembersDatabaseCommon & member_database(osmium::item_type type) noexcept
Definition relations_manager.hpp:141
osmium::relations::RelationsManagerBase::m_member_ways_db
relations::MembersDatabase< osmium::Way > m_member_ways_db
Definition relations_manager.hpp:85
osmium::relations::RelationsManagerBase::m_output
osmium::memory::CallbackBuffer m_output
Output buffer.
Definition relations_manager.hpp:89
osmium::relations::RelationsManagerBase::read
osmium::memory::Buffer read()
Return the contents of the output buffer.
Definition relations_manager.hpp:279
osmium::relations::RelationsManagerBase::member_nodes_database
const osmium::relations::MembersDatabase< osmium::Node > & member_nodes_database() const noexcept
Access the internal database containing member nodes.
Definition relations_manager.hpp:111
osmium::relations::RelationsManagerBase::RelationsManagerBase
RelationsManagerBase()
Definition relations_manager.hpp:93
osmium::relations::RelationsManagerBase::member_nodes_database
osmium::relations::MembersDatabase< osmium::Node > & member_nodes_database() noexcept
Access the internal database containing member nodes.
Definition relations_manager.hpp:106
osmium::relations::RelationsManagerBase::prepare_for_lookup
void prepare_for_lookup()
Definition relations_manager.hpp:239
osmium::relations::RelationsManagerBase::flush_output
void flush_output()
Flush the output buffer.
Definition relations_manager.hpp:269
osmium::relations::RelationsManagerBase::possibly_flush
void possibly_flush()
Flush the output buffer if it is full.
Definition relations_manager.hpp:274
osmium::relations::RelationsManagerBase::member_database
const relations::MembersDatabaseCommon & member_database(osmium::item_type type) const noexcept
Definition relations_manager.hpp:163
osmium::relations::RelationsManagerBase::get_member_object
const osmium::OSMObject * get_member_object(const osmium::RelationMember &member) const noexcept
Definition relations_manager.hpp:185
osmium::relations::RelationsManagerBase::member_relations_database
osmium::relations::MembersDatabase< osmium::Relation > & member_relations_database() noexcept
Access the internal database containing member relations.
Definition relations_manager.hpp:126
osmium::relations::RelationsManagerBase::relations_database
osmium::relations::RelationsDatabase & relations_database() noexcept
Access the internal RelationsDatabase.
Definition relations_manager.hpp:101
osmium::relations::RelationsManagerBase::buffer
osmium::memory::Buffer & buffer() noexcept
Access the output buffer.
Definition relations_manager.hpp:259
osmium::relations::RelationsManagerBase::get_member_node
const osmium::Node * get_member_node(osmium::object_id_type id) const noexcept
Definition relations_manager.hpp:199
osmium::relations::RelationsManagerBase::member_ways_database
const osmium::relations::MembersDatabase< osmium::Way > & member_ways_database() const noexcept
Access the internal database containing member ways.
Definition relations_manager.hpp:121
osmium::relations::RelationsManagerBase::used_memory
relations_manager_memory_usage used_memory() const noexcept
Definition relations_manager.hpp:248
osmium::relations::RelationsManagerBase::m_member_nodes_db
relations::MembersDatabase< osmium::Node > m_member_nodes_db
Databases of all members we are interested in.
Definition relations_manager.hpp:84
osmium::relations::RelationsManagerBase::m_relations_db
relations::RelationsDatabase m_relations_db
Database of all relations we are interested in.
Definition relations_manager.hpp:81
osmium::relations::RelationsManagerBase::m_member_relations_db
relations::MembersDatabase< osmium::Relation > m_member_relations_db
Definition relations_manager.hpp:86
osmium::relations::RelationsManagerBase::m_stash
osmium::ItemStash m_stash
Definition relations_manager.hpp:78
osmium::relations::RelationsManagerBase::member_ways_database
osmium::relations::MembersDatabase< osmium::Way > & member_ways_database() noexcept
Access the internal database containing member ways.
Definition relations_manager.hpp:116
osmium::relations::RelationsManagerBase::set_callback
void set_callback(const std::function< void(osmium::memory::Buffer &&)> &callback)
Set the callback called when the output buffer is full.
Definition relations_manager.hpp:264
osmium::relations::RelationsManagerBase::get_member_relation
const osmium::Relation * get_member_relation(osmium::object_id_type id) const noexcept
Definition relations_manager.hpp:227
osmium::relations::RelationsManagerBase::member_relations_database
const osmium::relations::MembersDatabase< osmium::Relation > & member_relations_database() const noexcept
Access the internal database containing member relations.
Definition relations_manager.hpp:131
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::relation
void relation(const osmium::Relation &relation)
Definition relations_manager.hpp:488
osmium::relations::RelationsManager::before_way
void before_way(const osmium::Way &) const noexcept
Definition relations_manager.hpp:392
osmium::relations::RelationsManager::relation_not_in_any_relation
void relation_not_in_any_relation(const osmium::Relation &) const noexcept
Definition relations_manager.hpp:432
osmium::relations::RelationsManager::after_relation
void after_relation(const osmium::Relation &) const noexcept
Definition relations_manager.hpp:442
osmium::relations::RelationsManager::handle_complete_relation
void handle_complete_relation(RelationHandle &rel_handle)
Definition relations_manager.hpp:449
osmium::relations::RelationsManager::RelationsManager
RelationsManager()
Definition relations_manager.hpp:464
osmium::relations::RelationsManager::check_order_handler
std::conditional_t< TCheckOrder, osmium::handler::CheckOrder, osmium::handler::Handler > check_order_handler
Definition relations_manager.hpp:307
osmium::relations::RelationsManager::complete_relation
void complete_relation(const osmium::Relation &) const noexcept
Definition relations_manager.hpp:352
osmium::relations::RelationsManager::m_handler_pass2
SecondPassHandler< RelationsManager > m_handler_pass2
Definition relations_manager.hpp:311
osmium::relations::RelationsManager::handler
SecondPassHandler< RelationsManager > & handler(const std::function< void(osmium::memory::Buffer &&)> &callback=nullptr)
Definition relations_manager.hpp:473
osmium::relations::RelationsManager::m_check_order_handler
check_order_handler m_check_order_handler
Definition relations_manager.hpp:309
osmium::relations::RelationsManager::handle_way
void handle_way(const osmium::Way &way)
Definition relations_manager.hpp:520
osmium::relations::RelationsManager::handle_node
void handle_node(const osmium::Node &node)
Definition relations_manager.hpp:505
osmium::relations::RelationsManager::new_relation
bool new_relation(const osmium::Relation &) const noexcept
Definition relations_manager.hpp:328
osmium::relations::RelationsManager::node_not_in_any_relation
void node_not_in_any_relation(const osmium::Node &) const noexcept
Definition relations_manager.hpp:372
osmium::relations::RelationsManager::before_relation
void before_relation(const osmium::Relation &) const noexcept
Definition relations_manager.hpp:422
osmium::relations::RelationsManager::before_node
void before_node(const osmium::Node &) const noexcept
Definition relations_manager.hpp:362
osmium::relations::RelationsManager::after_node
void after_node(const osmium::Node &) const noexcept
Definition relations_manager.hpp:382
osmium::relations::RelationsManager::wanted_type
static bool wanted_type(osmium::item_type type) noexcept
Definition relations_manager.hpp:313
osmium::relations::RelationsManager::for_each_incomplete_relation
void for_each_incomplete_relation(TFunc &&func)
Definition relations_manager.hpp:559
osmium::relations::RelationsManager::way_not_in_any_relation
void way_not_in_any_relation(const osmium::Way &) const noexcept
Definition relations_manager.hpp:402
osmium::relations::RelationsManager::derived
TManager & derived() noexcept
Definition relations_manager.hpp:445
osmium::relations::RelationsManager::after_way
void after_way(const osmium::Way &) const noexcept
Definition relations_manager.hpp:412
osmium::relations::RelationsManager::handle_relation
void handle_relation(const osmium::Relation &relation)
Definition relations_manager.hpp:535
osmium::relations::RelationsManager::new_member
bool new_member(const osmium::Relation &, const osmium::RelationMember &, std::size_t) const noexcept
Definition relations_manager.hpp:342
osmium::relations::SecondPassHandler
Definition manager_util.hpp:64
handler.hpp
item_stash.hpp
item_type.hpp
manager_util.hpp
members_database.hpp
osmium::relations
Code related to the assembly of OSM relations.
Definition multipolygon_collector.hpp:53
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::item_type
item_type
Definition item_type.hpp:45
osmium::item_type::relation
@ relation
Definition item_type.hpp:50
osmium::item_type::node
@ node
Definition item_type.hpp:48
osmium::item_type::way
@ way
Definition item_type.hpp:49
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition types.hpp:45
relation.hpp
relations_database.hpp
osmium::relations::relations_manager_memory_usage
Definition manager_util.hpp:160
tag.hpp
taglist.hpp
tags_filter.hpp
way.hpp