From 36f3043f11e761ea3a738f61174ec4ab5e363c31 Mon Sep 17 00:00:00 2001 From: "philip.cline" Date: Mon, 20 Nov 2023 15:59:08 -0500 Subject: [PATCH] refactor(st interpolation): Update calculation method, respond to comments --- .../conveyal/gtfs/loader/JdbcTableWriter.java | 25 ++++++++++++++++--- .../gtfs/loader/JDBCTableWriterTest.java | 12 +++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/conveyal/gtfs/loader/JdbcTableWriter.java b/src/main/java/com/conveyal/gtfs/loader/JdbcTableWriter.java index a8d60705..01d8d2d8 100644 --- a/src/main/java/com/conveyal/gtfs/loader/JdbcTableWriter.java +++ b/src/main/java/com/conveyal/gtfs/loader/JdbcTableWriter.java @@ -256,6 +256,13 @@ 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. @@ -817,6 +824,7 @@ private int updateStopTimesForPatternStops(List patternStops, boole 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) { @@ -832,10 +840,19 @@ private int updateStopTimesForPatternStops(List patternStops, boole 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. + 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(); diff --git a/src/test/java/com/conveyal/gtfs/loader/JDBCTableWriterTest.java b/src/test/java/com/conveyal/gtfs/loader/JDBCTableWriterTest.java index 1b1d4df7..44dab2b0 100644 --- a/src/test/java/com/conveyal/gtfs/loader/JDBCTableWriterTest.java +++ b/src/test/java/com/conveyal/gtfs/loader/JDBCTableWriterTest.java @@ -842,7 +842,15 @@ public void canCreateUpdateAndDeleteFrequencyTripForFrequencyPattern() throws IO )); } - private static String normalizeStopsForPattern(PatternStopDTO[] patternStops, int updatedStopSequence, boolean interpolateStopTimes, int initialTravelTime, int updatedTravelTime, int startTime, String patternId) throws SQLException, InvalidNamespaceException, IOException { + private static String normalizeStopsForPattern( + PatternStopDTO[] patternStops, + int updatedStopSequence, + boolean interpolateStopTimes, + int initialTravelTime, + int updatedTravelTime, + int startTime, + String patternId + ) throws SQLException, InvalidNamespaceException, IOException { final Table tripsTable = Table.TRIPS; PatternDTO pattern = createRouteAndPattern(newUUID(), @@ -875,7 +883,7 @@ private static String normalizeStopsForPattern(PatternStopDTO[] patternStops, in * Checks that {@link JdbcTableWriter#normalizeStopTimesForPattern(int, int, boolean)} can interpolate stop times between timepoints. */ @Test - public void canInterpolatePatternStopTimes() throws IOException, SQLException, InvalidNamespaceException { + private void canInterpolatePatternStopTimes() throws IOException, SQLException, InvalidNamespaceException { // Parameters are shared with canNormalizePatternStopTimes, but maintained for test flexibility. int startTime = 6 * 60 * 60; // 6AM int initialTravelTime = 60; // seconds