Skip to content

Commit

Permalink
Fix linkedconnections#3. Arrivals possible between time offset.
Browse files Browse the repository at this point in the history
  • Loading branch information
brechtvdv committed Sep 11, 2015
1 parent fc005d6 commit 120ad4e
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/arrdep2connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,48 @@ 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 {
console.error("no next arrival");
}
});
};
// 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 {
Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 120ad4e

Please sign in to comment.