Skip to content

Commit

Permalink
Merge pull request #71 from linkedconnections/development
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
julianrojas87 committed Mar 6, 2023
2 parents 163d103 + 1ae1058 commit 8d58e61
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- name: Checkout
Expand Down
12 changes: 12 additions & 0 deletions dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
# Check for updates once a week
interval: "weekly"
# Raise pull requests for version updates against the `development` branch
target-branch: "development"
# Labels on pull requests for security and version updates
labels:
- "npm dependencies"
12 changes: 7 additions & 5 deletions lib/GtfsIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class GtfsIndex {
let stop_times_index = null;
let tripsByRoute = null;
let firstStops = null;
let calendar = null;
let calendar_index = null;
let calendarDates = null;
let tp = null;
let stp = null;
Expand All @@ -84,7 +84,6 @@ class GtfsIndex {
if (deduce) {
tripsByRoute = new Map();
firstStops = new Map();
calendar = new Map();
calendarDates = new Map();
}

Expand All @@ -96,6 +95,8 @@ class GtfsIndex {
routes_index = new Map();
trips_index = new Map();
stop_times_index = new Map();
calendar_index = new Map();


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

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

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

if (deduce) {
cp = this.createIndex(this.auxPath + '/calendar.txt', calendar, 'service_id');
if (deduce) {
cdp = this.processCalendarDates(this.auxPath + '/calendar_dates.txt', calendarDates);
}

Expand All @@ -153,9 +155,9 @@ class GtfsIndex {
"trips": trips_index,
"stops": stops_index,
"stop_times": stop_times_index,
"calendar": calendar_index,
"tripsByRoute": tripsByRoute,
"firstStops": firstStops,
"calendar": calendar,
"calendarDates": calendarDates
};
}
Expand Down
84 changes: 64 additions & 20 deletions lib/Gtfsrt2LC.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const zlib = require('zlib');
const gtfsrt = require('gtfs-realtime-bindings').transit_realtime;
const JSONStream = require('JSONStream');
const N3 = require('n3');
const { format, addHours, addMinutes, addSeconds } = require('date-fns');
const { format, addHours, addMinutes, addSeconds, addDays } = require('date-fns');
const Connections2JSONLD = require('./Connections2JSONLD');
const Connections2CSV = require('./Connections2CSV');
const Connections2Triples = require('./Connections2Triples');
Expand Down Expand Up @@ -40,7 +40,7 @@ class Gtfsrt2LC {
"routeLabel": "routes.route_long_name.replace(/\\s/gi, '');",
"tripLabel": "routes.route_short_name + routes.route_id;",
"tripStartTime": "format(trips.startTime, \"yyyyMMdd'T'HHmm\");"
}
}
};
}

Expand Down Expand Up @@ -79,8 +79,7 @@ class Gtfsrt2LC {
if (entity.tripUpdate) {
let tripUpdate = entity.tripUpdate;
let tripId = tripUpdate.trip.tripId;
let startDate = tripUpdate.trip.startDate;
let serviceDay = new Date(startDate.substr(0, 4), parseInt(startDate.substr(4, 2)) - 1, startDate.substr(6, 2));

const timestamp = tripUpdate.timestamp || this.jsonData.header.timestamp;

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

let r = null;
Expand All @@ -115,11 +109,35 @@ class Gtfsrt2LC {
return;
}

// Figure service date and trip start time
let serviceDay = null;
let tripStartTime = null;
if (tripUpdate.trip.startTime) {
tripStartTime = this.addDuration(serviceDay, this.parseGTFSDuration(tripUpdate.trip.startTime));

if (tripUpdate.trip.startDate) {
const rawStartDate = tripUpdate.trip.startDate;
serviceDay = new Date(
rawStartDate.substr(0, 4),
parseInt(rawStartDate.substr(4, 2)) - 1,
rawStartDate.substr(6, 2)
);
if (tripUpdate.trip.startTime) {
tripStartTime = this.addDuration(serviceDay, this.parseGTFSDuration(tripUpdate.trip.startTime));
} else {
// Extract from static data
tripStartTime = this.addDuration(
serviceDay,
this.parseGTFSDuration((await this.getStopTimes(tripId))[0]['departure_time'])
);
}
} else {
tripStartTime = this.addDuration(serviceDay, this.parseGTFSDuration((await this.getStopTimes(tripId))[0]['departure_time']));
// Extract from static data
const serviceObj = this.calendar.get(t['service_id']);
const depTime = (await this.getStopTimes(tripId))[0]['departure_time'];
serviceDay = this.findTripStartDate(depTime, serviceObj);
tripStartTime = this.addDuration(
serviceDay,
this.parseGTFSDuration((await this.getStopTimes(tripId))[0]['departure_time'])
);
}

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

// Add start time to trip object
t.startTime = tripStartTime;

// Build JSON Connection
let json_connection = {
type,
Expand Down Expand Up @@ -342,7 +360,7 @@ class Gtfsrt2LC {
// Find the trip that runs on the same day
let today = new Date(startDate.substring(0, 4), parseInt(startDate.substring(4, 6)) - 1, startDate.substring(6, 8));
for (let i = 0; i < trips.length; i++) {
const service = this.calendar.get(trips[i]['service_id']);
const service = await this.calendar.get(trips[i]['service_id']);
const exceptions = this.calendarDates.get(trips[i]['service_id']) || {};
const minDate = new Date(service['start_date'].substring(0, 4), parseInt(service['start_date'].substring(4, 6)) - 1, service['start_date'].substring(6, 8));
const maxDate = new Date(service['end_date'].substring(0, 4), parseInt(service['end_date'].substring(4, 6)) - 1, service['end_date'].substring(6, 8));
Expand Down Expand Up @@ -370,12 +388,34 @@ class Gtfsrt2LC {
}
}

correctTimes(tripUpdate) {
for (let i = 0; i < tripUpdate.stopTimeUpdate.length; i++) {
let dep = parseInt(tripUpdate.stopTimeUpdate[i].departure.time) + 86400;
let arr = parseInt(tripUpdate.stopTimeUpdate[i].arrival.time) + 86400;
tripUpdate.stopTimeUpdate[i].departure.time = dep;
tripUpdate.stopTimeUpdate[i].arrival.time = arr;
findTripStartDate(depTime, service) {
const now = new Date();
const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
const today = format(now, 'EEEE').toLowerCase();
const tomorrow = days[days.indexOf(today) > 5 ? 0 : days.indexOf(today) + 1];
const yesterday = days[days.indexOf(today) < 1 ? 6 : days.indexOf(today) - 1];

const todayServiceDate = this.addDuration(
new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()),
this.parseGTFSDuration(depTime)
);
const tomorrowServiceDate = addDays(todayServiceDate, 1);
const yesterdayServiceDate = addDays(todayServiceDate, -1);

const todayDistance = service[today] === '1' ? Math.abs(now - todayServiceDate) : Number.POSITIVE_INFINITY;
const tomorrowDistance = service[tomorrow] === '1' ? Math.abs(now - tomorrowServiceDate) : Number.POSITIVE_INFINITY;
const yesterdayDistance = service[yesterday] === '1' ? Math.abs(now - yesterdayServiceDate) : Number.POSITIVE_INFINITY;

if(todayDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
return todayServiceDate.setUTCHours(0, 0, 0 ,0);
}

if(tomorrowDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
return tomorrowServiceDate.setUTCHours(0, 0, 0 ,0);
}

if(yesterdayDistance === Math.min(todayDistance, tomorrowDistance, yesterdayDistance)) {
return yesterdayServiceDate.setUTCHours(0, 0, 0 ,0);
}
}

Expand Down Expand Up @@ -785,6 +825,10 @@ class Gtfsrt2LC {
return this._stops;
}

set stops(stops) {
this._stops = stops;
}

get stop_times() {
return this._stop_times;
}
Expand Down
66 changes: 52 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gtfsrt2lc",
"version": "2.0.5",
"version": "2.1.1",
"description": "Converts the GTFS-RT to Linked Connections",
"main": "./Gtfsrt2LC.js",
"bin": {
Expand Down
Binary file added test/data/bustang.gtfs.zip
Binary file not shown.
Binary file added test/data/bustang.pb
Binary file not shown.
Loading

0 comments on commit 8d58e61

Please sign in to comment.