From 120ad4ec727da0fbf698def8c850cc35fcb6d317 Mon Sep 17 00:00:00 2001 From: brechtvdv Date: Fri, 11 Sep 2015 13:04:28 +0200 Subject: [PATCH] Fix #3. Arrivals possible between time offset. --- lib/arrdep2connections.js | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/arrdep2connections.js b/lib/arrdep2connections.js index fc1808b..aea4db3 100644 --- a/lib/arrdep2connections.js +++ b/lib/arrdep2connections.js @@ -41,18 +41,33 @@ ArrDep2Connections.prototype._transform = function (departure, encoding, done) { departure["lc:departureTime"] = new Date(this._normalizeISO8601(departure["date"] + 'T' + departure["gtfs:departureTime"])); var self = this; this._arrivalsQueueIndex = 0; + this._arrivalTimeOffsetKey = 'minutes'; + this._arrivalTimeOffsetUnits = 120; // amount of time that arrivals are possible after a departure + this._maxArrivalTime = null; // holds maximum arrivalTime that is possible for a departure this._getNextArrival(function (arrival) { if (arrival) { var processNextArrival = function () { self._getNextArrival(function (nextArrival) { if (nextArrival) { - //TODO: check if gtfs:stopSequence and maxStopSequence is set... otherwise, ignore this feature? - if (departure["gtfs:stopSequence"] != departure["maxStopSequence"]) { + if (self._maxArrivalTime != null) { + // Is arrival still possible + if (nextArrival["lc:arrivalTime"].getTime() <= self._maxArrivalTime.getTime()) { + setImmediate(function () { + self._processArrival(departure, nextArrival, processNextArrival, done); + }); + } else { + // No possible arrival found + this._arrivalsQueueIndex = 0; // Turn back index to process arrivals again for next departure + this._maxArrivalTime = null; // reset + done(); // next departure + } + } else if (departure["gtfs:stopSequence"] != departure["maxStopSequence"]) { setImmediate(function () { - debugger; self._processArrival(departure, nextArrival, processNextArrival, done); }); } else { + this._arrivalsQueueIndex = 0; // Turn back index to process arrivals again for next departure + this._maxArrivalTime = null; // reset done(); // next departure } } else { @@ -60,6 +75,14 @@ ArrDep2Connections.prototype._transform = function (departure, encoding, done) { } }); }; + // Is stopSequence feature available to know if end of trip is reached? + if (typeof departure["gtfs:stopSequence"] === "undefined" || typeof departure["maxStopSequence"] === "undefined") { + // Use time offset of arrivalTimes that are possible + this._maxArrivalTime = moment(departure["lc:departureTime"]).add(self._arrivalTimeOffsetUnits, self._arrivalTimeOffsetKey).toDate(); + } else { + this._maxArrivalTime = null; + } + //we can call nextArrival if no connections have been found, or done when a connection has been found self._processArrival(departure, arrival, processNextArrival, done); } else { @@ -139,16 +162,15 @@ ArrDep2Connections.prototype._processArrival = function (departure, arrival, nex } this._arrivalsQueueIndex = 0; next(); - } else if (departure["gtfs:trip"] === arrival["gtfs:trip"] && parseInt(departure["gtfs:stopSequence"])+1 === parseInt(arrival["gtfs:stopSequence"])) { - //TODO: what if stopSequence is not set? Can we still calculate arrivals and departures? - //first one to encounter each other: it's a match! + } else if (departure["gtfs:trip"] === arrival["gtfs:trip"]) { + // first one to encounter each other: it's a match! var connection = this._createConnection(arrival, departure); + this._arrivalsQueueIndex = 0; this.push(connection); done(); } else { //arrival is part of the next one part of another trip. next(); - //done(); } }