use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class DefaultFareServiceFactory method fillFareRules.
protected void fillFareRules(String agencyId, Collection<FareAttribute> fareAttributes, Collection<FareRule> fareRules, Map<AgencyAndId, FareRuleSet> fareRuleSet) {
/*
* Create an empty FareRuleSet for each FareAttribute, as some FareAttribute may have no
* rules attached to them.
*/
for (FareAttribute fare : fareAttributes) {
AgencyAndId id = fare.getId();
FareRuleSet fareRule = fareRuleSet.get(id);
if (fareRule == null) {
fareRule = new FareRuleSet(fare);
fareRuleSet.put(id, fareRule);
if (agencyId != null) {
// TODO With the new GTFS lib, use fareAttribute.agency_id directly
fareRule.setAgency(agencyId);
}
}
}
/*
* For each fare rule, add it to the FareRuleSet of the fare.
*/
for (FareRule rule : fareRules) {
FareAttribute fare = rule.getFare();
AgencyAndId id = fare.getId();
FareRuleSet fareRule = fareRuleSet.get(id);
if (fareRule == null) {
// Should never happen by design
LOG.error("Inexistant fare ID in fare rule: " + id);
continue;
}
String contains = rule.getContainsId();
if (contains != null) {
fareRule.addContains(contains);
}
String origin = rule.getOriginId();
String destination = rule.getDestinationId();
if (origin != null || destination != null) {
fareRule.addOriginDestination(origin, destination);
}
Route route = rule.getRoute();
if (route != null) {
AgencyAndId routeId = route.getId();
fareRule.addRoute(routeId);
}
}
}
use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class BusRouteStreetMatcher method buildGraph.
/*
The "extra" parameter is a mechanism for passing arbitrary things between graph builder modules.
Whether or not this is a good idea is open to debate, but that's what it is.
An EdgesForRoute instance is generated by MapBuilder and StreetMatcher, then retrieved later by the
NetworkLinkerLibrary later (actually in LinkRequests).
*/
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
// Mapbuilder needs transit index
graph.index(new DefaultStreetVertexIndexFactory());
StreetMatcher matcher = new StreetMatcher(graph);
EdgesForRoute edgesForRoute = new EdgesForRoute();
extra.put(EdgesForRoute.class, edgesForRoute);
log.info("Finding corresponding street edges for trip patterns...");
// Why do we need to iterate over the routes? Why not just patterns?
for (Route route : graph.index.routeForId.values()) {
for (TripPattern pattern : graph.index.patternsForRoute.get(route)) {
if (pattern.mode == TraverseMode.BUS) {
/* we can only match geometry to streets on bus routes */
log.debug("Matching {}", pattern);
// that is why pattern.geometry is null in that case
if (pattern.geometry == null) {
continue;
}
List<Edge> edges = matcher.match(pattern.geometry);
if (edges == null || edges.isEmpty()) {
log.warn("Could not match to street network: {}", pattern);
continue;
}
List<Coordinate> coordinates = new ArrayList<Coordinate>();
for (Edge e : edges) {
coordinates.addAll(Arrays.asList(e.getGeometry().getCoordinates()));
edgesForRoute.edgesForRoute.put(route, e);
}
Coordinate[] coordinateArray = new Coordinate[coordinates.size()];
LineString ls = GeometryUtils.getGeometryFactory().createLineString(coordinates.toArray(coordinateArray));
// Replace the pattern's geometry from GTFS with that of the equivalent OSM edges.
pattern.geometry = ls;
}
}
}
}
use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class IndexAPI method getPatternsForRoute.
/**
* Return all stop patterns used by trips on the given route.
*/
@GET
@Path("/routes/{routeId}/patterns")
public Response getPatternsForRoute(@PathParam("routeId") String routeIdString) {
AgencyAndId routeId = GtfsLibrary.convertIdFromString(routeIdString);
Route route = index.routeForId.get(routeId);
if (route != null) {
Collection<TripPattern> patterns = index.patternsForRoute.get(route);
return Response.status(Status.OK).entity(PatternShort.list(patterns)).build();
} else {
return Response.status(Status.NOT_FOUND).entity(MSG_404).build();
}
}
use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class IndexAPI method getRoutes.
/**
* Return a list of all routes in the graph.
*/
// with repeated hasStop parameters, replaces old routesBetweenStops
@GET
@Path("/routes")
public Response getRoutes(@QueryParam("hasStop") List<String> stopIds) {
Collection<Route> routes = index.routeForId.values();
// Filter routes to include only those that pass through all given stops
if (stopIds != null) {
// Protective copy, we are going to calculate the intersection destructively
routes = Lists.newArrayList(routes);
for (String stopId : stopIds) {
Stop stop = index.stopForId.get(GtfsLibrary.convertIdFromString(stopId));
if (stop == null)
return Response.status(Status.NOT_FOUND).entity(MSG_404).build();
Set<Route> routesHere = Sets.newHashSet();
for (TripPattern pattern : index.patternsForStop.get(stop)) {
routesHere.add(pattern.route);
}
routes.retainAll(routesHere);
}
}
return Response.status(Status.OK).entity(RouteShort.list(routes)).build();
}
use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSource method handleAddedTrip.
/**
* Handle GTFS-RT TripUpdate message containing an ADDED trip.
*
* @param graph graph to update
* @param tripUpdate GTFS-RT TripUpdate message
* @param stops the stops of each StopTimeUpdate in the TripUpdate message
* @param feedId
* @param serviceDate service date for added trip
* @return true iff successful
*/
private boolean handleAddedTrip(final Graph graph, final TripUpdate tripUpdate, final List<Stop> stops, final String feedId, final ServiceDate serviceDate) {
// Preconditions
Preconditions.checkNotNull(stops);
Preconditions.checkArgument(tripUpdate.getStopTimeUpdateCount() == stops.size(), "number of stop should match the number of stop time updates");
// Check whether trip id has been used for previously ADDED trip message and cancel
// previously created trip
final String tripId = tripUpdate.getTrip().getTripId();
cancelPreviouslyAddedTrip(feedId, tripId, serviceDate);
//
// Create added trip
//
Route route = null;
if (tripUpdate.getTrip().hasRouteId()) {
// Try to find route
route = getRouteForRouteId(feedId, tripUpdate.getTrip().getRouteId());
}
if (route == null) {
// Create new Route
route = new Route();
// Use route id of trip descriptor if available
if (tripUpdate.getTrip().hasRouteId()) {
route.setId(new AgencyAndId(feedId, tripUpdate.getTrip().getRouteId()));
} else {
route.setId(new AgencyAndId(feedId, tripId));
}
route.setAgency(dummyAgency);
// Guess the route type as it doesn't exist yet in the specifications
// Bus. Used for short- and long-distance bus routes.
route.setType(3);
// Create route name
route.setLongName(tripId);
}
// Create new Trip
final Trip trip = new Trip();
// TODO: which Agency ID to use? Currently use feed id.
trip.setId(new AgencyAndId(feedId, tripUpdate.getTrip().getTripId()));
trip.setRoute(route);
// Find service ID running on this service date
final Set<AgencyAndId> serviceIds = graph.getCalendarService().getServiceIdsOnDate(serviceDate);
if (serviceIds.isEmpty()) {
// No service id exists: return error for now
LOG.warn("ADDED trip has service date for which no service id is available, skipping.");
return false;
} else {
// Just use first service id of set
trip.setServiceId(serviceIds.iterator().next());
}
final boolean success = addTripToGraphAndBuffer(feedId, graph, trip, tripUpdate, stops, serviceDate, RealTimeState.ADDED);
return success;
}
Aggregations