Libosmium  2.3.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-2015 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  }
57 
61  namespace area {
62 
74  template <class 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  std::swap(buffer, m_output_buffer);
91  this->callback()(std::move(buffer));
92  }
93  }
94 
96  if (m_output_buffer.committed() > max_buffer_size_for_flush) {
98  }
99  }
100 
101  public:
102 
103  explicit MultipolygonCollector(const assembler_config_type& assembler_config) :
104  collector_type(),
105  m_assembler_config(assembler_config),
106  m_output_buffer(initial_output_buffer_size, osmium::memory::Buffer::auto_grow::yes) {
107  }
108 
115  bool keep_relation(const osmium::Relation& relation) const {
116  const char* type = relation.tags().get_value_by_key("type");
117 
118  // ignore relations without "type" tag
119  if (!type) {
120  return false;
121  }
122 
123  if ((!strcmp(type, "multipolygon")) || (!strcmp(type, "boundary"))) {
124  return true;
125  }
126 
127  return false;
128  }
129 
133  bool keep_member(const osmium::relations::RelationMeta& /*relation_meta*/, const osmium::RelationMember& member) const {
134  // We are only interested in members of type way.
135  return member.type() == osmium::item_type::way;
136  }
137 
145  // you need at least 4 nodes to make up a polygon
146  if (way.nodes().size() <= 3) {
147  return;
148  }
149  try {
150  if (!way.nodes().front().location() || !way.nodes().back().location()) {
151  throw osmium::invalid_location("invalid location");
152  }
153  if (way.ends_have_same_location()) {
154  // way is closed and has enough nodes, build simple multipolygon
155  TAssembler assembler(m_assembler_config);
156  assembler(way, m_output_buffer);
158  }
159  } catch (osmium::invalid_location&) {
160  // XXX ignore
161  }
162  }
163 
164  void complete_relation(osmium::relations::RelationMeta& relation_meta) {
165  const osmium::Relation& relation = this->get_relation(relation_meta);
166  std::vector<size_t> offsets;
167  for (const auto& member : relation.members()) {
168  if (member.ref() != 0) {
169  offsets.push_back(this->get_offset(member.type(), member.ref()));
170  }
171  }
172  try {
173  TAssembler assembler(m_assembler_config);
174  assembler(relation, offsets, this->members_buffer(), m_output_buffer);
176  } catch (osmium::invalid_location&) {
177  // XXX ignore
178  }
179 
180  // clear member metas
181  for (const auto& member : relation.members()) {
182  if (member.ref() != 0) {
183  auto& mmv = this->member_meta(member.type());
184  auto range = std::equal_range(mmv.begin(), mmv.end(), osmium::relations::MemberMeta(member.ref()));
185  assert(range.first != range.second);
186 
187  // if this is the last time this object was needed
188  // then mark it as removed
189  if (osmium::relations::count_not_removed(range.first, range.second) == 1) {
190  this->get_member(range.first->buffer_offset()).set_removed(true);
191  }
192 
193  for (auto it = range.first; it != range.second; ++it) {
194  if (!it->removed() && relation.id() == this->get_relation(it->relation_pos()).id()) {
195  it->remove();
196  break;
197  }
198  }
199  }
200  }
201  }
202 
203  void flush() {
205  }
206 
208  osmium::memory::Buffer buffer(initial_output_buffer_size, osmium::memory::Buffer::auto_grow::yes);
209  std::swap(buffer, m_output_buffer);
210  return buffer;
211  }
212 
213  }; // class MultipolygonCollector
214 
215  } // namespace area
216 
217 } // namespace osmium
218 
219 #endif // OSMIUM_AREA_MULTIPOLYGON_COLLECTOR_HPP
WayNodeList & nodes()
Definition: way.hpp:75
osmium::memory::Buffer & members_buffer()
Definition: collector.hpp:466
void possibly_flush_output_buffer()
Definition: multipolygon_collector.hpp:95
type
Definition: entity_bits.hpp:60
bool keep_member(const osmium::relations::RelationMeta &, const osmium::RelationMember &member) const
Definition: multipolygon_collector.hpp:133
RelationMemberList & members()
Definition: relation.hpp:179
osmium::relations::Collector< MultipolygonCollector< TAssembler >, false, true, false > collector_type
Definition: multipolygon_collector.hpp:77
static constexpr size_t initial_output_buffer_size
Definition: multipolygon_collector.hpp:84
Definition: relation.hpp:167
MultipolygonCollector(const assembler_config_type &assembler_config)
Definition: multipolygon_collector.hpp:103
size_t size() const noexcept
Definition: node_ref_list.hpp:68
static constexpr size_t max_buffer_size_for_flush
Definition: multipolygon_collector.hpp:85
Definition: entity_bits.hpp:67
size_t get_offset(osmium::item_type type, osmium::object_id_type id)
Definition: collector.hpp:470
Definition: way.hpp:65
std::vector< MemberMeta > & member_meta(const item_type type)
Definition: collector.hpp:269
void way_not_in_any_relation(const osmium::Way &way)
Definition: multipolygon_collector.hpp:144
Definition: relation.hpp:54
void flush()
Definition: multipolygon_collector.hpp:203
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
Definition: collector.hpp:95
const osmium::Relation & get_relation(size_t offset) const
Definition: collector.hpp:363
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::OSMObject & get_member(size_t offset) const
Definition: collector.hpp:374
osmium::memory::Buffer read()
Definition: multipolygon_collector.hpp:207
bool keep_relation(const osmium::Relation &relation) const
Definition: multipolygon_collector.hpp:115
TAssembler::config_type assembler_config_type
Definition: multipolygon_collector.hpp:79
void complete_relation(osmium::relations::RelationMeta &relation_meta)
Definition: multipolygon_collector.hpp:164
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:295
item_type type() const noexcept
Definition: relation.hpp:128
Definition: multipolygon_collector.hpp:75
osmium::Location & location() noexcept
Definition: node_ref.hpp:73
osmium::memory::Buffer m_output_buffer
Definition: multipolygon_collector.hpp:82
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:110
size_t committed() const noexcept
Definition: buffer.hpp:217
Definition: buffer.hpp:94
const NodeRef & front() const noexcept
Definition: node_ref_list.hpp:91
const NodeRef & back() const noexcept
Definition: node_ref_list.hpp:101
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