Libosmium  2.13.1
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-2017 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 <algorithm>
37 #include <cstddef>
38 #include <cstdint>
39 #include <string>
40 
42 #include <osmium/geom/factory.hpp>
43 #include <osmium/util/cast.hpp>
44 #include <osmium/util/endian.hpp>
45 
46 namespace osmium {
47 
48  namespace geom {
49 
50  enum class wkb_type : bool {
51  wkb = false,
52  ewkb = true
53  }; // enum class wkb_type
54 
55  enum class out_type : bool {
56  binary = false,
57  hex = true
58  }; // enum class out_type
59 
60  namespace detail {
61 
62  template <typename T>
63  inline void str_push(std::string& str, T data) {
64  str.append(reinterpret_cast<const char*>(&data), sizeof(T));
65  }
66 
67  inline std::string convert_to_hex(const std::string& str) {
68  static const char* lookup_hex = "0123456789ABCDEF";
69  std::string out;
70  out.reserve(str.size() * 2);
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 
89  enum wkbGeometryType : uint32_t {
90  wkbPoint = 1,
91  wkbLineString = 2,
92  wkbPolygon = 3,
93  wkbMultiPoint = 4,
94  wkbMultiLineString = 5,
95  wkbMultiPolygon = 6,
96  wkbGeometryCollection = 7,
97 
98  // SRID-presence flag (EWKB)
99  wkbSRID = 0x20000000
100  }; // enum wkbGeometryType
101 
105  enum class wkb_byte_order_type : uint8_t {
106  XDR = 0, // Big Endian
107  NDR = 1 // Little Endian
108  }; // enum class wkb_byte_order_type
109 
110  std::string m_data;
111  uint32_t m_points = 0;
112  int m_srid;
113  wkb_type m_wkb_type;
114  out_type m_out_type;
115 
116  std::size_t m_linestring_size_offset = 0;
117  std::size_t m_polygons = 0;
118  std::size_t m_rings = 0;
119  std::size_t m_multipolygon_size_offset = 0;
120  std::size_t m_polygon_size_offset = 0;
121  std::size_t m_ring_size_offset = 0;
122 
123  std::size_t header(std::string& str, wkbGeometryType type, bool add_length) const {
124 #if __BYTE_ORDER == __LITTLE_ENDIAN
125  str_push(str, wkb_byte_order_type::NDR);
126 #else
127  str_push(str, wkb_byte_order_type::XDR);
128 #endif
129  if (m_wkb_type == wkb_type::ewkb) {
130  str_push(str, type | wkbSRID);
131  str_push(str, m_srid);
132  } else {
133  str_push(str, type);
134  }
135  const std::size_t offset = str.size();
136  if (add_length) {
137  str_push(str, static_cast<uint32_t>(0));
138  }
139  return offset;
140  }
141 
142  void set_size(const std::size_t offset, const std::size_t size) {
143  uint32_t s = static_cast_with_assert<uint32_t>(size);
144  std::copy_n(reinterpret_cast<char*>(&s), sizeof(uint32_t), &m_data[offset]);
145  }
146 
147  public:
148 
149  using point_type = std::string;
150  using linestring_type = std::string;
151  using polygon_type = std::string;
152  using multipolygon_type = std::string;
153  using ring_type = std::string;
154 
155  explicit WKBFactoryImpl(int srid, wkb_type wtype = wkb_type::wkb, out_type otype = out_type::binary) :
156  m_srid(srid),
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(std::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:51
Definition: factory.hpp:148
type
Definition: entity_bits.hpp:63
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:762
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: attr.hpp:333
Definition: coordinates.hpp:48
wkb_type
Definition: wkb.hpp:50
out_type
Definition: wkb.hpp:55
double x
Definition: coordinates.hpp:50