MATSIM
TransimsSnapshotWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * TransimsSnapshotWriter.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.vis.snapshotwriters;
22 
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 
26 import org.matsim.core.utils.io.IOUtils;
27 
34 public class TransimsSnapshotWriter implements SnapshotWriter {
35  private BufferedWriter out = null;
36  private double currentTime = -1;
37 
38  public static enum Labels { TIME, VEHICLE, EASTING, NORTHING, VELOCITY }
39 
40  public TransimsSnapshotWriter(String filename) {
41  try {
42  this.out = IOUtils.getBufferedWriter(IOUtils.getFileUrl(filename), IOUtils.CHARSET_UTF8, true);
43  String header = Labels.VEHICLE
44  + "\t" + Labels.TIME
45  + "\tLINK"
46  + "\tNODE"
47  + "\tLANE"
48  + "\tDISTANCE"
49  + "\t" + Labels.VELOCITY
50  + "\tVEHTYPE"
51  + "\tACCELER"
52  + "\tDRIVER"
53  + "\tPASSENGERS"
54  + "\t" + Labels.EASTING
55  + "\t" + Labels.NORTHING
56  + "\tELEVATION"
57  + "\tAZIMUTH"
58  + "\tUSER\n";
59  this.out.write(header);
60  } catch (IOException e) {
61  e.printStackTrace();
62  }
63  }
64 
65  @Override
66  public void addAgent(AgentSnapshotInfo position) {
67 
68  //drop all parking vehicles
70 
71  String buffer = position.getId().toString()
72  + "\t" + (int)this.currentTime
73  + "\t0\t0\t1\t0\t" + position.getColorValueBetweenZeroAndOne() // link(0), from node(0), lane(1), dist(0), speed
74  + "\t1\t0\t" + position.getId().toString() // vehtype(1), acceleration(0), driver-id
75  + "\t0\t" + position.getEasting() // # of passengers(0), easting
76  + "\t" + position.getNorthing()
77  + "\t0" // elevation
78  + "\t0" // azimuth
79  + "\t"+ "0" + "\n"; // user(0)
80  try {
81  out.write(buffer);
82  } catch (IOException e) {
83  e.printStackTrace();
84  }
85  }
86 
87  @Override
88  public void beginSnapshot(double time) {
89  this.currentTime = time;
90  }
91 
92  @Override
93  public void endSnapshot() {
94  this.currentTime = -1;
95  }
96 
97  @Override
98  public void finish() {
99  if (this.out != null) {
100  try {
101  out.close();
102  } catch (IOException e) {
103  e.printStackTrace();
104  }
105  }
106  }
107 }
static final Charset CHARSET_UTF8
Definition: IOUtils.java:182
static URL getFileUrl(String filename)
Definition: IOUtils.java:501
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:390