001/* *********************************************************************** *
002 * project: org.matsim.*
003 * TransimsSnapshotWriter.java
004 *                                                                         *
005 * *********************************************************************** *
006 *                                                                         *
007 * copyright       : (C) 2007 by the members listed in the COPYING,        *
008 *                   LICENSE and WARRANTY file.                            *
009 * email           : info at matsim dot org                                *
010 *                                                                         *
011 * *********************************************************************** *
012 *                                                                         *
013 *   This program is free software; you can redistribute it and/or modify  *
014 *   it under the terms of the GNU General Public License as published by  *
015 *   the Free Software Foundation; either version 2 of the License, or     *
016 *   (at your option) any later version.                                   *
017 *   See also COPYING, LICENSE and WARRANTY file                           *
018 *                                                                         *
019 * *********************************************************************** */
020
021package org.matsim.vis.snapshotwriters;
022
023import java.io.BufferedWriter;
024import java.io.IOException;
025
026import org.matsim.core.utils.io.IOUtils;
027
028/**
029 * Writes the current position of vehicles into a file that can be read by
030 * transims.
031 *
032 * @author mrieser
033 */
034public class TransimsSnapshotWriter implements SnapshotWriter {
035        private BufferedWriter out = null;
036        private double currentTime = -1;
037        
038        public static enum Labels { TIME, VEHICLE, EASTING, NORTHING, VELOCITY } ;
039
040        public TransimsSnapshotWriter(String filename) {
041                try {
042                        this.out = IOUtils.getBufferedWriter(IOUtils.getFileUrl(filename), IOUtils.CHARSET_UTF8, true);
043                        String header = Labels.VEHICLE
044            + "\t" + Labels.TIME
045            + "\tLINK"
046            + "\tNODE"
047            + "\tLANE"
048            + "\tDISTANCE"
049            + "\t" + Labels.VELOCITY
050            + "\tVEHTYPE"
051            + "\tACCELER"
052            + "\tDRIVER"
053            + "\tPASSENGERS"
054            + "\t" + Labels.EASTING
055            + "\t" + Labels.NORTHING
056            + "\tELEVATION"
057            + "\tAZIMUTH"
058            + "\tUSER\n";
059                        this.out.write(header);
060                } catch (IOException e) {
061                        e.printStackTrace();
062                }
063        }
064
065        @Override
066        public void addAgent(AgentSnapshotInfo position) {
067
068                //drop all parking vehicles
069                if (position.getAgentState() == AgentSnapshotInfo.AgentState.PERSON_AT_ACTIVITY) return;
070
071                String buffer = position.getId().toString()
072                                                                + "\t" + (int)this.currentTime
073                                + "\t0\t0\t1\t0\t" + position.getColorValueBetweenZeroAndOne() // link(0), from node(0), lane(1), dist(0), speed
074                                + "\t1\t0\t" + position.getId().toString()   // vehtype(1), acceleration(0), driver-id
075                                + "\t0\t" + position.getEasting()   // # of passengers(0), easting
076                                + "\t" + position.getNorthing()
077                                + "\t0" // elevation
078                                + "\t0" // azimuth
079                                + "\t"+ "0" + "\n"; // user(0)
080                try {
081                        out.write(buffer);
082                } catch (IOException e) {
083                        e.printStackTrace();
084                }
085        }
086
087        @Override
088        public void beginSnapshot(double time) {
089                this.currentTime = time;
090        }
091
092        @Override
093        public void endSnapshot() {
094                this.currentTime = -1;
095        }
096
097        @Override
098        public void finish() {
099                if (this.out != null) {
100                        try {
101                                out.close();
102                        } catch (IOException e) {
103                                e.printStackTrace();
104                        }
105                }
106        }
107}