Libosmium  2.5.3
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  }
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 
126  }; // class TagListBuilder
127 
128  template <typename T>
129  class NodeRefListBuilder : public ObjectBuilder<T> {
130 
131  public:
132 
133  explicit NodeRefListBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
134  ObjectBuilder<T>(buffer, parent) {
135  }
136 
138  static_cast<Builder*>(this)->add_padding();
139  }
140 
141  void add_node_ref(const NodeRef& node_ref) {
142  new (static_cast<Builder*>(this)->reserve_space_for<osmium::NodeRef>()) osmium::NodeRef(node_ref);
143  static_cast<Builder*>(this)->add_size(sizeof(osmium::NodeRef));
144  }
145 
146  void add_node_ref(const object_id_type ref, const osmium::Location& location = Location{}) {
147  add_node_ref(NodeRef(ref, location));
148  }
149 
150  }; // class NodeRefListBuilder
151 
155 
156  class RelationMemberListBuilder : public ObjectBuilder<RelationMemberList> {
157 
167  void add_role(osmium::RelationMember& member, const char* role, const size_t length) {
168  if (length > osmium::max_osm_string_length) {
169  throw std::length_error("OSM relation member role is too long");
170  }
171  member.set_role_size(osmium::string_size_type(length) + 1);
173  add_padding(true);
174  }
175 
176  public:
177 
179  ObjectBuilder<RelationMemberList>(buffer, parent) {
180  }
181 
183  add_padding();
184  }
185 
199  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) {
200  osmium::RelationMember* member = reserve_space_for<osmium::RelationMember>();
201  new (member) osmium::RelationMember(ref, type, full_member != nullptr);
202  add_size(sizeof(RelationMember));
203  add_role(*member, role, role_length);
204  if (full_member) {
205  add_item(full_member);
206  }
207  }
208 
220  void add_member(osmium::item_type type, object_id_type ref, const char* role, const osmium::OSMObject* full_member = nullptr) {
221  add_member(type, ref, role, std::strlen(role), full_member);
222  }
223 
235  void add_member(osmium::item_type type, object_id_type ref, const std::string& role, const osmium::OSMObject* full_member = nullptr) {
236  add_member(type, ref, role.data(), role.size(), full_member);
237  }
238 
239  }; // class RelationMemberListBuilder
240 
241  class ChangesetDiscussionBuilder : public ObjectBuilder<ChangesetDiscussion> {
242 
244 
245  void add_user(osmium::ChangesetComment& comment, const char* user, const size_t length) {
246  if (length > osmium::max_osm_string_length) {
247  throw std::length_error("OSM user name is too long");
248  }
249  comment.set_user_size(osmium::string_size_type(length) + 1);
251  }
252 
253  void add_text(osmium::ChangesetComment& comment, const char* text, const size_t length) {
254  // XXX There is no limit on the length of a comment text. We
255  // limit it here to 2^16-2 characters, because that's all that
256  // will fit into our internal data structure. This is not ideal,
257  // and will have to be discussed and cleared up.
258  if (length > std::numeric_limits<osmium::string_size_type>::max() - 1) {
259  throw std::length_error("OSM changeset comment is too long");
260  }
261  comment.set_text_size(osmium::string_size_type(length) + 1);
263  add_padding(true);
264  }
265 
266  public:
267 
269  ObjectBuilder<ChangesetDiscussion>(buffer, parent) {
270  }
271 
273  assert(!m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
274  add_padding();
275  }
276 
277  void add_comment(osmium::Timestamp date, osmium::user_id_type uid, const char* user) {
278  assert(!m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
279  m_comment = reserve_space_for<osmium::ChangesetComment>();
280  new (m_comment) osmium::ChangesetComment(date, uid);
281  add_size(sizeof(ChangesetComment));
282  add_user(*m_comment, user, std::strlen(user));
283  }
284 
285  void add_comment_text(const char* text) {
286  assert(m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
287  add_text(*m_comment, text, std::strlen(text));
288  m_comment = nullptr;
289  }
290 
291  void add_comment_text(const std::string& text) {
292  assert(m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
293  add_text(*m_comment, text.c_str(), text.size());
294  m_comment = nullptr;
295  }
296 
297  }; // class ChangesetDiscussionBuilder
298 
299  template <typename T>
300  class OSMObjectBuilder : public ObjectBuilder<T> {
301 
302  public:
303 
304  explicit OSMObjectBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
305  ObjectBuilder<T>(buffer, parent) {
306  static_cast<Builder*>(this)->reserve_space_for<string_size_type>();
307  static_cast<Builder*>(this)->add_size(sizeof(string_size_type));
308  }
309 
310  void add_tags(const std::initializer_list<std::pair<const char*, const char*>>& tags) {
311  osmium::builder::TagListBuilder tl_builder(static_cast<Builder*>(this)->buffer(), this);
312  for (const auto& p : tags) {
313  tl_builder.add_tag(p.first, p.second);
314  }
315  }
316 
317  }; // class OSMObjectBuilder
318 
321 
322  class WayBuilder : public OSMObjectBuilder<osmium::Way> {
323 
324  public:
325 
326  explicit WayBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
327  OSMObjectBuilder<osmium::Way>(buffer, parent) {
328  }
329 
330  void add_node_refs(const std::initializer_list<osmium::NodeRef>& nodes) {
332  for (const auto& node_ref : nodes) {
333  builder.add_node_ref(node_ref);
334  }
335  }
336 
337  }; // class WayBuilder
338 
339  class AreaBuilder : public OSMObjectBuilder<osmium::Area> {
340 
341  public:
342 
343  explicit AreaBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
344  OSMObjectBuilder<osmium::Area>(buffer, parent) {
345  }
346 
351  osmium::Area& area = object();
352  area.set_id(osmium::object_id_to_area_id(source.id(), source.type()));
353  area.set_version(source.version());
354  area.set_changeset(source.changeset());
355  area.set_timestamp(source.timestamp());
356  area.set_visible(source.visible());
357  area.set_uid(source.uid());
358 
359  add_user(source.user());
360  }
361 
362  }; // class AreaBuilder
363 
365 
366  } // namespace builder
367 
368 } // namespace osmium
369 
370 #endif // OSMIUM_BUILDER_OSM_OBJECT_BUILDER_HPP
Definition: osm_object_builder.hpp:300
NodeRefListBuilder< InnerRing > InnerRingBuilder
Definition: osm_object_builder.hpp:154
~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
WayBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:326
AreaBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:343
void add_user(osmium::ChangesetComment &comment, const char *user, const size_t length)
Definition: osm_object_builder.hpp:245
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:220
void initialize_from_object(const osmium::OSMObject &source)
Definition: osm_object_builder.hpp:350
void add_user(const char *user, const string_size_type length)
Definition: builder.hpp:206
OSMObjectBuilder< osmium::Node > NodeBuilder
Definition: osm_object_builder.hpp:319
item_type
Definition: item_type.hpp:43
~RelationMemberListBuilder()
Definition: osm_object_builder.hpp:182
Definition: area.hpp:113
Definition: relation.hpp:150
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:178
Definition: changeset.hpp:57
Definition: osm_object_builder.hpp:241
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:152
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:253
Definition: osm_object_builder.hpp:129
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:310
void add_node_refs(const std::initializer_list< osmium::NodeRef > &nodes)
Definition: osm_object_builder.hpp:330
void add_comment(osmium::Timestamp date, osmium::user_id_type uid, const char *user)
Definition: osm_object_builder.hpp:277
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:304
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
NodeRefListBuilder< OuterRing > OuterRingBuilder
Definition: osm_object_builder.hpp:153
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:199
Definition: location.hpp:79
osmium::ChangesetComment * m_comment
Definition: osm_object_builder.hpp:243
Definition: osm_object_builder.hpp:322
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:96
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:235
NodeRefListBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: osm_object_builder.hpp:133
void add_comment_text(const char *text)
Definition: osm_object_builder.hpp:285
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:137
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:291
ObjectBuilder< osmium::Changeset > ChangesetBuilder
Definition: osm_object_builder.hpp:364
void add_role(osmium::RelationMember &member, const char *role, const size_t length)
Definition: osm_object_builder.hpp:167
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:146
OSMObjectBuilder< osmium::Relation > RelationBuilder
Definition: osm_object_builder.hpp:320
~ChangesetDiscussionBuilder()
Definition: osm_object_builder.hpp:272
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:156
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:268
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:141
Definition: osm_object_builder.hpp:339
void add_padding(bool self=false)
Definition: builder.hpp:103