use of org.opentripplanner.model.plan.Itinerary in project OpenTripPlanner by opentripplanner.
the class GroupByTripIdAndDistanceTest method calculateTotalDistanceTest.
@Test
public void calculateTotalDistanceTest() {
Itinerary i = newItinerary(A).bus(21, 1, 2, B).walk(2, C).bus(31, 5, 7, D).build();
Leg l1 = i.legs.get(0);
Leg l2 = i.legs.get(1);
Leg l3 = i.legs.get(2);
// 3 minutes on a bus
double expectedDistanceRidingABus = BUS_SPEED * 3 * 60;
// 2 minute walking
double expectedDistanceWalking = WALK_SPEED * 2 * 60;
// total
double expectedDistance = expectedDistanceRidingABus + expectedDistanceWalking;
assertEquals(expectedDistanceRidingABus, calculateTotalDistance(List.of(l1, l3)), 0.001);
assertEquals(expectedDistanceWalking, calculateTotalDistance(List.of(l2)), 0.001);
assertEquals(expectedDistance, calculateTotalDistance(List.of(l1, l2, l3)), 0.001);
}
use of org.opentripplanner.model.plan.Itinerary in project OpenTripPlanner by opentripplanner.
the class TestIntermediatePlaces method handleRequest.
private void handleRequest(GenericLocation from, GenericLocation to, GenericLocation[] via, TraverseModeSet modes, boolean arriveBy) {
RoutingRequest request = new RoutingRequest(modes);
request.setDateTime("2016-04-20", "13:00", timeZone);
request.setArriveBy(arriveBy);
request.from = from;
request.to = to;
for (GenericLocation intermediateLocation : via) {
request.addIntermediatePlace(intermediateLocation);
}
List<GraphPath> paths = graphPathFinder.graphPathFinderEntryPoint(request);
assertNotNull(paths);
assertFalse(paths.isEmpty());
List<Itinerary> itineraries = GraphPathToItineraryMapper.mapItineraries(paths, request);
TripPlan plan = TripPlanMapper.mapTripPlan(request, itineraries);
assertLocationIsVeryCloseToPlace(from, plan.from);
assertLocationIsVeryCloseToPlace(to, plan.to);
assertTrue(1 <= plan.itineraries.size());
for (Itinerary itinerary : plan.itineraries) {
validateIntermediatePlacesVisited(itinerary, via);
assertTrue(via.length < itinerary.legs.size());
validateLegsTemporally(request, itinerary);
validateLegsSpatially(plan, itinerary);
if (modes.contains(TraverseMode.TRANSIT)) {
assert itinerary.transitTimeSeconds > 0;
}
}
}
use of org.opentripplanner.model.plan.Itinerary in project OpenTripPlanner by opentripplanner.
the class RoutingWorker method routeTransit.
private Collection<Itinerary> routeTransit(Router router) {
request.setRoutingContext(router.graph);
if (request.modes.transitModes.isEmpty()) {
return Collections.emptyList();
}
if (!router.graph.transitFeedCovers(request.dateTime)) {
throw new RoutingValidationException(List.of(new RoutingError(RoutingErrorCode.OUTSIDE_SERVICE_PERIOD, InputField.DATE_TIME)));
}
TransitLayer transitLayer = request.ignoreRealtimeUpdates ? router.graph.getTransitLayer() : router.graph.getRealtimeTransitLayer();
RaptorRoutingRequestTransitData requestTransitDataProvider;
requestTransitDataProvider = new RaptorRoutingRequestTransitData(transitLayer, request.getDateTime().toInstant(), ADDITIONAL_SEARCH_DAYS_BEFORE_TODAY, ADDITIONAL_SEARCH_DAYS_AFTER_TODAY, request.modes.transitModes, request.rctx.bannedRoutes, request.walkSpeed);
this.debugAggregator.finishedPatternFiltering();
// Prepare access/egress transfers
Collection<AccessEgress> accessTransfers = AccessEgressRouter.streetSearch(request, false, 2000, transitLayer.getStopIndex());
Collection<AccessEgress> egressTransfers = AccessEgressRouter.streetSearch(request, true, 2000, transitLayer.getStopIndex());
verifyEgressAccess(accessTransfers, egressTransfers);
this.debugAggregator.finishedAccessEgress();
// Prepare transit search
RaptorRequest<TripSchedule> raptorRequest = RaptorRequestMapper.mapRequest(request, requestTransitDataProvider.getStartOfTime(), accessTransfers, egressTransfers);
// Route transit
RaptorResponse<TripSchedule> transitResponse = raptorService.route(raptorRequest, requestTransitDataProvider);
LOG.debug("Found {} transit itineraries", transitResponse.paths().size());
LOG.debug("Transit search params used: {}", transitResponse.requestUsed().searchParams());
this.debugAggregator.finishedRaptorSearch();
// Create itineraries
RaptorPathToItineraryMapper itineraryMapper = new RaptorPathToItineraryMapper(transitLayer, requestTransitDataProvider.getStartOfTime(), request);
FareService fareService = request.getRoutingContext().graph.getService(FareService.class);
List<Itinerary> itineraries = new ArrayList<>();
for (Path<TripSchedule> path : transitResponse.paths()) {
// Convert the Raptor/Astar paths to OTP API Itineraries
Itinerary itinerary = itineraryMapper.createItinerary(path);
// fare calculation. We derive the fares from the internal Path objects and add them to the itinerary.
if (fareService != null) {
itinerary.fare = fareService.getCost(path, transitLayer);
}
itineraries.add(itinerary);
}
checkIfTransitConnectionExists(transitResponse);
// Filter itineraries away that depart after the latest-departure-time for depart after
// search. These itineraries is a result of time-shifting the access leg and is needed for
// the raptor to prune the results. These itineraries are often not ideal, but if they
// pareto optimal for the "next" window, they will appear when a "next" search is performed.
searchWindowUsedInSeconds = transitResponse.requestUsed().searchParams().searchWindowInSeconds();
if (!request.arriveBy && searchWindowUsedInSeconds > 0) {
filterOnLatestDepartureTime = Instant.ofEpochSecond(request.dateTime + searchWindowUsedInSeconds);
}
this.debugAggregator.finishedItineraryCreation();
return itineraries;
}
use of org.opentripplanner.model.plan.Itinerary in project OpenTripPlanner by opentripplanner.
the class GraphPathToItineraryMapper method mapItineraries.
/**
* Generates a TripPlan from a set of paths
*/
public static List<Itinerary> mapItineraries(List<GraphPath> paths, RoutingRequest request) {
List<Itinerary> itineraries = new LinkedList<>();
for (GraphPath path : paths) {
Itinerary itinerary = generateItinerary(path, request.locale);
if (itinerary.legs.isEmpty()) {
continue;
}
itinerary = adjustItinerary(request, itinerary);
itineraries.add(itinerary);
}
return itineraries;
}
use of org.opentripplanner.model.plan.Itinerary in project OpenTripPlanner by opentripplanner.
the class GraphPathToItineraryMapper method generateItinerary.
/**
* Generate an itinerary from a {@link GraphPath}. This method first slices the list of states
* at the leg boundaries. These smaller state arrays are then used to generate legs. Finally the
* rest of the itinerary is generated based on the complete state array.
*
* @param path The graph path to base the itinerary on
* @return The generated itinerary
*/
public static Itinerary generateItinerary(GraphPath path, Locale requestedLocale) {
State[] states = new State[path.states.size()];
State lastState = path.states.getLast();
states = path.states.toArray(states);
Edge[] edges = new Edge[path.edges.size()];
edges = path.edges.toArray(edges);
Graph graph = path.getRoutingContext().graph;
State[][] legsStates = sliceStates(states);
List<Leg> legs = new ArrayList<>();
for (State[] legStates : legsStates) {
legs.add(generateLeg(graph, legStates, requestedLocale));
}
addWalkSteps(graph, legs, legsStates, requestedLocale);
boolean first = true;
for (Leg leg : legs) {
AlertToLegMapper.addAlertPatchesToLeg(graph, leg, first, requestedLocale);
first = false;
}
setLegPathwayFlag(legs, legsStates);
Itinerary itinerary = new Itinerary(legs);
calculateElevations(itinerary, edges);
itinerary.generalizedCost = (int) lastState.weight;
return itinerary;
}
Aggregations