Libosmium  2.15.1
Fast and flexible C++ library for working with OpenStreetMap data
assembler_legacy.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_AREA_ASSEMBLER_LEGACY_HPP
2 #define OSMIUM_AREA_ASSEMBLER_LEGACY_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2019 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 
37 #include <osmium/area/detail/basic_assembler_with_tags.hpp>
38 #include <osmium/area/detail/proto_ring.hpp>
39 #include <osmium/area/detail/segment_list.hpp>
41 #include <osmium/area/stats.hpp>
43 #include <osmium/memory/buffer.hpp>
45 #include <osmium/osm/area.hpp>
46 #include <osmium/osm/item_type.hpp>
47 #include <osmium/osm/node_ref.hpp>
48 #include <osmium/osm/relation.hpp>
49 #include <osmium/osm/tag.hpp>
50 #include <osmium/osm/way.hpp>
51 #include <osmium/tags/filter.hpp>
52 
53 #include <algorithm>
54 #include <cassert>
55 #include <cstring>
56 #include <functional>
57 #include <iostream>
58 #include <iterator>
59 #include <map>
60 #include <set>
61 #include <string>
62 #include <utility>
63 #include <vector>
64 
65 namespace osmium {
66 
67  namespace area {
68 
73  class AssemblerLegacy : public detail::BasicAssemblerWithTags {
74 
75  void add_tags_to_area(osmium::builder::AreaBuilder& builder, const osmium::Way& way) const {
76  builder.add_item(way.tags());
77  }
78 
79  void add_common_tags(osmium::builder::TagListBuilder& tl_builder, std::set<const osmium::Way*>& ways) const {
80  std::map<std::string, std::size_t> counter;
81  for (const osmium::Way* way : ways) {
82  for (const auto& tag : way->tags()) {
83  std::string kv{tag.key()};
84  kv.append(1, '\0');
85  kv.append(tag.value());
86  ++counter[kv];
87  }
88  }
89 
90  const std::size_t num_ways = ways.size();
91  for (const auto& t_c : counter) {
92  if (debug()) {
93  std::cerr << " tag " << t_c.first << " is used " << t_c.second << " times in " << num_ways << " ways\n";
94  }
95  if (t_c.second == num_ways) {
96  const std::size_t len = std::strlen(t_c.first.c_str());
97  tl_builder.add_tag(t_c.first.c_str(), t_c.first.c_str() + len + 1);
98  }
99  }
100  }
101 
103 
104  MPFilter() : osmium::tags::KeyFilter(true) {
105  add(false, "type");
106  add(false, "created_by");
107  add(false, "source");
108  add(false, "note");
109  add(false, "test:id");
110  add(false, "test:section");
111  }
112 
113  }; // struct MPFilter
114 
115  static const MPFilter& filter() noexcept {
116  static const MPFilter filter;
117  return filter;
118  }
119 
121  const auto count = std::count_if(relation.tags().cbegin(), relation.tags().cend(), std::cref(filter()));
122 
123  if (debug()) {
124  std::cerr << " found " << count << " tags on relation (without ignored ones)\n";
125  }
126 
127  if (count > 0) {
128  if (debug()) {
129  std::cerr << " use tags from relation\n";
130  }
131 
132  if (config().keep_type_tag) {
133  builder.add_item(relation.tags());
134  } else {
135  copy_tags_without_type(builder, relation.tags());
136  }
137  } else {
138  ++stats().no_tags_on_relation;
139  if (debug()) {
140  std::cerr << " use tags from outer ways\n";
141  }
142  std::set<const osmium::Way*> ways;
143  for (const auto& ring : rings()) {
144  if (ring.is_outer()) {
145  ring.get_ways(ways);
146  }
147  }
148  if (ways.size() == 1) {
149  if (debug()) {
150  std::cerr << " only one outer way\n";
151  }
152  builder.add_item((*ways.cbegin())->tags());
153  } else {
154  if (debug()) {
155  std::cerr << " multiple outer ways, get common tags\n";
156  }
157  osmium::builder::TagListBuilder tl_builder{builder};
158  add_common_tags(tl_builder, ways);
159  }
160  }
161  }
162 
163  bool create_area(osmium::memory::Buffer& out_buffer, const osmium::Way& way) {
164  osmium::builder::AreaBuilder builder{out_buffer};
165  builder.initialize_from_object(way);
166 
167  const bool area_okay = create_rings();
168  if (area_okay || config().create_empty_areas) {
169  add_tags_to_area(builder, way);
170  }
171  if (area_okay) {
172  add_rings_to_area(builder);
173  }
174 
175  if (report_ways()) {
176  config().problem_reporter->report_way(way);
177  }
178 
179  return area_okay || config().create_empty_areas;
180  }
181 
182  bool create_area(osmium::memory::Buffer& out_buffer, const osmium::Relation& relation, const std::vector<const osmium::Way*>& members) {
183  set_num_members(members.size());
184  osmium::builder::AreaBuilder builder{out_buffer};
185  builder.initialize_from_object(relation);
186 
187  const bool area_okay = create_rings();
188  if (area_okay || config().create_empty_areas) {
189  add_tags_to_area(builder, relation);
190  }
191  if (area_okay) {
192  add_rings_to_area(builder);
193  }
194 
195  if (report_ways()) {
196  for (const osmium::Way* way : members) {
197  config().problem_reporter->report_way(*way);
198  }
199  }
200 
201  return area_okay || config().create_empty_areas;
202  }
203 
204  public:
205 
206  explicit AssemblerLegacy(const config_type& config) :
207  detail::BasicAssemblerWithTags(config) {
208  }
209 
217  bool operator()(const osmium::Way& way, osmium::memory::Buffer& out_buffer) {
218  if (!config().create_way_polygons) {
219  return true;
220  }
221 
222  if (way.tags().has_tag("area", "no")) {
223  return true;
224  }
225 
226  if (config().problem_reporter) {
227  config().problem_reporter->set_object(osmium::item_type::way, way.id());
228  config().problem_reporter->set_nodes(way.nodes().size());
229  }
230 
231  // Ignore (but count) ways without segments.
232  if (way.nodes().size() < 2) {
233  ++stats().short_ways;
234  return false;
235  }
236 
237  if (!way.ends_have_same_id()) {
238  ++stats().duplicate_nodes;
239  if (config().problem_reporter) {
240  config().problem_reporter->report_duplicate_node(way.nodes().front().ref(), way.nodes().back().ref(), way.nodes().front().location());
241  }
242  }
243 
244  ++stats().from_ways;
245  stats().invalid_locations = segment_list().extract_segments_from_way(config().problem_reporter,
246  stats().duplicate_nodes,
247  way);
248  if (!config().ignore_invalid_locations && stats().invalid_locations > 0) {
249  return false;
250  }
251 
252  if (config().debug_level > 0) {
253  std::cerr << "\nAssembling way " << way.id() << " containing " << segment_list().size() << " nodes\n";
254  }
255 
256  // Now create the Area object and add the attributes and tags
257  // from the way.
258  const bool okay = create_area(out_buffer, way);
259  if (okay) {
260  out_buffer.commit();
261  } else {
262  out_buffer.rollback();
263  }
264 
265  if (debug()) {
266  std::cerr << "Done: " << stats() << "\n";
267  }
268 
269  return okay;
270  }
271 
279  bool operator()(const osmium::Relation& relation, const std::vector<const osmium::Way*>& members, osmium::memory::Buffer& out_buffer) {
280  assert(relation.members().size() >= members.size());
281 
282  if (config().problem_reporter) {
283  config().problem_reporter->set_object(osmium::item_type::relation, relation.id());
284  }
285 
286  if (relation.members().empty()) {
287  ++stats().no_way_in_mp_relation;
288  return false;
289  }
290 
291  ++stats().from_relations;
292  stats().invalid_locations = segment_list().extract_segments_from_ways(config().problem_reporter,
293  stats().duplicate_nodes,
294  stats().duplicate_ways,
295  relation,
296  members);
297  if (!config().ignore_invalid_locations && stats().invalid_locations > 0) {
298  return false;
299  }
300  stats().member_ways = members.size();
301 
302  if (stats().member_ways == 1) {
303  ++stats().single_way_in_mp_relation;
304  }
305 
306  if (config().debug_level > 0) {
307  std::cerr << "\nAssembling relation " << relation.id() << " containing " << members.size() << " way members with " << segment_list().size() << " nodes\n";
308  }
309 
310  const std::size_t area_offset = out_buffer.committed();
311 
312  // Now create the Area object and add the attributes and tags
313  // from the relation.
314  bool okay = create_area(out_buffer, relation, members);
315  if (okay) {
316  if ((config().create_new_style_polygons && stats().no_tags_on_relation == 0) ||
317  (config().create_old_style_polygons && stats().no_tags_on_relation != 0)) {
318  out_buffer.commit();
319  } else {
320  out_buffer.rollback();
321  }
322  } else {
323  out_buffer.rollback();
324  }
325 
326  const osmium::TagList& area_tags = out_buffer.get<osmium::Area>(area_offset).tags(); // tags of the area we just built
327 
328  // Find all closed ways that are inner rings and check their
329  // tags. If they are not the same as the tags of the area we
330  // just built, add them to a list and later build areas for
331  // them, too.
332  std::vector<const osmium::Way*> ways_that_should_be_areas;
333  if (stats().wrong_role == 0) {
334  detail::for_each_member(relation, members, [this, &ways_that_should_be_areas, &area_tags](const osmium::RelationMember& member, const osmium::Way& way) {
335  if (!std::strcmp(member.role(), "inner")) {
336  if (!way.nodes().empty() && way.is_closed() && !way.tags().empty()) {
337  const auto d = std::count_if(way.tags().cbegin(), way.tags().cend(), std::cref(filter()));
338  if (d > 0) {
339  osmium::tags::KeyFilter::iterator way_fi_begin(std::cref(filter()), way.tags().cbegin(), way.tags().cend());
340  osmium::tags::KeyFilter::iterator way_fi_end(std::cref(filter()), way.tags().cend(), way.tags().cend());
341  osmium::tags::KeyFilter::iterator area_fi_begin(std::cref(filter()), area_tags.cbegin(), area_tags.cend());
342  osmium::tags::KeyFilter::iterator area_fi_end(std::cref(filter()), area_tags.cend(), area_tags.cend());
343 
344  if (!std::equal(way_fi_begin, way_fi_end, area_fi_begin) || d != std::distance(area_fi_begin, area_fi_end)) {
345  ways_that_should_be_areas.push_back(&way);
346  } else {
347  ++stats().inner_with_same_tags;
348  if (config().problem_reporter) {
349  config().problem_reporter->report_inner_with_same_tags(way);
350  }
351  }
352  }
353  }
354  }
355  });
356  }
357 
358  if (debug()) {
359  std::cerr << "Done: " << stats() << "\n";
360  }
361 
362  // Now build areas for all ways found in the last step.
363  for (const osmium::Way* way : ways_that_should_be_areas) {
364  AssemblerLegacy assembler{config()};
365  if (!assembler(*way, out_buffer)) {
366  okay = false;
367  }
368  stats() += assembler.stats();
369  }
370 
371  return okay;
372  }
373 
374  }; // class AssemblerLegacy
375 
376  } // namespace area
377 
378 } // namespace osmium
379 
380 #endif // OSMIUM_AREA_ASSEMBLER_LEGACY_HPP
std::size_t commit()
Definition: buffer.hpp:468
Definition: filter.hpp:94
void add_item(const osmium::memory::Item &item)
Definition: builder.hpp:215
Definition: tag.hpp:119
Definition: relation.hpp:168
Definition: area.hpp:126
const_iterator cbegin() const noexcept
Definition: collection.hpp:164
Filter & add(bool result, const key_type &key, const value_type &value)
Definition: filter.hpp:136
Definition: entity_bits.hpp:72
double distance(const osmium::geom::Coordinates &c1, const osmium::geom::Coordinates &c2)
Definition: haversine.hpp:66
Definition: way.hpp:72
bool create_area(osmium::memory::Buffer &out_buffer, const osmium::Way &way)
Definition: assembler_legacy.hpp:163
std::size_t committed() const noexcept
Definition: buffer.hpp:356
Filter< std::string > KeyFilter
Definition: filter.hpp:179
Definition: relation.hpp:57
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: assembler_legacy.hpp:73
Definition: attr.hpp:333
Definition: assembler_legacy.hpp:102
T & get(const std::size_t offset) const
Definition: buffer.hpp:518
const_iterator cend() const noexcept
Definition: collection.hpp:168
bool operator()(const osmium::Way &way, osmium::memory::Buffer &out_buffer)
Definition: assembler_legacy.hpp:217
bool operator()(const osmium::Relation &relation, const std::vector< const osmium::Way * > &members, osmium::memory::Buffer &out_buffer)
Definition: assembler_legacy.hpp:279
MPFilter()
Definition: assembler_legacy.hpp:104
AssemblerLegacy(const config_type &config)
Definition: assembler_legacy.hpp:206
void add_common_tags(osmium::builder::TagListBuilder &tl_builder, std::set< const osmium::Way * > &ways) const
Definition: assembler_legacy.hpp:79
void add_tags_to_area(osmium::builder::AreaBuilder &builder, const osmium::Way &way) const
Definition: assembler_legacy.hpp:75
Definition: buffer.hpp:97
const char * role() const noexcept
Definition: relation.hpp:140
static const MPFilter & filter() noexcept
Definition: assembler_legacy.hpp:115
bool create_area(osmium::memory::Buffer &out_buffer, const osmium::Relation &relation, const std::vector< const osmium::Way * > &members)
Definition: assembler_legacy.hpp:182
void rollback()
Definition: buffer.hpp:484
void add_tags_to_area(osmium::builder::AreaBuilder &builder, const osmium::Relation &relation)
Definition: assembler_legacy.hpp:120
void add_tag(const char *key, const char *value)
Definition: osm_object_builder.hpp:103
boost::filter_iterator< filter_type, osmium::TagList::const_iterator > iterator
Definition: filter.hpp:129
Definition: osm_object_builder.hpp:73
Definition: osm_object_builder.hpp:570