-
Notifications
You must be signed in to change notification settings - Fork 32
/
tdata.c
606 lines (541 loc) · 25.6 KB
/
tdata.c
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* Copyright 2013 Bliksem Labs. See the LICENSE file at the top-level directory of this distribution and at https://github.com/bliksemlabs/rrrr/. */
/* tdata.c : handles memory mapped data file containing transit timetable etc. */
#include "tdata.h" // make sure it works alone
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <stdbool.h>
#include "config.h"
#include "util.h"
#include "radixtree.h"
#include "gtfs-realtime.pb-c.h"
// file-visible struct
typedef struct tdata_header tdata_header_t;
struct tdata_header {
char version_string[8]; // should read "TTABLEV1"
uint64_t calendar_start_time;
calendar_t dst_active;
uint32_t n_stops;
uint32_t n_routes;
uint32_t n_trips;
/* uint32_t n_agencies; */
uint32_t loc_stops;
uint32_t loc_stop_attributes;
uint32_t loc_stop_coords;
uint32_t loc_routes;
uint32_t loc_route_stops;
uint32_t loc_route_stop_attributes;
uint32_t loc_stop_times;
uint32_t loc_trips;
uint32_t loc_trip_attributes;
uint32_t loc_stop_routes;
uint32_t loc_transfer_target_stops;
uint32_t loc_transfer_dist_meters;
uint32_t loc_trip_active;
uint32_t loc_route_active;
uint32_t loc_platformcodes;
uint32_t loc_stop_names;
uint32_t loc_stop_nameidx;
uint32_t loc_agency_ids;
uint32_t loc_agency_names;
uint32_t loc_agency_urls;
uint32_t loc_headsigns;
uint32_t loc_route_shortnames;
uint32_t loc_productcategories;
uint32_t loc_route_ids;
uint32_t loc_stop_ids;
uint32_t loc_trip_ids;
};
inline char *tdata_route_id_for_index(tdata_t *td, uint32_t route_index) {
if (route_index == NONE) return "NONE";
return td->route_ids + (td->route_id_width * route_index);
}
inline char *tdata_stop_id_for_index(tdata_t *td, uint32_t stop_index) {
return td->stop_ids + (td->stop_id_width * stop_index);
}
inline uint8_t *tdata_stop_attributes_for_index(tdata_t *td, uint32_t stop_index) {
return td->stop_attributes + stop_index;
}
inline char *tdata_trip_id_for_index(tdata_t *td, uint32_t trip_index) {
return td->trip_ids + (td->trip_id_width * trip_index);
}
inline char *tdata_trip_id_for_route_trip_index(tdata_t *td, uint32_t route_index, uint32_t trip_index) {
return td->trip_ids + (td->trip_id_width * (td->routes[route_index].trip_ids_offset + trip_index));
}
inline char *tdata_agency_id_for_index(tdata_t *td, uint32_t agency_index) {
return td->agency_ids + (td->agency_id_width * agency_index);
}
inline char *tdata_agency_name_for_index(tdata_t *td, uint32_t agency_index) {
return td->agency_names + (td->agency_name_width * agency_index);
}
inline char *tdata_agency_url_for_index(tdata_t *td, uint32_t agency_index) {
return td->agency_urls + (td->agency_url_width * agency_index);
}
inline char *tdata_headsign_for_offset(tdata_t *td, uint32_t headsign_offset) {
return td->headsigns + headsign_offset;
}
inline char *tdata_route_shortname_for_index(tdata_t *td, uint32_t route_shortname_index) {
return td->route_shortnames + (td->route_shortname_width * route_shortname_index);
}
inline char *tdata_productcategory_for_index(tdata_t *td, uint32_t productcategory_index) {
return td->productcategories + (td->productcategory_width * productcategory_index);
}
inline char *tdata_stop_name_for_index(tdata_t *td, uint32_t stop_index) {
switch (stop_index) {
case NONE :
return "NONE";
case ONBOARD :
return "ONBOARD";
default :
return td->stop_names + td->stop_nameidx[stop_index];
}
}
inline char *tdata_platformcode_for_index(tdata_t *td, uint32_t stop_index) {
switch (stop_index) {
case NONE :
return NULL;
case ONBOARD :
return NULL;
default :
return td->platformcodes + (td->platformcode_width * stop_index);
}
}
inline uint32_t tdata_stopidx_by_stop_name(tdata_t *td, char* stop_desc, uint32_t start_index) {
for (uint32_t stop_index = start_index; stop_index < td->n_stops; stop_index++) {
if (strcasestr(td->stop_names + td->stop_nameidx[stop_index], stop_desc)) {
return stop_index;
}
}
return NONE;
}
inline uint32_t tdata_stopidx_by_stop_id(tdata_t *td, char* stop_id, uint32_t start_index) {
for (uint32_t stop_index = start_index; stop_index < td->n_stops; stop_index++) {
if (strcasestr(td->stop_ids + (td->stop_id_width * stop_index), stop_id)) {
return stop_index;
}
}
return NONE;
}
inline uint32_t tdata_routeidx_by_route_id(tdata_t *td, char* route_id, uint32_t start_index) {
for (uint32_t route_index = start_index; route_index < td->n_routes; route_index++) {
if (strcasestr(td->route_ids + (td->route_id_width * route_index), route_id)) {
return route_index;
}
}
return NONE;
}
inline char *tdata_trip_ids_for_route(tdata_t *td, uint32_t route_index) {
route_t route = (td->routes)[route_index];
uint32_t char_offset = route.trip_ids_offset * td->trip_id_width;
return td->trip_ids + char_offset;
}
inline calendar_t *tdata_trip_masks_for_route(tdata_t *td, uint32_t route_index) {
route_t route = (td->routes)[route_index];
return td->trip_active + route.trip_ids_offset;
}
inline char *tdata_headsign_for_route(tdata_t *td, uint32_t route_index) {
if (route_index == NONE) return "NONE";
route_t route = (td->routes)[route_index];
return td->headsigns + route.headsign_offset;
}
inline char *tdata_shortname_for_route(tdata_t *td, uint32_t route_index) {
if (route_index == NONE) return "NONE";
route_t route = (td->routes)[route_index];
return td->route_shortnames + (td->route_shortname_width * route.shortname_index);
}
inline char *tdata_productcategory_for_route(tdata_t *td, uint32_t route_index) {
if (route_index == NONE) return "NONE";
route_t route = (td->routes)[route_index];
return td->productcategories + (td->productcategory_width * route.productcategory_index);
}
inline uint32_t tdata_agencyidx_by_agency_name(tdata_t *td, char* agency_name, uint32_t start_index) {
for (uint32_t agency_index = start_index; agency_index < td->n_agencies; agency_index++) {
if (strcasestr(td->agency_names + (td->agency_name_width * agency_index), agency_name)) {
return agency_index;
}
}
return NONE;
}
inline char *tdata_agency_id_for_route(tdata_t *td, uint32_t route_index) {
if (route_index == NONE) return "NONE";
route_t route = (td->routes)[route_index];
return td->agency_ids + (td->agency_id_width * route.agency_index);
}
inline char *tdata_agency_name_for_route(tdata_t *td, uint32_t route_index) {
if (route_index == NONE) return "NONE";
route_t route = (td->routes)[route_index];
return td->agency_names + (td->agency_name_width * route.agency_index);
}
inline char *tdata_agency_url_for_route(tdata_t *td, uint32_t route_index) {
if (route_index == NONE) return "NONE";
route_t route = (td->routes)[route_index];
return td->agency_urls + (td->agency_url_width * route.agency_index);
}
void tdata_check_coherent (tdata_t *tdata) {
printf ("checking tdata coherency...\n");
/* Check that all lat/lon look like valid coordinates. */
float min_lat = -55.0; // farther south than Ushuaia, Argentina
float max_lat = +70.0; // farther north than Tromsø and Murmansk
float min_lon = -180.0;
float max_lon = +180.0;
for (uint32_t s = 0; s < tdata->n_stops; ++s) {
latlon_t ll = tdata->stop_coords[s];
if (ll.lat < min_lat || ll.lat > max_lat || ll.lon < min_lon || ll.lon > max_lon) {
printf ("stop lat/lon out of range: lat=%f, lon=%f \n", ll.lat, ll.lon);
}
}
/* Check that all timedemand types start at 0 and consist of monotonically increasing times. */
for (uint32_t r = 0; r < tdata->n_routes; ++r) {
route_t route = tdata->routes[r];
trip_t *trips = tdata->trips + route.trip_ids_offset;
int n_nonincreasing_trips = 0;
for (int t = 0; t < route.n_trips; ++t) {
trip_t trip = trips[t];
stoptime_t *prev_st = NULL;
for (int s = 0; s < route.n_stops; ++s) {
stoptime_t *st = tdata->stop_times + trip.stop_times_offset + s;
if (s == 0 && st->arrival != 0) printf ("timedemand type begins at %d,%d not 0.\n", st->arrival, st->departure);
if (st->departure < st->arrival) printf ("departure before arrival at route %d, trip %d, stop %d.\n", r, t, s);
if (prev_st != NULL) {
if (st->arrival < prev_st->departure) {
// printf ("negative travel time arriving at route %d, trip %d, stop %d.\n", r, t, s);
// printf ("(%d, %d) -> (%d, %d)\n", prev_st->arrival, prev_st->departure, st->arrival, st->departure);
n_nonincreasing_trips += 1;
} // there are also lots of 0 travel times...
}
prev_st = st;
}
}
if (n_nonincreasing_trips > 0) printf ("route %d has %d trips with negative travel times\n", r, n_nonincreasing_trips);
}
/* Check that all transfers are symmetric. */
int n_transfers_checked = 0;
for (uint32_t stop_index_from = 0; stop_index_from < tdata->n_stops; ++stop_index_from) {
/* Iterate over all transfers going out of this stop */
uint32_t t = tdata->stops[stop_index_from ].transfers_offset;
uint32_t tN = tdata->stops[stop_index_from + 1].transfers_offset;
for ( ; t < tN ; ++t) {
uint32_t stop_index_to = tdata->transfer_target_stops[t];
uint32_t forward_distance = tdata->transfer_dist_meters[t] << 4; // actually in units of 16 meters
if (stop_index_to == stop_index_from) printf ("loop transfer from/to stop %d.\n", stop_index_from);
/* Find the reverse transfer (stop_index_to -> stop_index_from) */
bool found_reverse = false;
uint32_t u = tdata->stops[stop_index_to ].transfers_offset;
uint32_t uN = tdata->stops[stop_index_to + 1].transfers_offset;
for ( ; u < uN ; ++u) {
n_transfers_checked += 1;
if (tdata->transfer_target_stops[u] == stop_index_from) {
/* this is the same transfer in reverse */
uint32_t reverse_distance = tdata->transfer_dist_meters[u] << 4;
if (reverse_distance != forward_distance) {
printf ("transfer from %d to %d is not symmetric. "
"forward distance is %d, reverse distance is %d.\n",
stop_index_from, stop_index_to, forward_distance, reverse_distance);
}
found_reverse = true;
break;
}
}
if ( ! found_reverse) printf ("transfer from %d to %d does not have an equivalent reverse transfer.\n", stop_index_from, stop_index_to);
}
}
printf ("checked %d transfers for symmetry.\n", n_transfers_checked);
}
/* Map an input file into memory and reconstruct pointers to its contents. */
void tdata_load(char *filename, tdata_t *td) {
int fd = open(filename, O_RDWR);
if (fd == -1)
die("could not find input file");
struct stat st;
if (stat(filename, &st) == -1)
die("could not stat input file");
td->base = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
td->size = st.st_size;
if (td->base == (void*)(-1))
die("could not map input file");
void *b = td->base;
tdata_header_t *header = b;
if( strncmp("TTABLEV2", header->version_string, 8) )
die("the input file does not appear to be a timetable or is of the wrong version");
td->calendar_start_time = header->calendar_start_time;
td->dst_active = header->dst_active;
td->n_stops = header->n_stops;
td->n_routes = header->n_routes;
td->n_trips = header->n_trips;
/* td->n_agencies = header->n_agencies; */
td->stops = (stop_t*) (b + header->loc_stops);
td->stop_attributes = (uint8_t*) (b + header->loc_stop_attributes);
td->stop_coords = (latlon_t*) (b + header->loc_stop_coords);
td->routes = (route_t*) (b + header->loc_routes);
td->route_stops = (uint32_t *) (b + header->loc_route_stops);
td->route_stop_attributes = (uint8_t *) (b + header->loc_route_stop_attributes);
td->stop_times = (stoptime_t*) (b + header->loc_stop_times);
td->trips = (trip_t*) (b + header->loc_trips);
td->stop_routes = (uint32_t *) (b + header->loc_stop_routes);
td->transfer_target_stops = (uint32_t *) (b + header->loc_transfer_target_stops);
td->transfer_dist_meters = (uint8_t *) (b + header->loc_transfer_dist_meters);
//maybe replace with pointers because there's a lot of wasted space?
td->platformcode_width = *((uint32_t *) (b + header->loc_platformcodes));
td->platformcodes = (char*) (b + header->loc_platformcodes + sizeof(uint32_t));
td->stop_names = (char*) (b + header->loc_stop_names);
td->stop_nameidx = (uint32_t *) (b + header->loc_stop_nameidx);
td->agency_id_width = *((uint32_t *) (b + header->loc_agency_ids));
td->agency_ids = (char*) (b + header->loc_agency_ids + sizeof(uint32_t));
td->agency_name_width = *((uint32_t *) (b + header->loc_agency_names));
td->agency_names = (char*) (b + header->loc_agency_names + sizeof(uint32_t));
td->agency_url_width = *((uint32_t *) (b + header->loc_agency_urls));
td->agency_urls = (char*) (b + header->loc_agency_urls + sizeof(uint32_t));
td->headsigns = (char*) (b + header->loc_headsigns);
td->route_shortname_width = *((uint32_t *) (b + header->loc_route_shortnames));
td->route_shortnames = (char*) (b + header->loc_route_shortnames + sizeof(uint32_t));
td->productcategory_width = *((uint32_t *) (b + header->loc_productcategories));
td->productcategories = (char*) (b + header->loc_productcategories + sizeof(uint32_t));
td->route_id_width = *((uint32_t *) (b + header->loc_route_ids));
td->route_ids = (char*) (b + header->loc_route_ids + sizeof(uint32_t));
td->stop_id_width = *((uint32_t *) (b + header->loc_stop_ids));
td->stop_ids = (char*) (b + header->loc_stop_ids + sizeof(uint32_t));
td->trip_id_width = *((uint32_t *) (b + header->loc_trip_ids));
td->trip_ids = (char*) (b + header->loc_trip_ids + sizeof(uint32_t));
td->trip_active = (uint32_t*) (b + header->loc_trip_active);
td->route_active = (uint32_t*) (b + header->loc_route_active);
td->trip_attributes = (uint8_t*) (b + header->loc_trip_attributes);
td->alerts = NULL;
// This should be migrated to n_agencies from the timetable generation in my humble option.
td->n_agencies = 0;
for (uint32_t r = 0; r < td->n_routes; ++r) {
if (td->routes[r].agency_index > td->n_agencies)
td->n_agencies = td->routes[r].agency_index;
}
// This is probably a bit slow and is not strictly necessary, but does page in all the timetable entries.
tdata_check_coherent(td);
D tdata_dump(td);
}
void tdata_close(tdata_t *td) {
munmap(td->base, td->size);
}
// TODO should pass pointer to tdata?
inline uint32_t *tdata_stops_for_route(tdata_t *td, uint32_t route) {
route_t route0 = td->routes[route];
return td->route_stops + route0.route_stops_offset;
}
inline uint8_t *tdata_stop_attributes_for_route(tdata_t *td, uint32_t route) {
route_t route0 = td->routes[route];
return td->route_stop_attributes + route0.route_stops_offset;
}
inline uint32_t tdata_routes_for_stop(tdata_t *td, uint32_t stop, uint32_t **routes_ret) {
stop_t stop0 = td->stops[stop];
stop_t stop1 = td->stops[stop + 1];
*routes_ret = td->stop_routes + stop0.stop_routes_offset;
return stop1.stop_routes_offset - stop0.stop_routes_offset;
}
// TODO used only in dumping routes; trip_index is not used in the expression?
inline stoptime_t *tdata_timedemand_type(tdata_t *td, uint32_t route_index, uint32_t trip_index) {
return td->stop_times + td->trips[td->routes[route_index].trip_ids_offset + trip_index].stop_times_offset;
}
inline trip_t *tdata_trips_for_route (tdata_t *td, uint32_t route_index) {
return td->trips + td->routes[route_index].trip_ids_offset;
}
inline uint8_t *tdata_trip_attributes_for_route (tdata_t *td, uint32_t route_index) {
return td->trip_attributes + td->routes[route_index].trip_ids_offset;
}
/* Signed delay of the specified trip, in seconds. */
inline float tdata_delay_min (tdata_t *td, uint32_t route_index, uint32_t trip_index) {
trip_t *trips = tdata_trips_for_route(td, route_index);
return RTIME_TO_SEC_SIGNED(trips[trip_index].realtime_delay) / 60.0;
}
void tdata_dump_route(tdata_t *td, uint32_t route_idx, uint32_t trip_idx) {
uint32_t *stops = tdata_stops_for_route(td, route_idx);
route_t route = td->routes[route_idx];
printf("\nRoute details for %s %s %s '%s %s' [%d] (n_stops %d, n_trips %d)\n", tdata_agency_name_for_route(td, route_idx),
tdata_agency_id_for_route(td, route_idx), tdata_agency_url_for_route(td, route_idx),
tdata_shortname_for_route(td, route_idx), tdata_headsign_for_route(td, route_idx), route_idx, route.n_stops, route.n_trips);
printf("tripid, stop sequence, stop name (index), departures \n");
for (uint32_t ti = (trip_idx == NONE ? 0 : trip_idx); ti < (trip_idx == NONE ? route.n_trips : trip_idx + 1); ++ti) {
// TODO should this really be a 2D array ?
stoptime_t (*times)[route.n_stops] = (void*) tdata_timedemand_type(td, route_idx, ti);
printf("%s ", tdata_trip_id_for_index(td, route.trip_ids_offset + ti));
for (uint32_t si = 0; si < route.n_stops; ++si) {
char *stop_id = tdata_stop_name_for_index (td, stops[si]);
printf("%4d %35s [%06d] : %s", si, stop_id, stops[si], timetext(times[0][si].departure + td->trips[route.trip_ids_offset + ti].begin_time + RTIME_ONE_DAY));
}
printf("\n");
}
printf("\n");
}
void tdata_dump(tdata_t *td) {
printf("\nCONTEXT\n"
"n_stops: %d\n"
"n_routes: %d\n", td->n_stops, td->n_routes);
printf("\nSTOPS\n");
for (uint32_t i = 0; i < td->n_stops; i++) {
printf("stop %d at lat %f lon %f\n", i, td->stop_coords[i].lat, td->stop_coords[i].lon);
stop_t s0 = td->stops[i];
stop_t s1 = td->stops[i+1];
uint32_t j0 = s0.stop_routes_offset;
uint32_t j1 = s1.stop_routes_offset;
uint32_t j;
printf("served by routes ");
for (j=j0; j<j1; ++j) {
printf("%d ", td->stop_routes[j]);
}
printf("\n");
}
printf("\nROUTES\n");
for (uint32_t i = 0; i < td->n_routes; i++) {
printf("route %d\n", i);
printf("having trips %d\n", td->routes[i].n_trips);
route_t r0 = td->routes[i];
route_t r1 = td->routes[i+1];
uint32_t j0 = r0.route_stops_offset;
uint32_t j1 = r1.route_stops_offset;
uint32_t j;
printf("serves stops ");
for (j=j0; j<j1; ++j) {
printf("%d ", td->route_stops[j]);
}
printf("\n");
}
printf("\nSTOPIDS\n");
for (uint32_t i = 0; i < td->n_stops; i++) {
printf("stop %03d has id %s \n", i, tdata_stop_name_for_index(td, i));
}
#if 0
printf("\nROUTEIDS, TRIPIDS\n");
for (uint32_t i = 0; i < td->n_routes; i++) {
printf("route %03d has id %s and first trip id %s \n", i,
tdata_route_desc_for_index(td, i),
tdata_trip_ids_for_route(td, i));
}
#endif
}
/*
Decodes the GTFS-RT message of lenth len in buffer buf, extracting vehicle position messages
and using the delay extension (1003) to update RRRR's per-trip delay information.
*/
void tdata_apply_gtfsrt (tdata_t *tdata, RadixTree *tripid_index, uint8_t *buf, size_t len) {
TransitRealtime__FeedMessage *msg;
msg = transit_realtime__feed_message__unpack (NULL, len, buf);
if (msg == NULL) {
fprintf (stderr, "error unpacking incoming gtfs-rt message\n");
return;
}
printf("Received feed message with %zu entities.\n", msg->n_entity);
for (size_t e = 0; e < msg->n_entity; ++e) {
TransitRealtime__FeedEntity *entity = msg->entity[e];
if (entity == NULL) goto cleanup;
// printf(" entity %d has id %s\n", e, entity->id);
TransitRealtime__VehiclePosition *vehicle = entity->vehicle;
if (vehicle == NULL) goto cleanup;
TransitRealtime__TripDescriptor *trip = vehicle->trip;
if (trip == NULL) goto cleanup;
char *trip_id = trip->trip_id;
int32_t delay_sec = 0;
if (trip->schedule_relationship == TRANSIT_REALTIME__TRIP_DESCRIPTOR__SCHEDULE_RELATIONSHIP__CANCELED) {
delay_sec = CANCELED;
} else {
TransitRealtime__OVapiVehiclePosition *ovapi_vehicle_position = vehicle->ovapi_vehicle_position;
if (ovapi_vehicle_position == NULL) printf (" entity contains no delay message.\n");
else delay_sec = ovapi_vehicle_position->delay;
if (abs(delay_sec) > 60 * 120) {
printf (" filtering out extreme delay of %d sec.\n", delay_sec);
delay_sec = 0;
}
}
/* Apply delay. */
uint32_t trip_index = rxt_find (tripid_index, trip_id);
if (trip_index == RADIX_TREE_NONE) {
printf (" trip id was not found in the radix tree.\n");
} else {
// printf (" trip_id %s, trip number %d, applying delay of %d sec.\n", trip_id, trip_index, delay_sec);
trip_t *trip = tdata->trips + trip_index;
trip->realtime_delay = SEC_TO_RTIME(delay_sec);
}
}
cleanup:
transit_realtime__feed_message__free_unpacked (msg, NULL);
}
void tdata_clear_gtfsrt (tdata_t *tdata) {
/* If we had the total number of trips nested loops would not be necessary. */
for (uint32_t r = 0; r < tdata->n_routes; ++r) {
route_t route = tdata->routes[r];
trip_t *trips = tdata_trips_for_route(tdata, r);
for (int t = 0; t < route.n_trips; ++t) {
trips[t].realtime_delay = 0;
}
}
}
void tdata_apply_gtfsrt_file (tdata_t *tdata, RadixTree *tripid_index, char *filename) {
int fd = open(filename, O_RDONLY);
if (fd == -1) die("Could not find GTFS_RT input file.\n");
struct stat st;
if (stat(filename, &st) == -1) die("Could not stat GTFS_RT input file.\n");
uint8_t *buf = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) die("Could not map GTFS-RT input file.\n");
tdata_apply_gtfsrt (tdata, tripid_index, buf, st.st_size);
munmap (buf, st.st_size);
}
void tdata_apply_gtfsrt_alerts (tdata_t *tdata, RadixTree *routeid_index, RadixTree *stopid_index, RadixTree *tripid_index, uint8_t *buf, size_t len) {
TransitRealtime__FeedMessage *msg = transit_realtime__feed_message__unpack (NULL, len, buf);
if (msg == NULL) {
fprintf (stderr, "error unpacking incoming gtfs-rt message\n");
return;
}
printf("Received feed message with %zu entities.\n", msg->n_entity);
for (size_t e = 0; e < msg->n_entity; ++e) {
TransitRealtime__FeedEntity *entity = msg->entity[e];
if (entity == NULL) goto cleanup;
// printf(" entity %d has id %s\n", e, entity->id);
TransitRealtime__Alert *alert = entity->alert;
if (alert == NULL) goto cleanup;
for (size_t ie = 0; ie < alert->n_informed_entity; ++ie) {
TransitRealtime__EntitySelector *informed_entity = alert->informed_entity[ie];
if (!informed_entity) continue;
if (informed_entity->route_id) {
uint32_t route_index = rxt_find (routeid_index, informed_entity->route_id);
if (route_index == RADIX_TREE_NONE) {
printf (" route id was not found in the radix tree.\n");
}
memcpy (informed_entity->route_id, &route_index, sizeof(route_index));
}
if (informed_entity->stop_id) {
uint32_t stop_index = rxt_find (stopid_index, informed_entity->stop_id);
if (stop_index == RADIX_TREE_NONE) {
printf (" stop id was not found in the radix tree.\n");
}
memcpy (informed_entity->stop_id, &stop_index, sizeof(stop_index));
}
if (informed_entity->trip && informed_entity->trip->trip_id) {
uint32_t trip_index = rxt_find (tripid_index, informed_entity->trip->trip_id);
if (trip_index == RADIX_TREE_NONE) {
printf (" trip id was not found in the radix tree.\n");
}
memcpy (informed_entity->trip->trip_id, &trip_index, sizeof(trip_index));
}
}
}
tdata->alerts = msg;
return;
cleanup:
transit_realtime__feed_message__free_unpacked (msg, NULL);
}
void tdata_clear_gtfsrt_alerts (tdata_t *tdata) {
if (tdata->alerts) {
transit_realtime__feed_message__free_unpacked (tdata->alerts, NULL);
tdata->alerts = NULL;
}
}
void tdata_apply_gtfsrt_alerts_file (tdata_t *tdata, RadixTree *routeid_index, RadixTree *stopid_index, RadixTree *tripid_index, char *filename) {
int fd = open(filename, O_RDONLY);
if (fd == -1) die("Could not find GTFS_RT input file.\n");
struct stat st;
if (stat(filename, &st) == -1) die("Could not stat GTFS_RT input file.\n");
uint8_t *buf = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) die("Could not map GTFS-RT input file.\n");
tdata_apply_gtfsrt_alerts (tdata, routeid_index, stopid_index, tripid_index, buf, st.st_size);
munmap (buf, st.st_size);
}
// tdata_get_route_stops
/* optional stop ids, names, coordinates... */