21 package org.matsim.visum;
23 import java.io.BufferedReader;
24 import java.io.IOException;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
45 BufferedReader infile = null;
52 String header = infile.readLine();
56 if (header.equals(
"$VN;Y5")) {
58 }
else if (header.startsWith(
"$O")) {
59 new SparseMatrixReader(this.matrix).read(infile);
61 throw new RuntimeException(
"Visum file format '" + header +
"' is not supported.");
67 }
catch (IOException e) {
72 try { infile.close(); }
73 catch (IOException e) { log.warn(
"Could not close input-stream.", e); }
100 public void read(
final BufferedReader in)
throws IOException {
102 while ( (line = in.readLine()) != null) {
104 if (!line.startsWith(
"*")) {
112 if (this.state == STATE_DATA) {
114 String[] data = line.split(
"\t");
115 if (data.length !=
this.nofZones) {
116 throw new RuntimeException(
"Expected " + this.nofZones +
" data items, but found " + data.length +
117 " in line " +
this.lineCounter +
".");
119 for (
int i = 0; i < this.
nofZones; i++) {
120 this.matrix.
setEntry(this.zoneNames[this.zoneCounter], this.zoneNames[i], Double.parseDouble(data[i]));
123 if (this.zoneCounter == this.nofZones) {
127 }
else if (this.state == STATE_HEADER) {
132 }
else if (this.state == STATE_ANZBEZIRKE) {
134 this.nofZones = Integer.parseInt(line);
137 }
else if (this.state == STATE_BEZIRKE) {
139 this.zoneNames = line.split(
"\t");
140 if (this.zoneNames.length !=
this.nofZones) {
142 "The actual number of zones (" + this.zoneNames.length +
") does not " +
143 "correspond with the expected number of zones (" +
this.nofZones +
")." 146 this.zoneCounter = 0;
156 static class SparseMatrixReader {
161 private final static int STATE_VERKEHRSMITTEL = 1;
162 private final static int STATE_TIME = 2;
163 private final static int STATE_FAKTOR = 3;
170 public SparseMatrixReader(
final Matrix matrix) {
174 public void read(
final BufferedReader in)
throws IOException {
176 while ( (line = in.readLine()) != null) {
178 if (!line.startsWith(
"*")) {
184 private void parseLine(
final String line) {
186 if (this.state == STATE_DATA) {
188 String[] data = line.trim().split(
"\\s+");
189 if (data.length != 3) {
190 throw new RuntimeException(
"Expected 3 tokens, but found " + data.length +
" in line " +
this.lineCounter +
"." 194 String from = data[0];
203 double value = Double.NaN;
204 if (!from.equals(to)) {
205 value = Double.parseDouble(data[2]);
209 }
else if (this.state == STATE_HEADER) {
212 this.state = STATE_VERKEHRSMITTEL;
214 }
else if (this.state == STATE_VERKEHRSMITTEL) {
217 this.state = STATE_TIME;
219 }
else if (this.state == STATE_TIME) {
222 this.state = STATE_FAKTOR;
224 }
else if (this.state == STATE_FAKTOR) {
final Entry createAndAddEntry(final String fromLocId, final String toLocId, final double value)
static final int STATE_BEZIRKE
DenseMatrixReader(final Matrix matrix)
VisumMatrixReader(final Matrix matrix)
void read(final BufferedReader in)
static BufferedReader getBufferedReader(URL url, Charset charset)
static final int STATE_GARBAGE
Matrix readFile(final String filename)
static final int STATE_HEADER
final Entry setEntry(final String fromLocId, final String toLocId, final double value)
static final int STATE_DATA
void parseLine(final String line)
static final int STATE_ANZBEZIRKE
final void setDesc(final String desc)