diff --git a/src/main/java/org/opentripplanner/gtfs/GtfsLibrary.java b/src/main/java/org/opentripplanner/gtfs/GtfsLibrary.java index cdef90b8bc7..70c77bcbf92 100644 --- a/src/main/java/org/opentripplanner/gtfs/GtfsLibrary.java +++ b/src/main/java/org/opentripplanner/gtfs/GtfsLibrary.java @@ -95,8 +95,7 @@ public static String getRouteName(Route route) { return route.getLongName(); } - public static TraverseMode getTraverseMode(Route route) { - int routeType = route.getType(); + public static TraverseMode getTraverseMode(int routeType) { /* TPEG Extension https://groups.google.com/d/msg/gtfs-changes/keT5rTPS7Y0/71uMz2l6ke0J */ if (routeType >= 100 && routeType < 200){ // Railway Service return TraverseMode.RAIL; @@ -130,27 +129,33 @@ public static TraverseMode getTraverseMode(Route route) { } /* Original GTFS route types. Should these be checked before TPEG types? */ switch (routeType) { - case 0: - return TraverseMode.TRAM; - case 1: - return TraverseMode.SUBWAY; - case 2: - return TraverseMode.RAIL; - case 3: - return TraverseMode.BUS; - case 4: - return TraverseMode.FERRY; - case 5: - return TraverseMode.CABLE_CAR; - case 6: - return TraverseMode.GONDOLA; - case 7: - return TraverseMode.FUNICULAR; - default: - throw new IllegalArgumentException("unknown gtfs route type " + routeType); + case 0: + return TraverseMode.TRAM; + case 1: + return TraverseMode.SUBWAY; + case 2: + return TraverseMode.RAIL; + case 3: + return TraverseMode.BUS; + case 4: + return TraverseMode.FERRY; + case 5: + return TraverseMode.CABLE_CAR; + case 6: + return TraverseMode.GONDOLA; + case 7: + return TraverseMode.FUNICULAR; + default: + throw new IllegalArgumentException("unknown gtfs route type " + routeType); } } + public static TraverseMode getTraverseMode(Route route) { + int routeType = route.getType(); + + return getTraverseMode(routeType); + } + private static class GtfsContextImpl implements GtfsContext { private GtfsFeedId _feedId; diff --git a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java index ff382b57ab5..83653eaa184 100644 --- a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java +++ b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java @@ -14,6 +14,7 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -1253,6 +1254,27 @@ public IndexGraphQLSchema(GraphIndex index) { .description("The raw GTFS route type used by routes which pass through this stop. For the list of possible values, see: https://developers.google.com/transit/gtfs/reference/#routestxt and https://developers.google.com/transit/gtfs/reference/extended-route-types") .type(Scalars.GraphQLInt) .build()) + .field(GraphQLFieldDefinition.newFieldDefinition() + .name("vehicleMode") + .description("Transport mode (e.g. `BUS`) used by routes which pass through this stop or `null` if mode cannot be determined, e.g. in case no routes pass through the stop. \n Note that also other types of vehicles may use the stop, e.g. tram replacement buses might use stops which have `TRAM` as their mode.") + .type(modeEnum) + .dataFetcher(environment -> { + try { + return GtfsLibrary.getTraverseMode(((Stop)environment.getSource()).getVehicleType()); + } catch (IllegalArgumentException iae) { + //If 'vehicleType' is not specified, guess vehicle mode from list of patterns + return index.patternsForStop.get(environment.getSource()) + .stream() + .map(pattern -> pattern.mode) + .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) + .entrySet() + .stream() + .max(Comparator.comparing(Map.Entry::getValue)) + .map(e -> e.getKey()) + .orElse(null); + } + }) + .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("platformCode") .description("Identifier of the platform, usually a number. This value is only present for stops that are part of a station")