Libosmium  2.15.4
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 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2019 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/handler.hpp>
38 #include <osmium/memory/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>
48 #include <osmium/tags/taglist.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 
60 namespace 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 
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 
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 
127  return m_member_relations_db;
128  }
129 
132  return m_member_relations_db;
133  }
134 
142  switch (type) {
144  return m_member_nodes_db;
146  return m_member_ways_db;
148  return m_member_relations_db;
149  default:
150  break;
151  }
152  throw std::logic_error{"Should not be here."};
153  }
154 
162  switch (type) {
164  return m_member_nodes_db;
166  return m_member_ways_db;
168  return m_member_relations_db;
169  default:
170  break;
171  }
172  throw std::logic_error{"Should not be here."};
173  }
174 
181  const osmium::OSMObject* get_member_object(const osmium::RelationMember& member) const noexcept {
182  if (member.ref() == 0) {
183  return nullptr;
184  }
185  return member_database(member.type()).get_object(member.ref());
186  }
187 
196  if (id == 0) {
197  return nullptr;
198  }
199  return member_nodes_database().get(id);
200  }
201 
210  if (id == 0) {
211  return nullptr;
212  }
213  return member_ways_database().get(id);
214  }
215 
224  if (id == 0) {
225  return nullptr;
226  }
227  return member_relations_database().get(id);
228  }
229 
236  m_member_nodes_db.prepare_for_lookup();
237  m_member_ways_db.prepare_for_lookup();
238  m_member_relations_db.prepare_for_lookup();
239  }
240 
245  return {
246  m_relations_db.used_memory(),
247  m_member_nodes_db.used_memory()
248  + m_member_ways_db.used_memory()
249  + m_member_relations_db.used_memory(),
251  };
252  }
253 
256  return m_output.buffer();
257  }
258 
260  void set_callback(const std::function<void(osmium::memory::Buffer&&)>& callback) {
261  m_output.set_callback(callback);
262  }
263 
265  void flush_output() {
266  m_output.flush();
267  }
268 
270  void possibly_flush() {
272  }
273 
276  return m_output.read();
277  }
278 
279  }; // class RelationsManagerBase
280 
300  template <typename TManager, bool TNodes, bool TWays, bool TRelations, bool TCheckOrder = true>
302 
304 
306 
308 
309  static bool wanted_type(osmium::item_type type) noexcept {
310  return (TNodes && type == osmium::item_type::node) ||
311  (TWays && type == osmium::item_type::way) ||
312  (TRelations && type == osmium::item_type::relation);
313  }
314 
324  bool new_relation(const osmium::Relation& /*relation*/) const noexcept {
325  return true;
326  }
327 
338  bool new_member(const osmium::Relation& /*relation*/, const osmium::RelationMember& /*member*/, std::size_t /*n*/) const noexcept {
339  return true;
340  }
341 
348  void complete_relation(const osmium::Relation& /*relation*/) const noexcept {
349  }
350 
358  void before_node(const osmium::Node& /*node*/) const noexcept {
359  }
360 
368  void node_not_in_any_relation(const osmium::Node& /*node*/) const noexcept {
369  }
370 
378  void after_node(const osmium::Node& /*node*/) const noexcept {
379  }
380 
388  void before_way(const osmium::Way& /*way*/) const noexcept {
389  }
390 
398  void way_not_in_any_relation(const osmium::Way& /*way*/) const noexcept {
399  }
400 
408  void after_way(const osmium::Way& /*way*/) const noexcept {
409  }
410 
418  void before_relation(const osmium::Relation& /*relation*/) const noexcept {
419  }
420 
428  void relation_not_in_any_relation(const osmium::Relation& /*relation*/) const noexcept {
429  }
430 
438  void after_relation(const osmium::Relation& /*relation*/) const noexcept {
439  }
440 
441  TManager& derived() noexcept {
442  return *static_cast<TManager*>(this);
443  }
444 
446  derived().complete_relation(*rel_handle);
447  possibly_flush();
448 
449  for (const auto& member : rel_handle->members()) {
450  if (member.ref() != 0) {
451  member_database(member.type()).remove(member.ref(), rel_handle->id());
452  }
453  }
454 
455  rel_handle.remove();
456  }
457 
458  public:
459 
462  m_check_order_handler(),
463  m_handler_pass2(*this) {
464  }
465 
469  SecondPassHandler<RelationsManager>& handler(const std::function<void(osmium::memory::Buffer&&)>& callback = nullptr) {
470  set_callback(callback);
471  return m_handler_pass2;
472  }
473 
485  if (derived().new_relation(relation)) {
486  auto rel_handle = relations_database().add(relation);
487 
488  std::size_t n = 0;
489  for (auto& member : rel_handle->members()) {
490  if (wanted_type(member.type()) &&
491  derived().new_member(relation, member, n)) {
492  member_database(member.type()).track(rel_handle, member.ref(), n);
493  } else {
494  member.set_ref(0); // set member id to zero to indicate we are not interested
495  }
496  ++n;
497  }
498  }
499  }
500 
502  if (TNodes) {
503  m_check_order_handler.node(node);
504  derived().before_node(node);
505  const bool added = member_nodes_database().add(node, [this](RelationHandle& rel_handle) {
506  handle_complete_relation(rel_handle);
507  });
508  if (!added) {
509  derived().node_not_in_any_relation(node);
510  }
511  derived().after_node(node);
512  possibly_flush();
513  }
514  }
515 
516  void handle_way(const osmium::Way& way) {
517  if (TWays) {
518  m_check_order_handler.way(way);
519  derived().before_way(way);
520  const bool added = member_ways_database().add(way, [this](RelationHandle& rel_handle) {
521  handle_complete_relation(rel_handle);
522  });
523  if (!added) {
524  derived().way_not_in_any_relation(way);
525  }
526  derived().after_way(way);
527  possibly_flush();
528  }
529  }
530 
532  if (TRelations) {
533  m_check_order_handler.relation(relation);
534  derived().before_relation(relation);
535  const bool added = member_relations_database().add(relation, [this](RelationHandle& rel_handle) {
536  handle_complete_relation(rel_handle);
537  });
538  if (!added) {
539  derived().relation_not_in_any_relation(relation);
540  }
541  derived().after_relation(relation);
542  possibly_flush();
543  }
544  }
545 
554  template <typename TFunc>
555  void for_each_incomplete_relation(TFunc&& func) {
556  relations_database().for_each_relation(std::forward<TFunc>(func));
557  }
558 
559  }; // class RelationsManager
560 
561  } // namespace relations
562 
563 } // namespace osmium
564 
565 #endif // OSMIUM_RELATIONS_RELATIONS_MANAGER_HPP
void remove()
Definition: relations_database.hpp:274
std::size_t used_memory() const noexcept
Definition: members_database.hpp:187
void handle_node(const osmium::Node &node)
Definition: relations_manager.hpp:501
void set_callback(const callback_func_type &callback=nullptr) noexcept
Definition: callback_buffer.hpp:144
type
Definition: entity_bits.hpp:63
osmium::memory::Buffer read()
Return the contents of the output buffer.
Definition: relations_manager.hpp:275
void flush_output()
Flush the output buffer.
Definition: relations_manager.hpp:265
void way(const osmium::Way &) const noexcept
Definition: handler.hpp:81
RelationMemberList & members()
Get a reference to the member list.
Definition: relation.hpp:186
relations_manager_memory_usage used_memory() const noexcept
Definition: relations_manager.hpp:244
Definition: relations_manager.hpp:74
void node(const osmium::Node &) const noexcept
Definition: handler.hpp:78
osmium::memory::Buffer & buffer() noexcept
Access the output buffer.
Definition: relations_manager.hpp:255
void for_each_relation(TFunc &&func)
Definition: relations_database.hpp:323
const TObject * get(osmium::object_id_type id) const
Definition: members_database.hpp:395
bool add(const TObject &object, TFunc &&func)
Definition: members_database.hpp:358
item_type
Definition: item_type.hpp:43
Definition: relation.hpp:168
relations::MembersDatabase< osmium::Relation > m_member_relations_db
Definition: relations_manager.hpp:86
void handle_way(const osmium::Way &way)
Definition: relations_manager.hpp:516
osmium::memory::Buffer & buffer() noexcept
Definition: callback_buffer.hpp:133
Definition: handler.hpp:71
relations::MembersDatabase< osmium::Way > m_member_ways_db
Definition: relations_manager.hpp:85
std::size_t used_memory() const noexcept
Definition: item_stash.hpp:208
void possibly_flush()
Definition: callback_buffer.hpp:170
bool new_member(const osmium::Relation &, const osmium::RelationMember &, std::size_t) const noexcept
Definition: relations_manager.hpp:338
SecondPassHandler< RelationsManager > m_handler_pass2
Definition: relations_manager.hpp:307
void node_not_in_any_relation(const osmium::Node &) const noexcept
Definition: relations_manager.hpp:368
std::size_t used_memory() const noexcept
Definition: relations_database.hpp:144
TManager & derived() noexcept
Definition: relations_manager.hpp:441
Definition: way.hpp:72
Definition: manager_util.hpp:64
void before_way(const osmium::Way &) const noexcept
Definition: relations_manager.hpp:388
const osmium::Relation * get_member_relation(osmium::object_id_type id) const noexcept
Definition: relations_manager.hpp:223
SecondPassHandler< RelationsManager > & handler(const std::function< void(osmium::memory::Buffer &&)> &callback=nullptr)
Definition: relations_manager.hpp:469
Definition: item_stash.hpp:57
void way_not_in_any_relation(const osmium::Way &) const noexcept
Definition: relations_manager.hpp:398
const osmium::relations::MembersDatabase< osmium::Way > & member_ways_database() const noexcept
Access the internal database containing member ways.
Definition: relations_manager.hpp:121
typename std::conditional< true, osmium::handler::CheckOrder, osmium::handler::Handler >::type check_order_handler
Definition: relations_manager.hpp:303
osmium::ItemStash m_stash
Definition: relations_manager.hpp:78
Definition: relation.hpp:57
osmium::relations::MembersDatabase< osmium::Relation > & member_relations_database() noexcept
Access the internal database containing member relations.
Definition: relations_manager.hpp:126
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
void relation(const osmium::Relation &relation)
Definition: relations_manager.hpp:484
osmium::memory::Buffer read()
Definition: callback_buffer.hpp:181
RelationsManagerBase()
Definition: relations_manager.hpp:93
bool new_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:324
void handle_complete_relation(RelationHandle &rel_handle)
Definition: relations_manager.hpp:445
void after_way(const osmium::Way &) const noexcept
Definition: relations_manager.hpp:408
osmium::relations::MembersDatabase< osmium::Node > & member_nodes_database() noexcept
Access the internal database containing member nodes.
Definition: relations_manager.hpp:106
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
void possibly_flush()
Flush the output buffer if it is full.
Definition: relations_manager.hpp:270
relations::MembersDatabaseCommon & member_database(osmium::item_type type)
Definition: relations_manager.hpp:141
const osmium::relations::MembersDatabase< osmium::Node > & member_nodes_database() const noexcept
Access the internal database containing member nodes.
Definition: relations_manager.hpp:111
Definition: relations_database.hpp:208
const osmium::Way * get_member_way(osmium::object_id_type id) const noexcept
Definition: relations_manager.hpp:209
void prepare_for_lookup()
Definition: relations_manager.hpp:235
osmium::relations::RelationsDatabase & relations_database() noexcept
Access the internal RelationsDatabase.
Definition: relations_manager.hpp:101
Definition: relations_database.hpp:82
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:122
const relations::MembersDatabaseCommon & member_database(osmium::item_type type) const
Definition: relations_manager.hpp:161
void relation(const osmium::Relation &) const noexcept
Definition: handler.hpp:84
void flush()
Definition: callback_buffer.hpp:156
Definition: buffer.hpp:97
RelationHandle add(const osmium::Relation &relation)
Definition: relations_database.hpp:317
Definition: node.hpp:48
void before_node(const osmium::Node &) const noexcept
Definition: relations_manager.hpp:358
osmium::memory::CallbackBuffer m_output
Output buffer.
Definition: relations_manager.hpp:89
relations::MembersDatabase< osmium::Node > m_member_nodes_db
Databases of all members we are interested in.
Definition: relations_manager.hpp:84
const osmium::Node * get_member_node(osmium::object_id_type id) const noexcept
Definition: relations_manager.hpp:195
relations::RelationsDatabase m_relations_db
Database of all relations we are interested in.
Definition: relations_manager.hpp:81
RelationsManager()
Definition: relations_manager.hpp:460
static bool wanted_type(osmium::item_type type) noexcept
Definition: relations_manager.hpp:309
const osmium::OSMObject * get_member_object(const osmium::RelationMember &member) const noexcept
Definition: relations_manager.hpp:181
Definition: members_database.hpp:62
void after_node(const osmium::Node &) const noexcept
Definition: relations_manager.hpp:378
Definition: callback_buffer.hpp:70
Definition: relations_manager.hpp:301
void before_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:418
check_order_handler m_check_order_handler
Definition: relations_manager.hpp:305
const osmium::relations::MembersDatabase< osmium::Relation > & member_relations_database() const noexcept
Access the internal database containing member relations.
Definition: relations_manager.hpp:131
void prepare_for_lookup()
Definition: members_database.hpp:259
void handle_relation(const osmium::Relation &relation)
Definition: relations_manager.hpp:531
void relation_not_in_any_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:428
void for_each_incomplete_relation(TFunc &&func)
Definition: relations_manager.hpp:555
void after_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:438
void complete_relation(const osmium::Relation &) const noexcept
Definition: relations_manager.hpp:348
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:260
osmium::relations::MembersDatabase< osmium::Way > & member_ways_database() noexcept
Access the internal database containing member ways.
Definition: relations_manager.hpp:116
Definition: object.hpp:64