Libosmium  2.2.0
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 
40 // Windows is only available for little endian architectures
41 // http://stackoverflow.com/questions/6449468/can-i-safely-assume-that-windows-installations-will-always-be-little-endian
42 #if !defined(_WIN32) && !defined(__APPLE__)
43 # include <endian.h>
44 #else
45 # define __LITTLE_ENDIAN 1234
46 # define __BYTE_ORDER __LITTLE_ENDIAN
47 #endif
48 
50 #include <osmium/geom/factory.hpp>
51 #include <osmium/util/cast.hpp>
52 
53 namespace osmium {
54 
55  namespace geom {
56 
57  enum class wkb_type : bool {
58  wkb = false,
59  ewkb = true
60  }; // enum class wkb_type
61 
62  enum class out_type : bool {
63  binary = false,
64  hex = true
65  }; // enum class out_type
66 
67  namespace detail {
68 
69  template <typename T>
70  inline void str_push(std::string& str, T data) {
71  size_t size = str.size();
72  str.resize(size + sizeof(T));
73  std::copy_n(reinterpret_cast<char*>(&data), sizeof(T), &str[size]);
74  }
75 
76  inline std::string convert_to_hex(const std::string& str) {
77  static const char* lookup_hex = "0123456789ABCDEF";
78  std::string out;
79 
80  for (char c : str) {
81  out += lookup_hex[(c >> 4) & 0xf];
82  out += lookup_hex[c & 0xf];
83  }
84 
85  return out;
86  }
87 
88  class WKBFactoryImpl {
89 
91  static constexpr uint32_t srid = 4326;
92 
100  enum wkbGeometryType : uint32_t {
101  wkbPoint = 1,
102  wkbLineString = 2,
103  wkbPolygon = 3,
104  wkbMultiPoint = 4,
105  wkbMultiLineString = 5,
106  wkbMultiPolygon = 6,
107  wkbGeometryCollection = 7,
108 
109  // SRID-presence flag (EWKB)
110  wkbSRID = 0x20000000
111  }; // enum wkbGeometryType
112 
116  enum class wkb_byte_order_type : uint8_t {
117  XDR = 0, // Big Endian
118  NDR = 1 // Little Endian
119  }; // enum class wkb_byte_order_type
120 
121  std::string m_data;
122  uint32_t m_points {0};
123  wkb_type m_wkb_type;
124  out_type m_out_type;
125 
126  size_t m_linestring_size_offset = 0;
127  size_t m_polygons = 0;
128  size_t m_rings = 0;
129  size_t m_multipolygon_size_offset = 0;
130  size_t m_polygon_size_offset = 0;
131  size_t m_ring_size_offset = 0;
132 
133  size_t header(std::string& str, wkbGeometryType type, bool add_length) const {
134 #if __BYTE_ORDER == __LITTLE_ENDIAN
135  str_push(str, wkb_byte_order_type::NDR);
136 #else
137  str_push(str, wkb_byte_order_type::XDR);
138 #endif
139  if (m_wkb_type == wkb_type::ewkb) {
140  str_push(str, type | wkbSRID);
141  str_push(str, srid);
142  } else {
143  str_push(str, type);
144  }
145  size_t offset = str.size();
146  if (add_length) {
147  str_push(str, static_cast<uint32_t>(0));
148  }
149  return offset;
150  }
151 
152  void set_size(const size_t offset, const size_t size) {
153  *reinterpret_cast<uint32_t*>(&m_data[offset]) = static_cast_with_assert<uint32_t>(size);
154  }
155 
156  public:
157 
158  typedef std::string point_type;
159  typedef std::string linestring_type;
160  typedef std::string polygon_type;
161  typedef std::string multipolygon_type;
162  typedef std::string ring_type;
163 
164  explicit WKBFactoryImpl(wkb_type wtype = wkb_type::wkb, out_type otype = out_type::binary) :
165  m_wkb_type(wtype),
166  m_out_type(otype) {
167  }
168 
169  /* Point */
170 
171  point_type make_point(const osmium::geom::Coordinates& xy) const {
172  std::string data;
173  header(data, wkbPoint, false);
174  str_push(data, xy.x);
175  str_push(data, xy.y);
176 
177  if (m_out_type == out_type::hex) {
178  return convert_to_hex(data);
179  } else {
180  return data;
181  }
182  }
183 
184  /* LineString */
185 
186  void linestring_start() {
187  m_data.clear();
188  m_linestring_size_offset = header(m_data, wkbLineString, true);
189  }
190 
191  void linestring_add_location(const osmium::geom::Coordinates& xy) {
192  str_push(m_data, xy.x);
193  str_push(m_data, xy.y);
194  }
195 
196  linestring_type linestring_finish(size_t num_points) {
197  set_size(m_linestring_size_offset, num_points);
198  std::string data;
199  std::swap(data, m_data);
200 
201  if (m_out_type == out_type::hex) {
202  return convert_to_hex(data);
203  } else {
204  return data;
205  }
206  }
207 
208  /* MultiPolygon */
209 
210  void multipolygon_start() {
211  m_data.clear();
212  m_polygons = 0;
213  m_multipolygon_size_offset = header(m_data, wkbMultiPolygon, true);
214  }
215 
216  void multipolygon_polygon_start() {
217  ++m_polygons;
218  m_rings = 0;
219  m_polygon_size_offset = header(m_data, wkbPolygon, true);
220  }
221 
222  void multipolygon_polygon_finish() {
223  set_size(m_polygon_size_offset, m_rings);
224  }
225 
226  void multipolygon_outer_ring_start() {
227  ++m_rings;
228  m_points = 0;
229  m_ring_size_offset = m_data.size();
230  str_push(m_data, static_cast<uint32_t>(0));
231  }
232 
233  void multipolygon_outer_ring_finish() {
234  set_size(m_ring_size_offset, m_points);
235  }
236 
237  void multipolygon_inner_ring_start() {
238  ++m_rings;
239  m_points = 0;
240  m_ring_size_offset = m_data.size();
241  str_push(m_data, static_cast<uint32_t>(0));
242  }
243 
244  void multipolygon_inner_ring_finish() {
245  set_size(m_ring_size_offset, m_points);
246  }
247 
248  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
249  str_push(m_data, xy.x);
250  str_push(m_data, xy.y);
251  ++m_points;
252  }
253 
254  multipolygon_type multipolygon_finish() {
255  set_size(m_multipolygon_size_offset, m_polygons);
256  std::string data;
257  std::swap(data, m_data);
258 
259  if (m_out_type == out_type::hex) {
260  return convert_to_hex(data);
261  } else {
262  return data;
263  }
264  }
265 
266  }; // class WKBFactoryImpl
267 
268  } // namespace detail
269 
270  template <class TProjection = IdentityProjection>
272 
273  } // namespace geom
274 
275 } // namespace osmium
276 
277 #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:57
out_type
Definition: wkb.hpp:62
double x
Definition: coordinates.hpp:49