MATSIM
CreatePseudoNetwork.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 java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 
31 import org.matsim.api.core.v01.Id;
46 
57 public class CreatePseudoNetwork {
58 
59  private final TransitSchedule schedule;
60  private final Network network;
61  private final String prefix;
62  private final double linkFreeSpeed;
63  private final double linkCapacity;
64 
65 
66  private final Map<Tuple<Node, Node>, Link> links = new HashMap<Tuple<Node, Node>, Link>();
67  private final Map<Tuple<Node, Node>, TransitStopFacility> stopFacilities = new HashMap<Tuple<Node, Node>, TransitStopFacility>();
68  private final Map<TransitStopFacility, Node> nodes = new HashMap<TransitStopFacility, Node>();
69  private final Map<TransitStopFacility, List<TransitStopFacility>> facilityCopies = new HashMap<TransitStopFacility, List<TransitStopFacility>>();
70 
71  private long linkIdCounter = 0;
72 
73  private final Set<String> transitModes = Collections.singleton(TransportMode.pt);
74 
75  public CreatePseudoNetwork(final TransitSchedule schedule, final Network network, final String networkIdPrefix) {
76  this.schedule = schedule;
77  this.network = network;
78  this.prefix = networkIdPrefix;
79  this.linkFreeSpeed = 100.0 / 3.6;
80  this.linkCapacity = 100000.0;
81  }
82 
83  public CreatePseudoNetwork(final TransitSchedule schedule, final Network network, final String networkIdPrefix,
84  final double linkFreeSpeed, final double linkCapacity) {
85  this.schedule = schedule;
86  this.network = network;
87  this.prefix = networkIdPrefix;
88  this.linkFreeSpeed = linkFreeSpeed;
89  this.linkCapacity = linkCapacity;
90  }
91 
92  public void createNetwork() {
93 
94  List<Tuple<TransitLine, TransitRoute>> toBeRemoved = new LinkedList<Tuple<TransitLine, TransitRoute>>();
95 
96  for (TransitLine tLine : this.schedule.getTransitLines().values()) {
97  for (TransitRoute tRoute : tLine.getRoutes().values()) {
98  ArrayList<Id<Link>> routeLinks = new ArrayList<Id<Link>>();
99  TransitRouteStop prevStop = null;
100  for (TransitRouteStop stop : tRoute.getStops()) {
101  Link link = getNetworkLink(prevStop, stop);
102  routeLinks.add(link.getId());
103  prevStop = stop;
104  }
105 
106  if (routeLinks.size() > 0) {
107  NetworkRoute route = RouteUtils.createNetworkRoute(routeLinks );
108  tRoute.setRoute(route);
109  } else {
110  System.err.println("Line " + tLine.getId() + " route " + tRoute.getId() + " has less than two stops. Removing this route from schedule.");
111  toBeRemoved.add(new Tuple<TransitLine, TransitRoute>(tLine, tRoute));
112  }
113  }
114  }
115 
116  for (Tuple<TransitLine, TransitRoute> remove : toBeRemoved) {
117  remove.getFirst().removeRoute(remove.getSecond());
118  }
119  }
120 
121  private Link getNetworkLink(final TransitRouteStop fromStop, final TransitRouteStop toStop) {
122  TransitStopFacility fromFacility = (fromStop == null) ? toStop.getStopFacility() : fromStop.getStopFacility();
123  TransitStopFacility toFacility = toStop.getStopFacility();
124 
125  Node fromNode = this.nodes.get(fromFacility);
126  if (fromNode == null) {
127  fromNode = this.network.getFactory().createNode(Id.create(this.prefix + toFacility.getId(), Node.class), fromFacility.getCoord());
128  this.network.addNode(fromNode);
129  this.nodes.put(toFacility, fromNode);
130  }
131 
132  Node toNode = this.nodes.get(toFacility);
133  if (toNode == null) {
134  toNode = this.network.getFactory().createNode(Id.create(this.prefix + toFacility.getId(), Node.class), toFacility.getCoord());
135  this.network.addNode(toNode);
136  this.nodes.put(toFacility, toNode);
137  }
138 
139  Tuple<Node, Node> connection = new Tuple<Node, Node>(fromNode, toNode);
140  Link link = this.links.get(connection);
141  if (link == null) {
142  link = createAndAddLink(fromNode, toNode, connection);
143 
144  if (toFacility.getLinkId() == null) {
145  toFacility.setLinkId(link.getId());
146  this.stopFacilities.put(connection, toFacility);
147  } else {
148  List<TransitStopFacility> copies = this.facilityCopies.get(toFacility);
149  if (copies == null) {
150  copies = new ArrayList<TransitStopFacility>();
151  this.facilityCopies.put(toFacility, copies);
152  }
153  Id<TransitStopFacility> newId = Id.create(toFacility.getId().toString() + "." + Integer.toString(copies.size() + 1), TransitStopFacility.class);
154  TransitStopFacility newFacility = this.schedule.getFactory().createTransitStopFacility(newId, toFacility.getCoord(), toFacility.getIsBlockingLane());
155  Id<TransitStopArea> transitStopAreaId;
156  if (toFacility.getStopAreaId() == null) {
157  transitStopAreaId = Id.create(toFacility.getId(), TransitStopArea.class);
158  toFacility.setStopAreaId(transitStopAreaId);
159  } else {
160  transitStopAreaId = toFacility.getStopAreaId();
161  }
162  newFacility.setStopAreaId(transitStopAreaId);
163  newFacility.setLinkId(link.getId());
164  newFacility.setName(toFacility.getName());
165  copies.add(newFacility);
166  this.nodes.put(newFacility, toNode);
167  this.schedule.addStopFacility(newFacility);
168  toStop.setStopFacility(newFacility);
169  this.stopFacilities.put(connection, newFacility);
170  }
171  } else {
172  toStop.setStopFacility(this.stopFacilities.get(connection));
173  }
174  return link;
175  }
176 
177  private Link createAndAddLink(Node fromNode, Node toNode,
178  Tuple<Node, Node> connection) {
179  Link link;
180  link = this.network.getFactory().createLink(Id.create(this.prefix + this.linkIdCounter++, Link.class), fromNode, toNode);
181  if (fromNode == toNode) {
182  link.setLength(50);
183  } else {
184  link.setLength(CoordUtils.calcEuclideanDistance(fromNode.getCoord(), toNode.getCoord()));
185  }
186  link.setFreespeed(linkFreeSpeed);
187  link.setCapacity(linkCapacity);
188  link.setNumberOfLanes(1);
189  this.network.addLink(link);
190  link.setAllowedModes(this.transitModes);
191  this.links.put(connection, link);
192  return link;
193  }
194 
195  public Link getLinkBetweenStops(final TransitStopFacility fromStop, final TransitStopFacility toStop) {
196  Node fromNode = this.nodes.get(fromStop);
197  Node toNode = this.nodes.get(toStop);
198  Tuple<Node, Node> connection = new Tuple<Node, Node>(fromNode, toNode);
199  return this.links.get(connection);
200  }
201 
202 }
static double calcEuclideanDistance(Coord coord, Coord other)
final Map< Tuple< Node, Node >, TransitStopFacility > stopFacilities
void setStopAreaId(Id< TransitStopArea > stopAreaId)
abstract void setStopFacility(final TransitStopFacility stopFacility)
final Map< TransitStopFacility, List< TransitStopFacility > > facilityCopies
Link createAndAddLink(Node fromNode, Node toNode, Tuple< Node, Node > connection)
final Map< Tuple< Node, Node >, Link > links
Link getLinkBetweenStops(final TransitStopFacility fromStop, final TransitStopFacility toStop)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
final Map< TransitStopFacility, Node > nodes
CreatePseudoNetwork(final TransitSchedule schedule, final Network network, final String networkIdPrefix, final double linkFreeSpeed, final double linkCapacity)
Link createLink(final Id< Link > id, final Node fromNode, final Node toNode)
static NetworkRoute createNetworkRoute(List< Id< Link >> routeLinkIds, Network network)
Link getNetworkLink(final TransitRouteStop fromStop, final TransitRouteStop toStop)
abstract TransitStopFacility getStopFacility()
Node createNode(final Id< Node > id, final Coord coord)
abstract TransitStopFacility createTransitStopFacility(final Id< TransitStopFacility > facilityId, final Coord coordinate, final boolean blocksLane)
CreatePseudoNetwork(final TransitSchedule schedule, final Network network, final String networkIdPrefix)
void addStopFacility(final TransitStopFacility stop)
Map< Id< TransitLine >, TransitLine > getTransitLines()