Libosmium: include/osmium/handler/dump.hpp Source File

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

Loading...
Searching...
No Matches
dump.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_HANDLER_DUMP_HPP
2#define OSMIUM_HANDLER_DUMP_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/handler.hpp>
37#include <osmium/memory/collection.hpp>
38#include <osmium/memory/item.hpp>
39#include <osmium/osm/area.hpp>
40#include <osmium/osm/box.hpp>
41#include <osmium/osm/changeset.hpp>
42#include <osmium/osm/item_type.hpp>
43#include <osmium/osm/location.hpp>
44#include <osmium/osm/node.hpp>
45#include <osmium/osm/node_ref.hpp>
46#include <osmium/osm/object.hpp>
47#include <osmium/osm/relation.hpp>
48#include <osmium/osm/tag.hpp>
49#include <osmium/osm/timestamp.hpp>
50#include <osmium/osm/way.hpp>
51#include <osmium/visitor.hpp>
52
53#include <iomanip>
54#include <iostream>
55#include <string>
56
57namespace osmium {
58
59 namespace handler {
60
61 class Dump : public osmium::handler::Handler {
62
63 std::ostream* m_out;
64 bool m_with_size;
65 std::string m_prefix;
66
67 void print_title(const char* title, const osmium::memory::Item& item) {
68 *m_out << m_prefix
69 << title
70 << ":";
71
72 if (m_with_size) {
73 *m_out << " ["
74 << item.byte_size()
75 << "]";
76 }
77
78 *m_out << "\n";
79 }
80
81 void print_meta(const osmium::OSMObject& object) {
82 *m_out << m_prefix
83 << " id="
84 << object.id()
85 << "\n";
86 *m_out << m_prefix
87 << " version="
88 << object.version()
89 << "\n";
90 *m_out << m_prefix
91 << " uid="
92 << object.uid()
93 << "\n";
94 *m_out << m_prefix
95 << " user=|"
96 << object.user()
97 << "|\n";
98 *m_out << m_prefix
99 << " changeset="
100 << object.changeset()
101 << "\n";
102 *m_out << m_prefix
103 << " timestamp="
104 << object.timestamp().to_iso()
105 << "\n";
106 *m_out << m_prefix
107 << " visible="
108 << (object.visible() ? "yes" : "no")
109 << "\n";
110
111 Dump dump{*m_out, m_with_size, m_prefix + " "};
112 osmium::apply(object.cbegin(), object.cend(), dump);
113 }
114
115 void print_location(const osmium::Node& node) {
116 const osmium::Location& location = node.location();
117
118 if (location) {
119 *m_out << m_prefix
120 << " lon="
121 << std::fixed
122 << std::setprecision(7)
123 << location.lon_without_check()
124 << "\n";
125 *m_out << m_prefix
126 << " lat="
127 << location.lat_without_check()
128 << "\n";
129 } else {
130 *m_out << m_prefix
131 << " lon=\n"
132 << m_prefix
133 << " lat=\n";
134 }
135 }
136
137 public:
138
139 explicit Dump(std::ostream& out, bool with_size = true, std::string prefix = "") :
140 m_out(&out),
141 m_with_size(with_size),
142 m_prefix(std::move(prefix)) {
143 }
144
145 void tag_list(const osmium::TagList& tags) {
146 print_title("TAGS", tags);
147 for (const auto& tag : tags) {
148 *m_out << m_prefix
149 << " k=|"
150 << tag.key()
151 << "| v=|"
152 << tag.value()
153 << "|"
154 << "\n";
155 }
156 }
157
158 void way_node_list(const osmium::WayNodeList& wnl) {
159 print_title("NODES", wnl);
160 for (const auto& node_ref : wnl) {
161 *m_out << m_prefix
162 << " ref="
163 << node_ref.ref();
164 if (node_ref.location()) {
165 *m_out << " pos="
166 << node_ref.location();
167 }
168 *m_out << "\n";
169 }
170 }
171
172 void relation_member_list(const osmium::RelationMemberList& rml) {
173 print_title("MEMBERS", rml);
174 for (const auto& member : rml) {
175 *m_out << m_prefix
176 << " type="
177 << item_type_to_name(member.type())
178 << " ref="
179 << member.ref()
180 << " role=|"
181 << member.role()
182 << "|\n";
183 if (member.full_member()) {
184 Dump dump(*m_out, m_with_size, m_prefix + " | ");
185 osmium::apply_item(member.get_object(), dump);
186 }
187 }
188 }
189
190 void outer_ring(const osmium::OuterRing& ring) {
191 print_title("OUTER RING", ring);
192 for (const auto& node_ref : ring) {
193 *m_out << m_prefix
194 << " ref="
195 << node_ref.ref();
196 if (node_ref.location()) {
197 *m_out << " pos="
198 << node_ref.location();
199 }
200 *m_out << "\n";
201 }
202 }
203
204 void inner_ring(const osmium::InnerRing& ring) {
205 print_title("INNER RING", ring);
206 for (const auto& node_ref : ring) {
207 *m_out << m_prefix
208 << " ref="
209 << node_ref.ref();
210 if (node_ref.location()) {
211 *m_out << " pos="
212 << node_ref.location();
213 }
214 *m_out << "\n";
215 }
216 }
217
218 void node(const osmium::Node& node) {
219 print_title("NODE", node);
220 print_meta(node);
221 print_location(node);
222 }
223
224 void way(const osmium::Way& way) {
225 print_title("WAY", way);
226 print_meta(way);
227 }
228
229 void relation(const osmium::Relation& relation) {
230 print_title("RELATION", relation);
231 print_meta(relation);
232 }
233
234 void area(const osmium::Area& area) {
235 print_title("AREA", area);
236 print_meta(area);
237 }
238
239 void changeset(const osmium::Changeset& changeset) {
240 print_title("CHANGESET", changeset);
241 *m_out << m_prefix
242 << " id="
243 << changeset.id()
244 << "\n";
245 *m_out << m_prefix
246 << " num_changes="
247 << changeset.num_changes()
248 << "\n";
249 *m_out << m_prefix
250 << " uid="
251 << changeset.uid()
252 << "\n";
253 *m_out << m_prefix
254 << " user=|"
255 << changeset.user()
256 << "|\n";
257 *m_out << m_prefix
258 << " created_at="
259 << changeset.created_at().to_iso()
260 << "\n";
261 *m_out << m_prefix
262 << " closed_at="
263 << changeset.closed_at().to_iso()
264 << "\n";
265 *m_out << m_prefix
266 << " bounds=";
267
268 if (changeset.bounds()) {
269 *m_out << '('
270 << changeset.bounds().bottom_left().lon_without_check()
271 << ','
272 << changeset.bounds().bottom_left().lat_without_check()
273 << ','
274 << changeset.bounds().top_right().lon_without_check()
275 << ','
276 << changeset.bounds().top_right().lat_without_check()
277 << ')';
278 } else {
279 *m_out << "(undefined)";
280 }
281
282 *m_out << "\n";
283
284 Dump dump{*m_out, m_with_size, m_prefix + " "};
285 osmium::apply(changeset.cbegin(), changeset.cend(), dump);
286 }
287
288 }; // class Dump
289
290 } // namespace handler
291
292} // namespace osmium
293
294#endif // OSMIUM_HANDLER_DUMP_HPP
area.hpp
box.hpp
changeset.hpp
osmium::Area
Definition area.hpp:125
osmium::Changeset
An OSM Changeset, a group of changes made by a single user over a short period of time.
Definition changeset.hpp:146
osmium::InnerRing
Definition area.hpp:80
osmium::Location
Definition location.hpp:279
osmium::Location::lon_without_check
double lon_without_check() const noexcept
Definition location.hpp:448
osmium::Location::lat_without_check
double lat_without_check() const noexcept
Definition location.hpp:467
osmium::Node
Definition node.hpp:48
osmium::OSMObject
Definition object.hpp:64
osmium::OuterRing
Definition area.hpp:60
osmium::RelationMemberList
Definition relation.hpp:147
osmium::Relation
Definition relation.hpp:161
osmium::TagList
Definition tag.hpp:119
osmium::WayNodeList
Definition way.hpp:55
osmium::Way
Definition way.hpp:72
osmium::handler::Dump::area
void area(const osmium::Area &area)
Definition dump.hpp:234
osmium::handler::Dump::tag_list
void tag_list(const osmium::TagList &tags)
Definition dump.hpp:145
osmium::handler::Dump::print_meta
void print_meta(const osmium::OSMObject &object)
Definition dump.hpp:81
osmium::handler::Dump::outer_ring
void outer_ring(const osmium::OuterRing &ring)
Definition dump.hpp:190
osmium::handler::Dump::m_with_size
bool m_with_size
Definition dump.hpp:64
osmium::handler::Dump::m_prefix
std::string m_prefix
Definition dump.hpp:65
osmium::handler::Dump::way
void way(const osmium::Way &way)
Definition dump.hpp:224
osmium::handler::Dump::relation_member_list
void relation_member_list(const osmium::RelationMemberList &rml)
Definition dump.hpp:172
osmium::handler::Dump::print_title
void print_title(const char *title, const osmium::memory::Item &item)
Definition dump.hpp:67
osmium::handler::Dump::Dump
Dump(std::ostream &out, bool with_size=true, std::string prefix="")
Definition dump.hpp:139
osmium::handler::Dump::changeset
void changeset(const osmium::Changeset &changeset)
Definition dump.hpp:239
osmium::handler::Dump::node
void node(const osmium::Node &node)
Definition dump.hpp:218
osmium::handler::Dump::print_location
void print_location(const osmium::Node &node)
Definition dump.hpp:115
osmium::handler::Dump::relation
void relation(const osmium::Relation &relation)
Definition dump.hpp:229
osmium::handler::Dump::m_out
std::ostream * m_out
Definition dump.hpp:63
osmium::handler::Dump::inner_ring
void inner_ring(const osmium::InnerRing &ring)
Definition dump.hpp:204
osmium::handler::Dump::way_node_list
void way_node_list(const osmium::WayNodeList &wnl)
Definition dump.hpp:158
osmium::handler::Handler
Definition handler.hpp:71
osmium::memory::Item
Definition item.hpp:105
osmium::memory::Item::byte_size
item_size_type byte_size() const noexcept
Definition item.hpp:163
collection.hpp
handler.hpp
item.hpp
item_type.hpp
location.hpp
osmium::handler
Osmium handlers provide callbacks for OSM objects.
Definition dynamic_handler.hpp:49
osmium::tags
Code related to working with OSM tags.
Definition filter.hpp:47
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::apply_item
void apply_item(TItem &item, THandlers &&... handlers)
Definition visitor.hpp:306
osmium::item_type_to_name
const char * item_type_to_name(const item_type type) noexcept
Definition item_type.hpp:154
osmium::apply
void apply(TIterator it, TIterator end, THandlers &&... handlers)
Definition visitor.hpp:326
std
Definition location.hpp:654
node.hpp
node_ref.hpp
object.hpp
relation.hpp
tag.hpp
timestamp.hpp
visitor.hpp
way.hpp