MATSIM
LinkToLinkRoutingModule.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * InvertedNetworkLegRouter
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2011 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 org.matsim.core.router;
21 
22 import org.matsim.api.core.v01.Id;
29 import org.matsim.core.gbl.Gbl;
36 
37 import java.util.Arrays;
38 import java.util.List;
39 
40 
51 class LinkToLinkRoutingModule implements RoutingModule {
52  private final Network network;
53  private final Network invertedNetwork;
54  private final LeastCostPathCalculator leastCostPathCalculator;
55  private final PopulationFactory populationFactory;
56  private final String mode;
57 
58  LinkToLinkRoutingModule(final String mode, final PopulationFactory populationFactory,
59  Network network, Network invertedNetwork, InvertedLeastPathCalculator leastCostPathCalc) {
60  this.network = network;
61  this.invertedNetwork = invertedNetwork;
62  this.populationFactory = populationFactory;
63  this.mode = mode;
64  this.leastCostPathCalculator = leastCostPathCalc;
65  }
66 
67  @Override
68  public List<? extends PlanElement> calcRoute(RoutingRequest request) {
69  final Facility fromFacility = request.getFromFacility();
70  final Facility toFacility = request.getToFacility();
71  final double departureTime = request.getDepartureTime();
72  final Person person = request.getPerson();
73 
74  Leg newLeg = this.populationFactory.createLeg( this.mode );
75 
76  Gbl.assertNotNull(fromFacility);
77  Gbl.assertNotNull(toFacility);
78 
79  if (!toFacility.getLinkId().equals(fromFacility.getLinkId())) {
80  // (a "true" route)
81  Node fromInvNode = this.invertedNetwork.getNodes()
82  .get(Id.create(fromFacility.getLinkId(), Node.class));
83  Node toInvNode = this.invertedNetwork.getNodes().get(Id.create(toFacility.getLinkId(), Node.class));
84 
85  Path path = leastCostPathCalculator.calcLeastCostPath(fromInvNode, toInvNode, departureTime, person, null);
86  if (path == null) {
87  throw new RuntimeException("No route found on inverted network from link "
88  + fromFacility.getLinkId() + " to link " + toFacility.getLinkId() + ".");
89  }
90 
91  NetworkRoute route = this.populationFactory.getRouteFactories().createRoute(NetworkRoute.class, fromFacility.getLinkId(), toFacility.getLinkId());
92  route.setLinkIds(fromFacility.getLinkId(), NetworkUtils.getLinkIds(path.links), toFacility.getLinkId());
93  route.setTravelTime(path.travelTime);
94  route.setTravelCost(path.travelCost);
95  route.setDistance(RouteUtils.calcDistance(route, 1.0, 1.0, this.network));
96  newLeg.setRoute(route);
97  newLeg.setTravelTime(path.travelTime);
98  } else {
99  // create an empty route == staying on place if toLink == endLink
100  // note that we still do a route: someone may drive from one location to another on the link. kai, dec'15
101  NetworkRoute route = this.populationFactory.getRouteFactories().createRoute(NetworkRoute.class, fromFacility.getLinkId(), toFacility.getLinkId());
102  route.setTravelTime(0);
103  route.setDistance(0.0);
104  newLeg.setRoute(route);
105  newLeg.setTravelTime(0);
106  }
107  newLeg.setDepartureTime(departureTime);
108  return Arrays.asList( newLeg );
109  }
110 
111 
112 }