Loading...
Searching...
No Matches
relations_manager.hpp
Go to the documentation of this file.
#ifndef OSMIUM_RELATIONS_RELATIONS_MANAGER_HPP
#define OSMIUM_RELATIONS_RELATIONS_MANAGER_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/handler.hpp>
#include <osmium/handler/check_order.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/memory/callback_buffer.hpp>
#include <osmium/osm/item_type.hpp>
#include <osmium/osm/relation.hpp>
#include <osmium/osm/tag.hpp>
#include <osmium/osm/way.hpp>
#include <osmium/relations/manager_util.hpp>
#include <osmium/relations/members_database.hpp>
#include <osmium/relations/relations_database.hpp>
#include <osmium/storage/item_stash.hpp>
#include <osmium/tags/taglist.hpp>
#include <osmium/tags/tags_filter.hpp>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <type_traits>
#include <vector>
namespace osmium {
namespace relations {
class RelationsManagerBase : public osmium::handler::Handler {
// All relations and members we are interested in will be kept
// in here.
osmium::ItemStash m_stash;
relations::RelationsDatabase m_relations_db;
relations::MembersDatabase<osmium::Node> m_member_nodes_db;
relations::MembersDatabase<osmium::Way> m_member_ways_db;
relations::MembersDatabase<osmium::Relation> m_member_relations_db;
osmium::memory::CallbackBuffer m_output;
public:
RelationsManagerBase() :
m_relations_db(m_stash),
m_member_nodes_db(m_stash, m_relations_db),
m_member_ways_db(m_stash, m_relations_db),
m_member_relations_db(m_stash, m_relations_db) {
}
osmium::relations::RelationsDatabase& relations_database() noexcept {
return m_relations_db;
}
osmium::relations::MembersDatabase<osmium::Node>& member_nodes_database() noexcept {
return m_member_nodes_db;
}
const osmium::relations::MembersDatabase<osmium::Node>& member_nodes_database() const noexcept {
return m_member_nodes_db;
}
osmium::relations::MembersDatabase<osmium::Way>& member_ways_database() noexcept {
return m_member_ways_db;
}
const osmium::relations::MembersDatabase<osmium::Way>& member_ways_database() const noexcept {
return m_member_ways_db;
}
osmium::relations::MembersDatabase<osmium::Relation>& member_relations_database() noexcept {
return m_member_relations_db;
}
const osmium::relations::MembersDatabase<osmium::Relation>& member_relations_database() const noexcept {
return m_member_relations_db;
}
relations::MembersDatabaseCommon& member_database(osmium::item_type type) noexcept {
switch (type) {
case osmium::item_type::node:
return m_member_nodes_db;
case osmium::item_type::way:
return m_member_ways_db;
case osmium::item_type::relation:
return m_member_relations_db;
default:
break;
}
assert(false && "Should not be here");
return m_member_nodes_db;
}
const relations::MembersDatabaseCommon& member_database(osmium::item_type type) const noexcept {
switch (type) {
case osmium::item_type::node:
return m_member_nodes_db;
case osmium::item_type::way:
return m_member_ways_db;
case osmium::item_type::relation:
return m_member_relations_db;
default:
break;
}
assert(false && "Should not be here");
return m_member_nodes_db;
}
const osmium::OSMObject* get_member_object(const osmium::RelationMember& member) const noexcept {
if (member.ref() == 0) {
return nullptr;
}
return member_database(member.type()).get_object(member.ref());
}
const osmium::Node* get_member_node(osmium::object_id_type id) const noexcept {
if (id == 0) {
return nullptr;
}
return member_nodes_database().get(id);
}
const osmium::Way* get_member_way(osmium::object_id_type id) const noexcept {
if (id == 0) {
return nullptr;
}
return member_ways_database().get(id);
}
const osmium::Relation* get_member_relation(osmium::object_id_type id) const noexcept {
if (id == 0) {
return nullptr;
}
return member_relations_database().get(id);
}
void prepare_for_lookup() {
m_member_nodes_db.prepare_for_lookup();
m_member_ways_db.prepare_for_lookup();
m_member_relations_db.prepare_for_lookup();
}
relations_manager_memory_usage used_memory() const noexcept {
return {
m_relations_db.used_memory(),
m_member_nodes_db.used_memory()
+ m_member_ways_db.used_memory()
+ m_member_relations_db.used_memory(),
m_stash.used_memory()
};
}
osmium::memory::Buffer& buffer() noexcept {
return m_output.buffer();
}
void set_callback(const std::function<void(osmium::memory::Buffer&&)>& callback) {
m_output.set_callback(callback);
}
void flush_output() {
m_output.flush();
}
void possibly_flush() {
m_output.possibly_flush();
}
osmium::memory::Buffer read() {
return m_output.read();
}
}; // class RelationsManagerBase
template <typename TManager, bool TNodes, bool TWays, bool TRelations, bool TCheckOrder = true>
class RelationsManager : public RelationsManagerBase {
using check_order_handler = std::conditional_t<TCheckOrder, osmium::handler::CheckOrder, osmium::handler::Handler>;
check_order_handler m_check_order_handler;
SecondPassHandler<RelationsManager> m_handler_pass2;
static bool wanted_type(osmium::item_type type) noexcept {
return (TNodes && type == osmium::item_type::node) ||
(TWays && type == osmium::item_type::way) ||
(TRelations && type == osmium::item_type::relation);
}
bool new_relation(const osmium::Relation& /*relation*/) const noexcept {
return true;
}
bool new_member(const osmium::Relation& /*relation*/, const osmium::RelationMember& /*member*/, std::size_t /*n*/) const noexcept {
return true;
}
void complete_relation(const osmium::Relation& /*relation*/) const noexcept {
}
void before_node(const osmium::Node& /*node*/) const noexcept {
}
void node_not_in_any_relation(const osmium::Node& /*node*/) const noexcept {
}
void after_node(const osmium::Node& /*node*/) const noexcept {
}
void before_way(const osmium::Way& /*way*/) const noexcept {
}
void way_not_in_any_relation(const osmium::Way& /*way*/) const noexcept {
}
void after_way(const osmium::Way& /*way*/) const noexcept {
}
void before_relation(const osmium::Relation& /*relation*/) const noexcept {
}
void relation_not_in_any_relation(const osmium::Relation& /*relation*/) const noexcept {
}
void after_relation(const osmium::Relation& /*relation*/) const noexcept {
}
TManager& derived() noexcept {
return *static_cast<TManager*>(this);
}
void handle_complete_relation(RelationHandle& rel_handle) {
derived().complete_relation(*rel_handle);
possibly_flush();
for (const auto& member : rel_handle->members()) {
if (member.ref() != 0) {
member_database(member.type()).remove(member.ref(), rel_handle->id());
}
}
rel_handle.remove();
}
public:
RelationsManager() :
RelationsManagerBase(),
m_check_order_handler(),
m_handler_pass2(*this) {
}
SecondPassHandler<RelationsManager>& handler(const std::function<void(osmium::memory::Buffer&&)>& callback = nullptr) {
set_callback(callback);
return m_handler_pass2;
}
void relation(const osmium::Relation& relation) {
if (derived().new_relation(relation)) {
auto rel_handle = relations_database().add(relation);
std::size_t n = 0;
for (auto& member : rel_handle->members()) {
if (wanted_type(member.type()) &&
derived().new_member(relation, member, n)) {
member_database(member.type()).track(rel_handle, member.ref(), n);
} else {
member.set_ref(0); // set member id to zero to indicate we are not interested
}
++n;
}
}
}
void handle_node(const osmium::Node& node) {
if (TNodes) {
m_check_order_handler.node(node);
derived().before_node(node);
const bool added = member_nodes_database().add(node, [this](RelationHandle& rel_handle) {
handle_complete_relation(rel_handle);
});
if (!added) {
derived().node_not_in_any_relation(node);
}
derived().after_node(node);
possibly_flush();
}
}
void handle_way(const osmium::Way& way) {
if (TWays) {
m_check_order_handler.way(way);
derived().before_way(way);
const bool added = member_ways_database().add(way, [this](RelationHandle& rel_handle) {
handle_complete_relation(rel_handle);
});
if (!added) {
derived().way_not_in_any_relation(way);
}
derived().after_way(way);
possibly_flush();
}
}
void handle_relation(const osmium::Relation& relation) {
if (TRelations) {
m_check_order_handler.relation(relation);
derived().before_relation(relation);
const bool added = member_relations_database().add(relation, [this](RelationHandle& rel_handle) {
handle_complete_relation(rel_handle);
});
if (!added) {
derived().relation_not_in_any_relation(relation);
}
derived().after_relation(relation);
possibly_flush();
}
}
template <typename TFunc>
void for_each_incomplete_relation(TFunc&& func) {
relations_database().for_each_relation(std::forward<TFunc>(func));
}
}; // class RelationsManager
} // namespace relations
} // namespace osmium
#endif // OSMIUM_RELATIONS_RELATIONS_MANAGER_HPP
buffer.hpp
callback_buffer.hpp
check_order.hpp
osmium::ItemStash
Definition item_stash.hpp:57
osmium::ItemStash::used_memory
std::size_t used_memory() const noexcept
Definition item_stash.hpp:208
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::memory::CallbackBuffer
Definition callback_buffer.hpp:70
osmium::memory::CallbackBuffer::buffer
osmium::memory::Buffer & buffer() noexcept
Definition callback_buffer.hpp:133
osmium::memory::CallbackBuffer::set_callback
void set_callback(const callback_func_type &callback=nullptr) noexcept
Definition callback_buffer.hpp:144
osmium::memory::CallbackBuffer::read
osmium::memory::Buffer read()
Definition callback_buffer.hpp:181
osmium::memory::CallbackBuffer::possibly_flush
void possibly_flush()
Definition callback_buffer.hpp:170
osmium::memory::CallbackBuffer::flush
void flush()
Definition callback_buffer.hpp:156
osmium::relations::MembersDatabaseCommon
Definition members_database.hpp:63
osmium::relations::MembersDatabaseCommon::prepare_for_lookup
void prepare_for_lookup()
Definition members_database.hpp:260
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::MembersDatabaseCommon::used_memory
std::size_t used_memory() const noexcept
Definition members_database.hpp:188
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::RelationsDatabase::used_memory
std::size_t used_memory() const noexcept
Definition relations_database.hpp:144
osmium::relations::RelationsManagerBase
Definition relations_manager.hpp:74
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
Definition relations_manager.hpp:305
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
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition types.hpp:45
osmium::item_type
item_type
Definition item_type.hpp:45
osmium::item_type::relation
@ relation
osmium::item_type::node
@ node
osmium::item_type::way
@ way
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