Repository navigation
Expand file tree
/
Copy pathgold_dataset.cpp
More file actions
564 lines (514 loc) · 20.9 KB
/
Copy pathgold_dataset.cpp
File metadata and controls
564 lines (514 loc) · 20.9 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/**
* Gold dataset reference test for the GeoTrace C++ SDK.
*
* Reads the CSV fixtures in tests/fixtures/gold_dataset/, builds a .gtd file,
* then verifies the round-trip. Run from the repository root:
*
* ./sdk/cpp/build/gold/examples/gold_dataset
*/
#include "parse_number.hpp"
#include <geotrace/geotrace.hpp>
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace fs = std::filesystem;
namespace {
std::string rtrim(std::string text) {
while (!text.empty() && (text.back() == '\r' || text.back() == '\n' || text.back() == ' ')) {
text.pop_back();
}
return text;
}
std::vector<std::string> split(const std::string &line, char delim) {
std::vector<std::string> cols;
std::istringstream stream(line);
std::string col;
while (std::getline(stream, col, delim)) {
cols.push_back(std::move(col));
}
// std::getline drops the trailing empty field for lines that end with the delimiter.
if (!line.empty() && line.back() == delim) {
cols.emplace_back();
}
return cols;
}
std::vector<std::string> split_csv(const std::string &line) {
return split(line, ',');
}
// The first N comma-separated fields of `line`, or `std::nullopt` when it holds fewer.
template <std::size_t N>
std::optional<std::array<std::string, N>> split_csv_fields(const std::string &line) {
auto cols = split_csv(line);
if (cols.size() < N) {
return std::nullopt;
}
std::array<std::string, N> fields;
std::move(cols.begin(), cols.begin() + static_cast<std::ptrdiff_t>(N), fields.begin());
return fields;
}
// Every timestamp cell of the gold CSV fixtures is an ISO 8601 timestamp or empty.
std::optional<geotrace::Timestamp> parse_timestamp_or_absent(const std::string &text) {
const geotrace::Result<geotrace::Timestamp> parsed =
geotrace::Timestamp::try_from_iso8601(text);
if (const geotrace::Timestamp *timestamp = parsed.get_if()) {
return *timestamp;
}
return std::nullopt;
}
double required_double(const std::string &text) {
const auto value = examples::parse_decimal<double>(text);
if (!value) {
throw geotrace::IoError("not a number: " + text);
}
return *value;
}
std::optional<double> parse_opt_double(const std::string &text) {
if (text.empty()) {
return std::nullopt;
}
return required_double(text);
}
std::uint32_t required_prn(const std::string &text) {
const auto prn = examples::parse_decimal<std::uint32_t>(text);
if (!prn) {
throw geotrace::IoError("satellites.csv: invalid PRN: " + text);
}
return *prn;
}
std::optional<float> parse_opt_float(const std::string &text) {
const auto value = parse_opt_double(text);
return value ? std::optional<float>{static_cast<float>(*value)} : std::nullopt;
}
struct SatRow {
std::string gps_time;
std::string sys_time;
geotrace::Satellite sat;
};
geotrace::FixTime required_fix_time(const geotrace::RecordedFixTimestamps &recorded,
const std::string &source) {
const auto time = geotrace::FixTime::from_recorded(recorded);
if (!time) {
throw geotrace::IoError(source + " has no timestamp");
}
return *time;
}
std::ifstream open_csv(const fs::path &base, const std::string &name) {
auto path = base / name;
std::ifstream file(path);
if (!file.is_open()) {
throw geotrace::IoError("cannot open: " + path.string());
}
return file;
}
void load_meta(geotrace::FileBuilder &builder, const fs::path &base) {
auto file = open_csv(base, "meta.csv");
std::string line;
std::getline(file, line); // skip header
if (!std::getline(file, line)) {
throw geotrace::IoError("meta.csv: missing data row");
}
auto fields = split_csv_fields<5>(rtrim(std::move(line)));
if (!fields) {
throw geotrace::IoError("meta.csv: need 5 columns");
}
const auto &[title, device, notes, identity, travel_mode_name] = *fields;
auto travel_mode = geotrace::travel_mode_from_name(travel_mode_name);
if (!travel_mode) {
throw geotrace::IoError("meta.csv: unknown travel mode: " + travel_mode_name);
}
builder.title(title).device(device).notes(notes).identity(identity).travel_mode(*travel_mode);
}
void load_event_styles(geotrace::FileBuilder &builder, const fs::path &base) {
auto file = open_csv(base, "event_styles.csv");
std::string line;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
line = rtrim(std::move(line));
if (line.empty()) {
continue;
}
auto fields = split_csv_fields<3>(line);
if (!fields) {
continue;
}
const auto &[variant_path, icon, color] = *fields;
// An empty icon cell leaves the icon to the application.
const std::optional<geotrace::MarkerIcon> parsed =
icon.empty() ? std::nullopt : std::optional{geotrace::marker_icon_from_name(icon)};
builder.add_event_marker_style(geotrace::EventMarkerStyle{variant_path, parsed, color});
}
}
std::vector<SatRow> load_satellites(const fs::path &base) {
auto file = open_csv(base, "satellites.csv");
std::string line;
std::getline(file, line); // skip header
std::vector<SatRow> rows;
while (std::getline(file, line)) {
line = rtrim(std::move(line));
if (line.empty()) {
continue;
}
auto fields = split_csv_fields<8>(line);
if (!fields) {
continue;
}
const auto &[gps_time, sys_time, constellation, prn, in_fix, elevation, azimuth, snr] =
*fields;
rows.push_back(SatRow{
gps_time,
sys_time,
geotrace::Satellite{
geotrace::constellation_from_name(constellation),
required_prn(prn),
in_fix == "true",
parse_opt_float(elevation),
parse_opt_float(azimuth),
parse_opt_float(snr),
},
});
}
return rows;
}
void load_fixes(geotrace::FileBuilder &builder, const fs::path &base,
const std::vector<SatRow> &sats) {
auto file = open_csv(base, "fixes.csv");
std::set<std::pair<std::string, std::string>> fix_times;
std::string line;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
line = rtrim(std::move(line));
if (line.empty()) {
continue;
}
auto fields = split_csv_fields<9>(line);
if (!fields) {
continue;
}
[[maybe_unused]] const auto &[track_id, gps_time, sys_time, lat, lon, heading_deg,
speed_kmh, eph_m, speed_knots] = *fields;
geotrace::RecordedFixTimestamps recorded{};
recorded.gps_time = parse_timestamp_or_absent(gps_time);
recorded.sys_time = parse_timestamp_or_absent(sys_time);
const auto time = required_fix_time(recorded, "fixes.csv row " + line);
auto hdg = parse_opt_double(heading_deg);
auto kmh = parse_opt_double(speed_kmh);
auto knots = parse_opt_double(speed_knots);
if (kmh && knots) {
throw geotrace::IoError("fixes.csv row " + line +
" has a speed in both speed_kmh and speed_knots");
}
std::optional<geotrace::Velocity> speed;
if (kmh) {
speed = geotrace::Velocity::kmh(*kmh);
} else if (knots) {
speed = geotrace::Velocity::knots(*knots);
}
builder.add(geotrace::NavFix{
time,
geotrace::Angle::degrees(required_double(lat)),
geotrace::Angle::degrees(required_double(lon)),
hdg ? std::optional{geotrace::Angle::degrees(*hdg)} : std::nullopt,
speed,
parse_opt_double(eph_m),
});
std::vector<geotrace::Satellite> tracked;
for (const auto &row : sats) {
if (row.gps_time == gps_time && row.sys_time == sys_time) {
tracked.push_back(row.sat);
}
}
if (!tracked.empty()) {
builder.add(geotrace::SatelliteReport{time, std::move(tracked)});
}
fix_times.emplace(gps_time, sys_time);
}
// Reports at a time no fix row holds. The builder gives each one a ghost fix.
std::map<std::pair<std::string, std::string>, std::vector<geotrace::Satellite>> orphans;
for (const auto &row : sats) {
if (fix_times.find({row.gps_time, row.sys_time}) == fix_times.end()) {
orphans[{row.gps_time, row.sys_time}].push_back(row.sat);
}
}
for (const auto &[times, tracked] : orphans) {
geotrace::RecordedFixTimestamps recorded{};
recorded.gps_time = parse_timestamp_or_absent(times.first);
recorded.sys_time = parse_timestamp_or_absent(times.second);
const auto time = required_fix_time(recorded, "satellites.csv row (" + times.first + ", " +
times.second + ")");
builder.add(geotrace::SatelliteReport{time, tracked});
}
}
void load_markers(geotrace::FileBuilder &builder, const fs::path &base) {
auto file = open_csv(base, "markers.csv");
std::string line;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
line = rtrim(std::move(line));
if (line.empty()) {
continue;
}
auto fields = split_csv_fields<3>(line);
if (!fields) {
continue;
}
const auto &[time, label, icon] = *fields;
auto timestamp = parse_timestamp_or_absent(time);
if (!timestamp) {
throw geotrace::IoError("markers.csv: missing timestamp");
}
builder.add(geotrace::Annotation{*timestamp, label, geotrace::marker_icon_from_name(icon)});
}
}
void load_events(geotrace::FileBuilder &builder, const fs::path &base) {
auto file = open_csv(base, "events.csv");
std::string line;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
line = rtrim(std::move(line));
if (line.empty()) {
continue;
}
auto fields = split_csv_fields<3>(line);
if (!fields) {
continue;
}
const auto &[sys_time, variant_path, annotation] = *fields;
auto timestamp = parse_timestamp_or_absent(sys_time);
if (!timestamp) {
throw geotrace::IoError("events.csv: missing sys_time");
}
builder.add(geotrace::EventMarker{variant_path, *timestamp, annotation});
}
}
// Returns the channel named in the row. Appends a new channel from the row's
// unit, period, description and component columns when no earlier row named it.
geotrace::Channel &channel_for_row(std::vector<geotrace::Channel> &channels,
const std::array<std::string, 7> &fields) {
const auto &[name, unit, period_deg, description, components, time, values] = fields;
for (auto &existing : channels) {
if (existing.name == name) {
return existing;
}
}
geotrace::Channel channel;
channel.name = name;
if (!unit.empty()) {
channel.unit = geotrace::ChannelUnit::parse_recognized(unit);
}
if (!period_deg.empty()) {
channel.period = geotrace::Angle::degrees(required_double(period_deg));
}
channel.description = description;
if (!components.empty()) {
channel.components = split(components, ';');
}
channels.push_back(std::move(channel));
return channels.back();
}
void load_channels(geotrace::FileBuilder &builder, const fs::path &base) {
auto file = open_csv(base, "channels.csv");
std::string line;
std::getline(file, line); // skip header
// One Channel per name, built up across its rows (each row is one sample).
std::vector<geotrace::Channel> channels;
while (std::getline(file, line)) {
line = rtrim(std::move(line));
if (line.empty()) {
continue;
}
auto fields = split_csv_fields<7>(line);
if (!fields) {
continue;
}
[[maybe_unused]] const auto &[name, unit, period_deg, description, components, time,
values] = *fields;
geotrace::Channel &channel = channel_for_row(channels, *fields);
auto timestamp = parse_timestamp_or_absent(time);
if (!timestamp) {
throw geotrace::IoError("channels.csv: invalid timestamp");
}
channel.times.push_back(*timestamp);
for (const auto &value : split(values, ';')) {
channel.values.push_back(required_double(value));
}
}
for (const auto &channel : channels) {
builder.add_channel(channel);
}
}
void verify_counts(const geotrace::NavFile &file) {
auto check = [](bool cond, const char *msg) {
if (!cond) {
throw std::runtime_error(std::string("FAIL: ") + msg);
}
};
auto contains = [](std::optional<std::string_view> value, std::string_view part) {
return value && value->find(part) != std::string_view::npos;
};
check(contains(file.title(), "Gold Dataset"), "title missing");
check(contains(file.device(), "Synthetic Generator"), "device missing");
check(contains(file.notes(), "cross-SDK"), "notes missing");
check(file.identity() == "gold-standard-v2", "identity wrong");
check(file.travel_mode() == "bicycle", "travel mode wrong");
auto nav_points = file.nav_point_count();
if (nav_points != 209) {
throw std::runtime_error("expected 209 nav points, got " + std::to_string(nav_points));
}
std::size_t anti = 0;
for (std::size_t i = 0; i < nav_points; i++) {
auto point = file.nav_point(i);
if (point.lon.as_degrees() > 179.9 || point.lon.as_degrees() < -179.9) {
anti++;
}
}
if (anti != 10) {
throw std::runtime_error("expected 10 antimeridian pts, got " + std::to_string(anti));
}
auto event_markers = file.event_marker_count();
if (event_markers != 7) {
throw std::runtime_error("expected 7 event markers, got " + std::to_string(event_markers));
}
auto channel_count = file.channel_count();
if (channel_count != 2) {
throw std::runtime_error("expected 2 channels, got " + std::to_string(channel_count));
}
for (std::size_t i = 0; i < channel_count; i++) {
auto channel = file.channel(i);
if (channel.name == "accel") {
check(channel.is_vector() &&
channel.components == std::vector<std::string>{"x", "y", "z"},
"accel components wrong");
}
if (channel.name == "heading_raw") {
check(channel.period.has_value() && channel.period->as_degrees() == 360.0,
"heading_raw period wrong");
}
}
}
void write_navigation_evidence(const fs::path &base) {
using geotrace::Angle;
using geotrace::Constellation;
using geotrace::FileBuilder;
using geotrace::FixTime;
using geotrace::NavFix;
using geotrace::PositionProvenance;
using geotrace::SatelliteCoverage;
using geotrace::SatelliteMembership;
using geotrace::SatelliteObservationReport;
using geotrace::SatelliteScope;
using geotrace::SatelliteUsageReport;
using geotrace::SolutionDimension;
using geotrace::SolutionMethod;
using geotrace::SolutionState;
using geotrace::Timestamp;
FileBuilder builder;
const std::array<double, 4> latitudes{55.0, 55.5, 55.1, 55.2};
const std::array<std::optional<SolutionState>, 4> states{
SolutionState::Fix, std::nullopt, SolutionState::Unknown, SolutionState::NoFix};
const std::array<std::optional<std::uint32_t>, 4> counts{8, std::nullopt, 0, 12};
const std::array<std::optional<PositionProvenance>, 4> origins{
PositionProvenance::SourceProvided, std::nullopt, PositionProvenance::SourceProvided,
PositionProvenance::SdkSynthesized};
for (std::size_t i = 0; i < latitudes.size(); ++i) {
NavFix fix{
FixTime::both(Timestamp::from_seconds(1735689600 + static_cast<std::int64_t>(i)),
Timestamp::from_seconds(1735689610 + static_cast<std::int64_t>(i))),
Angle::degrees(latitudes.at(i)), Angle::degrees(12.0 + (static_cast<double>(i) * 0.1))};
fix.state = states.at(i);
fix.used_count = counts.at(i);
fix.hdop = 0.9;
fix.vdop = 1.2;
fix.pdop = 1.5;
fix.method = SolutionMethod::RtkFixed;
fix.dimension = SolutionDimension::ThreeD;
fix.position_provenance = origins.at(i);
builder.add(fix);
}
const auto receiver = [](const char *time) {
return FixTime::receiver(Timestamp::from_iso8601(time));
};
const auto host = [](const char *time) { return FixTime::host(Timestamp::from_iso8601(time)); };
builder.add(SatelliteUsageReport{receiver("2025-01-01T00:00:00.4Z"),
SatelliteScope::global(),
SatelliteCoverage::Partial,
8,
{{{Constellation::Gps, 1}, SatelliteMembership::Unused},
{{Constellation::Gps, 1}, SatelliteMembership::Used},
{{Constellation::Gps, 2}, SatelliteMembership::Unknown}}});
builder.add(SatelliteObservationReport{host("2025-01-01T00:00:10.4Z"),
SatelliteScope::global(),
SatelliteCoverage::Partial,
std::nullopt,
{{{Constellation::Gps, 1}, std::nullopt, 90.0F, 0.0F}}});
const std::array<Constellation, 1> gps{Constellation::Gps};
builder.add(SatelliteObservationReport{receiver("2025-01-01T00:00:02.4Z"),
SatelliteScope::constellations(gps),
SatelliteCoverage::Complete, 0});
builder.add(SatelliteUsageReport{receiver("2025-01-01T00:00:04.5Z"), SatelliteScope::global(),
SatelliteCoverage::Complete, 0});
builder.add(SatelliteObservationReport{host("2025-01-01T00:00:14.5Z"), SatelliteScope::global(),
SatelliteCoverage::Partial});
const std::array<Constellation, 6> all{Constellation::Gps, Constellation::Glonass,
Constellation::Galileo, Constellation::Beidou,
Constellation::Navic, Constellation::Qzss};
builder.add(SatelliteUsageReport{receiver("2025-01-01T00:00:03Z"),
SatelliteScope::constellations(all),
SatelliteCoverage::Partial, UINT32_MAX});
builder.finish().write_to_file(base / "navigation_evidence_cpp.gtd");
FileBuilder polling;
for (const std::int64_t second : {0, 10}) {
NavFix fix{FixTime::host(Timestamp::from_seconds(1735689600 + second)), Angle::degrees(0),
Angle::degrees(static_cast<double>(second))};
fix.state = SolutionState::Fix;
fix.used_count = 8;
fix.hdop = 0.9;
polling.add(fix);
}
polling.add(SatelliteUsageReport{host("2025-01-01T00:00:02Z"), SatelliteScope::global(),
SatelliteCoverage::Partial, 8});
polling.add(SatelliteObservationReport{host("2025-01-01T00:00:02Z"), SatelliteScope::global(),
SatelliteCoverage::Partial, 12});
polling.finish().write_to_file(base / "host_clock_cpp.gtd");
}
} // namespace
int main(int argc, char **argv) {
try {
const std::vector<std::string> args(argv, std::next(argv, argc));
const fs::path base = args.size() >= 2 ? args.at(1) : "tests/fixtures/gold_dataset";
const fs::path out = base / "gold_cpp.gtd";
geotrace::FileBuilder builder{};
builder.lenient();
load_meta(builder, base);
load_event_styles(builder, base);
auto sats = load_satellites(base);
load_fixes(builder, base, sats);
load_markers(builder, base);
load_events(builder, base);
load_channels(builder, base);
auto nav = builder.finish();
nav.write_to_file(out);
verify_counts(nav);
write_navigation_evidence(base);
std::cout << "Written: " << out << "\n";
std::cout << "Gold dataset verified. Nav points: 209, Event markers: 7, Channels: 2\n";
} catch (const std::exception &e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
}