forked from MaZderMind/osm-history-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
softercut.hpp
338 lines (286 loc) · 12 KB
/
softercut.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
#ifndef SPLITTER_SOFTERCUT_HPP
#define SPLITTER_SOFTERCUT_HPP
#include "cut.hpp"
#include "growing_bitset.hpp"
#include <map>
#include <tuple>
#include <typeinfo>
/*
Softercut 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 SoftercutExtractInfo : 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
SoftercutExtractInfo(const std::string& name, const osmium::io::File& file, const osmium::io::Header& header) :
ExtractInfo(name, file, header) {}
};
class SoftercutInfo : public CutInfo<SoftercutExtractInfo> {
public:
std::multimap<osmium::object_id_type, osmium::object_id_type> cascading_relations_tracker;
};
class SoftercutPassOne : public Cut<SoftercutInfo> {
bool frist_node = true;
bool frist_way = true;
bool frist_relaction = true;
public:
SoftercutPassOne(SoftercutInfo *info) : Cut<SoftercutInfo>(info) {
std::cout << "Start Softercut:\n";
for (const auto& extract : info->extracts) {
std::cout << "\textract " << extract->name << "\n";
}
std::cout << "\n\n===softercut 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){
std::cout << "\n==node first-pass==\n";
frist_node = false;
}
if (debug) {
std::cerr << "softercut 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){
std::cout << "\n==way first-pass==\n";
frist_way = false;
}
// detect a new way
bool hit = false;
if (debug) {
std::cerr << "softercut 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){
std::cout << "\n==relation first-pass==\n";
frist_relaction = false;
}
bool hit = false;
if (debug) {
std::cerr << "softercut 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 SoftercutPassOne
class SoftercutPassTwo : public Cut<SoftercutInfo> {
bool frist_way = true;
public:
SoftercutPassTwo(SoftercutInfo *info) : Cut<SoftercutInfo>(info) {
if (debug) {
std::cerr << "softercut second-pass init\n";
}
std::cout << "\n\n===softercut 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){
std::cout << "\n==way second-pass==\n";
frist_way = false;
}
if (debug) {
std::cerr << "softercut 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());
}
}
}
}
}
}; // class SoftercutPassTwo
class SoftercutPassThree : public Cut<SoftercutInfo> {
public:
SoftercutPassThree(SoftercutInfo *info) : Cut<SoftercutInfo>(info) {
if (debug) {
std::cerr << "softercut third-pass init\n";
}
std::cout << "\n\n===softercut 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 << "softercut 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 << "softercut 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 << "softercut relation " << relation.id() << " v" << relation.version() << "\n";
}
for (const auto& extract : info->extracts) {
if (extract->relation_tracker.get(relation.id())){
extract->write(relation);
}
}
}
}; // class SoftercutPassThree
#endif // SPLITTER_SOFTERCUT_HPP