Skip to content

Commit 8d58e61

Browse files
Merge pull request #71 from linkedconnections/development
v2.1.0
2 parents 163d103 + 1ae1058 commit 8d58e61

File tree

9 files changed

+225
-41
lines changed

9 files changed

+225
-41
lines changed

.github/workflows/build-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
node-version: [12.x, 14.x, 16.x]
17+
node-version: [16.x]
1818
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
1919
steps:
2020
- name: Checkout

dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "npm"
4+
directory: "/"
5+
schedule:
6+
# Check for updates once a week
7+
interval: "weekly"
8+
# Raise pull requests for version updates against the `development` branch
9+
target-branch: "development"
10+
# Labels on pull requests for security and version updates
11+
labels:
12+
- "npm dependencies"

lib/GtfsIndex.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class GtfsIndex {
7373
let stop_times_index = null;
7474
let tripsByRoute = null;
7575
let firstStops = null;
76-
let calendar = null;
76+
let calendar_index = null;
7777
let calendarDates = null;
7878
let tp = null;
7979
let stp = null;
@@ -84,7 +84,6 @@ class GtfsIndex {
8484
if (deduce) {
8585
tripsByRoute = new Map();
8686
firstStops = new Map();
87-
calendar = new Map();
8887
calendarDates = new Map();
8988
}
9089

@@ -96,6 +95,8 @@ class GtfsIndex {
9695
routes_index = new Map();
9796
trips_index = new Map();
9897
stop_times_index = new Map();
98+
calendar_index = new Map();
99+
99100

100101
if (uTrips) {
101102
console.error(`Using grep to extract the stop_times of ${uTrips.length} trips`);
@@ -126,6 +127,7 @@ class GtfsIndex {
126127
routes_index = new Level('.rt_indexes/.routes', { valueEncoding: 'json' });
127128
trips_index = new Level('.rt_indexes/.trips', { valueEncoding: 'json' });
128129
stop_times_index = new Level('.rt_indexes/.stop_times', { valueEncoding: 'json' });
130+
calendar_index = new Level('.rt_indexes/.calendar', { valueEncoding: 'json' });
129131

130132
tp = this.createIndex(this.auxPath + '/trips.txt', trips_index, 'trip_id', tripsByRoute);
131133
// Make sure stop_times.txt is ordered by stop_sequence
@@ -139,9 +141,9 @@ class GtfsIndex {
139141

140142
let sp = this.createIndex(this.auxPath + '/stops.txt', stops_index, 'stop_id');
141143
let rp = this.createIndex(this.auxPath + '/routes.txt', routes_index, 'route_id');
144+
cp = this.createIndex(this.auxPath + '/calendar.txt', calendar_index, 'service_id');
142145

143-
if (deduce) {
144-
cp = this.createIndex(this.auxPath + '/calendar.txt', calendar, 'service_id');
146+
if (deduce) {
145147
cdp = this.processCalendarDates(this.auxPath + '/calendar_dates.txt', calendarDates);
146148
}
147149

@@ -153,9 +155,9 @@ class GtfsIndex {
153155
"trips": trips_index,
154156
"stops": stops_index,
155157
"stop_times": stop_times_index,
158+
"calendar": calendar_index,
156159
"tripsByRoute": tripsByRoute,
157160
"firstStops": firstStops,
158-
"calendar": calendar,
159161
"calendarDates": calendarDates
160162
};
161163
}

lib/Gtfsrt2LC.js

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const zlib = require('zlib');
77
const gtfsrt = require('gtfs-realtime-bindings').transit_realtime;
88
const JSONStream = require('JSONStream');
99
const N3 = require('n3');
10-
const { format, addHours, addMinutes, addSeconds } = require('date-fns');
10+
const { format, addHours, addMinutes, addSeconds, addDays } = require('date-fns');
1111
const Connections2JSONLD = require('./Connections2JSONLD');
1212
const Connections2CSV = require('./Connections2CSV');
1313
const Connections2Triples = require('./Connections2Triples');
@@ -40,7 +40,7 @@ class Gtfsrt2LC {
4040
"routeLabel": "routes.route_long_name.replace(/\\s/gi, '');",
4141
"tripLabel": "routes.route_short_name + routes.route_id;",
4242
"tripStartTime": "format(trips.startTime, \"yyyyMMdd'T'HHmm\");"
43-
}
43+
}
4444
};
4545
}
4646

@@ -79,8 +79,7 @@ class Gtfsrt2LC {
7979
if (entity.tripUpdate) {
8080
let tripUpdate = entity.tripUpdate;
8181
let tripId = tripUpdate.trip.tripId;
82-
let startDate = tripUpdate.trip.startDate;
83-
let serviceDay = new Date(startDate.substr(0, 4), parseInt(startDate.substr(4, 2)) - 1, startDate.substr(6, 2));
82+
8483
const timestamp = tripUpdate.timestamp || this.jsonData.header.timestamp;
8584

8685
// Check if tripId is directly provided or has to be found from GTFS source,
@@ -89,11 +88,6 @@ class Gtfsrt2LC {
8988
let deduced = await this.deduceTripId(tripUpdate.trip.routeId, tripUpdate.trip.startTime,
9089
tripUpdate.trip.startDate, tripUpdate.trip.directionId);
9190
tripId = deduced ? deduced['trip_id'] : null;
92-
// Correct startTime by adding 24 hours (error noticed for HSL Helsinki GTFS-RT)
93-
if (deduced && deduced['startTime']) {
94-
tripUpdate.trip.startTime = deduced['startTime'];
95-
this.correctTimes(tripUpdate);
96-
}
9791
}
9892

9993
let r = null;
@@ -115,11 +109,35 @@ class Gtfsrt2LC {
115109
return;
116110
}
117111

112+
// Figure service date and trip start time
113+
let serviceDay = null;
118114
let tripStartTime = null;
119-
if (tripUpdate.trip.startTime) {
120-
tripStartTime = this.addDuration(serviceDay, this.parseGTFSDuration(tripUpdate.trip.startTime));
115+
116+
if (tripUpdate.trip.startDate) {
117+
const rawStartDate = tripUpdate.trip.startDate;
118+
serviceDay = new Date(
119+
rawStartDate.substr(0, 4),
120+
parseInt(rawStartDate.substr(4, 2)) - 1,
121+
rawStartDate.substr(6, 2)
122+
);
123+
if (tripUpdate.trip.startTime) {
124+
tripStartTime = this.addDuration(serviceDay, this.parseGTFSDuration(tripUpdate.trip.startTime));
125+
} else {
126+
// Extract from static data
127+
tripStartTime = this.addDuration(
128+
serviceDay,
129+
this.parseGTFSDuration((await this.getStopTimes(tripId))[0]['departure_time'])
130+
);
131+
}
121132
} else {
122-
tripStartTime = this.addDuration(serviceDay, this.parseGTFSDuration((await this.getStopTimes(tripId))[0]['departure_time']));
133+
// Extract from static data
134+
const serviceObj = this.calendar.get(t['service_id']);
135+
const depTime = (await this.getStopTimes(tripId))[0]['departure_time'];
136+
serviceDay = this.findTripStartDate(depTime, serviceObj);
137+
tripStartTime = this.addDuration(
138+
serviceDay,
139+
this.parseGTFSDuration((await this.getStopTimes(tripId))[0]['departure_time'])
140+
);
123141
}
124142

125143
// Check if the trip has been canceled or not
@@ -179,7 +197,7 @@ class Gtfsrt2LC {
179197

180198
// Add start time to trip object
181199
t.startTime = tripStartTime;
182-
200+
183201
// Build JSON Connection
184202
let json_connection = {
185203
type,
@@ -342,7 +360,7 @@ class Gtfsrt2LC {
342360
// Find the trip that runs on the same day
343361
let today = new Date(startDate.substring(0, 4), parseInt(startDate.substring(4, 6)) - 1, startDate.substring(6, 8));
344362
for (let i = 0; i < trips.length; i++) {
345-
const service = this.calendar.get(trips[i]['service_id']);
363+
const service = await this.calendar.get(trips[i]['service_id']);
346364
const exceptions = this.calendarDates.get(trips[i]['service_id']) || {};
347365
const minDate = new Date(service['start_date'].substring(0, 4), parseInt(service['start_date'].substring(4, 6)) - 1, service['start_date'].substring(6, 8));
348366
const maxDate = new Date(service['end_date'].substring(0, 4), parseInt(service['end_date'].substring(4, 6)) - 1, service['end_date'].substring(6, 8));
@@ -370,12 +388,34 @@ class Gtfsrt2LC {
370388
}
371389
}
372390

373-
correctTimes(tripUpdate) {
374-
for (let i = 0; i < tripUpdate.stopTimeUpdate.length; i++) {
375-
let dep = parseInt(tripUpdate.stopTimeUpdate[i].departure.time) + 86400;
376-
let arr = parseInt(tripUpdate.stopTimeUpdate[i].arrival.time) + 86400;
377-
tripUpdate.stopTimeUpdate[i].departure.time = dep;
378-
tripUpdate.stopTimeUpdate[i].arrival.time = arr;
391+
findTripStartDate(depTime, service) {
392+
const now = new Date();
393+
const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
394+
const today = format(now, 'EEEE').toLowerCase();
395+
const tomorrow = days[days.indexOf(today) > 5 ? 0 : days.indexOf(today) + 1];
396+
const yesterday = days[days.indexOf(today) < 1 ? 6 : days.indexOf(today) - 1];
397+
398+
const todayServiceDate = this.addDuration(
399+
new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()),
400+
this.parseGTFSDuration(depTime)
401+
);
402+
const tomorrowServiceDate = addDays(todayServiceDate, 1);
403+
const yesterdayServiceDate = addDays(todayServiceDate, -1);
404+
405+
const todayDistance = service[today] === '1' ? Math.abs(now - todayServiceDate) : Number.POSITIVE_INFINITY;
406+
const tomorrowDistance = service[tomorrow] === '1' ? Math.abs(now - tomorrowServiceDate) : Number.POSITIVE_INFINITY;
407+
const yesterdayDistance = service[yesterday] === '1' ? Math.abs(now - yesterdayServiceDate) : Number.POSITIVE_INFINITY;
408+
409+
if(todayDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
410+
return todayServiceDate.setUTCHours(0, 0, 0 ,0);
411+
}
412+
413+
if(tomorrowDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
414+
return tomorrowServiceDate.setUTCHours(0, 0, 0 ,0);
415+
}
416+
417+
if(yesterdayDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
418+
return yesterdayServiceDate.setUTCHours(0, 0, 0 ,0);
379419
}
380420
}
381421

@@ -785,6 +825,10 @@ class Gtfsrt2LC {
785825
return this._stops;
786826
}
787827

828+
set stops(stops) {
829+
this._stops = stops;
830+
}
831+
788832
get stop_times() {
789833
return this._stop_times;
790834
}

package-lock.json

Lines changed: 52 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gtfsrt2lc",
3-
"version": "2.0.5",
3+
"version": "2.1.1",
44
"description": "Converts the GTFS-RT to Linked Connections",
55
"main": "./Gtfsrt2LC.js",
66
"bin": {

test/data/bustang.gtfs.zip

14.2 KB
Binary file not shown.

test/data/bustang.pb

1.11 KB
Binary file not shown.

0 commit comments

Comments
 (0)