-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusstops.cpp
201 lines (167 loc) · 5.54 KB
/
busstops.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
/*busstops.cpp*/
//
// A collection of bus stops on the NU Campus map
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <utility>
#include <curl/curl.h>
#include <stdexcept>
#include "json.hpp"
#include "curl_util.h"
#include "busstops.h"
#include "busstop.h"
#include "dist.h"
using json = nlohmann::json;
bool compareBusStops(BusStop& busStop1, BusStop& busStop2) {
int id1;
int id2;
id1 = busStop1.getStopID();
id2 = busStop2.getStopID();
return (id1 < id2);
}
void BusStops::readMapBusStops(string& filename)
{
//
// open the csv file
//
ifstream input;
input.open(filename);
if (!input.is_open()) {
cout << "**failed to open file**" << endl;
return;
}
while (!input.eof()) {
//
// read one line from input file
//
string line;
getline(input, line);
if (input.fail()) {
break; // end of file
}
stringstream parser(line); // set up to parse the line
string stopID, busRoute, stopName, direction, locationDesc, lat, lon;
getline(parser, stopID, ',');
getline(parser, busRoute, ',');
getline(parser, stopName, ',');
getline(parser, direction, ',');
getline(parser, locationDesc, ',');
getline(parser, lat, ',');
getline(parser, lon);
pair<double,double> position = make_pair(stod(lat), stod(lon));
//
// construct current busStop and put in vector
//
BusStop curr(stoi(stopID), stoi(busRoute), stopName, direction, locationDesc, position);
this->mapBusStops.emplace_back(curr);
// go to next line
}
//
// sort the mapBusStops vector by stopID
//
sort(this->mapBusStops.begin(), this->mapBusStops.end(), compareBusStops);
};
//
// print all bus stops
//
void BusStops::printAll() {
for (auto busStop : this->mapBusStops) {
busStop.print();
}
};
//
// predictBusArrival
//
void BusStops::predictBusArrival(BusStop& busstop, CURL* curl, string& response) {
// setup the request URL
string myAPIKey = "ExCdTPSD6TyijExYYG7aewhKD";
string URL = "https://ctabustracker.com/bustime/api/v2/getpredictions?key="+ myAPIKey + "&rt=" +
to_string(busstop.getBusRoute()) + "&stpid=" + to_string(busstop.getStopID()) + "&format=json";
bool success = callWebServer(curl, URL, response);
if (!success) {
cout << " <<bus predictions unavailable, call failed>>" << endl;
return;
}
else {
// use the json library to parse the data
auto jsondata = json::parse(response);
auto bus_response = jsondata["bustime-response"];
auto predictions = bus_response["prd"];
if (predictions.size() == 0) {
cout << " <<no predictions available>>" << endl;
}
// for each prediction (a map) in the list:
for (auto& M : predictions) {
try{
std::cout << " vehicle #" << stoi(M["vid"].get_ref<std::string &>())
<< " on route " << stoi(M["rt"].get_ref<std::string &>())
<< " travelling " << M["rtdir"].get_ref<std::string &>()
<< " to arrive in " << stoi(M["prdctdn"].get_ref<std::string &>()) << " mins" << endl;
// prdctdn more like predict DEEZ NUTS
}
catch (exception& e) {
std::cout << " error" << endl;
std::cout << " malformed CTA response, prediction unavailable"
<< "(error: " << e.what() << ")" << std::endl;
}
}
}
}
//
// getClosestBusStop
//
void BusStops::getClosestBusStop(pair<double, double> location, CURL* curl) {
//
// initialize locations to compare to
//
double southdist = 10000;
double northdist = 10000;
BusStop southBusStop;
BusStop northBusStop;
for (auto busStop : this->mapBusStops) {
pair<double, double> busStopPosition = busStop.getPosition();
string direction = busStop.getDirection();
double dist = distBetween2Points(busStopPosition.first, busStopPosition.second, location.first, location.second);
//
// if distance is closer, update closest distance and busstop
// else do nothing
//
if (direction == "Southbound" && dist < southdist) {
southdist = dist;
southBusStop = busStop;
}
else if (direction == "Northbound" && dist < northdist) {
northdist = dist;
northBusStop = busStop;
}
}
//
// print some info
//
cout << "Closest southbound bus stop:" << endl;
cout << " " << southBusStop.getStopID() << ": " << southBusStop.getStopName()
<< ", bus #" << southBusStop.getBusRoute() << ", " << southBusStop.getLocationDesc()
<< ", " << southdist << " miles" << endl;
// get predictions for southbound bus stop
string predictionSouth;
predictBusArrival(southBusStop, curl, predictionSouth);
cout << "Closest northbound bus stop:" << endl;
cout << " " << northBusStop.getStopID() << ": " << northBusStop.getStopName()
<< ", bus #" << northBusStop.getBusRoute() << ", " << northBusStop.getLocationDesc()
<< ", " << northdist << " miles" << endl;
// get predictions for northbound bus stop
string predictionNorth;
predictBusArrival(northBusStop, curl, predictionNorth);
// done!
return;
}
//
// get the number of bus stops
//
int BusStops::getNumBusStops() {
return this->mapBusStops.size();
}