From da148e10b0bd4d77efb853b934e757727b101b85 Mon Sep 17 00:00:00 2001 From: Jaakko Malkki Date: Thu, 16 Aug 2018 09:25:56 +0300 Subject: [PATCH 1/5] Add method which converts GTFS route type to TraverseMode --- .../org/opentripplanner/gtfs/GtfsLibrary.java | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) 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; From a9661b2c63b3be99eb5681b706eecdcec5f647fb Mon Sep 17 00:00:00 2001 From: Jaakko Malkki Date: Thu, 16 Aug 2018 10:45:43 +0300 Subject: [PATCH 2/5] Add 'vehicleMode' field to Stop --- .../opentripplanner/index/IndexGraphQLSchema.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java index ff382b57ab5..5c8c29367c1 100644 --- a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java +++ b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java @@ -1253,6 +1253,18 @@ 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. ") + .type(modeEnum) + .dataFetcher(environment -> { + try { + return GtfsLibrary.getTraverseMode(((Stop)environment.getSource()).getVehicleType()); + } catch (IllegalArgumentException iae) { //Handle unknown vehicle types + return 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") From f11b4ce21380dec8b1604ba3d3d1c751e6cb270c Mon Sep 17 00:00:00 2001 From: Jaakko Malkki Date: Thu, 16 Aug 2018 11:17:42 +0300 Subject: [PATCH 3/5] Add documentation --- src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java index 5c8c29367c1..b8dc833a196 100644 --- a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java +++ b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java @@ -1255,7 +1255,7 @@ public IndexGraphQLSchema(GraphIndex index) { .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("vehicleMode") - .description("Transport mode (e.g. `BUS`) used by routes which pass through this stop. ") + .description("Transport mode (e.g. `BUS`) used by routes which pass through this stop. \n Note that also other types of vehicles may use this stop, e.g. tram-replacement buses.") .type(modeEnum) .dataFetcher(environment -> { try { From 8b778deac82546df71d45870ea2fcd3a1fb734cc Mon Sep 17 00:00:00 2001 From: Jaakko Malkki Date: Thu, 16 Aug 2018 13:38:14 +0300 Subject: [PATCH 4/5] Guess vehicle mode from list of patterns --- .../opentripplanner/index/IndexGraphQLSchema.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java index b8dc833a196..38b030475b8 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; @@ -1260,8 +1261,17 @@ public IndexGraphQLSchema(GraphIndex index) { .dataFetcher(environment -> { try { return GtfsLibrary.getTraverseMode(((Stop)environment.getSource()).getVehicleType()); - } catch (IllegalArgumentException iae) { //Handle unknown vehicle types - return null; + } 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()) From 19232b62786f50629cc48096f6bd9323218834b5 Mon Sep 17 00:00:00 2001 From: Jaakko Malkki Date: Thu, 16 Aug 2018 13:44:50 +0300 Subject: [PATCH 5/5] Update description --- src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java index 38b030475b8..83653eaa184 100644 --- a/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java +++ b/src/main/java/org/opentripplanner/index/IndexGraphQLSchema.java @@ -1256,7 +1256,7 @@ public IndexGraphQLSchema(GraphIndex index) { .build()) .field(GraphQLFieldDefinition.newFieldDefinition() .name("vehicleMode") - .description("Transport mode (e.g. `BUS`) used by routes which pass through this stop. \n Note that also other types of vehicles may use this stop, e.g. tram-replacement buses.") + .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 {