MATSIM
NetworkInverter.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * NetworkInverter.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2019 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 
22  package org.matsim.core.network.algorithms;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
31 import org.matsim.api.core.v01.Id;
37 
48 public final class NetworkInverter {
49 
50  final private static Logger log = LogManager.getLogger(NetworkInverter.class);
51 
53 
54  private Network invertedNetwork = null;
55 
56  private Map<Id<Link>, List<TurnInfo>> inLinkTurnInfoMap = null;
57 
58  public NetworkInverter(Network originalNet, Map<Id<Link>, List<TurnInfo>> inLinkTurnInfoMap) {
59  this.originalNetwork = originalNet;
60  this.inLinkTurnInfoMap = inLinkTurnInfoMap;
61  }
62 
64  if (this.invertedNetwork == null){
65  invertNetwork();
66  }
67  return this.invertedNetwork;
68  }
69 
70  private void invertNetwork(){
71  this.invertedNetwork = NetworkUtils.createNetwork();
72  int numberOfNodesGenerated = 0;
73  int numberOfLinksGenerated = 0;
74 
75  for (Link link : this.originalNetwork.getLinks().values()) {
76  NetworkUtils.createAndAddNode(this.invertedNetwork, Id.create(link.getId(), Node.class), link.getToNode().getCoord());
77  numberOfNodesGenerated++;
78  }
79 
80  for (Node node : this.originalNetwork.getNodes().values()) {
81  for (Link inLink : node.getInLinks().values()) {
82  for (Link outLink : node.getOutLinks().values()) {
83  List<TurnInfo> turnInfos = this.inLinkTurnInfoMap.get(inLink.getId());
84  TurnInfo ti = NetworkTurnInfoBuilder.getTurnInfoForOutlinkId(turnInfos, outLink.getId());
85  if (ti != null){
86  numberOfLinksGenerated = this.createInvertedLink(inLink, outLink, numberOfLinksGenerated, ti.getModes());
87  }
88  }
89  }
90  }
91 
92  log.info("Generated " + numberOfNodesGenerated + " Nodes and " + numberOfLinksGenerated + " Links");
93 
94  // Debug only
95  // NetworkWriter myNetworkWriter = new NetworkWriter(wrappedNetwork,
96  // "wrappedNetwork");
97  // myNetworkWriter.write();
98  }
99 
100  private int createInvertedLink(Link inLink, Link outLink, int numberOfLinksGenerated, Set<String> modes){
101  Link link = NetworkUtils.createAndAddLink(this.invertedNetwork,Id.create(numberOfLinksGenerated + 1, Link.class), this.invertedNetwork.getNodes().get(Id.create(inLink.getId(), Node.class)), this.invertedNetwork.getNodes().get(Id.create(outLink.getId(), Node.class)), outLink.getLength(), outLink.getFreespeed(), outLink.getCapacity(), outLink.getNumberOfLanes() );
102  link.setAllowedModes(modes);
103 // log.error("created inverted link " + link.getId() + " from " + inLink.getId() + " to " + outLink.getId() + " with modes " + modes);
104  NetworkUtils.setType( ((Link) link), NetworkUtils.getType(((Link) outLink)));
105  return numberOfLinksGenerated + 1;
106  }
107 
108  public List<Link> convertInvertedNodesToLinks(List<Node> nodes) {
109  List<Link> ret = new ArrayList<Link>(nodes.size());
110  for (Node n : nodes){
111  ret.add(this.originalNetwork.getLinks().get(Id.create(n.getId(), Link.class)));
112  }
113  return ret;
114  }
115 
116 }
Map< Id< Node >, ? extends Node > getNodes()
List< Link > convertInvertedNodesToLinks(List< Node > nodes)
int createInvertedLink(Link inLink, Link outLink, int numberOfLinksGenerated, Set< String > modes)
static Node createAndAddNode(Network network, final Id< Node > id, final Coord coord)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
static String getType(Node node)
Map< Id< Link >, List< TurnInfo > > inLinkTurnInfoMap
Map< Id< Link >, ? extends Link > getLinks()
NetworkInverter(Network originalNet, Map< Id< Link >, List< TurnInfo >> inLinkTurnInfoMap)
static Link createAndAddLink(Network network, final Id< Link > id, final Node fromNode, final Node toNode, final double length, final double freespeed, final double capacity, final double numLanes)
static void setType(Node node, final String type)