-
Notifications
You must be signed in to change notification settings - Fork 0
/
busStop.pde
552 lines (497 loc) · 17.1 KB
/
busStop.pde
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
class busStop {
String lbsl; //unique ID, key field
int code; //5 digit for texting the bus tracker
String name; //what's it called?
int easting; //OS Grid map
int northing; //OS Grid map
int heading; //which direction it faces?
String stopArea; //from the data, unknown use
boolean virtual; //is it a virtual bus stop. if yes, remove it!
PVector screen; //where on the screen to draw it?
boolean onScreen;
PVector normal; //what direction is perpendicular to the average line going through this stop? (unit length)
float scale; //how big to draw it?
int busCount; //how many routes go here?
stopLabel label;
ArrayList<stopRoute> routes; //which routes stop here? which legs?
ArrayList<stopPairRoute> routePairs; //which pairs of stops stop here?
//interchanges
boolean tube = false;
boolean train = false;
boolean dlr = false;
boolean tram = false;
boolean river = false;
busStop(String[] stopData) {
//process the data from the file
this.lbsl = trim(stopData[0]);
this.code = int(trim(stopData[1]));
int eastingStart = 4;
//name - get the basic info into one string
if (stopData[3].charAt(0) != '"') {
this.name = stopData[3];
}
else {
String nameString = stopData[3];
while (eastingStart < stopData.length - 5) {
nameString = nameString + stopData[eastingStart];
eastingStart++;
}
this.name = nameString;
}
//see if it has connections!
if (this.name.indexOf("<>") != -1) {
this.tube = true;
this.name = this.name.replace("<>", "");
}
if (this.name.indexOf("#") != -1) {
this.train = true;
this.name = this.name.replace("#", "");
}
if (this.name.indexOf("[DLR]") != -1) {
this.dlr = true;
this.name = this.name.replace("[DLR]", "");
}
if (this.name.indexOf("(DLR)") != -1) {
this.dlr = true;
this.name = this.name.replace("(DLR)", "");
}
if (this.name.indexOf(">T<") != -1) {
this.tram = true;
this.name = this.name.replace(">T<", "");
}
if (this.name.indexOf("<T>") != -1) {
this.tram = true;
this.name = this.name.replace("<T>", "");
}
if (this.name.indexOf(">R<") != -1) {
this.river = true;
this.name = this.name.replace(">R<", "");
}
if (this.name.indexOf("<R>") != -1) {
this.river = true;
this.name = this.name.replace("<R>", "");
}
//tidy up the name
while (this.name.indexOf (" ") != -1) { //get rid of double spaces
this.name = this.name.replace(" ", " ");
}
this.name = this.name.replace("\"", ""); //get rid of speech marks
this.name = titleCase(this.name);
this.easting = int(trim(stopData[eastingStart]));
this.northing = int(trim(stopData[eastingStart + 1]));
this.heading = int(trim(stopData[eastingStart + 2]));
this.stopArea = trim(stopData[eastingStart + 3]);
//virtual bus stops are used to give the route waypoints but there's not an actual stop there
if (trim(stopData[eastingStart + 4]).equals("1")) {
this.virtual = true;
}
else {
this.virtual = false;
}
//work out starting place to draw it
this.screen = screenCoordinates(easting, northing);
this.onScreen = isOnScreen(this.screen);
//container to store routes
routes = new ArrayList<stopRoute>();
}
// adds a pop out label to the list
void addLabel() {
if (label == null) { //if it doesn't already have a label, see if there's one with the same name
stopLabel findLabel = stopLabels.find(this.name);
if (findLabel == null) { //if not, make one
new stopLabel(this);
}
else { //if yes, make it exist longer
findLabel.creationTime = millis();
}
}
else { //renewed lease of life for existing label
label.creationTime = millis();
}
}
//makes life easier for drawing -> compiles together a list of the routes, which is helpful for getting colours
ArrayList<busRoute> getRoutes() {
ArrayList<busRoute> routeList = new ArrayList<busRoute>();
for (int i = 0; i < routes.size(); i++) {
stopRoute thisStopRoute = (stopRoute)routes.get(i);
routeList.add(thisStopRoute.route);
}
return routeList;
}
//which combo of leg of this route is this stop on?
stopRoute getStopRoute(String routeNumber) {
for (int i = 0; i < routes.size(); i++) {
stopRoute thisStopRoute = (stopRoute)routes.get(i);
if (thisStopRoute.route.routeNumber.equals(routeNumber)) {
return thisStopRoute;
}
}
return null;
}
//draw the popup panel with details of the bus stop and its route
void drawDetails() {
//set up a bit at the bottom of the screen
fill(0);
noStroke();
rect(0, height - 100, width, 100);
stroke(255);
strokeWeight(1);
line(0, height - 100, width, height - 100);
//stop name
fill(255);
textAlign(LEFT);
text(this.name, 12, height - 80);
//symbols for interchanges
float startX = 12 + textWidth(this.name) + 4;
float startY = height - 90;
noStroke();
if (this.tube) {
fill(255);
rect(startX - 1, startY - 1, 18, 14);
shape(tubeLogo, startX, startY, 16, 12);
startX += 18;
}
if (this.train) {
fill(255);
rect(startX - 1, startY - 1, 18, 14);
shape(trainLogo, startX, startY, 16, 12);
startX += 18;
}
if (this.dlr) {
fill(255);
rect(startX - 1, startY - 1, 18, 14);
shape(dlrLogo, startX, startY, 16, 12);
startX += 18;
}
if (this.river) {
fill(255);
rect(startX - 1, startY - 1, 18, 14);
shape(riverLogo, startX, startY, 16, 12);
startX += 18;
}
if (this.tram) {
fill(255);
rect(startX - 1, startY - 1, 18, 14);
shape(tramLogo, startX, startY, 16, 12);
startX += 18;
}
//show the stop code
if (this.code > 0) {
textAlign(RIGHT);
text(this.code, width - 12, height - 80);
}
//boxes for routes -> up to 24 of the buggers
int widthDiff = 12;
int heightDiff = 72;
int boxRow = floor(width - widthDiff) / 48;
for (int i = 0; i < routes.size(); i++) {
if (i == boxRow) {
widthDiff = 12;
heightDiff -= 36;
}
//box for the route number
fill(routeColours.get(i).value);
noStroke();
rect(widthDiff, height - heightDiff, 40, 30);
//route number in the box
fill(0);
stopRoute thisStopRoute = (stopRoute)routes.get(i);
textAlign(CENTER);
int boxLeft = widthDiff;
int boxRight = widthDiff + 40;
text(thisStopRoute.route.routeNumber, (boxLeft + boxRight) / 2.0, height - heightDiff + 20);
widthDiff += 48;
}
}
//draw the lines stopping at this stop
void drawRoutes() {
//safety first - check we have routes and if not, get them
if (routePairs == null) {
getRoutePairs();
}
//now draw the lines!
strokeWeight(2); // leaves a gap between lines for clarity!
ArrayList<busRoute> routeList = getRoutes(); //need a list of the routes in order so we can get the colours right
for (int i = 0; i < routePairs.size(); i++) {
stopPairRoute thisPairRoute = (stopPairRoute)routePairs.get(i);
stopPair thisPair = thisPairRoute.pair;
for (int j = 0; j < thisPairRoute.routes.size(); j++) {
//colour each line according to the route
busRoute thisRoute = (busRoute)thisPairRoute.routes.get(j);
int colourNumber = routeList.indexOf(thisRoute);
stroke(routeColours.get(colourNumber).value);
//if either stop is on screen, work out the correct offset for this route at each stop, and join them up
if (thisPair.firstStop.onScreen || thisPair.secondStop.onScreen) {
PVector firstScreen = routeScreen(thisPair.firstStop, thisRoute);
PVector secondScreen = routeScreen(thisPair.secondStop, thisRoute);
line(firstScreen.x, firstScreen.y, secondScreen.x, secondScreen.y);
}
}
}
}
//when drawing multiple routes, need to offset them from where we draw the stop itself, so you can see them going in parallel
PVector routeScreen(busStop thatStop, busRoute thisRoute) {
ArrayList sharedRoutes = this.commonRoutes(thatStop);
int routeIndex = sharedRoutes.indexOf(thisRoute);
PVector stopNormal = thatStop.normal;
if (stopNormal == null) {
thatStop.getNormal();
stopNormal = thatStop.normal;
}
float offsetCount = routeIndex - ((sharedRoutes.size() - 1) / 2.0);
float xOffset = 3 * offsetCount * stopNormal.x;
float yOffset = 3 * offsetCount * stopNormal.y;
return new PVector(thatStop.screen.x - xOffset, thatStop.screen.y - yOffset);
}
//work out which line is perpendicular to the average line through the stop
void getNormal() {
float dx = 0;
float dy = 0;
for (int i = 0; i < routes.size(); i++) {
stopRoute thisStopRoute = (stopRoute)routes.get(i);
if (thisStopRoute.outwardLeg) {
PVector legResult = getNormalLeg(thisStopRoute, true);
dx += legResult.x;
dy += legResult.y;
}
if (thisStopRoute.returnLeg) {
PVector legResult = getNormalLeg(thisStopRoute, false);
dx += legResult.x;
dy += legResult.y;
}
}
normal = new PVector(-dy, dx);
normal.normalize();
}
//use to save duplication in getNormal() - where we have two legs
PVector getNormalLeg(stopRoute s, boolean outwardLeg) {
ArrayList<busStop> stopList;
if (outwardLeg) {
stopList = s.route.outwardLeg;
}
else {
stopList = s.route.returnLeg;
}
float dx = 0;
float dy = 0;
for (int j = 0; j < stopList.size(); j++) {
busStop thatStop = (busStop)stopList.get(j);
if (thatStop == this) {
busStop otherStop;
if (j > 0) {
otherStop = (busStop)stopList.get(j - 1);
dx += (thatStop.screen.x - otherStop.screen.x);
dy += (thatStop.screen.y - otherStop.screen.y);
}
if (j < stopList.size() - 1) {
otherStop = (busStop)stopList.get(j + 1);
dx += (otherStop.screen.x - thatStop.screen.x);
dy += (otherStop.screen.y - thatStop.screen.y);
}
}
}
return new PVector(dx, dy);
}
//work out which pairs of stops are on the routes that stop here
void getRoutePairs() {
routePairs = new ArrayList<stopPairRoute>(); //combo of pair + routes
//for each route
for (int i = 0; i < routes.size(); i++) {
stopRoute thisStopRoute = (stopRoute)routes.get(i);
busRoute thisRoute = thisStopRoute.route;
//get the right legs!
if (thisStopRoute.outwardLeg) {
extractPairs(thisStopRoute.route.outwardLeg, thisRoute);
}
if (thisStopRoute.returnLeg) {
extractPairs(thisStopRoute.route.returnLeg, thisRoute);
}
}
}
//extract pairs from a particular leg of a journey and add to routePiars
private void extractPairs(ArrayList<busStop> stops, busRoute thisRoute) {
//for each stop on the outward leg (except the last one)
for (int j = 0; j < stops.size() - 1; j++) {
busStop firstStop = (busStop)stops.get(j);
busStop secondStop = (busStop)stops.get(j + 1);
stopPair thisPair = new stopPair(firstStop, secondStop);
//does it exist already?
stopPairRoute thisStopPairRoute = null;
for (int k = 0; k < routePairs.size(); k++) {
stopPairRoute resultPair = (stopPairRoute)routePairs.get(k);
if (resultPair.pair.equals(thisPair)) {
thisStopPairRoute = resultPair;
break;
}
}
//if it exists, add this route to its routes
if (thisStopPairRoute != null) {
//only add it if we don't already have it
if (!thisStopPairRoute.routes.contains(thisRoute)) {
thisStopPairRoute.routes.add(thisRoute);
}
}
else {
thisStopPairRoute = new stopPairRoute(thisPair, thisRoute);
routePairs.add(thisStopPairRoute);
}
}
}
//which routes do this stop and that stop have in common?
ArrayList<busRoute> commonRoutes(busStop thatStop) {
ArrayList<busRoute> results = new ArrayList<busRoute>();
for (int i = 0; i < routes.size(); i++) {
stopRoute thisStopRoute = (stopRoute)routes.get(i);
busRoute thisRoute = thisStopRoute.route;
for (int j = 0; j < thatStop.routes.size(); j++) {
stopRoute thatStopRoute = (stopRoute)thatStop.routes.get(j);
busRoute thatRoute = thatStopRoute.route;
if (thisRoute == thatRoute) {
results.add(thisRoute);
break;
}
}
}
return results;
}
}
//container for bus stops, unsurprisingly
class busStopList {
private HashMap<String, busStop> stops;
int busiest; //track which is the busiest station
//create a list of stops from a data file
busStopList(String filePath) {
//load the data file
String[] stopFile = loadStrings(filePath);
println("Loaded bus stops by " + millis() + " milliseconds");
//set up the parameters
busiest = 0;
stops = new HashMap<String, busStop>();
//process the info, line by line
for (int i = 1; i < stopFile.length; i++) {
String[] stopData = split(stopFile[i], ',');
if (stopData.length >= 8) {
busStop thisStop = new busStop(stopData);
if ((thisStop.northing != 0) && (thisStop.northing != 999999)) {
stops.put(thisStop.lbsl, thisStop);
}
}
}
println("Processed bus stops by " + millis() + " milliseconds");
}
//work out where each stop should be on the screen - ie. when zoomed/panned
void updateScreen() {
Iterator i = stops.values().iterator();
while (i.hasNext ()) {
busStop thisStop = (busStop)i.next();
if (thisStop != null) {
thisStop.screen = screenCoordinates(thisStop.easting, thisStop.northing);
thisStop.onScreen = isOnScreen(thisStop.screen);
}
}
}
//how many bus stops in the list?
int size() {
return stops.size();
}
//give us a bus stop from the list
busStop get(String lsbl) {
return (busStop)stops.get(lsbl);
}
//look up a bus stop by 5 digit bustracker code
busStop getCode(int code) {
Iterator i = stops.values().iterator();
while (i.hasNext ()) {
busStop thisStop = (busStop)i.next();
if (thisStop != null) {
if (thisStop.code == code) {
return thisStop;
}
}
}
return null;
}
//sort the routes at each stop into order
void sortRoutes() {
Iterator i = stops.values().iterator();
while (i.hasNext ()) {
busStop thisStop = (busStop)i.next();
if (thisStop != null) {
Collections.sort(thisStop.routes);
}
}
println("Sorted bus routes at each stop by " + millis() + " milliseconds");
}
//draw a point for each stop
void draw() {
noFill();
if (selectedStop == null) {
strokeWeight(2);
stroke(stopColour.value);
}
//handle mouse movements
ArrayList<busStop> selectionCandidates = new ArrayList<busStop>();
strokeWeight(2);
stroke(stopColour.value);
Iterator i = stops.values().iterator();
while (i.hasNext ()) {
busStop thisStop = (busStop)i.next();
if (thisStop != null && !thisStop.virtual && thisStop.onScreen) {
//normal stops in normal colour
if (drawStops.value) {
point(thisStop.screen.x, thisStop.screen.y);
}
//if the mouse is near stations, make a force-directed label
if (!drawControlPanel.value) {
if ((abs(thisStop.screen.x - mouseX) <= labelMouseRange.value) && (abs(thisStop.screen.y - mouseY) <= labelMouseRange.value)) {
if (chooseStop) {
selectionCandidates.add(thisStop); //don't just pick any stop - get the nearest one
}
else {
thisStop.addLabel(); //add/update the label
}
}
}
}
}
//there may be several stops in range, so work out which is the nearest and select that one
if (chooseStop) {
float minDistance = 999999;
PVector mousePos = new PVector(mouseX, mouseY);
for (int j = 0; j < selectionCandidates.size(); j++) {
busStop thisStop = (busStop)selectionCandidates.get(j);
float thisDistance = thisStop.screen.dist(mousePos);
if (thisDistance < minDistance) {
chosenStop = true;
minDistance = thisDistance;
selectedStop = thisStop;
}
}
//will need the list of routes for drawing
if (selectedStop != null) {
if (selectedStop.routePairs == null) {
selectedStop.getRoutePairs(); //so we can draw them!
}
}
updateScreen();
}
//didn't find anything, so go back to overview mode
if (chooseStop && !chosenStop) {
if ((selectedStop != null) && (selectedStop.routePairs != null)) {
selectedStop.routePairs = null;
}
//make sure we never run out of memory
for (int j = 0; j < stops.size(); j++) {
busStop thisStop = (busStop)stops.get(j);
if (thisStop != null) {
if (thisStop.normal != null) {
thisStop.normal = null;
}
}
}
selectedStop = null;
updateScreen();
}
chooseStop = false;
}
}