MATSIM
PopulationWriterHandlerImplV0.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PlansWriterHandlerImplV0.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.population.io;
22 
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 
26 import org.matsim.api.core.v01.Coord;
40 import org.matsim.core.utils.misc.Time;
41 
42  /*package*/ class PopulationWriterHandlerImplV0 extends AbstractPopulationWriterHandler {
43 
44  private final CoordinateTransformation coordinateTransformation;
45  private final Network network;
46 
47  protected PopulationWriterHandlerImplV0(
48  final CoordinateTransformation coordinateTransformation,
49  final Network network) {
50  this.coordinateTransformation = coordinateTransformation;
51  this.network = network;
52  }
53 
55  //
56  // interface implementation
57  //
59 
60  @Override
61  public void writeHeaderAndStartElement(BufferedWriter out) throws IOException {
62  out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
63  out.write("<!DOCTYPE plans SYSTEM \"" + MatsimXmlWriter.DEFAULT_DTD_LOCATION + "plans_v0.dtd\">\n\n");
64  }
65 
67  // <plans ... > ... </plans>
69 
70  @Override
71  public void startPlans(final Population plans, final BufferedWriter out) throws IOException {
72  out.write("<plans>\n\n");
73  }
74 
75  @Override
76  public void endPlans(final BufferedWriter out) throws IOException {
77  out.write("</plans>\n");
78  out.flush();
79  }
80 
82  // <person ... > ... </person>
84 
85  @Override
86  public void startPerson(final Person person, final BufferedWriter out) throws IOException {
87  out.write("\t<person");
88  out.write(" id=\"" + person.getId() + "\"");
89  out.write(">\n");
90  }
91 
92  @Override
93  public void endPerson(final BufferedWriter out) throws IOException {
94  out.write("\t</person>\n\n");
95  }
96 
98  // <travelcard ... />
100 
101  @Override
102  public void startTravelCard(final String travelcard, final BufferedWriter out) throws IOException {
103  }
104 
105  @Override
106  public void endTravelCard(final BufferedWriter out) throws IOException {
107  }
108 
110  // <plan ... > ... </plan>
112 
113  @Override
114  public void startPlan(final Plan plan, final BufferedWriter out) throws IOException {
115  out.write("\t\t<plan");
116  if (plan.getScore() != null)
117  out.write(" score=\"" + plan.getScore().toString() + "\"");
118  if (PersonUtils.isSelected(plan))
119  out.write(" selected=\"" + "yes" + "\"");
120  else
121  out.write(" selected=\"" + "no" + "\"");
122  out.write(">\n");
123  }
124 
125  @Override
126  public void endPlan(final BufferedWriter out) throws IOException {
127  out.write("\t\t</plan>\n\n");
128  }
129 
131  // <act ... > ... </act>
133 
134  @Override
135  public void startAct(final Activity act, final BufferedWriter out) throws IOException {
136  out.write("\t\t\t<act");
137  out.write(" type=\"" + act.getType() + "\"");
138  if (act.getCoord() != null) {
139  final Coord coord = coordinateTransformation.transform( act.getCoord() );
140  out.write(" x100=\"" + coord.getX() + "\"");
141  out.write(" y100=\"" + coord.getY() + "\"");
142  }
143  if (act.getLinkId() != null)
144  out.write(" link=\"" + act.getLinkId() + "\"");
145  if (act.getStartTime().seconds() != Integer.MIN_VALUE)
146  out.write(" start_time=\"" + Time.writeTime(act.getStartTime().seconds()) + "\"");
147  if (act.getMaximumDuration().isDefined())
148  out.write(" dur=\"" + Time.writeTime(act.getMaximumDuration().seconds()) + "\"");
149  if (act.getEndTime().seconds() != Integer.MIN_VALUE)
150  out.write(" end_time=\"" + Time.writeTime(act.getEndTime().seconds()) + "\"");
151  out.write(" />\n");
152  }
153 
154  @Override
155  public void endAct(final BufferedWriter out) throws IOException {
156  }
157 
159  // <leg ... > ... </leg>
161 
162  @Override
163  public void startLeg(final Leg leg, final BufferedWriter out) throws IOException {
164  out.write("\t\t\t<leg");
165  out.write(" mode=\"" + leg.getMode() + "\"");
166  if (leg.getDepartureTime().seconds() != Integer.MIN_VALUE)
167  out.write(" dep_time=\"" + Time.writeTime(leg.getDepartureTime().seconds()) + "\"");
168  if (leg.getTravelTime().seconds() != Integer.MIN_VALUE)
169  out.write(" trav_time=\"" + Time.writeTime(leg.getTravelTime().seconds()) + "\"");
170 // if (leg instanceof LegImpl){
171 // LegImpl l = (LegImpl)leg;
172 // if (l.getDepartureTime() + l.getTravelTime() != Time.getUndefinedTime())
173 // out.write(" arr_time=\"" + Time.writeTime(l.getDepartureTime() + l.getTravelTime()) + "\"");
174 // }
175  // arrival time is in dtd, but no longer evaluated in code (according to not being in API). kai, jun'16
176  out.write(">\n");
177  }
178 
179  @Override
180  public void endLeg(final BufferedWriter out) throws IOException {
181  out.write("\t\t\t</leg>\n");
182  }
183 
185  // <route ... > ... </route>
187 
188  @Override
189  public void startRoute(final Route route, final BufferedWriter out) throws IOException {
190  out.write("\t\t\t\t<route>");
191 
192  if (route instanceof NetworkRoute) {
193  for (Node n : RouteUtils.getNodes((NetworkRoute) route, this.network)) {
194  out.write(n.getId() + " ");
195  }
196  } else {
197  out.write(route.getRouteDescription());
198  }
199  }
200 
201  @Override
202  public void endRoute(final BufferedWriter out) throws IOException {
203  out.write("</route>\n");
204  }
205 
207  // <!-- ============ ... ========== -->
209 
210  @Override
211  public void writeSeparator(final BufferedWriter out) throws IOException {
212  out.write("<!-- =================================================" +
213  "===================== -->\n\n");
214  }
215 }