Libosmium  2.5.4
Fast and flexible C++ library for working with OpenStreetMap data
osm_object_builder.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_BUILDER_OSM_OBJECT_BUILDER_HPP
2 #define OSMIUM_BUILDER_OSM_OBJECT_BUILDER_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 <cassert>
37 #include <cstring>
38 #include <initializer_list>
39 #include <new>
40 #include <stdexcept>
41 #include <string>
42 #include <utility>
43 
45 #include <osmium/osm.hpp>
46 #include <osmium/osm/item_type.hpp>
47 #include <osmium/osm/location.hpp>
48 #include <osmium/osm/node_ref.hpp>
49 #include <osmium/osm/object.hpp>
50 #include <osmium/osm/tag.hpp>
51 #include <osmium/osm/types.hpp>
52 #include <osmium/util/cast.hpp>
53 
54 namespace osmium {
55 
56  namespace memory {
57  class Buffer;
58  } // namespace memory
59 
60  namespace builder {
61 
62  class TagListBuilder : public ObjectBuilder<TagList> {
63 
64  public:
65 
66  explicit TagListBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
67  ObjectBuilder<TagList>(buffer, parent) {
68  }
69 
71  add_padding();
72  }
73 
80  void add_tag(const char* key, const char* value) {
81  if (std::strlen(key) > osmium::max_osm_string_length) {
82  throw std::length_error("OSM tag key is too long");
83  }
84  if (std::strlen(value) > osmium::max_osm_string_length) {
85  throw std::length_error("OSM tag value is too long");
86  }
87  add_size(append(key) + append(value));
88  }
89 
98  void add_tag(const char* key, const size_t key_length, const char* value, const size_t value_length) {
99  if (key_length > osmium::max_osm_string_length) {
100  throw std::length_error("OSM tag key is too long");
101  }
102  if (value_length > osmium::max_osm_string_length) {
103  throw std::length_error("OSM tag value is too long");
104  }
106  append(value, osmium::memory::item_size_type(value_length)) + append_zero());
107  }
108 
115  void add_tag(const std::string& key, const std::string& value) {
116  if (key.size() > osmium::max_osm_string_length) {
117  throw std::length_error("OSM tag key is too long");
118  }
119  if (value.size() > osmium::max_osm_string_length) {
120  throw std::length_error("OSM tag value is too long");
121  }
122  add_size(append(key.data(), osmium::memory::item_size_type(key.size()) + 1) +
123  append(value.data(), osmium::memory::item_size_type(value.size()) + 1));
124  }
125 
131  void add_tag(const osmium::Tag& tag) {
132  add_size(append(tag.key()) + append(tag.value()));
133  }
134 
140  void add_tag(const std::pair<const char*, const char*>& tag) {
141  add_tag(tag.first, tag.second);
142  }
143 
149  void add_tag(const std::pair<const std::string&, const std::string&>& tag) {
150  add_tag(tag.first, tag.second);
151  }
152 
153  }; // class TagListBuilder
154 
155  template <typename T>
156  class NodeRefListBuilder : public ObjectBuilder<T> {
157 
158  public:
159 
160  explicit NodeRefListBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
161  ObjectBuilder<T>(buffer, parent) {
162  }
163 
165  static_cast<Builder*>(this)->add_padding();
166  }
167 
168  void add_node_ref(const NodeRef& node_ref) {
169  new (static_cast<Builder*>(this)->reserve_space_for<osmium::NodeRef>()) osmium::NodeRef(node_ref);
170  static_cast<Builder*>(this)->add_size(sizeof(osmium::NodeRef));
171  }
172 
173  void add_node_ref(const object_id_type ref, const osmium::Location& location = Location{}) {
174  add_node_ref(NodeRef(ref, location));
175  }
176 
177  }; // class NodeRefListBuilder
178 
182 
183  class RelationMemberListBuilder : public ObjectBuilder<RelationMemberList> {
184 
194  void add_role(osmium::RelationMember& member, const char* role, const size_t length) {
195  if (length > osmium::max_osm_string_length) {
196  throw std::length_error("OSM relation member role is too long");
197  }
198  member.set_role_size(osmium::string_size_type(length) + 1);
200  add_padding(true);
201  }
202 
203  public:
204 
206  ObjectBuilder<RelationMemberList>(buffer, parent) {
207  }
208 
210  add_padding();
211  }
212 
226  void add_member(osmium::item_type type, object_id_type ref, const char* role, const size_t role_length, const osmium::OSMObject* full_member = nullptr) {
227  osmium::RelationMember* member = reserve_space_for<osmium::RelationMember>();
228  new (member) osmium::RelationMember(ref, type, full_member != nullptr);
229  add_size(sizeof(RelationMember));
230  add_role(*member, role, role_length);
231  if (full_member) {
232  add_item(full_member);
233  }
234  }
235 
247  void add_member(osmium::item_type type, object_id_type ref, const char* role, const osmium::OSMObject* full_member = nullptr) {
248  add_member(type, ref, role, std::strlen(role), full_member);
249  }
250 
262  void add_member(osmium::item_type type, object_id_type ref, const std::string& role, const osmium::OSMObject* full_member = nullptr) {
263  add_member(type, ref, role.data(), role.size(), full_member);
264  }
265 
266  }; // class RelationMemberListBuilder
267 
268  class ChangesetDiscussionBuilder : public ObjectBuilder<ChangesetDiscussion> {
269 
271 
272  void add_user(osmium::ChangesetComment& comment, const char* user, const size_t length) {
273  if (length > osmium::max_osm_string_length) {
274  throw std::length_error("OSM user name is too long");
275  }
276  comment.set_user_size(osmium::string_size_type(length) + 1);
278  }
279 
280  void add_text(osmium::ChangesetComment& comment, const char* text, const size_t length) {
281  // XXX There is no limit on the length of a comment text. We
282  // limit it here to 2^16-2 characters, because that's all that
283  // will fit into our internal data structure. This is not ideal,
284  // and will have to be discussed and cleared up.
285  if (length > std::numeric_limits<osmium::string_size_type>::max() - 1) {
286  throw std::length_error("OSM changeset comment is too long");
287  }
288  comment.set_text_size(osmium::string_size_type(length) + 1);
290  add_padding(true);
291  }
292 
293  public:
294 
296  ObjectBuilder<ChangesetDiscussion>(buffer, parent) {
297  }
298 
300  assert(!m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
301  add_padding();
302  }
303 
304  void add_comment(osmium::Timestamp date, osmium::user_id_type uid, const char* user) {
305  assert(!m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
306  m_comment = reserve_space_for<osmium::ChangesetComment>();
307  new (m_comment) osmium::ChangesetComment(date, uid);
308  add_size(sizeof(ChangesetComment));
309  add_user(*m_comment, user, std::strlen(user));
310  }
311 
312  void add_comment_text(const char* text) {
313  assert(m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
314  add_text(*m_comment, text, std::strlen(text));
315  m_comment = nullptr;
316  }
317 
318  void add_comment_text(const std::string& text) {
319  assert(m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
320  add_text(*m_comment, text.c_str(), text.size());
321  m_comment = nullptr;
322  }
323 
324  }; // class ChangesetDiscussionBuilder
325 
326  template <typename T>
327  class OSMObjectBuilder : public ObjectBuilder<T> {
328 
329  public:
330 
331  explicit OSMObjectBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
332  ObjectBuilder<T>(buffer, parent) {
333  static_cast<Builder*>(this)->reserve_space_for<string_size_type>();
334  static_cast<Builder*>(this)->add_size(sizeof(string_size_type));
335  }
336 
337  void add_tags(const std::initializer_list<std::pair<const char*, const char*>>& tags) {
338  osmium::builder::TagListBuilder tl_builder(static_cast<Builder*>(this)->buffer(), this);
339  for (const auto& p : tags) {
340  tl_builder.add_tag(p.first, p.second);
341  }
342  }
343 
344  }; // class OSMObjectBuilder
345 
348 
349  class WayBuilder : public OSMObjectBuilder<osmium::Way> {
350 
351  public:
352 
353  explicit WayBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
354  OSMObjectBuilder<osmium::Way>(buffer, parent) {
355  }
356 
357  void add_node_refs(const std::initializer_list<osmium::NodeRef>& nodes) {
359  for (const auto& node_ref : nodes) {
360  builder.add_node_ref(node_ref);
361  }
362  }
363 
364  }; // class WayBuilder
365 
366  class AreaBuilder : public OSMObjectBuilder<osmium::Area> {
367 
368  public:
369 
370  explicit AreaBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
371  OSMObjectBuilder<osmium::Area>(buffer, parent) {
372  }
373 
378  osmium::Area& area = object();
379  area.set_id(osmium::object_id_to_area_id(source.id(), source.type()));
380  area.set_version(source.version());
381  area.set_changeset(source.changeset());
382  area.set_timestamp(source.timestamp());
383  area.set_visible(source.visible());
384  area.set_uid(source.uid());
385 
386  add_user(source.user());
387  }
388 
389  }; // class AreaBuilder
390 
392 
393  } // namespace builder
394 
395 } // namespace osmium
396 
397 #endif // OSMIUM_BUILDER_OSM_OBJECT_BUILDER_HPP
Definition: tag.hpp:48
Definition: osm_object_builder.hpp:327
NodeRefListBuilder< InnerRing > InnerRingBuilder
Definition: osm_object_builder.hpp:181
~TagListBuilder()
Definition: osm_object_builder.hpp:70
Definition: changeset.hpp:128
changeset_id_type changeset() const noexcept
Get changeset id of this object.
Definition: object.hpp:210
osmium::memory::Buffer & buffer() noexcept
Return the buffer this builder is using.
Definition: builder.hpp:179
Definition: tag.hpp:105
type
Definition: entity_bits.hpp:60
osmium::memory::item_size_type append_zero()
Definition: builder.hpp:173
OSMObject & set_timestamp(const osmium::Timestamp &timestamp) noexcept
Definition: object.hpp:284
const char * value() const
Definition: tag.hpp:83
WayBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:353
AreaBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:370
void add_user(osmium::ChangesetComment &comment, const char *user, const size_t length)
Definition: osm_object_builder.hpp:272
void add_member(osmium::item_type type, object_id_type ref, const char *role, const osmium::OSMObject *full_member=nullptr)
Definition: osm_object_builder.hpp:247
void initialize_from_object(const osmium::OSMObject &source)
Definition: osm_object_builder.hpp:377
void add_user(const char *user, const string_size_type length)
Definition: builder.hpp:206
OSMObjectBuilder< osmium::Node > NodeBuilder
Definition: osm_object_builder.hpp:346
item_type
Definition: item_type.hpp:43
void add_tag(const osmium::Tag &tag)
Definition: osm_object_builder.hpp:131
~RelationMemberListBuilder()
Definition: osm_object_builder.hpp:209
Definition: area.hpp:113
Definition: relation.hpp:148
OSMObject & set_id(object_id_type id) noexcept
Definition: object.hpp:124
Definition: way.hpp:65
RelationMemberListBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:205
const char * key() const noexcept
Definition: tag.hpp:79
Definition: changeset.hpp:57
Definition: osm_object_builder.hpp:268
osmium::object_id_type object_id_to_area_id(osmium::object_id_type id, osmium::item_type type) noexcept
Definition: area.hpp:92
NodeRefListBuilder< WayNodeList > WayNodeListBuilder
Definition: osm_object_builder.hpp:179
object_version_type version() const noexcept
Get version of this object.
Definition: object.hpp:186
void add_text(osmium::ChangesetComment &comment, const char *text, const size_t length)
Definition: osm_object_builder.hpp:280
Definition: osm_object_builder.hpp:156
void add_tag(const std::pair< const std::string &, const std::string & > &tag)
Definition: osm_object_builder.hpp:149
void add_tag(const char *key, const size_t key_length, const char *value, const size_t value_length)
Definition: osm_object_builder.hpp:98
bool visible() const noexcept
Is this object marked visible (ie not deleted)?
Definition: object.hpp:144
Definition: relation.hpp:54
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
void add_tags(const std::initializer_list< std::pair< const char *, const char * >> &tags)
Definition: osm_object_builder.hpp:337
void add_node_refs(const std::initializer_list< osmium::NodeRef > &nodes)
Definition: osm_object_builder.hpp:357
void add_comment(osmium::Timestamp date, osmium::user_id_type uid, const char *user)
Definition: osm_object_builder.hpp:304
osmium::Timestamp timestamp() const noexcept
Get timestamp when this object last changed.
Definition: object.hpp:274
Definition: timestamp.hpp:55
OSMObjectBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:331
user_id_type uid() const noexcept
Get user id of this object.
Definition: object.hpp:234
uint16_t string_size_type
Definition: types.hpp:59
void add_tag(const std::pair< const char *, const char * > &tag)
Definition: osm_object_builder.hpp:140
NodeRefListBuilder< OuterRing > OuterRingBuilder
Definition: osm_object_builder.hpp:180
const char * user() const noexcept
Get user name for this object.
Definition: object.hpp:290
OSMObject & set_changeset(changeset_id_type changeset) noexcept
Definition: object.hpp:219
void add_member(osmium::item_type type, object_id_type ref, const char *role, const size_t role_length, const osmium::OSMObject *full_member=nullptr)
Definition: osm_object_builder.hpp:226
Definition: location.hpp:79
osmium::ChangesetComment * m_comment
Definition: osm_object_builder.hpp:270
Definition: osm_object_builder.hpp:349
void set_text_size(string_size_type size) noexcept
Definition: changeset.hpp:95
OSMObject & set_version(object_version_type version) noexcept
Definition: object.hpp:195
void set_role_size(string_size_type size) noexcept
Definition: relation.hpp:94
void add_member(osmium::item_type type, object_id_type ref, const std::string &role, const osmium::OSMObject *full_member=nullptr)
Definition: osm_object_builder.hpp:262
NodeRefListBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:160
void add_comment_text(const char *text)
Definition: osm_object_builder.hpp:312
Definition: buffer.hpp:97
Definition: builder.hpp:186
void set_user_size(string_size_type size) noexcept
Definition: changeset.hpp:91
OSMObject & set_visible(bool visible) noexcept
Definition: object.hpp:163
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:110
~NodeRefListBuilder()
Definition: osm_object_builder.hpp:164
osmium::memory::item_size_type append(const char *data, const osmium::memory::item_size_type length)
Definition: builder.hpp:152
void add_comment_text(const std::string &text)
Definition: osm_object_builder.hpp:318
ObjectBuilder< osmium::Changeset > ChangesetBuilder
Definition: osm_object_builder.hpp:391
void add_role(osmium::RelationMember &member, const char *role, const size_t length)
Definition: osm_object_builder.hpp:194
void add_size(uint32_t size)
Definition: builder.hpp:116
uint32_t item_size_type
Definition: item.hpp:50
void add_item(const osmium::memory::Item *item)
Definition: builder.hpp:127
void add_node_ref(const object_id_type ref, const osmium::Location &location=Location{})
Definition: osm_object_builder.hpp:173
OSMObjectBuilder< osmium::Relation > RelationBuilder
Definition: osm_object_builder.hpp:347
~ChangesetDiscussionBuilder()
Definition: osm_object_builder.hpp:299
void add_tag(const std::string &key, const std::string &value)
Definition: osm_object_builder.hpp:115
Definition: node_ref.hpp:50
osmium::Area & object() noexcept
Definition: builder.hpp:196
item_type type() const noexcept
Definition: item.hpp:155
OSMObject & set_uid(user_id_type uid) noexcept
Definition: object.hpp:243
Definition: builder.hpp:57
Definition: osm_object_builder.hpp:183
constexpr const int max_osm_string_length
Definition: types.hpp:62
TagListBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:66
void add_tag(const char *key, const char *value)
Definition: osm_object_builder.hpp:80
ChangesetDiscussionBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:295
Definition: object.hpp:58
uint32_t user_id_type
Type for OSM user IDs.
Definition: types.hpp:49
Definition: osm_object_builder.hpp:62
void add_node_ref(const NodeRef &node_ref)
Definition: osm_object_builder.hpp:168
Definition: osm_object_builder.hpp:366
void add_padding(bool self=false)
Definition: builder.hpp:103