Libosmium  2.4.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
wkb.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_GEOM_WKB_HPP
2 #define OSMIUM_GEOM_WKB_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 <cstddef>
37 #include <cstdint>
38 #include <string>
39 
41 #include <osmium/geom/factory.hpp>
42 #include <osmium/util/cast.hpp>
43 #include <osmium/util/endian.hpp>
44 
45 namespace osmium {
46 
47  namespace geom {
48 
49  enum class wkb_type : bool {
50  wkb = false,
51  ewkb = true
52  }; // enum class wkb_type
53 
54  enum class out_type : bool {
55  binary = false,
56  hex = true
57  }; // enum class out_type
58 
59  namespace detail {
60 
61  template <typename T>
62  inline void str_push(std::string& str, T data) {
63  size_t size = str.size();
64  str.resize(size + sizeof(T));
65  std::copy_n(reinterpret_cast<char*>(&data), sizeof(T), &str[size]);
66  }
67 
68  inline std::string convert_to_hex(const std::string& str) {
69  static const char* lookup_hex = "0123456789ABCDEF";
70  std::string out;
71 
72  for (char c : str) {
73  out += lookup_hex[(c >> 4) & 0xf];
74  out += lookup_hex[c & 0xf];
75  }
76 
77  return out;
78  }
79 
80  class WKBFactoryImpl {
81 
83  static constexpr uint32_t srid = 4326;
84 
92  enum wkbGeometryType : uint32_t {
93  wkbPoint = 1,
94  wkbLineString = 2,
95  wkbPolygon = 3,
96  wkbMultiPoint = 4,
97  wkbMultiLineString = 5,
98  wkbMultiPolygon = 6,
99  wkbGeometryCollection = 7,
100 
101  // SRID-presence flag (EWKB)
102  wkbSRID = 0x20000000
103  }; // enum wkbGeometryType
104 
108  enum class wkb_byte_order_type : uint8_t {
109  XDR = 0, // Big Endian
110  NDR = 1 // Little Endian
111  }; // enum class wkb_byte_order_type
112 
113  std::string m_data;
114  uint32_t m_points {0};
115  wkb_type m_wkb_type;
116  out_type m_out_type;
117 
118  size_t m_linestring_size_offset = 0;
119  size_t m_polygons = 0;
120  size_t m_rings = 0;
121  size_t m_multipolygon_size_offset = 0;
122  size_t m_polygon_size_offset = 0;
123  size_t m_ring_size_offset = 0;
124 
125  size_t header(std::string& str, wkbGeometryType type, bool add_length) const {
126 #if __BYTE_ORDER == __LITTLE_ENDIAN
127  str_push(str, wkb_byte_order_type::NDR);
128 #else
129  str_push(str, wkb_byte_order_type::XDR);
130 #endif
131  if (m_wkb_type == wkb_type::ewkb) {
132  str_push(str, type | wkbSRID);
133  str_push(str, srid);
134  } else {
135  str_push(str, type);
136  }
137  size_t offset = str.size();
138  if (add_length) {
139  str_push(str, static_cast<uint32_t>(0));
140  }
141  return offset;
142  }
143 
144  void set_size(const size_t offset, const size_t size) {
145  *reinterpret_cast<uint32_t*>(&m_data[offset]) = static_cast_with_assert<uint32_t>(size);
146  }
147 
148  public:
149 
150  typedef std::string point_type;
151  typedef std::string linestring_type;
152  typedef std::string polygon_type;
153  typedef std::string multipolygon_type;
154  typedef std::string ring_type;
155 
156  explicit WKBFactoryImpl(wkb_type wtype = wkb_type::wkb, out_type otype = out_type::binary) :
157  m_wkb_type(wtype),
158  m_out_type(otype) {
159  }
160 
161  /* Point */
162 
163  point_type make_point(const osmium::geom::Coordinates& xy) const {
164  std::string data;
165  header(data, wkbPoint, false);
166  str_push(data, xy.x);
167  str_push(data, xy.y);
168 
169  if (m_out_type == out_type::hex) {
170  return convert_to_hex(data);
171  } else {
172  return data;
173  }
174  }
175 
176  /* LineString */
177 
178  void linestring_start() {
179  m_data.clear();
180  m_linestring_size_offset = header(m_data, wkbLineString, true);
181  }
182 
183  void linestring_add_location(const osmium::geom::Coordinates& xy) {
184  str_push(m_data, xy.x);
185  str_push(m_data, xy.y);
186  }
187 
188  linestring_type linestring_finish(size_t num_points) {
189  set_size(m_linestring_size_offset, num_points);
190  std::string data;
191  std::swap(data, m_data);
192 
193  if (m_out_type == out_type::hex) {
194  return convert_to_hex(data);
195  } else {
196  return data;
197  }
198  }
199 
200  /* MultiPolygon */
201 
202  void multipolygon_start() {
203  m_data.clear();
204  m_polygons = 0;
205  m_multipolygon_size_offset = header(m_data, wkbMultiPolygon, true);
206  }
207 
208  void multipolygon_polygon_start() {
209  ++m_polygons;
210  m_rings = 0;
211  m_polygon_size_offset = header(m_data, wkbPolygon, true);
212  }
213 
214  void multipolygon_polygon_finish() {
215  set_size(m_polygon_size_offset, m_rings);
216  }
217 
218  void multipolygon_outer_ring_start() {
219  ++m_rings;
220  m_points = 0;
221  m_ring_size_offset = m_data.size();
222  str_push(m_data, static_cast<uint32_t>(0));
223  }
224 
225  void multipolygon_outer_ring_finish() {
226  set_size(m_ring_size_offset, m_points);
227  }
228 
229  void multipolygon_inner_ring_start() {
230  ++m_rings;
231  m_points = 0;
232  m_ring_size_offset = m_data.size();
233  str_push(m_data, static_cast<uint32_t>(0));
234  }
235 
236  void multipolygon_inner_ring_finish() {
237  set_size(m_ring_size_offset, m_points);
238  }
239 
240  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
241  str_push(m_data, xy.x);
242  str_push(m_data, xy.y);
243  ++m_points;
244  }
245 
246  multipolygon_type multipolygon_finish() {
247  set_size(m_multipolygon_size_offset, m_polygons);
248  std::string data;
249  std::swap(data, m_data);
250 
251  if (m_out_type == out_type::hex) {
252  return convert_to_hex(data);
253  } else {
254  return data;
255  }
256  }
257 
258  }; // class WKBFactoryImpl
259 
260  } // namespace detail
261 
262  template <class TProjection = IdentityProjection>
264 
265  } // namespace geom
266 
267 } // namespace osmium
268 
269 #endif // OSMIUM_GEOM_WKB_HPP
double y
Definition: coordinates.hpp:50
Definition: factory.hpp:146
type
Definition: entity_bits.hpp:60
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
Definition: coordinates.hpp:47
wkb_type
Definition: wkb.hpp:49
out_type
Definition: wkb.hpp:54
double x
Definition: coordinates.hpp:49