Libosmium  2.13.1
Fast and flexible C++ library for working with OpenStreetMap data
assembler.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_AREA_ASSEMBLER_HPP
2 #define OSMIUM_AREA_ASSEMBLER_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 <cassert>
37 #include <iostream>
38 #include <vector>
39 
41 #include <osmium/area/detail/basic_assembler_with_tags.hpp>
42 #include <osmium/area/detail/segment_list.hpp>
44 #include <osmium/area/stats.hpp>
46 #include <osmium/memory/buffer.hpp>
47 #include <osmium/osm/item_type.hpp>
48 #include <osmium/osm/node_ref.hpp>
49 #include <osmium/osm/relation.hpp>
50 #include <osmium/osm/tag.hpp>
51 #include <osmium/osm/way.hpp>
52 
53 namespace osmium {
54 
55  namespace area {
56 
61  class Assembler : public detail::BasicAssemblerWithTags {
62 
63  bool create_area(osmium::memory::Buffer& out_buffer, const osmium::Way& way) {
64  osmium::builder::AreaBuilder builder{out_buffer};
65  builder.initialize_from_object(way);
66 
67  const bool area_okay = create_rings();
68  if (area_okay || config().create_empty_areas) {
69  builder.add_item(way.tags());
70  }
71  if (area_okay) {
72  add_rings_to_area(builder);
73  }
74 
75  if (report_ways()) {
76  config().problem_reporter->report_way(way);
77  }
78 
79  return area_okay || config().create_empty_areas;
80  }
81 
82  bool create_area(osmium::memory::Buffer& out_buffer, const osmium::Relation& relation, const std::vector<const osmium::Way*>& members) {
83  set_num_members(members.size());
84  osmium::builder::AreaBuilder builder{out_buffer};
85  builder.initialize_from_object(relation);
86 
87  const bool area_okay = create_rings();
88  if (area_okay || config().create_empty_areas) {
89  if (config().keep_type_tag) {
90  builder.add_item(relation.tags());
91  } else {
92  copy_tags_without_type(builder, relation.tags());
93  }
94  }
95  if (area_okay) {
96  add_rings_to_area(builder);
97  }
98 
99  if (report_ways()) {
100  for (const osmium::Way* way : members) {
101  config().problem_reporter->report_way(*way);
102  }
103  }
104 
105  return area_okay || config().create_empty_areas;
106  }
107 
108  public:
109 
110  explicit Assembler(const config_type& config) :
111  detail::BasicAssemblerWithTags(config) {
112  }
113 
114  ~Assembler() noexcept = default;
115 
123  bool operator()(const osmium::Way& way, osmium::memory::Buffer& out_buffer) {
124  if (!config().create_way_polygons) {
125  return true;
126  }
127 
128  if (config().problem_reporter) {
129  config().problem_reporter->set_object(osmium::item_type::way, way.id());
130  config().problem_reporter->set_nodes(way.nodes().size());
131  }
132 
133  // Ignore (but count) ways without segments.
134  if (way.nodes().size() < 2) {
135  ++stats().short_ways;
136  return false;
137  }
138 
139  if (!way.ends_have_same_id()) {
140  ++stats().duplicate_nodes;
141  if (config().problem_reporter) {
142  config().problem_reporter->report_duplicate_node(way.nodes().front().ref(), way.nodes().back().ref(), way.nodes().front().location());
143  }
144  }
145 
146  ++stats().from_ways;
147  stats().invalid_locations = segment_list().extract_segments_from_way(config().problem_reporter,
148  stats().duplicate_nodes,
149  way);
150  if (!config().ignore_invalid_locations && stats().invalid_locations > 0) {
151  return false;
152  }
153 
154  if (config().debug_level > 0) {
155  std::cerr << "\nAssembling way " << way.id() << " containing " << segment_list().size() << " nodes\n";
156  }
157 
158  // Now create the Area object and add the attributes and tags
159  // from the way.
160  const bool okay = create_area(out_buffer, way);
161  if (okay) {
162  out_buffer.commit();
163  } else {
164  out_buffer.rollback();
165  }
166 
167  if (debug()) {
168  std::cerr << "Done: " << stats() << "\n";
169  }
170 
171  return okay;
172  }
173 
181  bool operator()(const osmium::Relation& relation, const std::vector<const osmium::Way*>& members, osmium::memory::Buffer& out_buffer) {
182  if (!config().create_new_style_polygons) {
183  return true;
184  }
185 
186  assert(relation.cmembers().size() >= members.size());
187 
188  if (config().problem_reporter) {
189  config().problem_reporter->set_object(osmium::item_type::relation, relation.id());
190  }
191 
192  if (relation.members().empty()) {
193  ++stats().no_way_in_mp_relation;
194  return false;
195  }
196 
197  ++stats().from_relations;
198  stats().invalid_locations = segment_list().extract_segments_from_ways(config().problem_reporter,
199  stats().duplicate_nodes,
200  stats().duplicate_ways,
201  relation,
202  members);
203  if (!config().ignore_invalid_locations && stats().invalid_locations > 0) {
204  return false;
205  }
206  stats().member_ways = members.size();
207 
208  if (stats().member_ways == 1) {
209  ++stats().single_way_in_mp_relation;
210  }
211 
212  if (config().debug_level > 0) {
213  std::cerr << "\nAssembling relation " << relation.id() << " containing " << members.size() << " way members with " << segment_list().size() << " nodes\n";
214  }
215 
216  // Now create the Area object and add the attributes and tags
217  // from the relation.
218  bool okay = create_area(out_buffer, relation, members);
219  if (okay) {
220  out_buffer.commit();
221  } else {
222  out_buffer.rollback();
223  }
224 
225  return okay;
226  }
227 
228  }; // class Assembler
229 
230  } // namespace area
231 
232 } // namespace osmium
233 
234 #endif // OSMIUM_AREA_ASSEMBLER_HPP
WayNodeList & nodes()
Definition: way.hpp:89
std::size_t commit()
Definition: buffer.hpp:355
bool operator()(const osmium::Way &way, osmium::memory::Buffer &out_buffer)
Definition: assembler.hpp:123
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:325
RelationMemberList & members()
Get a reference to the member list.
Definition: relation.hpp:186
Definition: relation.hpp:168
const RelationMemberList & cmembers() const
Get a const reference to the member list.
Definition: relation.hpp:196
Definition: entity_bits.hpp:72
size_type size() const noexcept
Definition: collection.hpp:152
Definition: way.hpp:72
bool create_area(osmium::memory::Buffer &out_buffer, const osmium::Way &way)
Definition: assembler.hpp:63
constexpr osmium::object_id_type ref() const noexcept
Definition: node_ref.hpp:65
~Assembler() noexcept=default
bool operator()(const osmium::Relation &relation, const std::vector< const osmium::Way *> &members, osmium::memory::Buffer &out_buffer)
Definition: assembler.hpp:181
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: attr.hpp:333
bool empty() const noexcept
Definition: collection.hpp:143
Assembler(const config_type &config)
Definition: assembler.hpp:110
osmium::Location & location() noexcept
Definition: node_ref.hpp:79
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:126
bool ends_have_same_id() const
Definition: way.hpp:116
Definition: buffer.hpp:98
bool create_area(osmium::memory::Buffer &out_buffer, const osmium::Relation &relation, const std::vector< const osmium::Way *> &members)
Definition: assembler.hpp:82
const NodeRef & front() const noexcept
Definition: node_ref_list.hpp:126
const NodeRef & back() const noexcept
Definition: node_ref_list.hpp:138
Definition: assembler.hpp:61
void rollback()
Definition: buffer.hpp:371
size_type size() const noexcept
Definition: node_ref_list.hpp:83
Definition: osm_object_builder.hpp:540