Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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 (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 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 <osmium/area/assembler_config.hpp>
37#include <osmium/area/detail/basic_assembler_with_tags.hpp>
38#include <osmium/area/detail/segment_list.hpp>
39#include <osmium/area/problem_reporter.hpp>
40#include <osmium/area/stats.hpp>
41#include <osmium/builder/osm_object_builder.hpp>
42#include <osmium/memory/buffer.hpp>
43#include <osmium/osm/item_type.hpp>
44#include <osmium/osm/node_ref.hpp>
45#include <osmium/osm/relation.hpp>
46#include <osmium/osm/tag.hpp>
47#include <osmium/osm/way.hpp>
48
49#include <cassert>
50#include <iostream>
51#include <vector>
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
121 bool operator()(const osmium::Way& way, osmium::memory::Buffer& out_buffer) {
122 if (!config().create_way_polygons) {
123 return true;
124 }
125
126 if (config().problem_reporter) {
127 config().problem_reporter->set_object(osmium::item_type::way, way.id());
128 config().problem_reporter->set_nodes(way.nodes().size());
129 }
130
131 // Ignore (but count) ways without segments.
132 if (way.nodes().size() < 2) {
133 ++stats().short_ways;
134 return false;
135 }
136
137 if (!way.ends_have_same_id()) {
138 ++stats().duplicate_nodes;
139 if (config().problem_reporter) {
140 config().problem_reporter->report_duplicate_node(way.nodes().front().ref(), way.nodes().back().ref(), way.nodes().front().location());
141 }
142 }
143
144 ++stats().from_ways;
145 stats().invalid_locations = segment_list().extract_segments_from_way(config().problem_reporter,
146 stats().duplicate_nodes,
147 way);
148 if (!config().ignore_invalid_locations && stats().invalid_locations > 0) {
149 return false;
150 }
151
152 if (config().debug_level > 0) {
153 std::cerr << "\nAssembling way " << way.id() << " containing " << segment_list().size() << " nodes\n";
154 }
155
156 // Now create the Area object and add the attributes and tags
157 // from the way.
158 const bool okay = create_area(out_buffer, way);
159 if (okay) {
160 out_buffer.commit();
161 } else {
162 out_buffer.rollback();
163 }
164
165 if (debug()) {
166 std::cerr << "Done: " << stats() << "\n";
167 }
168
169 return okay;
170 }
171
179 bool operator()(const osmium::Relation& relation, const std::vector<const osmium::Way*>& members, osmium::memory::Buffer& out_buffer) {
180 if (!config().create_new_style_polygons) {
181 return true;
182 }
183
184 assert(relation.cmembers().size() >= members.size());
185
186 if (config().problem_reporter) {
187 config().problem_reporter->set_object(osmium::item_type::relation, relation.id());
188 }
189
190 if (relation.members().empty()) {
191 ++stats().no_way_in_mp_relation;
192 return false;
193 }
194
195 ++stats().from_relations;
196 stats().invalid_locations = segment_list().extract_segments_from_ways(config().problem_reporter,
197 stats().duplicate_nodes,
198 stats().duplicate_ways,
199 relation,
200 members);
201 if (!config().ignore_invalid_locations && stats().invalid_locations > 0) {
202 return false;
203 }
204 stats().member_ways = members.size();
205
206 if (stats().member_ways == 1) {
207 ++stats().single_way_in_mp_relation;
208 }
209
210 if (config().debug_level > 0) {
211 std::cerr << "\nAssembling relation " << relation.id() << " containing " << members.size() << " way members with " << segment_list().size() << " nodes\n";
212 }
213
214 // Now create the Area object and add the attributes and tags
215 // from the relation.
216 const bool okay = create_area(out_buffer, relation, members);
217 if (okay) {
218 out_buffer.commit();
219 } else {
220 out_buffer.rollback();
221 }
222
223 return okay;
224 }
225
226 }; // class Assembler
227
228 } // namespace area
229
230} // namespace osmium
231
232#endif // OSMIUM_AREA_ASSEMBLER_HPP
assembler_config.hpp
buffer.hpp
osmium::Relation
Definition relation.hpp:161
osmium::Way
Definition way.hpp:72
osmium::area::Assembler
Definition assembler.hpp:61
osmium::area::Assembler::create_area
bool create_area(osmium::memory::Buffer &out_buffer, const osmium::Relation &relation, const std::vector< const osmium::Way * > &members)
Definition assembler.hpp:82
osmium::area::Assembler::operator()
bool operator()(const osmium::Way &way, osmium::memory::Buffer &out_buffer)
Definition assembler.hpp:121
osmium::area::Assembler::create_area
bool create_area(osmium::memory::Buffer &out_buffer, const osmium::Way &way)
Definition assembler.hpp:63
osmium::area::Assembler::Assembler
Assembler(const config_type &config)
Definition assembler.hpp:110
osmium::area::Assembler::operator()
bool operator()(const osmium::Relation &relation, const std::vector< const osmium::Way * > &members, osmium::memory::Buffer &out_buffer)
Definition assembler.hpp:179
osmium::builder::AreaBuilder
Definition osm_object_builder.hpp:567
osmium::builder::AreaBuilder::initialize_from_object
void initialize_from_object(const osmium::OSMObject &source)
Definition osm_object_builder.hpp:584
item_type.hpp
detail
Definition attr.hpp:342
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::item_type::relation
@ relation
osmium::item_type::way
@ way
node_ref.hpp
osm_object_builder.hpp
problem_reporter.hpp
relation.hpp
stats.hpp
tag.hpp
way.hpp