MATSIM
CreatePseudoNetworkWithLoopLinks.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CreatePseudoNetwork
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 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 
21 package org.matsim.pt.utils;
22 
23 import org.matsim.api.core.v01.Id;
33 
34 import java.util.*;
35 
45 
46  private final TransitSchedule schedule;
47  private final Network network;
48  private final String prefix;
49  private final double linkFreeSpeed;
50  private final double linkCapacity;
51 
52  private final Map<Tuple<Node, Node>, Link> links = new HashMap<>();
53  private final Map<TransitStopFacility, Node> nodes = new HashMap<>();
54 
55  private final Set<String> transitModes = Collections.singleton(TransportMode.pt);
56 
57  public CreatePseudoNetworkWithLoopLinks(final TransitSchedule schedule, final Network network, final String networkIdPrefix) {
58  this.schedule = schedule;
59  this.network = network;
60  this.prefix = networkIdPrefix;
61  this.linkFreeSpeed = 100.0 / 3.6;
62  this.linkCapacity = 100000.0;
63  }
64 
65  public CreatePseudoNetworkWithLoopLinks(final TransitSchedule schedule, final Network network, final String networkIdPrefix,
66  final double linkFreeSpeed, final double linkCapacity) {
67  this.schedule = schedule;
68  this.network = network;
69  this.prefix = networkIdPrefix;
70  this.linkFreeSpeed = linkFreeSpeed;
71  this.linkCapacity = linkCapacity;
72  }
73 
74  public void createNetwork() {
75 
77 
78  List<Tuple<TransitLine, TransitRoute>> toBeRemoved = new LinkedList<>();
79  for (TransitLine tLine : this.schedule.getTransitLines().values()) {
80  for (TransitRoute tRoute : tLine.getRoutes().values()) {
81 
82  if (tRoute.getStops().size() < 2) {
83  System.err.println("Line " + tLine.getId() + " route " + tRoute.getId() + " has less than two stops. Removing this route from schedule.");
84  toBeRemoved.add(new Tuple<>(tLine, tRoute));
85  continue;
86  }
87 
88  List<Id<Link>> routeLinks = new ArrayList<>();
89  TransitRouteStop prevStop = null;
90 
91  for (TransitRouteStop stop : tRoute.getStops()) {
92  if (prevStop != null) {
93  Link link = getNetworkLink(prevStop, stop);
94  routeLinks.add(link.getId());
95  }
96 
97  // Add the loop links of all stops to the route
98  routeLinks.add(getLoopLink(stop.getStopFacility()).getId());
99  prevStop = stop;
100  }
101 
102  NetworkRoute route = RouteUtils.createNetworkRoute(routeLinks);
103  tRoute.setRoute(route);
104  }
105  }
106 
107  for (Tuple<TransitLine, TransitRoute> remove : toBeRemoved) {
108  remove.getFirst().removeRoute(remove.getSecond());
109  }
110  }
111 
113  for (TransitStopFacility stop : this.schedule.getFacilities().values()) {
114  Node node = this.network.getFactory().createNode(Id.createNodeId(this.prefix + stop.getId()), stop.getCoord());
115  this.network.addNode(node);
116  this.nodes.put(stop, node);
117 
118  Link loopLink = this.network.getFactory().createLink(Id.createLinkId (this.prefix + stop.getId()), node, node);
119  // Loop links needs to have a length so that the travel time is not zero
120  loopLink.setLength(1);
121  loopLink.setFreespeed(linkFreeSpeed);
122  loopLink.setCapacity(linkCapacity);
123  // Ensure enough vehicles can be placed on the loop link
124  loopLink.setNumberOfLanes(linkCapacity);
125  loopLink.setAllowedModes(transitModes);
126 
127  stop.setLinkId(loopLink.getId());
128  this.network.addLink(loopLink);
129  Tuple<Node, Node> connection = new Tuple<>(node, node);
130  this.links.put(connection, loopLink);
131  }
132  }
133 
137  private Link getLoopLink(final TransitStopFacility stop) {
138  Node node = this.nodes.get(stop);
139  Tuple<Node, Node> connection = new Tuple<>(node, node);
140  return this.links.get(connection);
141  }
142 
143  private Link getNetworkLink(final TransitRouteStop fromStop, final TransitRouteStop toStop) {
144  TransitStopFacility fromFacility = fromStop.getStopFacility();
145  TransitStopFacility toFacility = toStop.getStopFacility();
146 
147  Node fromNode = this.nodes.get(fromFacility);
148  Node toNode = this.nodes.get(toFacility);
149 
150  Tuple<Node, Node> connection = new Tuple<>(fromNode, toNode);
151  Link link = this.links.get(connection);
152  return link == null ? createAndAddLink(connection) : link;
153  }
154 
156  Node fromNode = connection.getFirst();
157  Node toNode = connection.getSecond();
158  Link link;
159  link = this.network.getFactory().createLink(Id.createLinkId(fromNode.getId() + "-" + toNode.getId()),
160  fromNode, toNode);
161 
162  double dist = CoordUtils.calcEuclideanDistance(fromNode.getCoord(), toNode.getCoord());
163  link.setLength(dist);
164  link.setFreespeed(linkFreeSpeed);
165  link.setCapacity(linkCapacity);
166  link.setNumberOfLanes(1);
167 
168  this.network.addLink(link);
169  link.setAllowedModes(this.transitModes);
170  this.links.put(connection, link);
171  return link;
172  }
173 
174 }
Map< Id< TransitStopFacility >, TransitStopFacility > getFacilities()
static double calcEuclideanDistance(Coord coord, Coord other)
static Id< Link > createLinkId(final long key)
Definition: Id.java:202
Link createLink(final Id< Link > id, final Node fromNode, final Node toNode)
static NetworkRoute createNetworkRoute(List< Id< Link >> routeLinkIds, Network network)
abstract TransitStopFacility getStopFacility()
Node createNode(final Id< Node > id, final Coord coord)
Map< Id< TransitLine >, TransitLine > getTransitLines()
static Id< Node > createNodeId(final long key)
Definition: Id.java:211