Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
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-2023 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>
42#include <osmium/osm/tag.hpp>
43#include <osmium/osm/way.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
75
76 // All relations and members we are interested in will be kept
77 // in here.
79
82
87
90
91 public:
92
98 }
99
102 return m_relations_db;
103 }
104
107 return m_member_nodes_db;
108 }
109
112 return m_member_nodes_db;
113 }
114
117 return m_member_ways_db;
118 }
119
122 return m_member_ways_db;
123 }
124
128 }
129
133 }
134
142 switch (type) {
144 return m_member_nodes_db;
146 return m_member_ways_db;
149 default:
150 break;
151 }
152
153 assert(false && "Should not be here");
154 return m_member_nodes_db;
155 }
156
164 switch (type) {
166 return m_member_nodes_db;
168 return m_member_ways_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
200 if (id == 0) {
201 return nullptr;
202 }
203 return member_nodes_database().get(id);
204 }
205
214 if (id == 0) {
215 return nullptr;
216 }
217 return member_ways_database().get(id);
218 }
219
228 if (id == 0) {
229 return nullptr;
230 }
231 return member_relations_database().get(id);
232 }
233
243 }
244
249 return {
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
270 m_output.flush();
271 }
272
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>
306
308
310
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
450 derived().complete_relation(*rel_handle);
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
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
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
506 if (TNodes) {
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);
517 }
518 }
519
521 if (TWays) {
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);
532 }
533 }
534
536 if (TRelations) {
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);
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
Definition: item_stash.hpp:57
std::size_t used_memory() const noexcept
Definition: item_stash.hpp:208
Definition: node.hpp:48
Definition: object.hpp:64
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:118
Definition: relation.hpp:56
Definition: relation.hpp:161
RelationMemberList & members()
Get a reference to the member list.
Definition: relation.hpp:179
Definition: way.hpp:72
Definition: handler.hpp:71
void node(const osmium::Node &) const noexcept
Definition: handler.hpp:78
void way(const osmium::Way &) const noexcept
Definition: handler.hpp:81
Definition: callback_buffer.hpp:70
osmium::memory::Buffer & buffer() noexcept
Definition: callback_buffer.hpp:133
void set_callback(const callback_func_type &callback=nullptr) noexcept
Definition: callback_buffer.hpp:144
osmium::memory::Buffer read()
Definition: callback_buffer.hpp:181
void possibly_flush()
Definition: callback_buffer.hpp:170
void flush()
Definition: callback_buffer.hpp:156
Definition: members_database.hpp:62
void prepare_for_lookup()
Definition: members_database.hpp:259
void track(RelationHandle &rel_handle, osmium::object_id_type member_id, std::size_t member_num)
Definition: members_database.hpp:246
const osmium::OSMObject * get_object(osmium::object_id_type id) const
Definition: members_database.hpp:305
void remove(osmium::object_id_type member_id, osmium::object_id_type relation_id)
Definition: members_database.hpp:273
std::size_t used_memory() const noexcept
Definition: members_database.hpp:187
const TObject * get(osmium::object_id_type id) const
Definition: members_database.hpp:403
bool add(const TObject &object, TFunc &&func)
Definition: members_database.hpp:364
Definition: relations_database.hpp:208
void remove()
Definition: relations_database.hpp:274
Definition: relations_database.hpp:82
void for_each_relation(TFunc &&func)
Definition: relations_database.hpp:323
RelationHandle add(const osmium::Relation &relation)
Definition: relations_database.hpp:317
std::size_t used_memory() const noexcept
Definition: relations_database.hpp:144
Definition: relations_manager.hpp:74
relations::MembersDatabaseCommon & member_database(osmium::item_type type) noexcept
Definition: relations_manager.hpp:141
relations::MembersDatabase< osmium::Way > m_member_ways_db
Definition: relations_manager.hpp:85
osmium::memory::CallbackBuffer m_output
Output buffer.
Definition: relations_manager.hpp:89
osmium::memory::Buffer read()
Return the contents of the output buffer.
Definition: relations_manager.hpp:279
const osmium::relations::MembersDatabase< osmium::Node > & member_nodes_database() const noexcept
Access the internal database containing member nodes.
Definition: relations_manager.hpp:111
RelationsManagerBase()
Definition: relations_manager.hpp:93
osmium::relations::MembersDatabase< osmium::Node > & member_nodes_database() noexcept
Access the internal database containing member nodes.
Definition: relations_manager.hpp:106
void prepare_for_lookup()
Definition: relations_manager.hpp:239
void flush_output()
Flush the output buffer.
Definition: relations_manager.hpp:269
void possibly_flush()
Flush the output buffer if it is full.
Definition: relations_manager.hpp:274
const relations::MembersDatabaseCommon & member_database(osmium::item_type type) const noexcept
Definition: relations_manager.hpp:163
const osmium::OSMObject * get_member_object(const osmium::RelationMember &member) const noexcept
Definition: relations_manager.hpp:185
osmium::relations::MembersDatabase< osmium::Relation > & member_relations_database() noexcept
Access the internal database containing member relations.
Definition: relations_manager.hpp:126
osmium::relations::RelationsDatabase & relations_database() noexcept
Access the internal RelationsDatabase.
Definition: relations_manager.hpp:101
osmium::memory::Buffer & buffer() noexcept
Access the output buffer.
Definition: relations_manager.hpp:259
const osmium::Node * get_member_node(osmium::object_id_type id) const noexcept
Definition: relations_manager.hpp:199
const osmium::relations::MembersDatabase< osmium::Way > & member_ways_database() const noexcept
Access the internal database containing member ways.
Definition: relations_manager.hpp:121
relations_manager_memory_usage used_memory() const noexcept
Definition: relations_manager.hpp:248
relations::MembersDatabase< osmium::Node > m_member_nodes_db
Databases of all members we are interested in.
Definition: relations_manager.hpp:84
relations::RelationsDatabase m_relations_db
Database of all relations we are interested in.
Definition: relations_manager.hpp:81
relations::MembersDatabase< osmium::Relation > m_member_relations_db
Definition: relations_manager.hpp:86
osmium::ItemStash m_stash
Definition: relations_manager.hpp:78
osmium::relations::MembersDatabase< osmium::Way > & member_ways_database() noexcept
Access the internal database containing member ways.
Definition: relations_manager.hpp:116
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
const osmium::Relation * get_member_relation(osmium::object_id_type id) const noexcept
Definition: relations_manager.hpp:227
const osmium::relations::MembersDatabase< osmium::Relation > & member_relations_database() const noexcept
Access the internal database containing member relations.
Definition: relations_manager.hpp:131
const osmium::Way * get_member_way(osmium::object_id_type id) const noexcept
Definition: relations_manager.hpp:213
Definition: relations_manager.hpp:305
void relation(const osmium::Relation &relation)
Definition: relations_manager.hpp:488
void before_way(const osmium::Way &) const noexcept
Definition: relations_manager.hpp:392
void relation_not_in_any_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:432
void after_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:442
void handle_complete_relation(RelationHandle &rel_handle)
Definition: relations_manager.hpp:449
RelationsManager()
Definition: relations_manager.hpp:464
void complete_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:352
SecondPassHandler< RelationsManager > m_handler_pass2
Definition: relations_manager.hpp:311
SecondPassHandler< RelationsManager > & handler(const std::function< void(osmium::memory::Buffer &&)> &callback=nullptr)
Definition: relations_manager.hpp:473
check_order_handler m_check_order_handler
Definition: relations_manager.hpp:309
void handle_way(const osmium::Way &way)
Definition: relations_manager.hpp:520
void handle_node(const osmium::Node &node)
Definition: relations_manager.hpp:505
bool new_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:328
void node_not_in_any_relation(const osmium::Node &) const noexcept
Definition: relations_manager.hpp:372
void before_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:422
void before_node(const osmium::Node &) const noexcept
Definition: relations_manager.hpp:362
void after_node(const osmium::Node &) const noexcept
Definition: relations_manager.hpp:382
static bool wanted_type(osmium::item_type type) noexcept
Definition: relations_manager.hpp:313
void for_each_incomplete_relation(TFunc &&func)
Definition: relations_manager.hpp:559
void way_not_in_any_relation(const osmium::Way &) const noexcept
Definition: relations_manager.hpp:402
TManager & derived() noexcept
Definition: relations_manager.hpp:445
void after_way(const osmium::Way &) const noexcept
Definition: relations_manager.hpp:412
typename std::conditional< TCheckOrder, osmium::handler::CheckOrder, osmium::handler::Handler >::type check_order_handler
Definition: relations_manager.hpp:307
void handle_relation(const osmium::Relation &relation)
Definition: relations_manager.hpp:535
bool new_member(const osmium::Relation &, const osmium::RelationMember &, std::size_t) const noexcept
Definition: relations_manager.hpp:342
Definition: manager_util.hpp:64
type
Definition: entity_bits.hpp:63
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
item_type
Definition: item_type.hpp:45