MATSIM
SwissRailRaptorRoutingModule.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.* *
3  *
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2023 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 package ch.sbb.matsim.routing.pt.raptor;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 import org.matsim.api.core.v01.Coord;
26 import org.matsim.api.core.v01.Id;
39 
47 
48  private final SwissRailRaptor raptor;
49  private final RoutingModule walkRouter;
51  private final Network network;
52 
54  final TransitSchedule transitSchedule,
55  Network network, final RoutingModule walkRouter) {
56  if (raptor == null) {
57  throw new NullPointerException("The router object is null, but is required later.");
58  }
59  this.raptor = raptor;
60  this.transitSchedule = transitSchedule;
61  this.network = network;
62  if (walkRouter == null) {
63  throw new NullPointerException("The walkRouter object is null, but is required later.");
64  }
65  this.walkRouter = walkRouter;
66  }
67 
68  @Override
69  public List<? extends PlanElement> calcRoute(RoutingRequest request) {
70  List<? extends PlanElement> legs = this.raptor.calcRoute(request);
71  return legs != null ?
72  fillWithActivities(legs) :
73  walkRouter.calcRoute(request);
74  }
75 
76  private List<? extends PlanElement> fillWithActivities(List<? extends PlanElement> segments) {
77  List<PlanElement> planElements = new ArrayList<>(segments.size() * 2);
78  PlanElement prevLeg = null;
79  for (PlanElement pe : segments) {
80  if (prevLeg != null) {
81  // only add pt interaction activities between two legs
82  // otherwise we maintain interaction activities from
83  // access and egress trips
84  if (prevLeg instanceof Leg && pe instanceof Leg) {
85  Coord coord = findCoordinate((Leg)prevLeg, (Leg)pe);
86  Id<Link> linkId = ((Leg)pe).getRoute().getStartLinkId();
88  planElements.add(act);
89  }
90  }
91  planElements.add(pe);
92  prevLeg = pe;
93  }
94  return planElements;
95  }
96 
97  private Coord findCoordinate(Leg prevLeg, Leg nextLeg) {
98  if (prevLeg.getRoute() instanceof TransitPassengerRoute) {
99  Id<TransitStopFacility> stopId = ((TransitPassengerRoute) prevLeg.getRoute()).getEgressStopId();
100  return this.transitSchedule.getFacilities().get(stopId).getCoord();
101  }
102  if (nextLeg.getRoute() instanceof TransitPassengerRoute) {
103  Id<TransitStopFacility> stopId = ((TransitPassengerRoute) nextLeg.getRoute()).getAccessStopId();
104  return this.transitSchedule.getFacilities().get(stopId).getCoord();
105  }
106  // fallback: prevLeg and nextLeg are not pt routes, so we have to guess the coordinate based on the link id
107  Id<Link> linkId = prevLeg.getRoute().getEndLinkId();
108  Link link = this.network.getLinks().get(linkId);
109  return link.getToNode().getCoord();
110  }
111 
112 }
Map< Id< TransitStopFacility >, TransitStopFacility > getFacilities()
SwissRailRaptorRoutingModule(final SwissRailRaptor raptor, final TransitSchedule transitSchedule, Network network, final RoutingModule walkRouter)
List<? extends PlanElement > calcRoute(RoutingRequest request)
List<? extends PlanElement > calcRoute(RoutingRequest request)
Map< Id< Link >, ? extends Link > getLinks()
List<? extends PlanElement > fillWithActivities(List<? extends PlanElement > segments)
static Activity createStageActivityFromCoordLinkIdAndModePrefix(final Coord interactionCoord, final Id< Link > interactionLink, String modePrefix)
List<? extends PlanElement > calcRoute(RoutingRequest request)