-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Stop Time Interpolation #399
Changes from 6 commits
1e2adad
5f34e04
b585c9d
36ccb09
8a1fe2a
36f3043
dbd09ce
da9cc6b
5fd655d
a331d74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,13 +256,20 @@ public String update(Integer id, String json, boolean autoCommit) throws SQLExce | |
} | ||
} | ||
|
||
/** | ||
* Deprecated method to normalize stop times before stop time interpolation. Defaults to | ||
* false for interpolation. | ||
*/ | ||
public int normalizeStopTimesForPattern(int id, int beginWithSequence) throws SQLException { | ||
return normalizeStopTimesForPattern(id, beginWithSequence, false); | ||
} | ||
/** | ||
* For a given pattern id and starting stop sequence (inclusive), normalize all stop times to match the pattern | ||
* stops' travel times. | ||
* | ||
* @return number of stop times updated | ||
*/ | ||
public int normalizeStopTimesForPattern(int id, int beginWithSequence) throws SQLException { | ||
public int normalizeStopTimesForPattern(int id, int beginWithSequence, boolean interpolateStopTimes) throws SQLException { | ||
br648 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
JDBCTableReader<PatternStop> patternStops = new JDBCTableReader( | ||
Table.PATTERN_STOP, | ||
|
@@ -279,7 +286,7 @@ public int normalizeStopTimesForPattern(int id, int beginWithSequence) throws SQ | |
patternStopsToNormalize.add(patternStop); | ||
} | ||
} | ||
int stopTimesUpdated = updateStopTimesForPatternStops(patternStopsToNormalize); | ||
int stopTimesUpdated = updateStopTimesForPatternStops(patternStopsToNormalize, interpolateStopTimes); | ||
connection.commit(); | ||
return stopTimesUpdated; | ||
} catch (Exception e) { | ||
|
@@ -745,6 +752,33 @@ private int updateStopTimesForPatternStop(ObjectNode patternStop, int previousTr | |
return travelTime + dwellTime; | ||
} | ||
|
||
private int interpolateTimesFromTimepoints( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method comment to describe what this is doing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in dbd09ce |
||
PatternStop patternStop, | ||
List<PatternStop> timepoints, | ||
Integer timepointNumber, | ||
double previousShapeDistTraveled | ||
) { | ||
if (timepointNumber == 0) { | ||
throw new IllegalStateException("First pattern stop must be a timepoint to perform interpolation"); | ||
} else if (timepoints.size() == 1) { | ||
throw new IllegalStateException("Pattern must have more than one timepoint to perform interpolation"); | ||
} else if (timepointNumber >= timepoints.size()) { | ||
throw new IllegalStateException("Last stop must be a timepoint to perform interpolation"); | ||
} | ||
PatternStop nextTimepoint = timepoints.get(timepointNumber); | ||
PatternStop lastTimepoint = timepoints.get(timepointNumber-1); | ||
|
||
if (nextTimepoint == null) { | ||
throw new IllegalStateException("Stop time interpolation is not possible with null timepoints."); | ||
} else if (nextTimepoint.default_travel_time == Entity.INT_MISSING) { | ||
throw new IllegalStateException("All timepoints must have a default travel time specified."); | ||
} | ||
|
||
double timepointSpeed = (nextTimepoint.shape_dist_traveled - lastTimepoint.shape_dist_traveled) / nextTimepoint.default_travel_time; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I added checks for every pattern stop and both timepoints in dbd09ce |
||
int travelTime = (int) Math.round((patternStop.shape_dist_traveled - previousShapeDistTraveled) / timepointSpeed); | ||
return travelTime; | ||
} | ||
|
||
/** | ||
* Normalizes all stop times' arrivals and departures for an ordered set of pattern stops. This set can be the full | ||
* set of stops for a pattern or just a subset. Typical usage for this method would be to overwrite the arrival and | ||
|
@@ -755,8 +789,9 @@ private int updateStopTimesForPatternStop(ObjectNode patternStop, int previousTr | |
* | ||
* TODO? add param Set<String> serviceIdFilters service_id values to filter trips on | ||
*/ | ||
private int updateStopTimesForPatternStops(List<PatternStop> patternStops) throws SQLException { | ||
private int updateStopTimesForPatternStops(List<PatternStop> patternStops, boolean interpolateStopTimes) throws SQLException { | ||
PatternStop firstPatternStop = patternStops.iterator().next(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is best addressed by preventing the behaviour client side, although I'll defer to other reviewers if they'd like some additional checks |
||
List<PatternStop> timepoints = patternStops.stream().filter(ps -> ps.timepoint == 1).collect(Collectors.toList()); | ||
int firstStopSequence = firstPatternStop.stop_sequence; | ||
// Prepare SQL query to determine the time that should form the basis for adding the travel time values. | ||
int previousStopSequence = firstStopSequence > 0 ? firstStopSequence - 1 : 0; | ||
|
@@ -789,16 +824,35 @@ private int updateStopTimesForPatternStops(List<PatternStop> patternStops) throw | |
for (String tripId : timesForTripIds.keySet()) { | ||
// Initialize travel time with previous stop time value. | ||
int cumulativeTravelTime = timesForTripIds.get(tripId); | ||
int cumulativeInterpolatedTime = cumulativeTravelTime; | ||
int timepointNumber = 0; | ||
double previousShapeDistTraveled = 0; // Used for calculating timepoint speed for interpolation | ||
for (PatternStop patternStop : patternStops) { | ||
boolean isTimepoint = patternStop.timepoint == 1; | ||
if (isTimepoint) timepointNumber++; | ||
// Gather travel/dwell time for pattern stop (being sure to check for missing values). | ||
int travelTime = patternStop.default_travel_time == Entity.INT_MISSING ? 0 : patternStop.default_travel_time; | ||
if (!isTimepoint && interpolateStopTimes) { | ||
// Override travel time if we're interpolating between timepoints | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit. Full stop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in dbd09ce |
||
travelTime = interpolateTimesFromTimepoints(patternStop, timepoints, timepointNumber, previousShapeDistTraveled); | ||
} | ||
previousShapeDistTraveled += patternStop.shape_dist_traveled; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. likewise yes, I added a check here too dbd09ce |
||
int dwellTime = patternStop.default_dwell_time == Entity.INT_MISSING ? 0 : patternStop.default_dwell_time; | ||
int oneBasedIndex = 1; | ||
// Increase travel time by current pattern stop's travel and dwell times (and set values for update). | ||
cumulativeTravelTime += travelTime; | ||
updateStopTimeStatement.setInt(oneBasedIndex++, cumulativeTravelTime); | ||
cumulativeTravelTime += dwellTime; | ||
updateStopTimeStatement.setInt(oneBasedIndex++, cumulativeTravelTime); | ||
if (!isTimepoint && interpolateStopTimes) { | ||
// We don't want to increment the true cumulative travel time because that adjusts the timepoint | ||
// times later in the pattern. | ||
// TODO? We ignore dwell times in interpolation calculations right now. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODOs have a habit of not being done. Can dwell times be incorporated easily now? If not, explain why they are ignored and remove the TODO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added an explanation of why this is left out, and an explanation of how it could be implemented. Right now there's not much reason to since it doesn't fit the use case which is driving this feature. |
||
cumulativeInterpolatedTime += travelTime; | ||
updateStopTimeStatement.setInt(oneBasedIndex++, cumulativeInterpolatedTime); | ||
updateStopTimeStatement.setInt(oneBasedIndex++, cumulativeInterpolatedTime); | ||
} else { | ||
cumulativeTravelTime += travelTime; | ||
updateStopTimeStatement.setInt(oneBasedIndex++, cumulativeTravelTime); | ||
cumulativeTravelTime += dwellTime; | ||
updateStopTimeStatement.setInt(oneBasedIndex++, cumulativeTravelTime); | ||
} | ||
updateStopTimeStatement.setString(oneBasedIndex++, tripId); | ||
updateStopTimeStatement.setInt(oneBasedIndex++, patternStop.stop_sequence); | ||
stopTimesTracker.addBatch(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit. New line after closing bracket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in dbd09ce