Libosmium  2.5.3
Fast and flexible C++ library for working with OpenStreetMap data
dump.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_HANDLER_DUMP_HPP
2 #define OSMIUM_HANDLER_DUMP_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 <iomanip>
37 #include <iostream>
38 #include <string>
39 
40 #include <osmium/handler.hpp>
42 #include <osmium/memory/item.hpp>
43 #include <osmium/osm/area.hpp>
44 #include <osmium/osm/box.hpp>
45 #include <osmium/osm/changeset.hpp>
46 #include <osmium/osm/item_type.hpp>
47 #include <osmium/osm/location.hpp>
48 #include <osmium/osm/node.hpp>
49 #include <osmium/osm/node_ref.hpp>
50 #include <osmium/osm/object.hpp>
51 #include <osmium/osm/relation.hpp>
52 #include <osmium/osm/tag.hpp>
53 #include <osmium/osm/timestamp.hpp>
54 #include <osmium/osm/way.hpp>
55 #include <osmium/visitor.hpp>
56 
57 namespace osmium {
58 
59  namespace handler {
60 
61  class Dump : public osmium::handler::Handler {
62 
63  std::ostream* m_out;
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 
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, const std::string& prefix = "") :
140  m_out(&out),
141  m_with_size(with_size),
142  m_prefix(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 
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 
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 
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 
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
double lat_without_check() const
Definition: location.hpp:215
std::ostream * m_out
Definition: dump.hpp:63
const char * item_type_to_name(const item_type type) noexcept
Definition: item_type.hpp:155
Definition: tag.hpp:105
OSMIUM_CONSTEXPR Location bottom_left() const noexcept
Definition: box.hpp:164
osmium::Box & bounds() noexcept
Definition: changeset.hpp:340
osmium::Timestamp created_at() const noexcept
Get timestamp when this changeset was created.
Definition: changeset.hpp:257
void node(const osmium::Node &node)
Definition: dump.hpp:218
user_id_type uid() const noexcept
Get user id.
Definition: changeset.hpp:214
osmium::Location location() const noexcept
Definition: node.hpp:61
Definition: relation.hpp:167
const char * user() const
Get user name.
Definition: changeset.hpp:354
Definition: area.hpp:113
Definition: handler.hpp:45
Definition: relation.hpp:150
Definition: area.hpp:72
Definition: way.hpp:65
bool m_with_size
Definition: dump.hpp:64
void apply(TIterator it, TIterator end, THandlers &...handlers)
Definition: visitor.hpp:234
void way_node_list(const osmium::WayNodeList &wnl)
Definition: dump.hpp:158
item_size_type byte_size() const noexcept
Definition: item.hpp:147
void apply_item(const osmium::memory::Item &item, THandlers &...handlers)
Definition: visitor.hpp:224
Definition: dump.hpp:61
void relation_member_list(const osmium::RelationMemberList &rml)
Definition: dump.hpp:172
Definition: item.hpp:97
double lon_without_check() const
Definition: location.hpp:196
void changeset(const osmium::Changeset &changeset)
Definition: dump.hpp:239
Definition: way.hpp:52
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
changeset_id_type id() const noexcept
Get ID of this changeset.
Definition: changeset.hpp:188
const_iterator cend() const
Definition: changeset.hpp:401
void way(const osmium::Way &way)
Definition: dump.hpp:224
Definition: area.hpp:56
Definition: location.hpp:79
void print_title(const char *title, const osmium::memory::Item &item)
Definition: dump.hpp:67
std::string m_prefix
Definition: dump.hpp:65
void relation(const osmium::Relation &relation)
Definition: dump.hpp:229
void print_location(const osmium::Node &node)
Definition: dump.hpp:115
Definition: node.hpp:47
Dump(std::ostream &out, bool with_size=true, const std::string &prefix="")
Definition: dump.hpp:139
An OSM Changeset, a group of changes made by a single user over a short period of time...
Definition: changeset.hpp:154
std::string to_iso() const
Definition: timestamp.hpp:183
void outer_ring(const osmium::OuterRing &ring)
Definition: dump.hpp:190
void area(const osmium::Area &area)
Definition: dump.hpp:234
const_iterator cbegin() const
Definition: changeset.hpp:397
void print_meta(const osmium::OSMObject &object)
Definition: dump.hpp:81
void tag_list(const osmium::TagList &tags)
Definition: dump.hpp:145
osmium::Timestamp closed_at() const noexcept
Definition: changeset.hpp:267
void inner_ring(const osmium::InnerRing &ring)
Definition: dump.hpp:204
Definition: object.hpp:58
OSMIUM_CONSTEXPR Location top_right() const noexcept
Definition: box.hpp:178
num_changes_type num_changes() const noexcept
Get the number of changes in this changeset.
Definition: changeset.hpp:304