MATSIM
TransitRouterNetwork.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * TransitRouterNetwork.java
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.router;
22 
23 import java.util.Collection;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 
30 import org.apache.logging.log4j.LogManager;
31 import org.apache.logging.log4j.Logger;
32 import org.matsim.api.core.v01.Coord;
33 import org.matsim.api.core.v01.Id;
43 import org.matsim.core.utils.misc.Time;
49 
58 public final class TransitRouterNetwork implements Network {
59 
60  private final static Logger log = LogManager.getLogger(TransitRouterNetwork.class);
61 
62  private final Map<Id<Link>, TransitRouterNetworkLink> links = new LinkedHashMap<>();
63  private final Map<Id<Node>, TransitRouterNetworkNode> nodes = new LinkedHashMap<>();
65 
66  private long nextNodeId = 0;
67  private long nextLinkId = 0;
68 
69 
70  public static final class TransitRouterNetworkNode implements Node {
71 
72  public final TransitRouteStop stop;
73  public final TransitRoute route;
74  public final TransitLine line;
75  final Id<Node> id;
76  final Map<Id<Link>, TransitRouterNetworkLink> ingoingLinks = new IdentifiableArrayMap<>();
77  final Map<Id<Link>, TransitRouterNetworkLink> outgoingLinks = new IdentifiableArrayMap<>();
78 
79  public TransitRouterNetworkNode(final Id<Node> id, final TransitRouteStop stop, final TransitRoute route, final TransitLine line) {
80  this.id = id;
81  this.stop = stop;
82  this.route = route;
83  this.line = line;
84  }
85 
86  @Override
87  public Map<Id<Link>, ? extends Link> getInLinks() {
88  return this.ingoingLinks;
89  }
90 
91  @Override
92  public Map<Id<Link>, ? extends Link> getOutLinks() {
93  return this.outgoingLinks;
94  }
95 
96  @Override
97  public boolean addInLink(final Link link) {
98  throw new UnsupportedOperationException();
99  }
100 
101  @Override
102  public boolean addOutLink(final Link link) {
103  throw new UnsupportedOperationException();
104  }
105 
106  @Override
107  public Coord getCoord() {
108  return this.stop.getStopFacility().getCoord();
109  }
110 
111  @Override
112  public Id<Node> getId() {
113  return this.id;
114  }
115 
117  return stop;
118  }
119 
121  return route;
122  }
123 
124  public TransitLine getLine() {
125  return line;
126  }
127 
128  @Override
129  public String toString() {
130  return this.id.toString();
131  }
132 
133  @Override
134  public Link removeInLink(Id<Link> linkId) {
135  throw new RuntimeException("not implemented") ;
136  }
137 
138  @Override
139  public Link removeOutLink(Id<Link> outLinkId) {
140  throw new RuntimeException("not implemented") ;
141  }
142 
143  @Override
144  public void setCoord(Coord coord) {
145  throw new RuntimeException("not implemented") ;
146  }
147 
148  @Override
150  throw new UnsupportedOperationException();
151  }
152  }
153 
159  public static final class TransitRouterNetworkLink implements Link {
160 
163  final TransitRoute route;
164  final TransitLine line;
165  final Id<Link> id;
166  private final double length;
167 
168  public TransitRouterNetworkLink(final Id<Link> id, final TransitRouterNetworkNode fromNode, final TransitRouterNetworkNode toNode, final TransitRoute route, final TransitLine line, double length) {
169  this.id = id;
170  this.fromNode = fromNode;
171  this.toNode = toNode;
172  this.route = route;
173  this.line = line;
174  this.length = length;
175  }
176 
177  public TransitRouterNetworkLink(final Id<Link> id, final TransitRouterNetworkNode fromNode, final TransitRouterNetworkNode toNode, final TransitRoute route, final TransitLine line) {
178  this(id, fromNode, toNode, route, line, CoordUtils.calcEuclideanDistance(toNode.stop.getStopFacility().getCoord(), fromNode.stop.getStopFacility().getCoord()));
179  }
180 
181  @Override
183  return this.fromNode;
184  }
185 
186  @Override
188  return this.toNode;
189  }
190 
191  @Override
192  public double getCapacity() {
193  return 9999;
194  }
195 
196  @Override
197  public double getCapacity(final double time) {
198  return getCapacity();
199  }
200 
201  @Override
202  public double getFreespeed() {
203  return 10;
204  }
205 
206  @Override
207  public double getFreespeed(final double time) {
208  return getFreespeed();
209  }
210 
211  @Override
212  public Id<Link> getId() {
213  return this.id;
214  }
215 
216  @Override
217  public double getNumberOfLanes() {
218  return 1;
219  }
220 
221  @Override
222  public double getNumberOfLanes(final double time) {
223  return getNumberOfLanes();
224  }
225 
226  @Override
227  public double getLength() {
228  return this.length;
229  }
230 
231  @Override
232  public void setCapacity(final double capacity) {
233  throw new UnsupportedOperationException();
234  }
235 
236  @Override
237  public void setFreespeed(final double freespeed) {
238  throw new UnsupportedOperationException();
239  }
240 
241  @Override
242  public boolean setFromNode(final Node node) {
243  throw new UnsupportedOperationException();
244  }
245 
246  @Override
247  public void setNumberOfLanes(final double lanes) {
248  throw new UnsupportedOperationException();
249  }
250 
251  @Override
252  public void setLength(final double length) {
253  throw new UnsupportedOperationException();
254  }
255 
256  @Override
257  public boolean setToNode(final Node node) {
258  throw new UnsupportedOperationException();
259  }
260 
261  @Override
262  public Coord getCoord() {
263  throw new UnsupportedOperationException();
264  }
265 
266  @Override
267  public Set<String> getAllowedModes() {
268  return null;
269  }
270 
271  @Override
272  public void setAllowedModes(final Set<String> modes) {
273  throw new UnsupportedOperationException();
274  }
275 
277  return route;
278  }
279 
280  public TransitLine getLine() {
281  return line;
282  }
283 
284  @Override
285  public String toString() {
286  return "[" + this.id.toString() + " (" + this.getFromNode().id + " > " + this.getToNode().id + ")]";
287  }
288 
289  @Override
290  public double getCapacityPeriod() {
291  throw new UnsupportedOperationException();
292  }
293 
294  @Override
296  throw new UnsupportedOperationException();
297  }
298  }
299 
301  final TransitRouterNetworkNode node = new TransitRouterNetworkNode(Id.create(this.nextNodeId++, Node.class), stop, route, line);
302  this.nodes.put(node.getId(), node);
303  return node;
304  }
305 
307  final TransitRouterNetworkLink link = new TransitRouterNetworkLink(Id.create(this.nextLinkId++, Link.class), fromNode, toNode, route, line);
308  this.links.put(link.getId(), link);
309  fromNode.outgoingLinks.put(link.getId(), link);
310  toNode.ingoingLinks.put(link.getId(), link);
311  return link;
312  }
313 
314  @Override
315  public Map<Id<Node>, ? extends TransitRouterNetworkNode> getNodes() {
316  return this.nodes;
317  }
318 
319  @Override
320  public Map<Id<Link>, ? extends TransitRouterNetworkLink> getLinks() {
321  return this.links;
322  }
323 
324  public void finishInit() {
325  double minX = Double.POSITIVE_INFINITY;
326  double minY = Double.POSITIVE_INFINITY;
327  double maxX = Double.NEGATIVE_INFINITY;
328  double maxY = Double.NEGATIVE_INFINITY;
329 
330  for (TransitRouterNetworkNode node : this.nodes.values()) {
331  Coord c = node.stop.getStopFacility().getCoord();
332  if (c.getX() < minX) {
333  minX = c.getX();
334  }
335  if (c.getY() < minY) {
336  minY = c.getY();
337  }
338  if (c.getX() > maxX) {
339  maxX = c.getX();
340  }
341  if (c.getY() > maxY) {
342  maxY = c.getY();
343  }
344  }
345 
346  QuadTree<TransitRouterNetworkNode> quadTree = new QuadTree<TransitRouterNetworkNode>(minX, minY, maxX, maxY);
347  for (TransitRouterNetworkNode node : this.nodes.values()) {
348  Coord c = node.stop.getStopFacility().getCoord();
349  quadTree.put(c.getX(), c.getY(), node);
350  }
351  this.qtNodes = quadTree;
352  }
353 
354  public Collection<TransitRouterNetworkNode> getNearestNodes(final Coord coord, final double distance) {
355  return this.qtNodes.getDisk(coord.getX(), coord.getY(), distance);
356  }
357 
359  return this.qtNodes.getClosest(coord.getX(), coord.getY());
360  }
361 
362  @Override
363  public double getCapacityPeriod() {
364  return 3600.0;
365  }
366 
367  @Override
369  return null;
370  }
371 
372  @Override
373  public double getEffectiveLaneWidth() {
374  return 3;
375  }
376 
377  @Override
378  public void addNode(Node nn) {
379  throw new UnsupportedOperationException();
380  }
381 
382  @Override
383  public void addLink(Link ll) {
384  throw new UnsupportedOperationException();
385  }
386 
387  @Override
388  public Link removeLink(Id<Link> linkId) {
389  throw new UnsupportedOperationException();
390  }
391 
392  @Override
393  public Node removeNode(Id<Node> nodeId) {
394  throw new UnsupportedOperationException();
395  }
396 
397  public static TransitRouterNetwork createFromSchedule(final TransitSchedule schedule, final double maxBeelineWalkConnectionDistance) {
398  log.info("start creating transit network");
399  final TransitRouterNetwork network = new TransitRouterNetwork();
400  final Counter linkCounter = new Counter(" link #");
401  final Counter nodeCounter = new Counter(" node #");
402  // build nodes and links connecting the nodes according to the transit routes
403  for (TransitLine line : schedule.getTransitLines().values()) {
404  for (TransitRoute route : line.getRoutes().values()) {
405  TransitRouterNetworkNode prevNode = null;
406  for (TransitRouteStop stop : route.getStops()) {
408  nodeCounter.incCounter();
409  if (prevNode != null) {
410  network.createLink(prevNode, node, route, line);
411  linkCounter.incCounter();
412  }
413  prevNode = node;
414  }
415  }
416  }
417  network.finishInit(); // not nice to call "finishInit" here before we added all links...
418  // in my view, it would be possible to completely do without finishInit: do the
419  // additions to the central data structures as items come in, not near the end. I would
420  // prefer that because nobody could forget the "finishInit". kai, apr'10
421  // well, not really. finishInit creates the quadtree, for this, the extent must be known,
422  // which is not at the very start, so the quadtree data structure cannot be updated as
423  // links come in. mrieser, dec'10
424  log.info("add transfer links");
425 
426  List<Tuple<TransitRouterNetworkNode, TransitRouterNetworkNode>> toBeAdded = new LinkedList<Tuple<TransitRouterNetworkNode, TransitRouterNetworkNode>>();
427  // connect all stops with walking links if they're located less than beelineWalkConnectionDistance from each other
428  for (TransitRouterNetworkNode node : network.getNodes().values()) {
429  if (node.getInLinks().size() > 0) { // only add links from this node to other nodes if agents actually can arrive here
430  for (TransitRouterNetworkNode node2 : network.getNearestNodes(node.stop.getStopFacility().getCoord(), maxBeelineWalkConnectionDistance)) {
431  if ((node != node2) && (node2.getOutLinks().size() > 0)) { // only add links to other nodes when agents can depart there
432  if ((node.line != node2.line) || (node.stop.getStopFacility() != node2.stop.getStopFacility())) {
433  // do not yet add them to the network, as this would change in/out-links
434  toBeAdded.add(new Tuple<TransitRouterNetworkNode, TransitRouterNetworkNode>(node, node2));
435  }
436  }
437  }
438  }
439  }
440  log.info(toBeAdded.size() + " transfer links to be added.");
442  network.createLink(tuple.getFirst(), tuple.getSecond(), null, null);
443  linkCounter.incCounter();
444  }
445 
446  log.info("transit router network statistics:");
447  log.info(" # nodes: " + network.getNodes().size());
448  log.info(" # links total: " + network.getLinks().size());
449  log.info(" # transfer links: " + toBeAdded.size());
450 
451  return network;
452  }
453 
454  @Override
455  public void setCapacityPeriod(double capPeriod) {
456  throw new RuntimeException("not implemented") ;
457  }
458 
459  @Override
460  public void setEffectiveCellSize(double effectiveCellSize) {
461  throw new RuntimeException("not implemented") ;
462  }
463 
464  @Override
465  public void setEffectiveLaneWidth(double effectiveLaneWidth) {
466  throw new RuntimeException("not implemented") ;
467  }
468 
469  @Override
470  public void setName(String name) {
471  throw new RuntimeException("not implemented") ;
472  }
473 
474  @Override
475  public String getName() {
476  throw new RuntimeException("not implemented") ;
477  }
478 
479  @Override
480  public double getEffectiveCellSize() {
481  throw new RuntimeException("not implemented") ;
482  }
483 
484  @Override
486  throw new UnsupportedOperationException();
487  }
488 }
void setEffectiveLaneWidth(double effectiveLaneWidth)
Map< Id< TransitRoute >, TransitRoute > getRoutes()
final Map< Id< Node >, TransitRouterNetworkNode > nodes
static double calcEuclideanDistance(Coord coord, Coord other)
TransitRouterNetworkLink createLink(final TransitRouterNetworkNode fromNode, final TransitRouterNetworkNode toNode, final TransitRoute route, final TransitLine line)
Map< Id< Node >, ? extends TransitRouterNetworkNode > getNodes()
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
TransitRouterNetworkNode createNode(final TransitRouteStop stop, final TransitRoute route, final TransitLine line)
static TransitRouterNetwork createFromSchedule(final TransitSchedule schedule, final double maxBeelineWalkConnectionDistance)
boolean put(final double x, final double y, final T value)
Definition: QuadTree.java:86
T getClosest(final double x, final double y)
Definition: QuadTree.java:130
QuadTree< TransitRouterNetworkNode > qtNodes
abstract TransitStopFacility getStopFacility()
Collection< TransitRouterNetworkNode > getNearestNodes(final Coord coord, final double distance)
TransitRouterNetworkNode getNearestNode(final Coord coord)
Collection< T > getDisk(final double x, final double y, final double distance)
Definition: QuadTree.java:142
final Map< Id< Link >, TransitRouterNetworkLink > links
Map< Id< TransitLine >, TransitLine > getTransitLines()
TransitRouterNetworkNode(final Id< Node > id, final TransitRouteStop stop, final TransitRoute route, final TransitLine line)
void setEffectiveCellSize(double effectiveCellSize)
Map< Id< Link >, ? extends TransitRouterNetworkLink > getLinks()