MATSIM
NetworkWriteAsTable.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * NetworkWriteAsTable.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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.core.network.algorithms;
22 
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.matsim.api.core.v01.Coord;
35 import org.matsim.core.utils.io.IOUtils;
36 
37 public final class NetworkWriteAsTable implements NetworkRunnable {
38 
40  // member variables
42 
43  private final static Logger log = LogManager.getLogger(NetworkWriteAsTable.class);
44 
45  private final String outdir;
46  private final double offset;
47 
49  // constructors
51 
52  public NetworkWriteAsTable(final String outdir, double offset) {
53  this.offset = offset;
54  this.outdir = outdir;
55  }
56 
57  public NetworkWriteAsTable(final String outdir) {
58  this(outdir,0.0);
59  }
60 
62  this("output");
63  }
64 
66  // final method
68 
70  // private methods
72 
74  // run methods
76 
77  @Override
78  public void run(Network network) {
79  double capperiod = network.getCapacityPeriod();
80  capperiod = capperiod / 3600;
81  log.info("capperiod = " + capperiod);
82 
83  try {
84  BufferedWriter out_n = IOUtils.getBufferedWriter(this.outdir + "/nodes.txt.gz");
85  out_n.write("ID\tX\tY\n");
86  out_n.flush();
87  for (Node n : network.getNodes().values()) {
88  out_n.write(n.getId() + "\t" + n.getCoord().getX() + "\t" + n.getCoord().getY() + "\n");
89  out_n.flush();
90  }
91  out_n.flush();
92  out_n.close();
93 
94  BufferedWriter out_l = IOUtils.getBufferedWriter(this.outdir + "/links.txt.gz");
95  out_l.write("ID\tX1\tY1\tX2\tY2\tLENGHT\tFREESPEED\tCAPACITY\tPERMLANES\tMODES\n");
96  out_l.flush();
97  BufferedWriter out_et = IOUtils.getBufferedWriter(this.outdir + "/linksET.txt.gz");
98  out_et.write("ID\tFROMID\tTOID\tLENGTH\tSPEED\tCAP\tLANES\tORIGID\tTYPE\tMODES\n");
99  out_et.flush();
100  for (Link l : network.getLinks().values()) {
101  Node f = l.getFromNode();
102  Node t = l.getToNode();
103 
104  final double y = -t.getCoord().getX() + f.getCoord().getX();
105  Coord offsetVector = new Coord(t.getCoord().getY() - f.getCoord().getY(), y);
106  offsetVector = CoordUtils.scalarMult(offset/CoordUtils.length(offsetVector),offsetVector);
107  Coord fc = CoordUtils.plus(f.getCoord(),offsetVector);
108  Coord tc = CoordUtils.plus(t.getCoord(),offsetVector);
109 
110  out_l.write(l.getId() + "\t" + fc.getX() + "\t" + fc.getY() + "\t");
111  out_l.write(tc.getX() + "\t" + tc.getY() + "\t" + l.getLength() + "\t");
112  out_l.write(l.getFreespeed()+"\t"
113  +(l.getCapacity()/capperiod)+"\t"
115  +l.getAllowedModes().toString()+"\n");
116  out_l.flush();
117 
118  out_et.write(l.getId() + "\t" + l.getFromNode().getId() + "\t" + l.getToNode().getId() + "\t");
119  out_et.write(Math.round(l.getLength()) + "\t" + Math.round(l.getFreespeed()*3.6) + "\t");
120  out_et.write(Math.round(l.getCapacity()/capperiod) + "\t" + NetworkUtils.getNumberOfLanesAsInt(l) + "\t");
121  out_et.write(NetworkUtils.getOrigId( ((Link) l) ) + "\t" + NetworkUtils.getType(((Link) l)) + "\t"+l.getAllowedModes().toString()+"\n");
122  out_et.write(fc.getX() + "\t" + fc.getY() + "\n");
123  out_et.write(tc.getX() + "\t" + tc.getY() + "\n");
124  out_et.write("END\n");
125  }
126  out_l.flush();
127  out_l.close();
128  out_et.write("END\n");
129  out_et.flush();
130  out_et.close();
131  } catch (IOException e) {
132  throw new RuntimeException(e);
133  }
134  }
135 }
static int getNumberOfLanesAsInt(final double time, final Link link)
static double length(Coord coord)
Map< Id< Node >, ? extends Node > getNodes()
static String getOrigId(Node node)
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:390
static String getType(Node node)
static Coord plus(Coord coord1, Coord coord2)
Definition: CoordUtils.java:53
Map< Id< Link >, ? extends Link > getLinks()
static Coord scalarMult(double alpha, Coord coord)
Definition: CoordUtils.java:88