MATSIM
RunNetworkEmme2MatsimExample.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2010 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package tutorial.converter.networkFromEmme;
21 
22 import org.apache.log4j.Logger;
23 import org.matsim.api.core.v01.Coord;
24 import org.matsim.api.core.v01.Id;
25 import org.matsim.api.core.v01.Scenario;
30 import org.matsim.core.config.Config;
34 import org.matsim.core.utils.io.IOUtils;
35 
36 import java.io.BufferedReader;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 
60  private static final Logger log = Logger.getLogger(RunNetworkEmme2MatsimExample.class);
61 
62  private static final int PSRC = 0 ;
63  private static final int EUGENE = 1 ;
64 
65  private static final int NW_NAME = PSRC ;
66 
67  public static void readNetwork( Scenario sc ) {
68  Network network = sc.getNetwork() ;
69  ((Network) network).setCapacityPeriod(3600.) ;
70  ((Network) network).setEffectiveLaneWidth(3.75) ;
71 // network.setEffectiveCellSize(7.5) ;
72 
73  // read emme3 network
74  try {
75 // BufferedReader reader = IOUtils.getBufferedReader("/home/nagel/tmp/tab/net1.out" ) ;
76  BufferedReader reader = IOUtils.getBufferedReader("/Users/nagel/eclipse/shared-svn/studies/countries/us/psrc/network/emme-export/net1.out" ) ;
77 
78  boolean weAreReadingNodes = true ;
79  long linkCnt = 0 ;
80 
81  String line ;
82  while ( (line = reader.readLine()) != null ) {
83  String[] parts = line.split("[ \t\n]+");
84 
85  if ( parts[0].equals("c") ) {
86  // is a comment; ignore
87  } else if ( parts[0].equals("t") ) {
88  if ( parts[1].equals("links") ) {
89  weAreReadingNodes = false ;
90  }
91  } else if ( parts[0].equals("a") ) { // || parts[0].equals("a*") ) { // a* seem to be centroid connectors
92  if ( weAreReadingNodes ) {
93  String idStr = parts[1] ;
94  String xxStr = parts[2] ;
95  String yyStr = parts[3] ;
96  Node node = network.getFactory().createNode(Id.create(idStr, Node.class),
97  new Coord(Double.parseDouble(xxStr), Double.parseDouble(yyStr)));
98  network.addNode( node ) ;
99  } else {
100  Node fromNode = network.getNodes().get(Id.create(parts[1], Node.class));
101  Node toNode = network.getNodes().get(Id.create(parts[2], Node.class));
102  if ( fromNode==null || toNode==null ) {
103 // log.info("fromNode or toNode ==null; probably connector link; skipping it ...") ;
104  continue ;
105  }
106  if ( parts[4].equals("r") || parts[4].equals("b") ) {
107  log.info("rail only or bus only link; skipping it ...") ;
108  continue;
109  }
110  double length = 1600 * Double.parseDouble( parts[3] ) ; // probably miles
111 // String type = parts[5] ;
112 
113  double permlanes = Double.parseDouble( parts[6] ) ;
114  if ( permlanes <= 0 ) { permlanes = 0.5 ; }
115 
116  double capacity, freespeed ;
117  if ( NW_NAME==PSRC ) {
118  capacity = permlanes * Double.parseDouble( parts[8] ) ;
119  if ( capacity <= 500 ) { capacity = 500. ; }
120 
121  freespeed = Double.parseDouble( parts[9] ) ; // mph
122  if ( freespeed < 10. ) { freespeed = 10. ; }
123  freespeed *= 1600./3600. ;
124  } else if ( NW_NAME==EUGENE ) {
125  log.warn("For EUGENE, I have not clarified if capacity really needs to be multiplied by number of lanes.");
126  capacity = permlanes * Double.parseDouble( parts[10] ) ;
127  if ( capacity <= 500 ) { capacity = 500. ; }
128 
129  freespeed = Double.parseDouble( parts[9] ) ; // mph
130  if ( freespeed < 10. ) { freespeed = 10. ; }
131  freespeed *= 1600./3600. ;
132  } else {
133  log.error( "NW_NAME not known; aborting" ) ;
134  System.exit(-1);
135  }
136 
137  Id<Link> id = Id.create(linkCnt, Link.class);
138  linkCnt++;
139 
140  Link link = network.getFactory().createLink(id, fromNode, toNode ) ;
141  link.setLength(length) ;
142  link.setFreespeed(freespeed) ;
143  link.setCapacity(capacity) ;
144  link.setNumberOfLanes(permlanes) ;
145 
146  network.addLink(link);
147  }
148  } else {
149  // something else; do nothing
150  }
151  }
152  } catch (FileNotFoundException e) {
153  e.printStackTrace();
154  } catch (IOException e) {
155  e.printStackTrace();
156  }
157  }
158 
159 
160 
164  public static void main(String[] args) {
165  Config config = ConfigUtils.createConfig() ;
166  // (no api-only way to get a scenario without config)
167 
168  Scenario sc = ScenarioUtils.createScenario(config) ;
169  // (no api-only way to get a network, Id, Coord without scenario)
170 
171  Network network = sc.getNetwork();
172 
173  log.info("reading network ...");
174  readNetwork(sc) ;
175  log.info("... finished reading network.\n");
176 
177  log.info("cleaning network ...");
178  NetworkCleaner nwCleaner = new NetworkCleaner() ;
179  nwCleaner.run( network ) ;
180  log.info("... finished cleaning network.\n") ;
181 
182  log.info("writing network ...") ;
183  new NetworkWriter(network).write("/home/nagel/tmp/net.xml.gz");
184  log.info("... finished writing network.\n") ;
185  }
186 
187 }
Map< Id< Node >,?extends Node > getNodes()
static BufferedReader getBufferedReader(final String filename)
Definition: IOUtils.java:94
Link createLink(final Id< Link > id, final Node fromNode, final Node toNode)
static Config createConfig(final String filename)
void setCapacityPeriod(double capPeriod)
Node createNode(final Id< Node > id, final Coord coord)
static Scenario createScenario(final Config config)