-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathosmchange_responder.cpp
255 lines (196 loc) · 6.6 KB
/
osmchange_responder.cpp
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
/**
* SPDX-License-Identifier: GPL-2.0-only
*
* This file is part of openstreetmap-cgimap (https://github.com/zerebubuth/openstreetmap-cgimap/).
*
* Copyright (C) 2009-2023 by the CGImap developer community.
* For a full list of authors see the git log.
*/
#include "cgimap/logger.hpp"
#include "cgimap/osmchange_responder.hpp"
#include <chrono>
#include <fmt/core.h>
namespace {
struct element {
struct lonlat { double m_lon, m_lat; };
element_type m_type;
element_info m_info;
tags_t m_tags;
// only one of these will be non-empty depending on m_type
lonlat m_lonlat;
nodes_t m_nds;
members_t m_members;
};
bool operator<(const element &a, const element &b) {
// length of YYYY-MM-DDTHH:MM:SSZ is 20, and strings should always be that
// long.
assert(a.m_info.timestamp.size() == 20);
assert(b.m_info.timestamp.size() == 20);
auto cmp = strncmp(
a.m_info.timestamp.c_str(), b.m_info.timestamp.c_str(), 20);
if (cmp < 0) {
return true;
} else if (cmp > 0) {
return false;
} else if (a.m_info.version < b.m_info.version) {
return true;
} else if (a.m_info.version > b.m_info.version) {
return false;
} else if (a.m_type < b.m_type) {
return true;
} else if (a.m_type > b.m_type) {
return false;
} else {
return a.m_info.id < b.m_info.id;
}
}
struct sorting_formatter : public output_formatter {
virtual ~sorting_formatter() override = default;
mime::type mime_type() const override {
throw std::runtime_error("sorting_formatter::mime_type unimplemented");
}
void start_document(
const std::string &,
const std::string &) override {
throw std::runtime_error("sorting_formatter::start_document unimplemented");
}
void end_document() override {
throw std::runtime_error("sorting_formatter::end_document unimplemented");
}
void error(const std::exception &) override {
throw std::runtime_error("sorting_formatter::error unimplemented");
}
void write_bounds(const bbox &) override {
throw std::runtime_error("sorting_formatter::write_bounds unimplemented");
}
void start_element_type(element_type) override {
throw std::runtime_error("sorting_formatter::start_element_type unimplemented");
}
void end_element_type(element_type) override {
throw std::runtime_error("sorting_formatter::end_element_type unimplemented");
}
void write_node(
const element_info &elem,
double lon, double lat,
const tags_t &tags) override {
element node{
element_type::node, elem, tags, element::lonlat{lon, lat},
nodes_t(), members_t()};
m_elements.emplace_back(std::move(node));
}
void write_way(
const element_info &elem,
const nodes_t &nodes,
const tags_t &tags) override {
element way{
element_type::way, elem, tags, element::lonlat{},
nodes, members_t()};
m_elements.emplace_back(std::move(way));
}
void write_relation(
const element_info &elem,
const members_t &members,
const tags_t &tags) override {
element rel{
element_type::relation, elem, tags, element::lonlat{},
nodes_t(), members};
m_elements.emplace_back(std::move(rel));
}
void write_changeset(
const changeset_info &,
const tags_t &,
bool,
const comments_t &,
const std::chrono::system_clock::time_point &) override {
throw std::runtime_error("sorting_formatter::write_changeset unimplemented");
}
void write_diffresult_create_modify(const element_type,
const osm_nwr_signed_id_t,
const osm_nwr_id_t,
const osm_version_t) override {
throw std::runtime_error("sorting_formatter::write_diffresult_create_modify unimplemented");
}
void write_diffresult_delete(const element_type,
const osm_nwr_signed_id_t) override {
throw std::runtime_error("sorting_formatter::write_diffresult_delete unimplemented");
}
void flush() override {
throw std::runtime_error("sorting_formatter::flush unimplemented");
}
// write an error to the output stream
void error(const std::string &) override {
throw std::runtime_error("sorting_formatter::error unimplemented");
}
void start_action(action_type) override {
// this shouldn't be called here, as the things which call this don't have
// actions - they're added by this code.
throw std::runtime_error("Unexpected call to start_action.");
}
void end_action(action_type) override {
// this shouldn't be called here, as the things which call this don't have
// actions - they're added by this code.
throw std::runtime_error("Unexpected call to end_action.");
}
void write(output_formatter &fmt) {
std::sort(m_elements.begin(), m_elements.end());
for (const auto &e : m_elements) {
if (e.m_info.version == 1) {
fmt.start_action(action_type::create);
write_element(e, fmt);
fmt.end_action(action_type::create);
} else if (e.m_info.visible) {
fmt.start_action(action_type::modify);
write_element(e, fmt);
fmt.end_action(action_type::modify);
} else {
fmt.start_action(action_type::del);
write_element(e, fmt);
fmt.end_action(action_type::del);
}
}
}
private:
std::vector<element> m_elements;
void write_element(const element &e, output_formatter &fmt) {
switch (e.m_type) {
case element_type::node:
fmt.write_node(e.m_info, e.m_lonlat.m_lon, e.m_lonlat.m_lat, e.m_tags);
break;
case element_type::way:
fmt.write_way(e.m_info, e.m_nds, e.m_tags);
break;
case element_type::relation:
fmt.write_relation(e.m_info, e.m_members, e.m_tags);
break;
case element_type::changeset:
break;
}
}
};
} // anonymous namespace
osmchange_responder::osmchange_responder(mime::type mt, data_selection &s)
: osm_responder(mt, {}),
sel(s) {
}
std::vector<mime::type> osmchange_responder::types_available() const {
std::vector<mime::type> types; // TODO: don't reconstruct on every call
types.push_back(mime::type::application_xml);
return types;
}
void osmchange_responder::write(output_formatter& fmt,
const std::string &generator,
const std::chrono::system_clock::time_point &now) {
fmt.start_document(generator, "osmChange");
try {
sorting_formatter sorter;
sel.write_nodes(sorter);
sel.write_ways(sorter);
sel.write_relations(sorter);
sorter.write(fmt);
} catch (const std::exception &e) {
logger::message(fmt::format("Caught error in osmchange_responder: {}",
e.what()));
fmt.error(e);
}
fmt.end_document();
}