use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class GtfsRealtimeFuzzyTripMatcher method match.
public TripDescriptor match(String feedId, TripDescriptor trip) {
if (trip.hasTripId()) {
// trip_id already exists
return trip;
}
if (!trip.hasRouteId() || !trip.hasDirectionId() || !trip.hasStartTime() || !trip.hasStartDate()) {
// Could not determine trip_id, returning original TripDescriptor
return trip;
}
AgencyAndId routeId = new AgencyAndId(feedId, trip.getRouteId());
int time = StopTimeFieldMappingFactory.getStringAsSeconds(trip.getStartTime());
ServiceDate date;
try {
date = ServiceDate.parseString(trip.getStartDate());
} catch (ParseException e) {
return trip;
}
Route route = index.routeForId.get(routeId);
if (route == null) {
return trip;
}
int direction = trip.getDirectionId();
Trip matchedTrip = getTrip(route, direction, time, date);
if (matchedTrip == null) {
// Check if the trip is carried over from previous day
date = date.previous();
time += 24 * 60 * 60;
matchedTrip = getTrip(route, direction, time, date);
}
if (matchedTrip == null) {
return trip;
}
// If everything succeeds, build a new TripDescriptor with the matched trip_id
return trip.toBuilder().setTripId(matchedTrip.getId().getId()).build();
}
use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class RoutingRequestTest method testPreferencesPenaltyForRoute.
@Test
public void testPreferencesPenaltyForRoute() {
AgencyAndId agencyAndId = new AgencyAndId();
Agency agency = new Agency();
Route route = new Route();
Trip trip = new Trip();
RoutingRequest routingRequest = new RoutingRequest();
trip.setRoute(route);
route.setId(agencyAndId);
route.setAgency(agency);
assertEquals(0, routingRequest.preferencesPenaltyForRoute(trip.getRoute()));
}
use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class TestTransferTable method testTransferTable.
/**
* Test transfer table
*/
public void testTransferTable() {
// Setup from stop
Stop fromStop = new Stop();
fromStop.setId(new AgencyAndId("A1", "S1"));
// Setup to stop
Stop toStop = new Stop();
toStop.setId(new AgencyAndId("A1", "S2"));
// Setup to stop parent
Stop toStopParent = new Stop();
toStopParent.setId(new AgencyAndId("A1", "S3"));
toStop.setParentStation("S3");
// Setup from trip with route
Route fromRoute = new Route();
fromRoute.setId(new AgencyAndId("A1", "R1"));
Trip fromTrip = new Trip();
fromTrip.setId(new AgencyAndId("A1", "T1"));
fromTrip.setRoute(fromRoute);
// Setup to trip with route
Route toRoute = new Route();
toRoute.setId(new AgencyAndId("A1", "R2"));
Trip toTrip = new Trip();
toTrip.setId(new AgencyAndId("A1", "T2"));
toTrip.setRoute(toRoute);
// Setup second to trip with route
Route toRoute2 = new Route();
toRoute2.setId(new AgencyAndId("A1", "R3"));
Trip toTrip2 = new Trip();
toTrip2.setId(new AgencyAndId("A1", "T3"));
toTrip2.setRoute(toRoute2);
// Create transfer table
TransferTable table = new TransferTable();
// Check transfer times
assertEquals(StopTransfer.UNKNOWN_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip, true));
assertEquals(StopTransfer.UNKNOWN_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip2, true));
assertEquals(StopTransfer.UNKNOWN_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip, true));
assertEquals(StopTransfer.UNKNOWN_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip2, true));
// Add transfer to parent stop, specificity 0
table.addTransferTime(fromStop, toStopParent, null, null, null, null, StopTransfer.PREFERRED_TRANSFER);
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip, true));
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip2, true));
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip, true));
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip2, true));
// Check going forward and backwards in time
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip, true));
assertEquals(StopTransfer.UNKNOWN_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip, false));
assertEquals(StopTransfer.UNKNOWN_TRANSFER, table.getTransferTime(toStop, fromStop, toTrip, fromTrip, true));
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(toStop, fromStop, toTrip, fromTrip, false));
// Add transfer to child stop, specificity 1
table.addTransferTime(fromStop, toStop, null, toRoute, null, null, StopTransfer.FORBIDDEN_TRANSFER);
assertEquals(StopTransfer.FORBIDDEN_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip, true));
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip2, true));
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip, true));
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip2, true));
// Add transfer to parent stop, specificity 1
table.addTransferTime(fromStop, toStopParent, null, toRoute2, null, null, StopTransfer.TIMED_TRANSFER);
assertEquals(StopTransfer.FORBIDDEN_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip, true));
assertEquals(StopTransfer.TIMED_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip2, true));
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip, true));
assertEquals(StopTransfer.TIMED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip2, true));
// Add transfer to child stop, specificity 2
table.addTransferTime(fromStop, toStop, null, toRoute2, null, toTrip2, 4);
assertEquals(StopTransfer.FORBIDDEN_TRANSFER, table.getTransferTime(fromStop, toStop, fromTrip, toTrip, true));
assertEquals(4, table.getTransferTime(fromStop, toStop, fromTrip, toTrip2, true));
assertEquals(StopTransfer.PREFERRED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip, true));
assertEquals(StopTransfer.TIMED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip2, true));
// Add transfer to parent stop and unknown to child stop, specificity 3
// WARNING: don't add transfers with StopTransfer.UNKNOWN_TRANSFER in non-testing code
table.addTransferTime(fromStop, toStop, fromRoute, null, null, toTrip, StopTransfer.UNKNOWN_TRANSFER);
table.addTransferTime(fromStop, toStopParent, fromRoute, null, null, toTrip, 5);
assertEquals(5, table.getTransferTime(fromStop, toStop, fromTrip, toTrip, true));
assertEquals(4, table.getTransferTime(fromStop, toStop, fromTrip, toTrip2, true));
assertEquals(5, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip, true));
assertEquals(StopTransfer.TIMED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip2, true));
// Add transfer to child stop, specificity 4
table.addTransferTime(fromStop, toStop, null, null, fromTrip, toTrip2, 6);
assertEquals(5, table.getTransferTime(fromStop, toStop, fromTrip, toTrip, true));
assertEquals(6, table.getTransferTime(fromStop, toStop, fromTrip, toTrip2, true));
assertEquals(5, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip, true));
assertEquals(StopTransfer.TIMED_TRANSFER, table.getTransferTime(fromStop, toStopParent, fromTrip, toTrip2, true));
}
use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class OnBoardDepartServiceImplTest method testOnBoardDepartureTime.
@Test
public final void testOnBoardDepartureTime() {
Coordinate[] coordinates = new Coordinate[5];
coordinates[0] = new Coordinate(0.0, 0.0);
coordinates[1] = new Coordinate(0.0, 1.0);
coordinates[2] = new Coordinate(2.0, 1.0);
coordinates[3] = new Coordinate(5.0, 1.0);
coordinates[4] = new Coordinate(5.0, 5.0);
PatternDepartVertex depart = mock(PatternDepartVertex.class);
PatternArriveVertex dwell = mock(PatternArriveVertex.class);
PatternArriveVertex arrive = mock(PatternArriveVertex.class);
Graph graph = mock(Graph.class);
RoutingRequest routingRequest = mock(RoutingRequest.class);
ServiceDay serviceDay = mock(ServiceDay.class);
// You're probably not supposed to do this to mocks (access their fields directly)
// But I know of no other way to do this since the mock object has only action-free stub methods.
routingRequest.modes = new TraverseModeSet("WALK,TRANSIT");
when(graph.getTimeZone()).thenReturn(TimeZone.getTimeZone("GMT"));
GeometryFactory geometryFactory = GeometryUtils.getGeometryFactory();
CoordinateSequenceFactory coordinateSequenceFactory = geometryFactory.getCoordinateSequenceFactory();
CoordinateSequence coordinateSequence = coordinateSequenceFactory.create(coordinates);
LineString geometry = new LineString(coordinateSequence, geometryFactory);
ArrayList<Edge> hops = new ArrayList<Edge>(2);
RoutingContext routingContext = new RoutingContext(routingRequest, graph, null, arrive);
AgencyAndId agencyAndId = new AgencyAndId("Agency", "ID");
Agency agency = new Agency();
Route route = new Route();
ArrayList<StopTime> stopTimes = new ArrayList<StopTime>(3);
StopTime stopDepartTime = new StopTime();
StopTime stopDwellTime = new StopTime();
StopTime stopArriveTime = new StopTime();
Stop stopDepart = new Stop();
Stop stopDwell = new Stop();
Stop stopArrive = new Stop();
Trip trip = new Trip();
routingContext.serviceDays = new ArrayList<ServiceDay>(Collections.singletonList(serviceDay));
agency.setId(agencyAndId.getAgencyId());
route.setId(agencyAndId);
route.setAgency(agency);
stopDepart.setId(agencyAndId);
stopDwell.setId(agencyAndId);
stopArrive.setId(agencyAndId);
stopDepartTime.setStop(stopDepart);
stopDepartTime.setDepartureTime(0);
stopDwellTime.setArrivalTime(20);
stopDwellTime.setStop(stopDwell);
stopDwellTime.setDepartureTime(40);
stopArriveTime.setArrivalTime(60);
stopArriveTime.setStop(stopArrive);
stopTimes.add(stopDepartTime);
stopTimes.add(stopDwellTime);
stopTimes.add(stopArriveTime);
trip.setId(agencyAndId);
trip.setTripHeadsign("The right");
trip.setRoute(route);
TripTimes tripTimes = new TripTimes(trip, stopTimes, new Deduplicator());
StopPattern stopPattern = new StopPattern(stopTimes);
TripPattern tripPattern = new TripPattern(route, stopPattern);
TripPattern.generateUniqueIds(Arrays.asList(tripPattern));
when(depart.getTripPattern()).thenReturn(tripPattern);
when(dwell.getTripPattern()).thenReturn(tripPattern);
PatternHop patternHop0 = new PatternHop(depart, dwell, stopDepart, stopDwell, 0);
PatternHop patternHop1 = new PatternHop(dwell, arrive, stopDwell, stopArrive, 1);
hops.add(patternHop0);
hops.add(patternHop1);
when(graph.getEdges()).thenReturn(hops);
when(depart.getCoordinate()).thenReturn(new Coordinate(0, 0));
when(dwell.getCoordinate()).thenReturn(new Coordinate(0, 0));
when(arrive.getCoordinate()).thenReturn(new Coordinate(0, 0));
routingRequest.from = new GenericLocation();
routingRequest.startingTransitTripId = agencyAndId;
when(serviceDay.secondsSinceMidnight(anyInt())).thenReturn(9);
patternHop0.setGeometry(geometry);
tripPattern.add(tripTimes);
graph.index = new GraphIndex(graph);
coordinates = new Coordinate[3];
coordinates[0] = new Coordinate(3.5, 1.0);
coordinates[1] = new Coordinate(5.0, 1.0);
coordinates[2] = new Coordinate(5.0, 5.0);
coordinateSequence = coordinateSequenceFactory.create(coordinates);
geometry = new LineString(coordinateSequence, geometryFactory);
Vertex vertex = onBoardDepartServiceImpl.setupDepartOnBoard(routingContext);
Edge edge = vertex.getOutgoing().toArray(new Edge[1])[0];
assertEquals(vertex, edge.getFromVertex());
assertEquals(dwell, edge.getToVertex());
assertEquals("The right", edge.getDirection());
assertEquals(geometry, edge.getGeometry());
assertEquals(coordinates[0].x, vertex.getX(), 0.0);
assertEquals(coordinates[0].y, vertex.getY(), 0.0);
}
use of org.onebusaway.gtfs.model.Route in project OpenTripPlanner by opentripplanner.
the class TestStopTransfer method testStopTransfer.
/**
* Test different stop transfers
*/
public void testStopTransfer() {
// Setup from trip with route
Route fromRoute = new Route();
fromRoute.setId(new AgencyAndId("A1", "R1"));
Trip fromTrip = new Trip();
fromTrip.setId(new AgencyAndId("A1", "T1"));
fromTrip.setRoute(fromRoute);
// Setup to trip with route
Route toRoute = new Route();
toRoute.setId(new AgencyAndId("A1", "R2"));
Trip toTrip = new Trip();
toTrip.setId(new AgencyAndId("A1", "T2"));
toTrip.setRoute(toRoute);
// Setup second to trip with route
Route toRoute2 = new Route();
toRoute2.setId(new AgencyAndId("A1", "R3"));
Trip toTrip2 = new Trip();
toTrip2.setId(new AgencyAndId("A1", "T3"));
toTrip2.setRoute(toRoute2);
// Create StopTransfer
StopTransfer transfer = new StopTransfer();
assertEquals(StopTransfer.UNKNOWN_TRANSFER, transfer.getTransferTime(fromTrip, toTrip));
assertEquals(StopTransfer.UNKNOWN_TRANSFER, transfer.getTransferTime(fromTrip, toTrip2));
// Add empty SpecificTransfer, specificity 0
transfer.addSpecificTransfer(new SpecificTransfer((AgencyAndId) null, null, null, null, StopTransfer.FORBIDDEN_TRANSFER));
assertEquals(StopTransfer.FORBIDDEN_TRANSFER, transfer.getTransferTime(fromTrip, toTrip));
assertEquals(StopTransfer.FORBIDDEN_TRANSFER, transfer.getTransferTime(fromTrip, toTrip2));
// Add SpecificTransfer one route, specificity 1
transfer.addSpecificTransfer(new SpecificTransfer(null, toRoute2.getId(), null, null, StopTransfer.PREFERRED_TRANSFER));
assertEquals(StopTransfer.FORBIDDEN_TRANSFER, transfer.getTransferTime(fromTrip, toTrip));
assertEquals(StopTransfer.PREFERRED_TRANSFER, transfer.getTransferTime(fromTrip, toTrip2));
// Add SpecificTransfer one trip (and one ignored route), specificity 2
transfer.addSpecificTransfer(new SpecificTransfer(null, toRoute2.getId(), null, toTrip2.getId(), StopTransfer.TIMED_TRANSFER));
assertEquals(StopTransfer.FORBIDDEN_TRANSFER, transfer.getTransferTime(fromTrip, toTrip));
assertEquals(StopTransfer.TIMED_TRANSFER, transfer.getTransferTime(fromTrip, toTrip2));
// Add SpecificTransfer one trip and one route, specificity 3
transfer.addSpecificTransfer(new SpecificTransfer(fromRoute.getId(), toRoute2.getId(), fromTrip.getId(), null, StopTransfer.UNKNOWN_TRANSFER));
assertEquals(StopTransfer.FORBIDDEN_TRANSFER, transfer.getTransferTime(fromTrip, toTrip));
assertEquals(StopTransfer.UNKNOWN_TRANSFER, transfer.getTransferTime(fromTrip, toTrip2));
// Add SpecificTransfer one route, specificity 1
transfer.addSpecificTransfer(new SpecificTransfer(fromRoute.getId(), null, null, null, 3));
assertEquals(3, transfer.getTransferTime(fromTrip, toTrip));
assertEquals(StopTransfer.UNKNOWN_TRANSFER, transfer.getTransferTime(fromTrip, toTrip2));
}
Aggregations