Libosmium  2.6.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 <cassert>
38 #include <cstddef>
39 #include <cstring>
40 #include <vector>
41 
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  typedef typename TAssembler::config_type assembler_config_type;
80  const assembler_config_type m_assembler_config;
81 
83 
84  static constexpr size_t initial_output_buffer_size = 1024 * 1024;
85  static constexpr size_t max_buffer_size_for_flush = 100 * 1024;
86 
88  if (this->callback()) {
89  osmium::memory::Buffer buffer(initial_output_buffer_size);
90  using std::swap;
91  swap(buffer, m_output_buffer);
92  this->callback()(std::move(buffer));
93  }
94  }
95 
97  if (m_output_buffer.committed() > max_buffer_size_for_flush) {
98  flush_output_buffer();
99  }
100  }
101 
102  public:
103 
104  explicit MultipolygonCollector(const assembler_config_type& assembler_config) :
105  collector_type(),
106  m_assembler_config(assembler_config),
107  m_output_buffer(initial_output_buffer_size, osmium::memory::Buffer::auto_grow::yes) {
108  }
109 
116  bool keep_relation(const osmium::Relation& relation) const {
117  const char* type = relation.tags().get_value_by_key("type");
118 
119  // ignore relations without "type" tag
120  if (!type) {
121  return false;
122  }
123 
124  if ((!strcmp(type, "multipolygon")) || (!strcmp(type, "boundary"))) {
125  return true;
126  }
127 
128  return false;
129  }
130 
134  bool keep_member(const osmium::relations::RelationMeta& /*relation_meta*/, const osmium::RelationMember& member) const {
135  // We are only interested in members of type way.
136  return member.type() == osmium::item_type::way;
137  }
138 
146  // you need at least 4 nodes to make up a polygon
147  if (way.nodes().size() <= 3) {
148  return;
149  }
150  try {
151  if (!way.nodes().front().location() || !way.nodes().back().location()) {
152  throw osmium::invalid_location("invalid location");
153  }
154  if (way.ends_have_same_location()) {
155  // way is closed and has enough nodes, build simple multipolygon
156  TAssembler assembler(m_assembler_config);
157  assembler(way, m_output_buffer);
158  possibly_flush_output_buffer();
159  }
160  } catch (osmium::invalid_location&) {
161  // XXX ignore
162  }
163  }
164 
165  void complete_relation(osmium::relations::RelationMeta& relation_meta) {
166  const osmium::Relation& relation = this->get_relation(relation_meta);
167  std::vector<size_t> offsets;
168  for (const auto& member : relation.members()) {
169  if (member.ref() != 0) {
170  offsets.push_back(this->get_offset(member.type(), member.ref()));
171  }
172  }
173  try {
174  TAssembler assembler(m_assembler_config);
175  assembler(relation, offsets, this->members_buffer(), m_output_buffer);
176  possibly_flush_output_buffer();
177  } catch (osmium::invalid_location&) {
178  // XXX ignore
179  }
180  }
181 
182  void flush() {
183  flush_output_buffer();
184  }
185 
187  osmium::memory::Buffer buffer(initial_output_buffer_size, osmium::memory::Buffer::auto_grow::yes);
188 
189  using std::swap;
190  swap(buffer, m_output_buffer);
191 
192  return buffer;
193  }
194 
195  }; // class MultipolygonCollector
196 
197  } // namespace area
198 
199 } // namespace osmium
200 
201 #endif // OSMIUM_AREA_MULTIPOLYGON_COLLECTOR_HPP
WayNodeList & nodes()
Definition: way.hpp:75
void possibly_flush_output_buffer()
Definition: multipolygon_collector.hpp:96
type
Definition: entity_bits.hpp:60
bool keep_member(const osmium::relations::RelationMeta &, const osmium::RelationMember &member) const
Definition: multipolygon_collector.hpp:134
RelationMemberList & members()
Definition: relation.hpp:177
osmium::relations::Collector< MultipolygonCollector< TAssembler >, false, true, false > collector_type
Definition: multipolygon_collector.hpp:77
Definition: relation.hpp:165
MultipolygonCollector(const assembler_config_type &assembler_config)
Definition: multipolygon_collector.hpp:104
size_t size() const noexcept
Definition: node_ref_list.hpp:68
Definition: entity_bits.hpp:67
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:721
Definition: way.hpp:65
void way_not_in_any_relation(const osmium::Way &way)
Definition: multipolygon_collector.hpp:145
Definition: relation.hpp:54
void flush()
Definition: multipolygon_collector.hpp:182
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
Definition: collector.hpp:105
Definition: location.hpp:53
const assembler_config_type m_assembler_config
Definition: multipolygon_collector.hpp:80
bool ends_have_same_location() const
Definition: way.hpp:106
osmium::memory::Buffer read()
Definition: multipolygon_collector.hpp:186
bool keep_relation(const osmium::Relation &relation) const
Definition: multipolygon_collector.hpp:116
TAssembler::config_type assembler_config_type
Definition: multipolygon_collector.hpp:79
void complete_relation(osmium::relations::RelationMeta &relation_meta)
Definition: multipolygon_collector.hpp:165
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:295
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
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:119
void flush_output_buffer()
Definition: multipolygon_collector.hpp:87