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