forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wkb.hpp
355 lines (278 loc) · 8.07 KB
/
wkb.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#ifndef OSM2PGSQL_WKB_HPP
#define OSM2PGSQL_WKB_HPP
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <string>
#include <osmium/geom/coordinates.hpp>
#include <osmium/geom/factory.hpp>
namespace ewkb {
enum geometry_type : uint32_t
{
wkb_point = 1,
wkb_line = 2,
wkb_polygon = 3,
wkb_multi_point = 4,
wkb_multi_line = 5,
wkb_multi_polygon = 6,
wkb_collection = 7,
wkb_srid = 0x20000000 // SRID-presence flag (EWKB)
};
enum wkb_byte_order_type_t : uint8_t
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
Endian = 1 // Little Endian
#else
Endian = 0, // Big Endian
#endif
};
/**
* Writer for EWKB data suitable for postgres.
*
* Code has been largely derived from osmium::geom::WKBFactoryImpl.
*/
class writer_t
{
std::string m_data;
int m_srid;
size_t m_geometry_size_offset = 0;
size_t m_multigeometry_size_offset = 0;
size_t m_ring_size_offset = 0;
size_t header(std::string &str, geometry_type type, bool add_length) const
{
str_push(str, Endian);
str_push(str, type | wkb_srid);
str_push(str, m_srid);
const size_t offset = str.size();
if (add_length) {
str_push(str, static_cast<uint32_t>(0));
}
return offset;
}
void set_size(const size_t offset, const size_t size)
{
uint32_t s = static_cast<uint32_t>(size);
std::copy_n(reinterpret_cast<char *>(&s), sizeof(uint32_t),
&m_data[offset]);
}
template <typename T>
inline static void str_push(std::string &str, T data)
{
str.append(reinterpret_cast<const char *>(&data), sizeof(T));
}
public:
explicit writer_t(int srid) : m_srid(srid) {}
void add_sub_geometry(std::string const &part) { m_data.append(part); }
void add_location(const osmium::geom::Coordinates &xy)
{
str_push(m_data, xy.x);
str_push(m_data, xy.y);
}
/* Point */
std::string make_point(const osmium::geom::Coordinates &xy) const
{
std::string data;
header(data, wkb_point, false);
str_push(data, xy.x);
str_push(data, xy.y);
return data;
}
/* LineString */
void linestring_start()
{
m_geometry_size_offset = header(m_data, wkb_line, true);
}
std::string linestring_finish(size_t num_points)
{
set_size(m_geometry_size_offset, num_points);
std::string data;
using std::swap;
swap(data, m_data);
return data;
}
/* MultiLineString */
void multilinestring_start()
{
m_multigeometry_size_offset = header(m_data, wkb_multi_line, true);
}
std::string multilinestring_finish(size_t num_lines)
{
set_size(m_multigeometry_size_offset, num_lines);
std::string data;
using std::swap;
swap(data, m_data);
return data;
}
/* Polygon */
void polygon_start()
{
m_geometry_size_offset = header(m_data, wkb_polygon, true);
}
void polygon_ring_start()
{
m_ring_size_offset = m_data.size();
str_push(m_data, static_cast<uint32_t>(0));
}
void polygon_ring_finish(size_t num_points)
{
set_size(m_ring_size_offset, num_points);
}
std::string polygon_finish(size_t num_rings)
{
set_size(m_geometry_size_offset, num_rings);
std::string data;
using std::swap;
swap(data, m_data);
return data;
}
/* MultiPolygon */
void multipolygon_start()
{
m_multigeometry_size_offset = header(m_data, wkb_multi_polygon, true);
}
std::string multipolygon_finish(size_t num_polygons)
{
set_size(m_multigeometry_size_offset, num_polygons);
std::string data;
using std::swap;
swap(data, m_data);
return data;
}
};
/**
* Class that allows to iterate over the elements of a ewkb geometry.
*
* Note: this class assumes that the wkb was created by ewkb::writer_t.
* It implements the exact opposite decoding.
*/
class parser_t
{
public:
inline static std::string wkb_from_hex(std::string const &wkb)
{
std::string out;
bool front = true;
char outc;
for (char c : wkb) {
c -= 48;
if (c > 9) {
c -= 7;
}
if (front) {
outc = char(c << 4);
front = false;
} else {
out += outc | c;
front = true;
}
}
if (out[0] != Endian)
throw std::runtime_error(
#if __BYTE_ORDER == __LITTLE_ENDIAN
"Geometries in the database are returned in big-endian byte order. "
#else
"Geometries in the database are returned in little-endian byte order. "
#endif
"osm2pgsql can only process geometries in native byte order."
);
return out;
}
explicit parser_t(char const *wkb) : m_wkb(wkb), m_pos(0) {}
explicit parser_t(std::string const &wkb) : m_wkb(wkb.c_str()), m_pos(0) {}
size_t save_pos() const { return m_pos; }
void rewind(size_t pos) { m_pos = pos; }
int read_header()
{
m_pos += sizeof(uint8_t); // skip endianess
auto type = read_data<uint32_t>();
if (type & wkb_srid) {
m_pos += sizeof(int); // skip srid
}
return type & 0xff;
}
uint32_t read_length() { return read_data<uint32_t>(); }
osmium::geom::Coordinates read_point()
{
auto x = read_data<double>();
auto y = read_data<double>();
return osmium::geom::Coordinates(x, y);
}
void skip_points(size_t num) { m_pos += sizeof(double) * 2 * num; }
template <typename PROJ>
double get_area(PROJ *proj = nullptr)
{
double total = 0;
auto type = read_header();
if (type == wkb_polygon) {
total = get_polygon_area(proj);
} else if (type == wkb_multi_polygon) {
auto num_poly = read_length();
for (unsigned i = 0; i < num_poly; ++i) {
auto ptype = read_header();
(void)ptype;
assert(ptype == wkb_polygon);
total += get_polygon_area(proj);
}
}
return total;
}
private:
template <typename PROJ>
double get_polygon_area(PROJ *proj)
{
auto num_rings = read_length();
assert(num_rings > 0);
double total = get_ring_area(proj);
for (unsigned i = 1; i < num_rings; ++i) {
total -= get_ring_area(proj);
}
return total;
}
template <typename PROJ>
double get_ring_area(PROJ *proj)
{
// Algorithm borrowed from
// http://stackoverflow.com/questions/451426/how-do-i-calculate-the-area-of-a-2d-polygon
// XXX numerically not stable (useless for latlon)
auto num_pts = read_length();
assert(num_pts > 3);
double total = 0;
auto prev = read_point();
proj->target_to_tile(&prev.y, &prev.x);
for (unsigned i = 1; i < num_pts; ++i) {
auto cur = read_point();
proj->target_to_tile(&cur.y, &cur.x);
total += prev.x * cur.y - cur.x * prev.y;
prev = cur;
}
return std::abs(total) * 0.5;
}
double get_ring_area(osmium::geom::IdentityProjection *)
{
// Algorithm borrowed from
// http://stackoverflow.com/questions/451426/how-do-i-calculate-the-area-of-a-2d-polygon
auto num_pts = read_length();
assert(num_pts > 3);
double total = 0;
auto prev = read_point();
for (unsigned i = 1; i < num_pts; ++i) {
auto cur = read_point();
total += prev.x * cur.y - cur.x * prev.y;
prev = cur;
}
return std::abs(total) * 0.5;
}
template <typename T>
T read_data()
{
T data;
memcpy(&data, m_wkb + m_pos, sizeof(T));
m_pos += sizeof(T);
return data;
}
char const *m_wkb;
size_t m_pos;
};
} // namespace
#endif // OSM2PGSQL_WKB_HPP