Libosmium  2.14.0
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-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 
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;
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()) {
92  using std::swap;
93  swap(buffer, m_output_buffer);
94  this->callback()(std::move(buffer));
95  }
96  }
97 
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 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  return (!std::strcmp(type, "multipolygon")) || (!std::strcmp(type, "boundary"));
131  }
132 
136  bool keep_member(const osmium::relations::RelationMeta& /*relation_meta*/, const osmium::RelationMember& member) const {
137  // We are only interested in members of type way.
138  return member.type() == osmium::item_type::way;
139  }
140 
148  // you need at least 4 nodes to make up a polygon
149  if (way.nodes().size() <= 3) {
150  return;
151  }
152  try {
153  if (!way.nodes().front().location() || !way.nodes().back().location()) {
154  throw osmium::invalid_location{"invalid location"};
155  }
156  if (way.ends_have_same_location()) {
157  // way is closed and has enough nodes, build simple multipolygon
158  TAssembler assembler{m_assembler_config};
159  assembler(way, m_output_buffer);
160  m_stats += assembler.stats();
162  }
163  } catch (const osmium::invalid_location&) {
164  // XXX ignore
165  }
166  }
167 
168  void complete_relation(osmium::relations::RelationMeta& relation_meta) {
169  const osmium::Relation& relation = this->get_relation(relation_meta);
170  const osmium::memory::Buffer& buffer = this->members_buffer();
171 
172  std::vector<const osmium::Way*> ways;
173  ways.reserve(relation.members().size());
174  for (const auto& member : relation.members()) {
175  if (member.ref() != 0) {
176  const size_t offset = this->get_offset(member.type(), member.ref());
177  ways.push_back(&buffer.get<const osmium::Way>(offset));
178  }
179  }
180 
181  try {
182  TAssembler assembler{m_assembler_config};
183  assembler(relation, ways, m_output_buffer);
184  m_stats += assembler.stats();
186  } catch (const osmium::invalid_location&) {
187  // XXX ignore
188  }
189  }
190 
191  void flush() {
193  }
194 
197 
198  using std::swap;
199  swap(buffer, m_output_buffer);
200 
201  return buffer;
202  }
203 
204  }; // class MultipolygonCollector
205 
206  } // namespace area
207 
208 } // namespace osmium
209 
210 #endif // OSMIUM_AREA_MULTIPOLYGON_COLLECTOR_HPP
const area_stats & stats() const noexcept
Definition: multipolygon_collector.hpp:112
osmium::memory::Buffer & members_buffer()
Definition: collector.hpp:481
void possibly_flush_output_buffer()
Definition: multipolygon_collector.hpp:98
bool keep_relation(const osmium::Relation &relation) const
Definition: multipolygon_collector.hpp:122
type
Definition: entity_bits.hpp:63
static constexpr size_t initial_output_buffer_size
Definition: multipolygon_collector.hpp:86
Definition: relation.hpp:168
MultipolygonCollector(const assembler_config_type &assembler_config)
Definition: multipolygon_collector.hpp:106
static constexpr size_t max_buffer_size_for_flush
Definition: multipolygon_collector.hpp:87
Definition: entity_bits.hpp:72
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:755
size_t get_offset(osmium::item_type type, osmium::object_id_type id)
Definition: collector.hpp:511
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:147
Definition: relation.hpp:57
void flush()
Definition: multipolygon_collector.hpp:191
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:80
osmium::memory::Buffer read()
Definition: multipolygon_collector.hpp:195
Definition: stats.hpp:49
void complete_relation(osmium::relations::RelationMeta &relation_meta)
Definition: multipolygon_collector.hpp:168
item_type type() const noexcept
Definition: relation.hpp:132
Definition: multipolygon_collector.hpp:75
osmium::memory::Buffer m_output_buffer
Definition: multipolygon_collector.hpp:82
typename TAssembler::config_type assembler_config_type
Definition: multipolygon_collector.hpp:79
Definition: buffer.hpp:97
bool keep_member(const osmium::relations::RelationMeta &, const osmium::RelationMember &member) const
Definition: multipolygon_collector.hpp:136
const osmium::Relation & get_relation(size_t offset) const
Definition: collector.hpp:298
area_stats m_stats
Definition: multipolygon_collector.hpp:84
void flush_output_buffer()
Definition: multipolygon_collector.hpp:89