Skip to content

Fares V2 #8

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

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/com/conveyal/gtfs/GTFSFeed.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public class GTFSFeed implements Cloneable, Closeable {
/* A place to store an event bus that is passed through constructor. */
public transient EventBus eventBus;

public final Map<String, Area> areas;
public final Map<String, StopArea> stop_areas;
public final Map<String, FareProduct> fare_products;
public final Map<String, FareMedia> fare_medias;
public final Map<String, TimeFrame> time_frames;
public final Map<String, FareLegRule> fare_leg_rules;
public final Map<String, FareTransferRule> fare_transfer_rules;
public final Map<String, Network> networks;
public final Map<String, RouteNetwork> route_networks;

/**
* The order in which we load the tables is important for two reasons.
* 1. We must load feed_info first so we know the feed ID before loading any other entities. This could be relaxed
Expand Down Expand Up @@ -177,6 +187,18 @@ else if (feedId == null || feedId.isEmpty()) {
new Trip.Loader(this).loadTable(zip);
new Frequency.Loader(this).loadTable(zip);
new StopTime.Loader(this).loadTable(zip); // comment out this line for quick testing using NL feed

// Fares v2.
new Area.Loader(this).loadTable(zip);
new StopArea.Loader(this).loadTable(zip);
new TimeFrame.Loader(this).loadTable(zip);
new Network.Loader(this).loadTable(zip);
new RouteNetwork.Loader(this).loadTable(zip);
new FareMedia.Loader(this).loadTable(zip);
new FareProduct.Loader(this).loadTable(zip);
new FareLegRule.Loader(this).loadTable(zip);
new FareTransferRule.Loader(this).loadTable(zip);

LOG.info("{} errors", errors.size());
for (GTFSError error : errors) {
LOG.info("{}", error);
Expand Down Expand Up @@ -219,6 +241,17 @@ public void toFile (String file) {
new StopTime.Writer(this).writeTable(zip);
new Pattern.Writer(this).writeTable(zip);

// Fares v2.
new Area.Writer(this).writeTable(zip);
new StopArea.Writer(this).writeTable(zip);
new TimeFrame.Writer(this).writeTable(zip);
new Network.Writer(this).writeTable(zip);
new RouteNetwork.Writer(this).writeTable(zip);
new FareMedia.Writer(this).writeTable(zip);
new FareProduct.Writer(this).writeTable(zip);
new FareLegRule.Writer(this).writeTable(zip);
new FareTransferRule.Writer(this).writeTable(zip);

zip.close();

LOG.info("GTFS file written");
Expand Down Expand Up @@ -608,8 +641,15 @@ private GTFSFeed (DB db) {
this.db = db;

agency = db.getTreeMap("agency");
areas = db.getTreeMap("area");
fare_leg_rules = db.getTreeMap("fare_leg_rules");
fare_medias = db.getTreeMap("fare_medias");
fare_products = db.getTreeMap("fare_products");
fare_transfer_rules = db.getTreeMap("fare_transfer_rules");
feedInfo = db.getTreeMap("feed_info");
networks = db.getTreeMap("networks");
routes = db.getTreeMap("routes");
route_networks = db.getTreeMap("route_networks");
trips = db.getTreeMap("trips");
stop_times = db.getTreeMap("stop_times");
frequencies = db.getTreeSet("frequencies");
Expand All @@ -618,6 +658,8 @@ private GTFSFeed (DB db) {
fares = db.getTreeMap("fares");
services = db.getTreeMap("services");
shape_points = db.getTreeMap("shape_points");
stop_areas = db.getTreeMap("stop_areas");
time_frames = db.getTreeMap("time_frames");
translations = db.getTreeMap("translations");
attributions = db.getTreeMap("attributions");

Expand Down
130 changes: 130 additions & 0 deletions src/main/java/com/conveyal/gtfs/graphql/GraphQLGtfsFaresV2Schema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.conveyal.gtfs.graphql;

import com.conveyal.gtfs.graphql.fetchers.MapFetcher;
import com.conveyal.gtfs.model.Area;
import com.conveyal.gtfs.model.FareLegRule;
import com.conveyal.gtfs.model.FareMedia;
import com.conveyal.gtfs.model.FareProduct;
import com.conveyal.gtfs.model.FareTransferRule;
import com.conveyal.gtfs.model.Network;
import com.conveyal.gtfs.model.RouteNetwork;
import com.conveyal.gtfs.model.StopArea;
import com.conveyal.gtfs.model.TimeFrame;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;

import java.util.Arrays;
import java.util.List;

import static com.conveyal.gtfs.graphql.GraphQLUtil.createFieldDefinition;
import static graphql.Scalars.GraphQLInt;
import static graphql.schema.GraphQLObjectType.newObject;

public class GraphQLGtfsFaresV2Schema {

private static final String AREA_TYPE_NAME = "area";
private static final String STOP_AREA_TYPE_NAME = "stop_area";
private static final String TIME_FRAME_TYPE_NAME = "time_frame";
private static final String NETWORK_TYPE_NAME = "network";
private static final String ROUTE_NETWORK_TYPE_NAME = "route_network";
private static final String FARE_MEDIA_TYPE_NAME = "fare_media";
private static final String FARE_PRODUCT_TYPE_NAME = "fare_product";
private static final String FARE_LEG_RULE_TYPE_NAME = "fare_leg_rule";
private static final String FARE_TRANSFER_RULE_TYPE_NAME = "fare_transfer_rule";

private GraphQLGtfsFaresV2Schema() {}

public static final GraphQLObjectType stopAreaType = newObject().name(STOP_AREA_TYPE_NAME)
.description("A GTFS stop area object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(StopArea.AREA_ID_NAME))
.field(MapFetcher.field(StopArea.STOP_ID_NAME))
.build();

public static final GraphQLObjectType areaType = newObject().name(AREA_TYPE_NAME)
.description("A GTFS area object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(Area.AREA_ID_NAME))
.field(MapFetcher.field(Area.AREA_NAME_NAME))
.build();

public static final GraphQLObjectType timeFrameType = newObject().name(TIME_FRAME_TYPE_NAME)
.description("A GTFS time frame object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(TimeFrame.TIME_FRAME_GROUP_ID_NAME))
.field(MapFetcher.field(TimeFrame.START_TIME_NAME))
.field(MapFetcher.field(TimeFrame.END_TIME_NAME))
.field(MapFetcher.field(TimeFrame.SERVICE_ID_NAME))
.build();

public static final GraphQLObjectType networkType = newObject().name(NETWORK_TYPE_NAME)
.description("A GTFS network object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(Network.NETWORK_ID_NAME))
.field(MapFetcher.field(Network.NETWORK_NAME_NAME))
.build();

public static final GraphQLObjectType routeNetworkType = newObject().name(ROUTE_NETWORK_TYPE_NAME)
.description("A GTFS route network object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(RouteNetwork.NETWORK_ID_NAME))
.field(MapFetcher.field(RouteNetwork.ROUTE_ID_NAME))
.build();

public static final GraphQLObjectType fareMediaType = newObject().name(FARE_MEDIA_TYPE_NAME)
.description("A GTFS fare media object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(FareMedia.FARE_MEDIA_ID_NAME))
.field(MapFetcher.field(FareMedia.FARE_MEDIA_NAME_NAME))
.field(MapFetcher.field(FareMedia.FARE_MEDIA_TYPE_NAME))
.build();

public static final GraphQLObjectType fareProductType = newObject().name(FARE_PRODUCT_TYPE_NAME)
.description("A GTFS fare product object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(FareProduct.FARE_PRODUCT_ID_NAME))
.field(MapFetcher.field(FareProduct.FARE_PRODUCT_NAME_NAME))
.field(MapFetcher.field(FareProduct.FARE_MEDIA_ID_NAME))
.field(MapFetcher.field(FareProduct.AMOUNT_NAME))
.field(MapFetcher.field(FareProduct.CURRENCY_NAME))
.build();

public static final GraphQLObjectType fareLegRuleType = newObject().name(FARE_LEG_RULE_TYPE_NAME)
.description("A GTFS fare leg rule object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(FareLegRule.LEG_GROUP_ID_NAME))
.field(MapFetcher.field(FareLegRule.NETWORK_ID_NAME))
.field(MapFetcher.field(FareLegRule.FROM_AREA_ID_NAME))
.field(MapFetcher.field(FareLegRule.TO_AREA_ID_NAME))
.field(MapFetcher.field(FareLegRule.FROM_TIMEFRAME_GROUP_ID_NAME))
.field(MapFetcher.field(FareLegRule.TO_TIMEFRAME_GROUP_ID_NAME))
.field(MapFetcher.field(FareLegRule.FARE_PRODUCT_ID_NAME))
.field(MapFetcher.field(FareLegRule.RULE_PRIORITY_NAME))
.build();

public static final GraphQLObjectType fareTransferRuleType = newObject().name(FARE_TRANSFER_RULE_TYPE_NAME)
.description("A GTFS fare transfer rule object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(FareTransferRule.FROM_LEG_GROUP_ID_NAME))
.field(MapFetcher.field(FareTransferRule.TO_LEG_GROUP_ID_NAME))
.field(MapFetcher.field(FareTransferRule.TRANSFER_COUNT_NAME))
.field(MapFetcher.field(FareTransferRule.DURATION_LIMIT_NAME))
.field(MapFetcher.field(FareTransferRule.DURATION_LIMIT_TYPE_NAME))
.field(MapFetcher.field(FareTransferRule.FARE_TRANSFER_TYPE_NAME))
.field(MapFetcher.field(FareTransferRule.FARE_PRODUCT_ID_NAME))
.build();

public static List<GraphQLFieldDefinition> getFaresV2FieldDefinitions() {
return Arrays.asList(
createFieldDefinition(AREA_TYPE_NAME, areaType, Area.TABLE_NAME),
createFieldDefinition(FARE_LEG_RULE_TYPE_NAME, fareLegRuleType, FareLegRule.TABLE_NAME),
createFieldDefinition(FARE_MEDIA_TYPE_NAME, fareMediaType, FareMedia.TABLE_NAME),
createFieldDefinition(FARE_PRODUCT_TYPE_NAME, fareProductType, FareProduct.TABLE_NAME),
createFieldDefinition(FARE_TRANSFER_RULE_TYPE_NAME, fareTransferRuleType, FareTransferRule.TABLE_NAME),
createFieldDefinition(NETWORK_TYPE_NAME, networkType, Network.TABLE_NAME),
createFieldDefinition(ROUTE_NETWORK_TYPE_NAME, routeNetworkType, RouteNetwork.TABLE_NAME),
createFieldDefinition(STOP_AREA_TYPE_NAME, stopAreaType, StopArea.TABLE_NAME),
createFieldDefinition(TIME_FRAME_TYPE_NAME, timeFrameType, TimeFrame.TABLE_NAME)
);
}
}
Loading