Libosmium  2.5.4
Fast and flexible C++ library for working with OpenStreetMap data
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 
192  using std::swap;
193  swap(data, m_data);
194 
195  if (m_out_type == out_type::hex) {
196  return convert_to_hex(data);
197  } else {
198  return data;
199  }
200  }
201 
202  /* MultiPolygon */
203 
204  void multipolygon_start() {
205  m_data.clear();
206  m_polygons = 0;
207  m_multipolygon_size_offset = header(m_data, wkbMultiPolygon, true);
208  }
209 
210  void multipolygon_polygon_start() {
211  ++m_polygons;
212  m_rings = 0;
213  m_polygon_size_offset = header(m_data, wkbPolygon, true);
214  }
215 
216  void multipolygon_polygon_finish() {
217  set_size(m_polygon_size_offset, m_rings);
218  }
219 
220  void multipolygon_outer_ring_start() {
221  ++m_rings;
222  m_points = 0;
223  m_ring_size_offset = m_data.size();
224  str_push(m_data, static_cast<uint32_t>(0));
225  }
226 
227  void multipolygon_outer_ring_finish() {
228  set_size(m_ring_size_offset, m_points);
229  }
230 
231  void multipolygon_inner_ring_start() {
232  ++m_rings;
233  m_points = 0;
234  m_ring_size_offset = m_data.size();
235  str_push(m_data, static_cast<uint32_t>(0));
236  }
237 
238  void multipolygon_inner_ring_finish() {
239  set_size(m_ring_size_offset, m_points);
240  }
241 
242  void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
243  str_push(m_data, xy.x);
244  str_push(m_data, xy.y);
245  ++m_points;
246  }
247 
248  multipolygon_type multipolygon_finish() {
249  set_size(m_multipolygon_size_offset, m_polygons);
250  std::string data;
251 
252  using std::swap;
253  swap(data, m_data);
254 
255  if (m_out_type == out_type::hex) {
256  return convert_to_hex(data);
257  } else {
258  return data;
259  }
260  }
261 
262  }; // class WKBFactoryImpl
263 
264  } // namespace detail
265 
266  template <typename TProjection = IdentityProjection>
268 
269  } // namespace geom
270 
271 } // namespace osmium
272 
273 #endif // OSMIUM_GEOM_WKB_HPP
double y
Definition: coordinates.hpp:49
Definition: factory.hpp:146
type
Definition: entity_bits.hpp:60
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
Definition: coordinates.hpp:46
wkb_type
Definition: wkb.hpp:49
out_type
Definition: wkb.hpp:54
double x
Definition: coordinates.hpp:48