Skip to content

Commit

Permalink
Merge pull request #159 from naviqore/158-bug-gtfs-stops-with-no-depa…
Browse files Browse the repository at this point in the history
…rtures

FIX: 158 - Ensure GTFS and RAPTOR router have the same stop set
  • Loading branch information
clukas1 authored Jan 14, 2025
2 parents 56f2cc4 + 23a4e2d commit df3498c
Show file tree
Hide file tree
Showing 22 changed files with 838 additions and 656 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ public List<StopConnection> getIsolines(@RequestParam(required = false) String s
// determine routing case and get isolines
try {
if (sourceStop != null) {
return map(service.getIsoLines(sourceStop, dateTime, map(timeType), config), timeType,
return map(service.getIsolines(sourceStop, dateTime, map(timeType), config), timeType,
returnConnections);
} else {
return map(service.getIsoLines(sourceCoordinate, dateTime, map(timeType), config), timeType,
return map(service.getIsolines(sourceCoordinate, dateTime, map(timeType), config), timeType,
returnConnections);
}
} catch (ConnectionRoutingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ public List<Connection> getConnections(GeoCoordinate source, Stop target, LocalD
}

@Override
public Map<Stop, Connection> getIsoLines(GeoCoordinate source, LocalDateTime time, TimeType timeType,
public Map<Stop, Connection> getIsolines(GeoCoordinate source, LocalDateTime time, TimeType timeType,
ConnectionQueryConfig config) throws ConnectionRoutingException {
return delegate.getIsoLines(source, time, timeType, config);
return delegate.getIsolines(source, time, timeType, config);
}

@Override
public Map<Stop, Connection> getIsoLines(Stop source, LocalDateTime time, TimeType timeType,
public Map<Stop, Connection> getIsolines(Stop source, LocalDateTime time, TimeType timeType,
ConnectionQueryConfig config) throws ConnectionRoutingException {
return delegate.getIsoLines(source, time, timeType, config);
return delegate.getIsolines(source, time, timeType, config);
}

@Override
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/ch/naviqore/raptor/RaptorAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public interface RaptorAlgorithm {
* @param config Query configuration
* @return a list of pareto-optimal earliest arrival connections sorted in ascending order by the number of route
* legs (rounds)
* @throws InvalidStopException if departure or arrival stops are invalid
* @throws InvalidTimeException if departure or arrival times are invalid
* @throws IllegalArgumentException for other argument related errors
*/
List<Connection> routeEarliestArrival(Map<String, LocalDateTime> departureStops, Map<String, Integer> arrivalStops,
QueryConfig config);
Expand All @@ -26,6 +29,9 @@ List<Connection> routeEarliestArrival(Map<String, LocalDateTime> departureStops,
* @param config Query configuration
* @return a list of pareto-optimal latest departure connections sorted in ascending order by the number of route
* legs (rounds)
* @throws InvalidStopException if departure or arrival stops are invalid
* @throws InvalidTimeException if departure or arrival times are invalid
* @throws IllegalArgumentException for other argument related errors
*/
List<Connection> routeLatestDeparture(Map<String, Integer> departureStops, Map<String, LocalDateTime> arrivalStops,
QueryConfig config);
Expand All @@ -38,8 +44,23 @@ List<Connection> routeLatestDeparture(Map<String, Integer> departureStops, Map<S
* @param timeType is the type of time to route for (arrival or departure)
* @param config is the query configuration
* @return the earliest arrival (timeType=departure) or latest departure (timeType=arrival) connection for each stop
* @throws InvalidStopException if source stop is invalid
* @throws InvalidTimeException if source time is invalid
* @throws IllegalArgumentException for other argument related errors
*/
Map<String, Connection> routeIsolines(Map<String, LocalDateTime> sourceStops, TimeType timeType,
QueryConfig config);

class InvalidStopException extends IllegalArgumentException {
public InvalidStopException(String message) {
super(message);
}
}

class InvalidTimeException extends IllegalArgumentException {
public InvalidTimeException(String message) {
super(message);
}
}

}
16 changes: 8 additions & 8 deletions src/main/java/ch/naviqore/raptor/router/RaptorRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,24 @@ private static class InputValidator {

private static void checkNonNullOrEmptyStops(Map<String, ?> stops, String labelSource) {
if (stops == null) {
throw new IllegalArgumentException(String.format("%s stops must not be null.", labelSource));
throw new InvalidStopException(String.format("%s stops must not be null.", labelSource));
}
if (stops.isEmpty()) {
throw new IllegalArgumentException(String.format("%s stops must not be empty.", labelSource));
throw new InvalidStopException(String.format("%s stops must not be empty.", labelSource));
}
}

private static void validateSourceStopTimes(Map<String, LocalDateTime> sourceStops) {
// check that no null values are present
if (sourceStops.values().stream().anyMatch(Objects::isNull)) {
throw new IllegalArgumentException("Source stop times must not be null.");
throw new InvalidTimeException("Source stop times must not be null.");
}

// get min and max values
LocalDateTime min = sourceStops.values().stream().min(LocalDateTime::compareTo).orElseThrow();
LocalDateTime max = sourceStops.values().stream().max(LocalDateTime::compareTo).orElseThrow();
if (Duration.between(min, max).getSeconds() > MAX_DIFFERENCE_IN_SOURCE_STOP_TIMES) {
throw new IllegalArgumentException("Difference between source stop times must be less than 24 hours.");
throw new InvalidTimeException("Difference between source stop times must be less than 24 hours.");
}
}

Expand All @@ -176,7 +176,7 @@ private static void validateStopPermutations(Map<String, Integer> sourceStops,

// ensure departure and arrival stops are not the same
if (!Collections.disjoint(sourceStops.keySet(), targetStops.keySet())) {
throw new IllegalArgumentException("Source and target stop IDs must not be the same.");
throw new InvalidStopException("Source and target stop IDs must not be the same.");
}
}

Expand All @@ -191,14 +191,14 @@ private static void validateWalkingTimeToTarget(int walkingDurationToTarget) {
* Validate the stops provided in the query. This method will check that the map of stop ids and their
* corresponding departure / walk to target times are valid. This is done by checking if the map is not empty
* and then checking each entry if the stop id is present in the lookup. If not it is removed from the query. If
* no valid stops are found an IllegalArgumentException is thrown.
* no valid stops are found an InvalidStopException is thrown.
*
* @param stops the stops to validate.
* @return a map of valid stop IDs and their corresponding departure / walk to target times.
*/
private Map<Integer, Integer> validateStopsAndGetIndices(Map<String, Integer> stops) {
if (stops.isEmpty()) {
throw new IllegalArgumentException("At least one stop ID must be provided.");
throw new InvalidStopException("At least one stop ID must be provided.");
}

// loop over all stop pairs and check if stop exists in raptor, then validate departure time
Expand All @@ -215,7 +215,7 @@ private Map<Integer, Integer> validateStopsAndGetIndices(Map<String, Integer> st
}

if (validStopIds.isEmpty()) {
throw new IllegalArgumentException("No valid stops provided.");
throw new InvalidStopException("No valid stops provided.");
}

return validStopIds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ List<Connection> getConnections(Stop source, GeoCoordinate target, LocalDateTime
* @param config additional configuration for the query
* @return a map of stops to the shortest possible connection to each stop from the departure location
*/
Map<Stop, Connection> getIsoLines(GeoCoordinate source, LocalDateTime time, TimeType timeType,
Map<Stop, Connection> getIsolines(GeoCoordinate source, LocalDateTime time, TimeType timeType,
ConnectionQueryConfig config) throws ConnectionRoutingException;

/**
Expand All @@ -92,6 +92,6 @@ Map<Stop, Connection> getIsoLines(GeoCoordinate source, LocalDateTime time, Time
* @param config additional configuration for the query
* @return a map of stops to the shortest possible connection to each stop from the departure location
*/
Map<Stop, Connection> getIsoLines(Stop source, LocalDateTime time, TimeType timeType,
Map<Stop, Connection> getIsolines(Stop source, LocalDateTime time, TimeType timeType,
ConnectionQueryConfig config) throws ConnectionRoutingException;
}
Loading

0 comments on commit df3498c

Please sign in to comment.