Libosmium  2.14.2
Fast and flexible C++ library for working with OpenStreetMap data
multipolygon_collector.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_AREA_MULTIPOLYGON_COLLECTOR_HPP
2 #define OSMIUM_AREA_MULTIPOLYGON_COLLECTOR_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2018 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/area/stats.hpp>
37 #include <osmium/memory/buffer.hpp>
38 #include <osmium/osm/item_type.hpp>
39 #include <osmium/osm/location.hpp>
40 #include <osmium/osm/node_ref.hpp>
41 #include <osmium/osm/relation.hpp>
42 #include <osmium/osm/tag.hpp>
43 #include <osmium/osm/way.hpp>
45 
46 #include <algorithm>
47 #include <cstddef>
48 #include <cstring>
49 #include <vector>
50 
51 namespace osmium {
52 
53  namespace relations {
54  class RelationMeta;
55  } // namespace relations
56 
60  namespace area {
61 
76  template <typename TAssembler>
77  class MultipolygonCollector : public osmium::relations::Collector<MultipolygonCollector<TAssembler>, false, true, false> {
78 
80 
81  using assembler_config_type = typename TAssembler::config_type;
82  const assembler_config_type m_assembler_config;
83 
85 
87 
88  static constexpr size_t initial_output_buffer_size = 1024 * 1024;
89  static constexpr size_t max_buffer_size_for_flush = 100 * 1024;
90 
92  if (this->callback()) {
93  osmium::memory::Buffer buffer{initial_output_buffer_size};
94  using std::swap;
95  swap(buffer, m_output_buffer);
96  this->callback()(std::move(buffer));
97  }
98  }
99 
101  if (m_output_buffer.committed() > max_buffer_size_for_flush) {
102  flush_output_buffer();
103  }
104  }
105 
106  public:
107 
108  explicit MultipolygonCollector(const assembler_config_type& assembler_config) :
109  collector_type(),
110  m_assembler_config(assembler_config),
111  m_output_buffer(initial_output_buffer_size, osmium::memory::Buffer::auto_grow::yes) {
112  }
113 
114  const area_stats& stats() const noexcept {
115  return m_stats;
116  }
117 
124  bool keep_relation(const osmium::Relation& relation) const {
125  const char* type = relation.tags().get_value_by_key("type");
126 
127  // ignore relations without "type" tag
128  if (!type) {
129  return false;
130  }
131 
132  return (!std::strcmp(type, "multipolygon")) || (!std::strcmp(type, "boundary"));
133  }
134 
138  bool keep_member(const osmium::relations::RelationMeta& /*relation_meta*/, const osmium::RelationMember& member) const {
139  // We are only interested in members of type way.
140  return member.type() == osmium::item_type::way;
141  }
142 
150  // you need at least 4 nodes to make up a polygon
151  if (way.nodes().size() <= 3) {
152  return;
153  }
154  try {
155  if (!way.nodes().front().location() || !way.nodes().back().location()) {
156  throw osmium::invalid_location{"invalid location"};
157  }
158  if (way.ends_have_same_location()) {
159  // way is closed and has enough nodes, build simple multipolygon
160  TAssembler assembler{m_assembler_config};
161  assembler(way, m_output_buffer);
162  m_stats += assembler.stats();
163  possibly_flush_output_buffer();
164  }
165  } catch (const osmium::invalid_location&) {
166  // XXX ignore
167  }
168  }
169 
170  void complete_relation(osmium::relations::RelationMeta& relation_meta) {
171  const osmium::Relation& relation = this->get_relation(relation_meta);
172  const osmium::memory::Buffer& buffer = this->members_buffer();
173 
174  std::vector<const osmium::Way*> ways;
175  ways.reserve(relation.members().size());
176  for (const auto& member : relation.members()) {
177  if (member.ref() != 0) {
178  const size_t offset = this->get_offset(member.type(), member.ref());
179  ways.push_back(&buffer.get<const osmium::Way>(offset));
180  }
181  }
182 
183  try {
184  TAssembler assembler{m_assembler_config};
185  assembler(relation, ways, m_output_buffer);
186  m_stats += assembler.stats();
187  possibly_flush_output_buffer();
188  } catch (const osmium::invalid_location&) {
189  // XXX ignore
190  }
191  }
192 
193  void flush() {
194  flush_output_buffer();
195  }
196 
198  osmium::memory::Buffer buffer{initial_output_buffer_size, osmium::memory::Buffer::auto_grow::yes};
199 
200  using std::swap;
201  swap(buffer, m_output_buffer);
202 
203  return buffer;
204  }
205 
206  }; // class MultipolygonCollector
207 
208  } // namespace area
209 
210 } // namespace osmium
211 
212 #endif // OSMIUM_AREA_MULTIPOLYGON_COLLECTOR_HPP
WayNodeList & nodes()
Definition: way.hpp:89
const area_stats & stats() const noexcept
Definition: multipolygon_collector.hpp:114
void possibly_flush_output_buffer()
Definition: multipolygon_collector.hpp:100
bool keep_relation(const osmium::Relation &relation) const
Definition: multipolygon_collector.hpp:124
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:327
type
Definition: entity_bits.hpp:63
RelationMemberList & members()
Get a reference to the member list.
Definition: relation.hpp:186
Definition: relation.hpp:168
MultipolygonCollector(const assembler_config_type &assembler_config)
Definition: multipolygon_collector.hpp:108
Definition: entity_bits.hpp:72
size_type size() const noexcept
Definition: collection.hpp:152
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:755
Definition: way.hpp:72
std::size_t committed() const noexcept
Definition: buffer.hpp:261
void way_not_in_any_relation(const osmium::Way &way)
Definition: multipolygon_collector.hpp:149
Definition: relation.hpp:57
void flush()
Definition: multipolygon_collector.hpp:193
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: collector.hpp:97
T & get(const std::size_t offset) const
Definition: buffer.hpp:398
Definition: location.hpp:53
const assembler_config_type m_assembler_config
Definition: multipolygon_collector.hpp:82
osmium::memory::Buffer read()
Definition: multipolygon_collector.hpp:197
Definition: stats.hpp:49
void complete_relation(osmium::relations::RelationMeta &relation_meta)
Definition: multipolygon_collector.hpp:170
item_type type() const noexcept
Definition: relation.hpp:132
Definition: multipolygon_collector.hpp:77
osmium::Location & location() noexcept
Definition: node_ref.hpp:85
osmium::memory::Buffer m_output_buffer
Definition: multipolygon_collector.hpp:84
bool ends_have_same_location() const
Definition: way.hpp:142
Definition: buffer.hpp:97
bool keep_member(const osmium::relations::RelationMeta &, const osmium::RelationMember &member) const
Definition: multipolygon_collector.hpp:138
const NodeRef & front() const noexcept
Definition: node_ref_list.hpp:126
const NodeRef & back() const noexcept
Definition: node_ref_list.hpp:138
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:127
area_stats m_stats
Definition: multipolygon_collector.hpp:86
void flush_output_buffer()
Definition: multipolygon_collector.hpp:91
size_type size() const noexcept
Definition: node_ref_list.hpp:83