MATSIM
VisumMatrixWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * VisumMatrixWriter.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.visum;
22 
23 import java.io.BufferedWriter;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.util.Set;
27 import java.util.TreeSet;
28 
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
31 import org.matsim.matrices.Entry;
32 import org.matsim.matrices.Matrix;
33 
40 public class VisumMatrixWriter {
41 
42  /*package*/ final static Logger log = LogManager.getLogger(VisumMatrixWriter.class);
43 
44  Matrix matrix;
45  Set<String> ids;
46 
47  public VisumMatrixWriter(final Matrix matrix) {
48  super();
49  this.matrix = matrix;
50  this.ids = new TreeSet<>();
51  this.ids.addAll(matrix.getFromLocations().keySet());
52  this.ids.addAll(matrix.getToLocations().keySet());
53  }
54 
64  public void setIds(final Set<String> ids) {
65  this.ids = ids;
66  }
67 
68  public void writeFile(final String filename) {
69 
70  try (BufferedWriter out = new BufferedWriter(new FileWriter(filename))) {
71  out.write("$VN;Y5\n");
72  out.write("*\n");
73  out.write("*\tAnzahl Bezirke\n");
74  out.write(this.ids.size() + "\n");
75 
76  out.write("*\tBezirksNummern\n");
77  int cnt = 0;
78  for (String value : this.ids) {
79  cnt++;
80  if (cnt > 1) {
81  out.write("\t");
82  }
83  out.write(value);
84  }
85  out.write("\n");
86 
87  for (String from : this.ids) {
88  out.write("*\t" + from + "\n");
89  cnt = 0;
90  for (String to : this.ids) {
91  cnt++;
92  Entry e = this.matrix.getEntry(from, to);
93  if (cnt > 1) {
94  out.write("\t");
95  }
96  if (e == null) {
97  out.write("0");
98  } else {
99  out.write(Double.toString(e.getValue()));
100  }
101  }
102  out.write("\n");
103  }
104 
105  out.write("\n");
106 
107  } catch (IOException e) {
108  e.printStackTrace();
109  }
110  }
111 }
Definition: Entry.java:23
void writeFile(final String filename)
final Map< String, ArrayList< Entry > > getFromLocations()
Definition: Matrix.java:159
void setIds(final Set< String > ids)
final Map< String, ArrayList< Entry > > getToLocations()
Definition: Matrix.java:163
final double getValue()
Definition: Entry.java:69
final Entry getEntry(final String from, final String to)
Definition: Matrix.java:175