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