Libosmium  2.1.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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/relation.hpp>
45 #include <osmium/osm/tag.hpp>
46 #include <osmium/osm/way.hpp>
48 #include <osmium/relations/detail/member_meta.hpp>
49 
50 namespace osmium {
51 
52  struct invalid_location;
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  if (way.nodes().size() > 3 && way.ends_have_same_location()) {
146  // way is closed and has enough nodes, build simple multipolygon
147  try {
148  TAssembler assembler(m_assembler_config);
149  assembler(way, m_output_buffer);
151  } catch (osmium::invalid_location&) {
152  // XXX ignore
153  }
154  }
155  }
156 
157  void complete_relation(osmium::relations::RelationMeta& relation_meta) {
158  const osmium::Relation& relation = this->get_relation(relation_meta);
159  std::vector<size_t> offsets;
160  for (const auto& member : relation.members()) {
161  if (member.ref() != 0) {
162  offsets.push_back(this->get_offset(member.type(), member.ref()));
163  }
164  }
165  try {
166  TAssembler assembler(m_assembler_config);
167  assembler(relation, offsets, this->members_buffer(), m_output_buffer);
169  } catch (osmium::invalid_location&) {
170  // XXX ignore
171  }
172 
173  // clear member metas
174  for (const auto& member : relation.members()) {
175  if (member.ref() != 0) {
176  auto& mmv = this->member_meta(member.type());
177  auto range = std::equal_range(mmv.begin(), mmv.end(), osmium::relations::MemberMeta(member.ref()));
178  assert(range.first != range.second);
179 
180  // if this is the last time this object was needed
181  // then mark it as removed
182  if (osmium::relations::count_not_removed(range.first, range.second) == 1) {
183  this->get_member(range.first->buffer_offset()).set_removed(true);
184  }
185 
186  for (auto it = range.first; it != range.second; ++it) {
187  if (!it->removed() && relation.id() == this->get_relation(it->relation_pos()).id()) {
188  it->remove();
189  break;
190  }
191  }
192  }
193  }
194  }
195 
196  void flush() {
198  }
199 
201  osmium::memory::Buffer buffer(initial_output_buffer_size, osmium::memory::Buffer::auto_grow::yes);
202  std::swap(buffer, m_output_buffer);
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: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:174
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:162
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:196
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:200
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:157
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:294
item_type type() const noexcept
Definition: relation.hpp:123
Definition: multipolygon_collector.hpp:75
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:109
size_t committed() const noexcept
Definition: buffer.hpp:218
Definition: buffer.hpp:95
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