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

Loading...
Searching...
No Matches
object.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_OSM_OBJECT_HPP
2#define OSMIUM_OSM_OBJECT_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/memory/collection.hpp>
37#include <osmium/memory/item.hpp>
38#include <osmium/memory/item_iterator.hpp>
39#include <osmium/osm/entity.hpp>
40#include <osmium/osm/item_type.hpp>
41#include <osmium/osm/location.hpp>
42#include <osmium/osm/tag.hpp>
43#include <osmium/osm/timestamp.hpp>
44#include <osmium/osm/types.hpp>
45#include <osmium/osm/types_from_string.hpp>
46#include <osmium/util/misc.hpp>
47
48#include <cstdlib>
49#include <cstring>
50#include <stdexcept>
51#include <tuple>
52#include <type_traits>
53
54 namespace osmium {
55
56 namespace builder {
57 template <typename TDerived, typename T>
58 class OSMObjectBuilder;
59 } // namespace builder
60
64 class OSMObject : public osmium::OSMEntity {
65
66 template <typename TDerived, typename T>
67 friend class osmium::builder::OSMObjectBuilder;
68
69 object_id_type m_id = 0;
70 bool m_deleted : 1;
71 object_version_type m_version : 31;
72 osmium::Timestamp m_timestamp;
73 user_id_type m_uid = 0;
74 changeset_id_type m_changeset = 0;
75
76 size_t sizeof_object() const noexcept {
77 return sizeof(OSMObject) + (type() == item_type::node ? sizeof(osmium::Location) : 0) + sizeof(string_size_type);
78 }
79
80 unsigned char* user_position() noexcept {
81 return data() + sizeof_object() - sizeof(string_size_type);
82 }
83
84 const unsigned char* user_position() const noexcept {
85 return data() + sizeof_object() - sizeof(string_size_type);
86 }
87
88 string_size_type user_size() const noexcept {
89 return *reinterpret_cast<const string_size_type*>(user_position());
90 }
91
92 unsigned char* subitems_position() {
93 return data() + osmium::memory::padded_length(sizeof_object() + user_size());
94 }
95
96 const unsigned char* subitems_position() const {
97 return data() + osmium::memory::padded_length(sizeof_object() + user_size());
98 }
99
100 protected:
101
102 OSMObject(osmium::memory::item_size_type size, osmium::item_type type) :
103 OSMEntity(size, type),
104 m_deleted(false),
105 m_version(0) {
106 }
107
108 public:
109
110 constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
111 return t == osmium::item_type::node ||
112 t == osmium::item_type::way ||
113 t == osmium::item_type::relation ||
114 t == osmium::item_type::area;
115 }
116
118 object_id_type id() const noexcept {
119 return m_id;
120 }
121
123 unsigned_object_id_type positive_id() const noexcept {
124 return static_cast<unsigned_object_id_type>(std::abs(m_id));
125 }
126
132 OSMObject& set_id(object_id_type id) noexcept {
133 m_id = id;
134 return *this;
135 }
136
142 OSMObject& set_id(const char* id) {
143 return set_id(osmium::string_to_object_id(id));
144 }
145
147 bool deleted() const noexcept {
148 return m_deleted;
149 }
150
152 bool visible() const noexcept {
153 return !deleted();
154 }
155
161 OSMObject& set_deleted(bool deleted) noexcept {
162 m_deleted = deleted;
163 return *this;
164 }
165
171 OSMObject& set_visible(bool visible) noexcept {
172 m_deleted = !visible;
173 return *this;
174 }
175
182 OSMObject& set_visible(const char* visible) {
183 if (!std::strcmp("true", visible)) {
184 set_visible(true);
185 } else if (!std::strcmp("false", visible)) {
186 set_visible(false);
187 } else {
188 throw std::invalid_argument{"Unknown value for visible attribute (allowed is 'true' or 'false')"};
189 }
190 return *this;
191 }
192
194 object_version_type version() const noexcept {
195 return m_version;
196 }
197
203 OSMObject& set_version(object_version_type version) noexcept {
204 m_version = version;
205 return *this;
206 }
207
213 OSMObject& set_version(const char* version) {
214 return set_version(string_to_object_version(version));
215 }
216
218 changeset_id_type changeset() const noexcept {
219 return m_changeset;
220 }
221
227 OSMObject& set_changeset(changeset_id_type changeset) noexcept {
228 m_changeset = changeset;
229 return *this;
230 }
231
237 OSMObject& set_changeset(const char* changeset) {
238 return set_changeset(string_to_changeset_id(changeset));
239 }
240
242 user_id_type uid() const noexcept {
243 return m_uid;
244 }
245
251 OSMObject& set_uid(user_id_type uid) noexcept {
252 m_uid = uid;
253 return *this;
254 }
255
262 OSMObject& set_uid_from_signed(signed_user_id_type uid) noexcept {
263 m_uid = uid < 0 ? 0 : static_cast<user_id_type>(uid);
264 return *this;
265 }
266
272 OSMObject& set_uid(const char* uid) {
273 m_uid = string_to_uid(uid);
274 return *this;
275 }
276
278 bool user_is_anonymous() const noexcept {
279 return m_uid == 0;
280 }
281
283 osmium::Timestamp timestamp() const noexcept {
284 return m_timestamp;
285 }
286
293 OSMObject& set_timestamp(const osmium::Timestamp& timestamp) noexcept {
294 m_timestamp = timestamp;
295 return *this;
296 }
297
308 OSMObject& set_timestamp(const char* timestamp) {
309 assert(timestamp);
310 const char** str = &timestamp;
311 m_timestamp = detail::parse_timestamp(str);
312 if (**str != '\0') {
313 throw std::invalid_argument{"can not parse timestamp: garbage after timestamp"};
314 }
315 return *this;
316 }
317
319 const char* user() const noexcept {
320 return reinterpret_cast<const char*>(data() + sizeof_object());
321 }
322
324 void clear_user() noexcept {
325 std::memset(data() + sizeof_object(), 0, user_size());
326 }
327
329 const TagList& tags() const {
330 return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
331 }
332
339 const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
340 return tags().get_value_by_key(key, default_value);
341 }
342
350 OSMObject& set_attribute(const char* attr, const char* value) {
351 if (!std::strcmp(attr, "id")) {
352 set_id(value);
353 } else if (!std::strcmp(attr, "version")) {
354 set_version(value);
355 } else if (!std::strcmp(attr, "changeset")) {
356 set_changeset(value);
357 } else if (!std::strcmp(attr, "timestamp")) {
358 set_timestamp(value);
359 } else if (!std::strcmp(attr, "uid")) {
360 set_uid(value);
361 } else if (!std::strcmp(attr, "visible")) {
362 set_visible(value);
363 }
364
365 return *this;
366 }
367
368 using iterator = osmium::memory::CollectionIterator<Item>;
369 using const_iterator = osmium::memory::CollectionIterator<const Item>;
370
377 void remove_tags() noexcept {
378 for (auto& subitem : *this) {
379 if (subitem.type() == osmium::item_type::tag_list) {
380 subitem.set_removed(true);
381 }
382 }
383 }
384
385 iterator begin() {
386 return iterator(subitems_position());
387 }
388
389 iterator end() {
390 return iterator(next());
391 }
392
393 const_iterator cbegin() const {
394 return const_iterator(subitems_position());
395 }
396
397 const_iterator cend() const {
398 return const_iterator(next());
399 }
400
401 const_iterator begin() const {
402 return cbegin();
403 }
404
405 const_iterator end() const {
406 return cend();
407 }
408
414 template <typename T>
415 osmium::memory::ItemIteratorRange<T> subitems() {
416 return osmium::memory::ItemIteratorRange<T>{subitems_position(), next()};
417 }
418
424 template <typename T>
425 osmium::memory::ItemIteratorRange<const T> subitems() const {
426 return osmium::memory::ItemIteratorRange<const T>{subitems_position(), next()};
427 }
428
429 template <typename T>
430 using t_iterator = osmium::memory::ItemIterator<T>;
431
432 template <typename T>
433 using t_const_iterator = osmium::memory::ItemIterator<const T>;
434
435 template <typename T>
436 t_iterator<T> begin() {
437 return t_iterator<T>(subitems_position(), next());
438 }
439
440 template <typename T>
441 t_iterator<T> end() {
442 return t_iterator<T>(next(), next());
443 }
444
445 template <typename T>
446 t_const_iterator<T> cbegin() const {
447 return t_const_iterator<T>(subitems_position(), next());
448 }
449
450 template <typename T>
451 t_const_iterator<T> cend() const {
452 return t_const_iterator<T>(next(), next());
453 }
454
455 template <typename T>
456 t_const_iterator<T> begin() const {
457 return cbegin<T>();
458 }
459
460 template <typename T>
461 t_const_iterator<T> end() const {
462 return cend<T>();
463 }
464
465 }; // class OSMObject
466
467
471 inline bool operator==(const OSMObject& lhs, const OSMObject& rhs) noexcept {
472 return lhs.type() == rhs.type() &&
473 lhs.id() == rhs.id() &&
474 lhs.version() == rhs.version();
475 }
476
477 inline bool operator!=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
478 return !(lhs == rhs);
479 }
480
496 inline bool operator<(const OSMObject& lhs, const OSMObject& rhs) noexcept {
497 return const_tie(lhs.type(), lhs.id() > 0, lhs.positive_id(), lhs.version(),
498 ((lhs.timestamp().valid() && rhs.timestamp().valid()) ? lhs.timestamp() : osmium::Timestamp())) <
499 const_tie(rhs.type(), rhs.id() > 0, rhs.positive_id(), rhs.version(),
500 ((lhs.timestamp().valid() && rhs.timestamp().valid()) ? rhs.timestamp() : osmium::Timestamp()));
501 }
502
503 inline bool operator>(const OSMObject& lhs, const OSMObject& rhs) noexcept {
504 return rhs < lhs;
505 }
506
507 inline bool operator<=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
508 return !(rhs < lhs);
509 }
510
511 inline bool operator>=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
512 return !(lhs < rhs);
513 }
514
515} // namespace osmium
516
517#endif // OSMIUM_OSM_OBJECT_HPP
osmium::Location
Definition location.hpp:271
osmium::OSMEntity
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition entity.hpp:64
osmium::OSMObject
Definition object.hpp:64
osmium::OSMObject::begin
const_iterator begin() const
Definition object.hpp:401
osmium::OSMObject::user
const char * user() const noexcept
Get user name for this object.
Definition object.hpp:319
osmium::OSMObject::set_deleted
OSMObject & set_deleted(bool deleted) noexcept
Definition object.hpp:161
osmium::OSMObject::tags
const TagList & tags() const
Get the list of tags for this object.
Definition object.hpp:329
osmium::OSMObject::set_visible
OSMObject & set_visible(const char *visible)
Definition object.hpp:182
osmium::OSMObject::t_const_iterator
osmium::memory::ItemIterator< const T > t_const_iterator
Definition object.hpp:433
osmium::OSMObject::cend
const_iterator cend() const
Definition object.hpp:397
osmium::OSMObject::visible
bool visible() const noexcept
Is this object marked visible (ie not deleted)?
Definition object.hpp:152
osmium::OSMObject::m_timestamp
osmium::Timestamp m_timestamp
Definition object.hpp:72
osmium::OSMObject::user_is_anonymous
bool user_is_anonymous() const noexcept
Is this user anonymous?
Definition object.hpp:278
osmium::OSMObject::is_compatible_to
static constexpr bool is_compatible_to(osmium::item_type t) noexcept
Definition object.hpp:110
osmium::OSMObject::set_visible
OSMObject & set_visible(bool visible) noexcept
Definition object.hpp:171
osmium::OSMObject::t_iterator
osmium::memory::ItemIterator< T > t_iterator
Definition object.hpp:430
osmium::OSMObject::set_uid
OSMObject & set_uid(const char *uid)
Definition object.hpp:272
osmium::OSMObject::m_version
object_version_type m_version
Definition object.hpp:71
osmium::OSMObject::clear_user
void clear_user() noexcept
Clear user name.
Definition object.hpp:324
osmium::OSMObject::version
object_version_type version() const noexcept
Get version of this object.
Definition object.hpp:194
osmium::OSMObject::set_id
OSMObject & set_id(const char *id)
Definition object.hpp:142
osmium::OSMObject::timestamp
osmium::Timestamp timestamp() const noexcept
Get timestamp when this object last changed.
Definition object.hpp:283
osmium::OSMObject::user_size
string_size_type user_size() const noexcept
Definition object.hpp:88
osmium::OSMObject::deleted
bool deleted() const noexcept
Is this object marked as deleted?
Definition object.hpp:147
osmium::OSMObject::uid
user_id_type uid() const noexcept
Get user id of this object.
Definition object.hpp:242
osmium::OSMObject::end
iterator end()
Definition object.hpp:389
osmium::OSMObject::set_uid
OSMObject & set_uid(user_id_type uid) noexcept
Definition object.hpp:251
osmium::OSMObject::set_timestamp
OSMObject & set_timestamp(const char *timestamp)
Definition object.hpp:308
osmium::OSMObject::get_value_by_key
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition object.hpp:339
osmium::OSMObject::cend
t_const_iterator< T > cend() const
Definition object.hpp:451
osmium::OSMObject::subitems_position
unsigned char * subitems_position()
Definition object.hpp:92
osmium::OSMObject::set_id
OSMObject & set_id(object_id_type id) noexcept
Definition object.hpp:132
osmium::OSMObject::changeset
changeset_id_type changeset() const noexcept
Get changeset id of this object.
Definition object.hpp:218
osmium::OSMObject::m_id
object_id_type m_id
Definition object.hpp:69
osmium::OSMObject::user_position
const unsigned char * user_position() const noexcept
Definition object.hpp:84
osmium::OSMObject::remove_tags
void remove_tags() noexcept
Definition object.hpp:377
osmium::OSMObject::OSMObject
OSMObject(osmium::memory::item_size_type size, osmium::item_type type)
Definition object.hpp:102
osmium::OSMObject::end
const_iterator end() const
Definition object.hpp:405
osmium::OSMObject::id
object_id_type id() const noexcept
Get ID of this object.
Definition object.hpp:118
osmium::OSMObject::positive_id
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition object.hpp:123
osmium::OSMObject::iterator
osmium::memory::CollectionIterator< Item > iterator
Definition object.hpp:368
osmium::OSMObject::end
t_const_iterator< T > end() const
Definition object.hpp:461
osmium::OSMObject::begin
t_iterator< T > begin()
Definition object.hpp:436
osmium::OSMObject::sizeof_object
size_t sizeof_object() const noexcept
Definition object.hpp:76
osmium::OSMObject::subitems_position
const unsigned char * subitems_position() const
Definition object.hpp:96
osmium::OSMObject::cbegin
t_const_iterator< T > cbegin() const
Definition object.hpp:446
osmium::OSMObject::const_iterator
osmium::memory::CollectionIterator< const Item > const_iterator
Definition object.hpp:369
osmium::OSMObject::m_deleted
bool m_deleted
Definition object.hpp:70
osmium::OSMObject::user_position
unsigned char * user_position() noexcept
Definition object.hpp:80
osmium::OSMObject::set_version
OSMObject & set_version(const char *version)
Definition object.hpp:213
osmium::OSMObject::set_uid_from_signed
OSMObject & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition object.hpp:262
osmium::OSMObject::begin
t_const_iterator< T > begin() const
Definition object.hpp:456
osmium::OSMObject::m_uid
user_id_type m_uid
Definition object.hpp:73
osmium::OSMObject::set_changeset
OSMObject & set_changeset(const char *changeset)
Definition object.hpp:237
osmium::OSMObject::subitems
osmium::memory::ItemIteratorRange< const T > subitems() const
Definition object.hpp:425
osmium::OSMObject::begin
iterator begin()
Definition object.hpp:385
osmium::OSMObject::m_changeset
changeset_id_type m_changeset
Definition object.hpp:74
osmium::OSMObject::cbegin
const_iterator cbegin() const
Definition object.hpp:393
osmium::OSMObject::subitems
osmium::memory::ItemIteratorRange< T > subitems()
Definition object.hpp:415
osmium::OSMObject::end
t_iterator< T > end()
Definition object.hpp:441
osmium::OSMObject::set_timestamp
OSMObject & set_timestamp(const osmium::Timestamp &timestamp) noexcept
Definition object.hpp:293
osmium::OSMObject::set_version
OSMObject & set_version(object_version_type version) noexcept
Definition object.hpp:203
osmium::OSMObject::set_attribute
OSMObject & set_attribute(const char *attr, const char *value)
Definition object.hpp:350
osmium::OSMObject::set_changeset
OSMObject & set_changeset(changeset_id_type changeset) noexcept
Definition object.hpp:227
osmium::TagList
Definition tag.hpp:119
osmium::TagList::get_value_by_key
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition tag.hpp:137
osmium::Timestamp
Definition timestamp.hpp:175
osmium::builder::OSMObjectBuilder
Definition way.hpp:49
osmium::memory::CollectionIterator
Definition collection.hpp:47
osmium::memory::ItemIteratorRange
Definition item_iterator.hpp:175
osmium::memory::ItemIterator
Definition item_iterator.hpp:59
osmium::memory::Item::type
item_type type() const noexcept
Definition item.hpp:171
osmium::memory::Item::next
unsigned char * next() noexcept
Definition item.hpp:155
collection.hpp
entity.hpp
item.hpp
item_iterator.hpp
item_type.hpp
location.hpp
misc.hpp
osmium::memory::padded_length
constexpr std::size_t padded_length(std::size_t length) noexcept
Definition item.hpp:64
osmium::memory::item_size_type
uint32_t item_size_type
Definition item.hpp:57
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::string_to_changeset_id
changeset_id_type string_to_changeset_id(const char *input)
Definition types_from_string.hpp:148
osmium::string_to_uid
user_id_type string_to_uid(const char *input)
Definition types_from_string.hpp:162
osmium::string_to_object_id
object_id_type string_to_object_id(const char *input)
Definition types_from_string.hpp:59
osmium::const_tie
std::tuple< const Ts &... > const_tie(const Ts &... args) noexcept
Definition misc.hpp:52
osmium::operator==
bool operator==(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:440
osmium::signed_user_id_type
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition types.hpp:50
osmium::changeset_id_type
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition types.hpp:48
osmium::operator<=
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:459
osmium::operator>
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:455
osmium::string_size_type
uint16_t string_size_type
Definition types.hpp:59
osmium::unsigned_object_id_type
uint64_t unsigned_object_id_type
Type for OSM object (node, way, or relation) IDs where we only allow positive IDs.
Definition types.hpp:46
osmium::item_type
item_type
Definition item_type.hpp:45
osmium::item_type::tag_list
@ tag_list
osmium::item_type::relation
@ relation
osmium::item_type::node
@ node
osmium::item_type::area
@ area
osmium::item_type::way
@ way
osmium::string_to_object_version
object_version_type string_to_object_version(const char *input)
Definition types_from_string.hpp:134
osmium::operator>=
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:463
osmium::operator!=
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:444
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition types.hpp:45
osmium::object_version_type
uint32_t object_version_type
Type for OSM object version number.
Definition types.hpp:47
osmium::user_id_type
uint32_t user_id_type
Type for OSM user IDs.
Definition types.hpp:49
osmium::operator<
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:451
tag.hpp
timestamp.hpp
types.hpp
types_from_string.hpp