-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtfs.cpp
110 lines (82 loc) · 2.88 KB
/
gtfs.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
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include "gtfs-realtime.pb.h"
#include <google/protobuf/util/json_util.h>
#include <gtfs/gtfsrealtime/FeedMessage.h>
#include <zserio/DebugStringUtil.h>
#include <zserio/FileUtil.h>
#include <zserio/SerializeUtil.h>
using namespace std;
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc < 2) {
cerr << "Usage: " << argv[0] << " <create|convert|decodepb|decodezs> FILE" << endl;
return -1;
}
if(std::string(argv[1]) == "create") {
cout << "creating..." << endl;
transit_realtime::FeedMessage feedMsg;
// Read the existing GTFS file.
fstream input(argv[2], ios::in | ios::binary);
if (!feedMsg.ParseFromIstream(&input)) {
cerr << "Failed to parse feed message." << endl;
return -1;
}
std::string json;
google::protobuf::util::MessageToJsonString(feedMsg, &json);
// Write the json string to a file which we then need to transform using the python script
ofstream outfile ("input.json");
outfile << json;
outfile.close();
}
else if(std::string(argv[1]) == "convert")
{
cout << "converting..." << endl;
// Open the file
ifstream infile("output.json");
// Check if the file is open
if(!infile.is_open()) {
cerr << "Error opening file" << endl;
return -1;
}
// Create a string to store the json
std::string json;
// Read the contents of the file into the string
getline(infile, json, '\0');
// Close the file
infile.close();
gtfs::gtfsrealtime::FeedMessage zs_feed =
zserio::fromJsonString<gtfs::gtfsrealtime::FeedMessage>(json);
zserio::serializeToFile(zs_feed, "gtfs.zsbin");
}
else if(std::string(argv[1]) == "decodepb")
{
using clock = std::chrono::steady_clock;
std::chrono::time_point<clock> start_tp_;
start_tp_ = clock::now();
transit_realtime::FeedMessage feedMsg;
// Read the existing GTFS file.
fstream input(argv[2], ios::in | ios::binary);
if (!feedMsg.ParseFromIstream(&input)) {
cerr << "Failed to parse feed message." << endl;
return -1;
}
auto duration = std::chrono::duration<double>(clock::now() - start_tp_);
cout << "PB decode duration: " << duration.count() << endl;
}
else if(std::string(argv[1]) == "decodezs")
{
using clock = std::chrono::steady_clock;
std::chrono::time_point<clock> start_tp_;
start_tp_ = clock::now();
gtfs::gtfsrealtime::FeedMessage zs_feed =
zserio::deserializeFromFile<gtfs::gtfsrealtime::FeedMessage>(std::string(argv[2]));
auto duration = std::chrono::duration<double>(clock::now() - start_tp_);
cout << "ZS decode duration: " << duration.count() << endl;
}
return 0;
}