forked from MaZderMind/osm-history-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supersoftercut.hpp
400 lines (338 loc) · 14.1 KB
/
supersoftercut.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#ifndef SPLITTER_SUPERSOFTERCUT_HPP
#define SPLITTER_SUPERSOFTERCUT_HPP
#include "cut.hpp"
#include "growing_bitset.hpp"
#include <map>
#include <tuple>
#include <typeinfo>
/*
SuperSoftercut Algorithm
- walk over all node-versions
- walk over all bboxes
- if the current node-version is inside the bbox
- record its id in the bboxes inside-node-tracker
- walk over all way-versions
- walk over all way-nodes
- Adds the nodes that aren't in node-tracker to a vector
- if node is in the box hit becames true
- if hit is true and the vector is not empty (it means their are nodes that belong to a way that has at least one node inside the box - complete ways)
- Records the id of node in vector to outside-node-tracker
- walk over all relations-versions
- walk over all relations-nodes
- Adds the nodes and ways that aren't in node-tracker to a vector
- if node or way is in the box hit becames true
- if hit is true and the vector is not empty (it means their are nodes or ways that belong to a relation that has at least one node or way inside the box)
- Records the id of node or way to outside-node-tracker or outside-way-tracker
Second Pass
- walk over all way-versions
- walk over all bboxes
- if the way-id is recorded in the bboxes outside-way-tracker
- send the all the nodes of the way to outside-node-tracker
Third Pass
- walk over all node-versions
- walk over all bboxes
- if the node-id is recorded in the bboxes node-trackers
- send the way to the bboxes writer
- walk over all way-versions
- walk over all bboxes
- if the way-id is recorded in the bboxes way-trackers
- send the way to the bboxes writer
- walk over all relation-versions
- walk over all bboxes
- if the relation-id is recorded in the bboxes relation-tracker
- send the relation to the bboxes writer
features:
- if an object is in the extract, all versions of it are there
- ways and relations are not changed
- ways are reference-complete
- all the ways and nodes of a relation that has at least one node or way inside the box are added
disadvantages
- three pass
- more memory than Softcut algoritm
- relations will have dead references (other relations)
*/
class SuperSoftercutExtractInfo : public ExtractInfo {
public:
growing_bitset inside_node_tracker; //nodes inside the box
growing_bitset outside_node_tracker; //nodes outside the box
growing_bitset inside_way_tracker; //ways inside the box
growing_bitset outside_way_tracker; //ways outside the box
growing_bitset relation_tracker; //relations
SuperSoftercutExtractInfo(const std::string& name, const osmium::io::File& file, const osmium::io::Header& header) :
ExtractInfo(name, file, header) {}
};
class SuperSoftercutInfo : public CutInfo<SuperSoftercutExtractInfo> {
public:
std::multimap<osmium::object_id_type, osmium::object_id_type> cascading_relations_tracker;
};
class SuperSoftercutPassOne : public Cut<SuperSoftercutInfo> {
bool frist_node = true;
bool frist_way = true;
bool frist_relaction = true;
public:
SuperSoftercutPassOne(SuperSoftercutInfo *info) : Cut<SuperSoftercutInfo>(info) {
std::cout << "Start SuperSoftercut:\n";
for (const auto& extract : info->extracts) {
std::cout << "\textract " << extract->name << "\n";
}
if (debug) {
std::cerr << "\n\n===supersoftercut first-pass===\n\n";
}
}
// - walk over all node-versions
// - walk over all bboxes
// - if the current node-version is inside the bbox
// - record its id in the bboxes inside_node_tracker
void node(const osmium::Node& node) {
if (frist_node){
if (debug) {
std::cerr << "\n==node first-pass==\n";
}
frist_node = false;
}
if (debug) {
std::cerr << "supersoftercut node " << node.id() << " v" << node.version() << "\n";
}
for (const auto& extract : info->extracts) {
if (extract->contains(node)) {
if (debug)
std::cerr << "node is in extract, recording in node_tracker\n";
if(!extract->inside_node_tracker.get(node.id())){
extract->inside_node_tracker.set(node.id());
}
}
}
}
// - walk over all way-versions
// - walk over all way-nodes
// - Adds the nodes that aren't in node-tracker to a vector
// - if node is in the box hit becames true
// - if hit is true and the vector is not empty (it means their are nodes that belong to a way that has at least one node inside the box - complete ways)
// - Records the id of node to outside_node_tracker
void way(const osmium::Way& way) {
if (frist_way){
if (debug) {
std::cerr << "\n==way first-pass==\n";
}
frist_way = false;
}
// detect a new way
bool hit = false;
if (debug) {
std::cerr << "supersoftercut way " << way.id() << " v" << way.version() << "\n";
}
std::set<osmium::object_id_type> way_nodes;
for (const auto& extract : info->extracts) {
way_nodes.clear();
hit = false;
for (const auto& node_ref : way.nodes()) {
if (extract->inside_node_tracker.get(node_ref.ref())) {
hit = true;
if (debug) {
std::cerr << "way has a node (" << node_ref.ref() << ") inside extract, recording in way_tracker\n";
}
}
else{
way_nodes.insert(node_ref.ref());
}
}
if (hit){
if(!extract->inside_way_tracker.get(way.id())){
extract->inside_way_tracker.set(way.id());
}
//Add only the nodes that were not yet in the the node-tracker if hit is true
if (!way_nodes.empty()){
for (const auto id : way_nodes) {
if(!extract->outside_node_tracker.get(id)){
extract->outside_node_tracker.set(id);
}
}
}
}
}
}
// - walk over all relations-versions
// - walk over all relations-nodes
// - Adds the nodes and ways that aren't in node-tracker to a vector
// - if node or way is in the box hit becames true
// - if hit is true and the vector is not empty (it means their are nodes or ways that belong to a relation that has at least one node or way inside the box)
// - Records the id of node or way to outside_node_tracker or outside_way_tracker
void relation(const osmium::Relation& relation) {
if (frist_relaction){
if (debug) {
std::cerr << "\n==relation first-pass==\n";
}
frist_relaction = false;
}
bool hit = false;
if (debug) {
std::cerr << "supersoftercut relation " << relation.id() << " v" << relation.version() << "\n";
}
std::vector<const osmium::RelationMember*> members;
members.reserve(relation.members().size());
for (const auto& extract : info->extracts) {
members.clear();
hit = false;
for (const auto& member : relation.members()) {
if ((member.type() == osmium::item_type::node && extract->inside_node_tracker.get(member.ref())) || (member.type() == osmium::item_type::way && extract->inside_way_tracker.get(member.ref()))) {
hit = true;
}
else if ((member.type() == osmium::item_type::node && !extract->inside_node_tracker.get(member.ref())) || (member.type() == osmium::item_type::way && !extract->inside_way_tracker.get(member.ref()))) {
members.push_back(&member);
}
}
if (hit){
if(!extract->relation_tracker.get(relation.id())){
extract->relation_tracker.set(relation.id());
}
//Add only the nodes and ways that were not yet in the respective trackers if hit is true
if (!members.empty()){
for (auto memptr : members) {
if (memptr->type() == osmium::item_type::node && !extract->outside_node_tracker.get(memptr->ref())){
extract->outside_node_tracker.set(memptr->ref());
}
else if (memptr->type() == osmium::item_type::way && !extract->outside_way_tracker.get(memptr->ref())){
extract->outside_way_tracker.set(memptr->ref());
}
}
}
}
}
}
}; // class SuperSoftercutPassOne
class SuperSoftercutPassTwo : public Cut<SuperSoftercutInfo> {
bool frist_way = true;
bool frist_relaction = true;
public:
SuperSoftercutPassTwo(SuperSoftercutInfo *info) : Cut<SuperSoftercutInfo>(info) {
if (debug) {
std::cerr << "\n\n===supersoftercut second-pass===\n\n";
}
}
// - walk over all way-versions
// - walk over all bboxes
// - if the way-id is recorded in the outside_way_tracker and node-id of the way is not in outside_node_tracker
// - send the node-id node tracker
void way(const osmium::Way& way) {
if (frist_way){
if (debug) {
std::cerr << "\n==way second-pass==\n";
}
frist_way = false;
}
if (debug) {
std::cerr << "supersoftercut way " << way.id() << " v" << way.version() << "\n";
}
for (const auto& extract : info->extracts) {
if (extract->outside_way_tracker.get(way.id())) {
for (const auto& node_ref : way.nodes()) {
if (!extract->outside_node_tracker.get(node_ref.ref())) {
extract->outside_node_tracker.set(node_ref.ref());
}
}
}
}
}
// - walk over all way-versions
// - walk over all bboxes
// - if the way-id is recorded in the outside_way_tracker and node-id of the way is not in outside_node_tracker
// - send the node-id node tracker
void relation(const osmium::Relation& relation) {
if (frist_relaction){
if (debug) {
std::cerr << "\n==relation second-pass==\n";
}
frist_relaction = false;
}
bool hit = false;
if (debug) {
std::cerr << "supersoftercut relation " << relation.id() << " v" << relation.version() << "\n";
}
for (const auto& extract : info->extracts) {
hit = false;
for (const auto& member : relation.members()) {
if (member.type() == osmium::item_type::relation) {
info->cascading_relations_tracker.insert(std::make_pair(member.ref(), relation.id()));
if (extract->relation_tracker.get(member.ref())){
hit = true;
break;
}
}
}
if (hit){
if(!extract->relation_tracker.get(relation.id())){
extract->relation_tracker.set(relation.id());
}
cascading_relations(extract, relation.id());
}
}
}
void cascading_relations(SuperSoftercutExtractInfo *extract, osmium::object_id_type id) {
auto r = info->cascading_relations_tracker.equal_range(id);
for (auto it = r.first; it != r.second; ++it) {
if (debug) std::cerr << "\tcascading: " << it->second << "\n";
if (extract->relation_tracker.get(it->second)) {
continue;
}
extract->relation_tracker.set(it->second);
cascading_relations(extract, it->second);
}
}
}; // class SuperSoftercutPassTwo
class SuperSoftercutPassThree : public Cut<SuperSoftercutInfo> {
public:
SuperSoftercutPassThree(SuperSoftercutInfo *info) : Cut<SuperSoftercutInfo>(info) {
if (debug) {
std::cerr << "\n\n===supersoftercut third-pass===\n\n";
}
}
// - walk over all node-versions
// - walk over all bboxes
// - if the node-id is recorded in the bboxes node-trackers
// - send the node to the bboxes writer
void node(const osmium::Node& node) {
if (debug) {
std::cerr << "supersoftercut node " << node.id() << " v" << node.version() << "\n";
}
for (const auto& extract : info->extracts) {
if (extract->inside_node_tracker.get(node.id())){
extract->write(node);
}
else if (extract->outside_node_tracker.get(node.id())) {
extract->write(node);
}
}
}
// - walk over all way-versions
// - walk over all bboxes
// - if the way-id is recorded in the bboxes way-trackers
// - send the way to the bboxes writer
void way(const osmium::Way& way) {
if (debug) {
std::cerr << "supersoftercut way " << way.id() << " v" << way.version() << "\n";
}
for (const auto& extract : info->extracts) {
if (extract->inside_way_tracker.get(way.id())){
extract->write(way);
}
else if (extract->outside_way_tracker.get(way.id())) {
extract->write(way);
}
}
}
// - walk over all relation-versions
// - walk over all bboxes
// - if the relation-id is recorded in the bboxes relation-tracker
// - send the relation to the bboxes writer
void relation(const osmium::Relation& relation) {
if (debug) {
std::cerr << "supersoftercut relation " << relation.id() << " v" << relation.version() << "\n";
}
for (const auto& extract : info->extracts) {
if (extract->relation_tracker.get(relation.id())){
extract->write(relation);
}
}
}
}; // class SuperSoftercutPassThree
#endif // SPLITTER_SUPERSOFTERCUT_HPP