-
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathstrategy.hpp
More file actions
182 lines (133 loc) · 4.83 KB
/
strategy.hpp
File metadata and controls
182 lines (133 loc) · 4.83 KB
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
#ifndef EXTRACT_STRATEGY_HPP
#define EXTRACT_STRATEGY_HPP
/*
Osmium -- OpenStreetMap data manipulation command line tool
https://osmcode.org/osmium-tool/
Copyright (C) 2013-2026 Jochen Topf <jochen@topf.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "extract.hpp"
#include <osmium/io/file.hpp>
#include <osmium/io/reader.hpp>
#include <osmium/io/writer.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/memory/item.hpp>
#include <osmium/osm/node.hpp>
#include <osmium/osm/object.hpp>
#include <osmium/osm/relation.hpp>
#include <osmium/osm/types.hpp>
#include <osmium/osm/way.hpp>
#include <osmium/util/progress_bar.hpp>
#include <osmium/util/verbose_output.hpp>
#include <cassert>
#include <memory>
template <typename T>
class ExtractData : public T {
Extract* m_extract_ptr;
public:
explicit ExtractData(Extract& extract) :
T(),
m_extract_ptr(&extract) {
}
bool contains(const osmium::Location& location) const noexcept {
return m_extract_ptr->contains(location);
}
void write(const osmium::memory::Item& item) {
m_extract_ptr->write(item);
}
void close() {
m_extract_ptr->close_file();
}
}; // class ExtractData
class ExtractStrategy {
public:
ExtractStrategy() = default;
virtual ~ExtractStrategy() = default;
virtual const char* name() const noexcept = 0;
virtual void show_arguments(osmium::VerboseOutput& /*vout*/) {
}
virtual void run(osmium::VerboseOutput& vout, bool display_progress, const osmium::io::File& input_file) = 0;
}; // class ExtractStrategy
template <typename TStrategy, typename TChild>
class Pass {
TStrategy* m_strategy;
void run_impl(osmium::ProgressBar& progress_bar, osmium::io::Reader& reader) {
while (osmium::memory::Buffer buffer = reader.read()) {
progress_bar.update(reader.offset());
for (const auto& object : buffer) {
switch (object.type()) {
case osmium::item_type::node:
self().node(static_cast<const osmium::Node&>(object));
for (auto& e : extracts()) {
self().enode(&e, static_cast<const osmium::Node&>(object));
}
break;
case osmium::item_type::way:
self().way(static_cast<const osmium::Way&>(object));
self().eway_all(extracts(), static_cast<const osmium::Way&>(object));
break;
case osmium::item_type::relation:
self().relation(static_cast<const osmium::Relation&>(object));
for (auto& e : extracts()) {
self().erelation(&e, static_cast<const osmium::Relation&>(object));
}
break;
default:
break;
}
}
}
}
protected:
using extract_data = typename TStrategy::extract_data;
TStrategy& strategy() {
return *m_strategy;
}
std::vector<extract_data>& extracts() {
return m_strategy->m_extracts;
}
TChild& self() {
return *static_cast<TChild*>(this);
}
void node(const osmium::Node&) {
}
void way(const osmium::Way&) {
}
void relation(const osmium::Relation&) {
}
void enode(extract_data*, const osmium::Node&) {
}
void eway(extract_data*, const osmium::Way&) {
}
void erelation(extract_data*, const osmium::Relation&) {
}
// Default implementation: call eway() for each extract separately.
// Subclasses may override this to process all extracts in a single
// pass over way.nodes().
void eway_all(std::vector<extract_data>& exts, const osmium::Way& way) {
for (auto& e : exts) {
self().eway(&e, way);
}
}
public:
explicit Pass(TStrategy* strategy) :
m_strategy(strategy) {
assert(strategy);
}
template <typename... Args>
void run(osmium::ProgressBar& progress_bar, Args... args) {
osmium::io::Reader reader{std::forward<Args>(args)...};
run_impl(progress_bar, reader);
reader.close();
}
}; // class Pass
#endif // EXTRACT_STRATEGY_HPP