-
Notifications
You must be signed in to change notification settings - Fork 1
/
pbf_encoding.hpp
268 lines (229 loc) · 8.61 KB
/
pbf_encoding.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
#pragma once
#include <protozero/pbf_writer.hpp>
#include <protozero/pbf_reader.hpp>
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include "rapidjson/document.h"
#include <osmium/osm/types.hpp>
namespace osmwayback {
/*
PBF Object Encoding
===================
These functions encode osmium objects into simplified PBF messages to store in rocksdb
Message Keys for Encoding:
1. Timestamp
2. Chnageset
3. version
4. User ID
5. User (String)
6. Visible (bool)
7. Deleted (bool)
10: Tags
*/
const std::string encode_node(const osmium::Node& node) {
std::string data;
protozero::pbf_writer encoder(data);
encoder.add_fixed64(1, static_cast<int>(node.timestamp().seconds_since_epoch()));
encoder.add_uint32(2, node.changeset());
encoder.add_uint32(3, node.version());
encoder.add_uint32(4, node.uid());
encoder.add_string(5, node.user());
encoder.add_bool(6, node.visible());
encoder.add_bool(7, node.deleted());
//Add node coordinates
//If deleted, will always throw invalid location exception
if( !node.deleted() ){
try{
encoder.add_double(8, node.location().lon());
encoder.add_double(9, node.location().lat());
} catch (const osmium::invalid_location& ex) {
//Catch invlid locations, not sure why this would happen... but it could
//std::cerr<< ex.what() << std::endl;
}
}
//Add the tags
const osmium::TagList& tags = node.tags();
for (const osmium::Tag& tag : tags) {
encoder.add_string(10, tag.key());
encoder.add_string(10, tag.value());
}
return data;
}
const std::string encode_way(const osmium::Way& way) {
std::string data;
protozero::pbf_writer encoder(data);
encoder.add_fixed64(1, static_cast<int>(way.timestamp().seconds_since_epoch()));
encoder.add_uint32(2, way.changeset());
encoder.add_uint32(3, way.version());
encoder.add_uint32(4, way.uid());
encoder.add_string(5, way.user());
encoder.add_bool(6, way.visible());
encoder.add_bool(7, way.deleted());
//Add node references: TODO: Optimize
std::vector<int64_t> noderefs;
for (const osmium::NodeRef& nr : way.nodes()) {
noderefs.push_back(nr.ref());
}
encoder.add_packed_int64(8, noderefs.begin(), noderefs.end());
//Add the tags
const osmium::TagList& tags = way.tags();
for (const osmium::Tag& tag : tags) {
encoder.add_string(10, tag.key());
encoder.add_string(10, tag.value());
}
return data;
}
/*
PBF Object Decoding
===================
These functions decode PBF messages from rocksdb INTO json objects
//To save space within GeoJSON objects, historical attribute names are shorted; these can be expanded later in a per-tile basis, the entire history object is String Encoded, so it's best to keep it short.
@timestamp = t;
@changeset = c;
@uid = u; (user ID)
@user = h; (handle)
@version = i; (iteration)
@visible = v;
@deleted = d;
tags = a; //attributes
aA = attributes added;
aM = attributes modified;
aD = attributes deleted;
//Object specific attributes:
coordinates = p //p for point (only point geoms here)
nodes = n;
*/
// Decode PBF_Node as JSON Object (in place (?) )
void decode_node(std::string data, rapidjson::Document* doc) {
protozero::pbf_reader message(data);
//Initialize the object (object is defined in add_tags)
doc->SetObject();
rapidjson::Document::AllocatorType& a = doc->GetAllocator();
std::string previous_key{};
rapidjson::Value object_tags(rapidjson::kObjectType);
rapidjson::Value coordinates(rapidjson::kArrayType);
bool deleted;
while (message.next()) {
switch (message.tag()) {
case 1:
doc->AddMember("t", message.get_fixed64(), a);
break;
case 2:
doc->AddMember("c", message.get_uint32(), a);
break;
case 3:
doc->AddMember("i", message.get_uint32(), a);
break;
case 4:
doc->AddMember("u", message.get_uint32(), a);
break;
case 5:
doc->AddMember("h", message.get_string(), a);
break;
case 6:
message.get_bool();
// doc->AddMember("v", message.get_bool(), a);
break;
case 7:
deleted = message.get_bool();
if (deleted){
doc->AddMember("d", deleted, a);
}
break;
case 8:
coordinates.PushBack(message.get_double(),a);
break;
case 9:
coordinates.PushBack(message.get_double(),a);
break;
case 10:
//Tags
if (previous_key.empty()) {
previous_key = message.get_string();
} else {
rapidjson::Value key(previous_key.c_str(), a);
rapidjson::Value value(message.get_string(), a);
object_tags.AddMember(key, value, a);
previous_key = "";
}
break;
default:
message.skip();
}
}
if (!coordinates.ObjectEmpty() ){
doc->AddMember("p",coordinates,a);
}
if ( !object_tags.ObjectEmpty() ){
doc->AddMember("a",object_tags, a);
}
}
// Decode PBF Way as JSON Object (in place (?) )
void decode_way(std::string data, rapidjson::Document* doc) {
protozero::pbf_reader message(data);
//Initialize the object (object is defined in add_tags)
doc->SetObject();
rapidjson::Document::AllocatorType& a = doc->GetAllocator();
rapidjson::Value noderefs(rapidjson::kArrayType);
std::string previous_key{};
rapidjson::Value object_tags(rapidjson::kObjectType);
protozero::iterator_range<protozero::pbf_reader::const_int64_iterator> nodeIDs;
bool deleted;
while (message.next()) {
switch (message.tag()) {
case 1:
doc->AddMember("t", message.get_fixed64(), a);
break;
case 2:
doc->AddMember("c", message.get_uint32(), a);
break;
case 3:
doc->AddMember("i", message.get_uint32(), a);
break;
case 4:
doc->AddMember("u", message.get_uint32(), a);
break;
case 5:
doc->AddMember("h", message.get_string(), a);
break;
case 6:
message.get_bool();
// doc->AddMember("v", message.get_bool(), a);
break;
case 7:
deleted = message.get_bool();
if (deleted){
doc->AddMember("d", deleted, a);
}
break;
case 8:
//Node refs
nodeIDs = message.get_packed_int64();
for (auto nr : nodeIDs) {
noderefs.PushBack(nr,a);
}
break;
case 10:
//Tags
if (previous_key.empty()) {
previous_key = message.get_string();
} else {
rapidjson::Value key(previous_key.c_str(), a);
rapidjson::Value value(message.get_string(), a);
object_tags.AddMember(key, value, a);
previous_key = "";
}
break;
default:
message.skip();
}
}
if ( !noderefs.ObjectEmpty() ){
doc->AddMember("n",noderefs, a);
}
if ( !object_tags.ObjectEmpty() ){
doc->AddMember("a",object_tags, a);
}
}
}