Libosmium  2.7.2
Fast and flexible C++ library for working with OpenStreetMap data
changeset.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_OSM_CHANGESET_HPP
2 #define OSMIUM_OSM_CHANGESET_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 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 <cstring>
37 
39 #include <osmium/memory/item.hpp>
40 #include <osmium/osm/box.hpp>
41 #include <osmium/osm/entity.hpp>
42 #include <osmium/osm/item_type.hpp>
43 #include <osmium/osm/tag.hpp>
44 #include <osmium/osm/timestamp.hpp>
45 #include <osmium/osm/types.hpp>
47 
48 namespace osmium {
49 
50  namespace builder {
51  class ChangesetDiscussionBuilder;
52  template <typename T> class ObjectBuilder;
53  } // namespace builder
54 
55  class Changeset;
56 
57  class ChangesetComment : public osmium::memory::detail::ItemHelper {
58 
60 
65 
66  ChangesetComment(const ChangesetComment&) = delete;
68 
71 
72  unsigned char* endpos() {
73  return data() + osmium::memory::padded_length(sizeof(ChangesetComment) + m_user_size + m_text_size);
74  }
75 
76  const unsigned char* endpos() const {
77  return data() + osmium::memory::padded_length(sizeof(ChangesetComment) + m_user_size + m_text_size);
78  }
79 
80  template <typename TMember>
82 
83  unsigned char* next() {
84  return endpos();
85  }
86 
87  unsigned const char* next() const {
88  return endpos();
89  }
90 
91  void set_user_size(string_size_type size) noexcept {
92  m_user_size = size;
93  }
94 
95  void set_text_size(string_size_type size) noexcept {
96  m_text_size = size;
97  }
98 
99  public:
100 
101  static constexpr item_type collection_type = item_type::changeset_discussion;
102 
104  m_date(date),
105  m_uid(uid),
106  m_user_size(0),
107  m_text_size(0) {
108  }
109 
110  osmium::Timestamp date() const noexcept {
111  return m_date;
112  }
113 
114  osmium::user_id_type uid() const noexcept {
115  return m_uid;
116  }
117 
118  const char* user() const noexcept {
119  return reinterpret_cast<const char*>(data() + sizeof(ChangesetComment));
120  }
121 
122  const char* text() const noexcept {
123  return reinterpret_cast<const char*>(data() + sizeof(ChangesetComment) + m_user_size);
124  }
125 
126  }; // class ChangesetComment
127 
128  class ChangesetDiscussion : public osmium::memory::Collection<ChangesetComment, osmium::item_type::changeset_discussion> {
129 
131 
132  public:
133 
134  using size_type = size_t;
135 
137  osmium::memory::Collection<ChangesetComment, osmium::item_type::changeset_discussion>() {
138  }
139 
140  size_type size() const noexcept {
141  return static_cast<size_type>(std::distance(begin(), end()));
142  }
143 
144  }; // class ChangesetDiscussion
145 
146 
154  class Changeset : public osmium::OSMEntity {
155 
157 
158  osmium::Box m_bounds;
159  osmium::Timestamp m_created_at;
160  osmium::Timestamp m_closed_at;
162  num_changes_type m_num_changes {0};
163  num_comments_type m_num_comments {0};
164  user_id_type m_uid {0};
166  int16_t m_padding1 {0};
167  int32_t m_padding2 {0};
168 
170  OSMEntity(sizeof(Changeset), osmium::item_type::changeset) {
171  }
172 
174  m_user_size = size;
175  }
176 
177  unsigned char* subitems_position() {
178  return data() + osmium::memory::padded_length(sizeof(Changeset) + m_user_size);
179  }
180 
181  const unsigned char* subitems_position() const {
182  return data() + osmium::memory::padded_length(sizeof(Changeset) + m_user_size);
183  }
184 
185  public:
186 
188  changeset_id_type id() const noexcept {
189  return m_id;
190  }
191 
199  m_id = id;
200  return *this;
201  }
202 
209  Changeset& set_id(const char* id) {
210  return set_id(osmium::string_to_changeset_id(id));
211  }
212 
214  user_id_type uid() const noexcept {
215  return m_uid;
216  }
217 
224  Changeset& set_uid(user_id_type uid) noexcept {
225  m_uid = uid;
226  return *this;
227  }
228 
237  m_uid = uid < 0 ? 0 : static_cast<user_id_type>(uid);
238  return *this;
239  }
240 
247  Changeset& set_uid(const char* uid) {
248  return set_uid_from_signed(string_to_user_id(uid));
249  }
250 
252  bool user_is_anonymous() const noexcept {
253  return m_uid == 0;
254  }
255 
257  osmium::Timestamp created_at() const noexcept {
258  return m_created_at;
259  }
260 
267  osmium::Timestamp closed_at() const noexcept {
268  return m_closed_at;
269  }
270 
272  bool open() const noexcept {
273  return m_closed_at == osmium::Timestamp();
274  }
275 
277  bool closed() const noexcept {
278  return !open();
279  }
280 
288  m_created_at = timestamp;
289  return *this;
290  }
291 
299  m_closed_at = timestamp;
300  return *this;
301  }
302 
304  num_changes_type num_changes() const noexcept {
305  return m_num_changes;
306  }
307 
309  Changeset& set_num_changes(num_changes_type num_changes) noexcept {
310  m_num_changes = num_changes;
311  return *this;
312  }
313 
315  Changeset& set_num_changes(const char* num_changes) {
316  return set_num_changes(osmium::string_to_num_changes(num_changes));
317  }
318 
320  num_comments_type num_comments() const noexcept {
321  return m_num_comments;
322  }
323 
325  Changeset& set_num_comments(num_comments_type num_comments) noexcept {
326  m_num_comments = num_comments;
327  return *this;
328  }
329 
331  Changeset& set_num_comments(const char* num_comments) {
332  return set_num_comments(osmium::string_to_num_comments(num_comments));
333  }
334 
340  osmium::Box& bounds() noexcept {
341  return m_bounds;
342  }
343 
349  const osmium::Box& bounds() const noexcept {
350  return m_bounds;
351  }
352 
354  const char* user() const {
355  return reinterpret_cast<const char*>(data() + sizeof(Changeset));
356  }
357 
359  const TagList& tags() const {
360  return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
361  }
362 
370  void set_attribute(const char* attr, const char* value) {
371  if (!std::strcmp(attr, "id")) {
372  set_id(value);
373  } else if (!std::strcmp(attr, "num_changes")) {
374  set_num_changes(value);
375  } else if (!std::strcmp(attr, "comments_count")) {
376  set_num_comments(value);
377  } else if (!std::strcmp(attr, "created_at")) {
378  set_created_at(osmium::Timestamp(value));
379  } else if (!std::strcmp(attr, "closed_at")) {
380  set_closed_at(osmium::Timestamp(value));
381  } else if (!std::strcmp(attr, "uid")) {
382  set_uid(value);
383  }
384  }
385 
388 
390  return iterator(subitems_position());
391  }
392 
394  return iterator(data() + padded_size());
395  }
396 
398  return const_iterator(subitems_position());
399  }
400 
402  return const_iterator(data() + padded_size());
403  }
404 
406  return cbegin();
407  }
408 
409  const_iterator end() const {
410  return cend();
411  }
412 
414  return osmium::detail::subitem_of_type<ChangesetDiscussion>(begin(), end());
415  }
416 
418  return osmium::detail::subitem_of_type<const ChangesetDiscussion>(cbegin(), cend());
419  }
420 
421  }; // class Changeset
422 
423 
427  inline bool operator==(const Changeset& lhs, const Changeset& rhs) {
428  return lhs.id() == rhs.id();
429  }
430 
431  inline bool operator!=(const Changeset& lhs, const Changeset& rhs) {
432  return ! (lhs == rhs);
433  }
434 
438  inline bool operator<(const Changeset& lhs, const Changeset& rhs) {
439  return lhs.id() < rhs.id();
440  }
441 
442  inline bool operator>(const Changeset& lhs, const Changeset& rhs) {
443  return rhs < lhs;
444  }
445 
446  inline bool operator<=(const Changeset& lhs, const Changeset& rhs) {
447  return ! (rhs < lhs);
448  }
449 
450  inline bool operator>=(const Changeset& lhs, const Changeset& rhs) {
451  return ! (lhs < rhs);
452  }
453 
454 } // namespace osmium
455 
456 #endif // OSMIUM_OSM_CHANGESET_HPP
uint32_t user_id_type
Type for OSM user IDs.
Definition: types.hpp:49
Definition: changeset.hpp:128
const char * text() const noexcept
Definition: changeset.hpp:122
Definition: collection.hpp:47
num_comments_type num_comments() const noexcept
Get the number of comments in this changeset.
Definition: changeset.hpp:320
Definition: tag.hpp:106
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
osmium::Box & bounds() noexcept
Definition: changeset.hpp:340
bool open() const noexcept
Is this changeset open?
Definition: changeset.hpp:272
string_size_type m_user_size
Definition: changeset.hpp:165
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:222
void set_attribute(const char *attr, const char *value)
Definition: changeset.hpp:370
user_id_type uid() const noexcept
Get user id.
Definition: changeset.hpp:214
string_size_type m_user_size
Definition: changeset.hpp:63
item_type
Definition: item_type.hpp:43
Changeset & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition: changeset.hpp:236
const TagList & tags() const
Get the list of tags.
Definition: changeset.hpp:359
uint16_t string_size_type
Definition: types.hpp:59
const char * user() const
Get user name.
Definition: changeset.hpp:354
changeset_id_type id() const noexcept
Get ID of this changeset.
Definition: changeset.hpp:188
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:64
uint32_t num_changes_type
Type for changeset num_changes.
Definition: types.hpp:51
Changeset & set_num_changes(const char *num_changes)
Set the number of changes in this changeset.
Definition: changeset.hpp:315
double distance(const osmium::geom::Coordinates &c1, const osmium::geom::Coordinates &c2)
Definition: haversine.hpp:64
const_iterator begin() const
Definition: changeset.hpp:405
Definition: changeset.hpp:57
T padded_length(T length) noexcept
Definition: item.hpp:56
Definition: osm_object_builder.hpp:278
string_size_type m_text_size
Definition: changeset.hpp:64
iterator begin()
Definition: changeset.hpp:389
Changeset & set_id(changeset_id_type id) noexcept
Definition: changeset.hpp:198
osmium::Timestamp closed_at() const noexcept
Definition: changeset.hpp:267
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
Changeset & set_uid(const char *uid)
Definition: changeset.hpp:247
Changeset & set_num_changes(num_changes_type num_changes) noexcept
Set the number of changes in this changeset.
Definition: changeset.hpp:309
const unsigned char * endpos() const
Definition: changeset.hpp:76
uint32_t num_comments_type
Type for changeset num_comments.
Definition: types.hpp:52
void set_user_size(string_size_type size)
Definition: changeset.hpp:173
num_changes_type num_changes() const noexcept
Get the number of changes in this changeset.
Definition: changeset.hpp:304
Definition: collection.hpp:117
const_iterator cend() const
Definition: changeset.hpp:401
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
Definition: timestamp.hpp:56
unsigned char * next()
Definition: changeset.hpp:83
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
const_iterator end() const
Definition: changeset.hpp:409
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:48
changeset_id_type string_to_changeset_id(const char *input)
Definition: types_from_string.hpp:143
bool user_is_anonymous() const noexcept
Is this user anonymous?
Definition: changeset.hpp:252
Changeset & set_num_comments(num_comments_type num_comments) noexcept
Set the number of comments in this changeset.
Definition: changeset.hpp:325
osmium::Timestamp created_at() const noexcept
Get timestamp when this changeset was created.
Definition: changeset.hpp:257
Changeset()
Definition: changeset.hpp:169
osmium::user_id_type uid() const noexcept
Definition: changeset.hpp:114
size_type size() const noexcept
Definition: changeset.hpp:140
unsigned char * data() const noexcept
Definition: collection.hpp:91
Definition: box.hpp:50
unsigned char * endpos()
Definition: changeset.hpp:72
void set_text_size(string_size_type size) noexcept
Definition: changeset.hpp:95
iterator end()
Definition: changeset.hpp:393
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:450
signed_user_id_type string_to_user_id(const char *input)
Definition: types_from_string.hpp:157
ChangesetComment(osmium::Timestamp date, osmium::user_id_type uid) noexcept
Definition: changeset.hpp:103
Changeset & set_num_comments(const char *num_comments)
Set the number of comments in this changeset.
Definition: changeset.hpp:331
Definition: builder.hpp:186
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:442
size_t size_type
Definition: changeset.hpp:134
void set_user_size(string_size_type size) noexcept
Definition: changeset.hpp:91
osmium::Timestamp date() const noexcept
Definition: changeset.hpp:110
unsigned const char * next() const
Definition: changeset.hpp:87
unsigned char * subitems_position()
Definition: changeset.hpp:177
ChangesetDiscussion & discussion()
Definition: changeset.hpp:413
const unsigned char * subitems_position() const
Definition: changeset.hpp:181
Changeset & set_created_at(const osmium::Timestamp &timestamp)
Definition: changeset.hpp:287
An OSM Changeset, a group of changes made by a single user over a short period of time...
Definition: changeset.hpp:154
const osmium::Box & bounds() const noexcept
Definition: changeset.hpp:349
Changeset & set_closed_at(const osmium::Timestamp &timestamp)
Definition: changeset.hpp:298
bool closed() const noexcept
Is this changeset closed?
Definition: changeset.hpp:277
osmium::io::InputIterator< osmium::io::Reader > begin(osmium::io::Reader &reader)
Definition: reader_iterator.hpp:41
const_iterator cbegin() const
Definition: changeset.hpp:397
num_comments_type string_to_num_comments(const char *input)
Definition: types_from_string.hpp:188
osmium::Timestamp m_date
Definition: changeset.hpp:61
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:431
Changeset & set_id(const char *id)
Definition: changeset.hpp:209
num_changes_type string_to_num_changes(const char *input)
Definition: types_from_string.hpp:174
Builder & operator=(const Builder &)=delete
Changeset & set_uid(user_id_type uid) noexcept
Definition: changeset.hpp:224
const char * user() const noexcept
Definition: changeset.hpp:118
const ChangesetDiscussion & discussion() const
Definition: changeset.hpp:417