MATSIM
TransitScheduleWriterV2.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2018 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.pt.transitSchedule;
21 
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.io.UncheckedIOException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 
29 import org.matsim.api.core.v01.Coord;
30 import org.matsim.api.core.v01.Id;
47 
53 public class TransitScheduleWriterV2 extends MatsimXmlWriter implements MatsimSomeWriter {
54 
56  private final TransitSchedule schedule;
58 
59  public TransitScheduleWriterV2(final TransitSchedule schedule) {
60  this(new IdentityTransformation() , schedule);
61  }
62 
64  final CoordinateTransformation coordinateTransformation,
65  final TransitSchedule schedule) {
66  this.coordinateTransformation = coordinateTransformation;
67  this.schedule = schedule;
68  }
69 
70  public void write(OutputStream stream) throws UncheckedIOException {
71  this.openOutputStream(stream);
72  try {
73  writeData();
74  } catch (IOException e) {
75  throw new UncheckedIOException(e);
76  }
77  }
78 
79  public void write(final String filename) throws UncheckedIOException {
80  this.openFile(filename);
81  try {
82  writeData();
83  } catch (IOException e) {
84  throw new UncheckedIOException(e);
85  }
86  }
87 
88  private void writeData() throws IOException, UncheckedIOException {
89  this.writeXmlHead();
90  this.writeDoctype(Constants.TRANSIT_SCHEDULE, "http://www.matsim.org/files/dtd/transitSchedule_v2.dtd");
91  this.writeStartTag(Constants.TRANSIT_SCHEDULE, null);
92  this.writer.write(NL);
93  this.attributesWriter.writeAttributes( "\t" , this.writer , this.schedule.getAttributes() );
94 
95  this.writeTransitStops();
97  for (TransitLine line : this.schedule.getTransitLines().values()) {
98  writeTransitLine(line);
99  }
100  this.writeEndTag(Constants.TRANSIT_SCHEDULE);
101  this.close();
102  }
103 
104  private void writeTransitStops() throws IOException, UncheckedIOException {
105  this.writeStartTag(Constants.TRANSIT_STOPS, null);
106 
107  List<Tuple<String, String>> attributes = new ArrayList<>(5);
108  for (TransitStopFacility stop : this.schedule.getFacilities().values()) {
109  attributes.clear();
110  attributes.add(createTuple(Constants.ID, stop.getId().toString()));
111  final Coord coord = this.coordinateTransformation.transform( stop.getCoord() );
112  attributes.add(createTuple("x", coord.getX()));
113  attributes.add(createTuple("y", coord.getY()));
114  if (coord.hasZ()) {
115  attributes.add(createTuple("z", coord.getZ()));
116  }
117  if (stop.getLinkId() != null) {
118  attributes.add(createTuple("linkRefId", stop.getLinkId().toString()));
119  }
120  if (stop.getName() != null) {
121  attributes.add(createTuple("name", stop.getName()));
122  }
123  if (stop.getStopAreaId() != null) {
124  attributes.add(createTuple(Constants.STOP_AREA_ID, stop.getStopAreaId().toString()));
125  }
126  attributes.add(createTuple("isBlocking", stop.getIsBlockingLane()));
127  if (AttributesUtils.isEmpty(stop.getAttributes())) {
128  this.writeStartTag(Constants.STOP_FACILITY, attributes, true);
129  } else {
130  this.writeStartTag(Constants.STOP_FACILITY, attributes, false);
131  if (!AttributesUtils.isEmpty(stop.getAttributes())) {
132  this.writer.write(NL);
133  this.attributesWriter.writeAttributes("\t\t\t", this.writer, stop.getAttributes());
134  }
135  this.writeEndTag(Constants.STOP_FACILITY);
136 
137  }
138  }
139 
140  this.writeEndTag(Constants.TRANSIT_STOPS);
141  }
142 
143  private void writeMinimalTransferTimes() {
144  List<Tuple<String, String>> attributes = new ArrayList<>(5);
146  if (iter.hasNext()) {
147  this.writeStartTag(Constants.MINIMAL_TRANSFER_TIMES, attributes);
148  while (iter.hasNext()) {
149  iter.next();
150  attributes.clear();
151  attributes.add(createTuple(Constants.FROM_STOP, iter.getFromStopId().toString()));
152  attributes.add(createTuple(Constants.TO_STOP, iter.getToStopId().toString()));
153  attributes.add(createTuple(Constants.TRANSFER_TIME, iter.getSeconds()));
154  this.writeStartTag(Constants.RELATION, attributes, true);
155  }
156  this.writeEndTag(Constants.MINIMAL_TRANSFER_TIMES);
157  }
158  }
159 
160  private void writeTransitLine(final TransitLine line) throws IOException, UncheckedIOException {
161  List<Tuple<String, String>> attributes = new ArrayList<>(1);
162  attributes.add(createTuple(Constants.ID, line.getId().toString()));
163  if (line.getName() != null) {
164  attributes.add(createTuple(Constants.NAME, line.getName()));
165  }
166  this.writeStartTag(Constants.TRANSIT_LINE, attributes);
167  if (!AttributesUtils.isEmpty(line.getAttributes())) {
168  this.writer.write(NL);
169  this.attributesWriter.writeAttributes("\t\t", this.writer, line.getAttributes());
170  }
171 
172  for (TransitRoute route : line.getRoutes().values()) {
173  writeTransitRoute(route);
174  }
175 
176  this.writeEndTag(Constants.TRANSIT_LINE);
177  }
178 
179  private void writeTransitRoute(final TransitRoute route) throws IOException, UncheckedIOException {
180  List<Tuple<String, String>> attributes = new ArrayList<>(1);
181  attributes.add(createTuple(Constants.ID, route.getId().toString()));
182  this.writeStartTag(Constants.TRANSIT_ROUTE, attributes);
183 
184  if (!AttributesUtils.isEmpty(route.getAttributes())) {
185  this.writer.write(NL);
186  this.attributesWriter.writeAttributes("\t\t\t", this.writer, route.getAttributes());
187  }
188 
189  if (route.getDescription() != null) {
190  this.writeStartTag(Constants.DESCRIPTION, null);
191  this.writeContent(route.getDescription(), false);
192  this.writeEndTag(Constants.DESCRIPTION);
193  }
194 
195  this.writeStartTag(Constants.TRANSPORT_MODE, null);
196  this.writeContent(route.getTransportMode(), false);
197  this.writeEndTag(Constants.TRANSPORT_MODE);
198 
199  this.writeRouteProfile(route.getStops());
200  this.writeRoute(route.getRoute());
201  this.writeDepartures(route.getDepartures());
202 
203  this.writeEndTag(Constants.TRANSIT_ROUTE);
204  }
205 
206  private void writeRouteProfile(final List<TransitRouteStop> stops) throws UncheckedIOException {
207  this.writeStartTag(Constants.ROUTE_PROFILE, null);
208 
209  // optimization: only create one List for multiple departures
210  List<Tuple<String, String>> attributes = new ArrayList<>(4);
211  for (TransitRouteStop stop : stops) {
212  attributes.clear();
213  attributes.add(createTuple(Constants.REF_ID, stop.getStopFacility().getId().toString()));
214  stop.getArrivalOffset()
215  .ifDefined(offset -> attributes.add(createTimeTuple(Constants.ARRIVAL_OFFSET, offset)));
216  stop.getDepartureOffset().ifDefined(offset->
217  attributes.add(createTimeTuple(Constants.DEPARTURE_OFFSET, offset)));
218  // do not write out if it is true ==> the default value
219  if (!stop.isAllowBoarding()) {
220  attributes.add(createTuple(Constants.ALLOW_BOARDING, String.valueOf(stop.isAllowBoarding())));
221  }
222  if (!stop.isAllowAlighting()) {
223  attributes.add(createTuple(Constants.ALLOW_ALIGHTING, String.valueOf(stop.isAllowAlighting())));
224  }
225  attributes.add(createTuple(Constants.AWAIT_DEPARTURE, String.valueOf(stop.isAwaitDepartureTime())));
226  this.writeStartTag(Constants.STOP, attributes, true);
227  }
228 
229  this.writeEndTag(Constants.ROUTE_PROFILE);
230  }
231 
232  private void writeRoute(final NetworkRoute route) throws UncheckedIOException {
233  if (route != null) {
234  this.writeStartTag(Constants.ROUTE, null);
235 
236  // optimization: only create one List for multiple departures
237  List<Tuple<String, String>> attributes = new ArrayList<>(1);
238  attributes.add(createTuple(Constants.REF_ID, route.getStartLinkId().toString()));
239  this.writeStartTag(Constants.LINK, attributes, true);
240 
241  for (Id<Link> linkId : route.getLinkIds()) {
242  attributes.clear();
243  attributes.add(createTuple(Constants.REF_ID, linkId.toString()));
244  this.writeStartTag(Constants.LINK, attributes, true);
245  }
246 
247  attributes.clear();
248  attributes.add(createTuple(Constants.REF_ID, route.getEndLinkId().toString()));
249  this.writeStartTag(Constants.LINK, attributes, true);
250 
251  this.writeEndTag(Constants.ROUTE);
252  }
253  }
254 
255  private void writeDepartures(final Map<Id<Departure>, Departure> departures) throws IOException, UncheckedIOException {
256  this.writeStartTag(Constants.DEPARTURES, null);
257 
258  // optimization: only create one List for multiple departures
259  List<Tuple<String, String>> attributes = new ArrayList<>(3);
260 
261  for (Departure dep : departures.values()) {
262  attributes.clear();
263  attributes.add(createTuple(Constants.ID, dep.getId().toString()));
264  attributes.add(createTimeTuple(Constants.DEPARTURE_TIME, dep.getDepartureTime()));
265  if (dep.getVehicleId() != null) {
266  attributes.add(createTuple(Constants.VEHICLE_REF_ID, dep.getVehicleId().toString()));
267  }
268  if (AttributesUtils.isEmpty(dep.getAttributes())) {
269  this.writeStartTag(Constants.DEPARTURE, attributes, true);
270  } else {
271  this.writeStartTag(Constants.DEPARTURE, attributes, false);
272  this.writer.write(NL);
273  this.attributesWriter.writeAttributes("\t\t\t\t\t", this.writer, dep.getAttributes());
274  this.writeEndTag(Constants.DEPARTURE);
275  }
276 
277  }
278 
279  this.writeEndTag(Constants.DEPARTURES);
280  }
281 }
void writeRouteProfile(final List< TransitRouteStop > stops)
Map< Id< TransitStopFacility >, TransitStopFacility > getFacilities()
void writeDepartures(final Map< Id< Departure >, Departure > departures)
static Tuple< String, String > createTimeTuple(String one, double sec)
final void openOutputStream(OutputStream outputStream)
final void writeContent(String content, boolean allowWhitespaces)
final void writeAttributes(final String indentation, final BufferedWriter writer, final Attributes attributes)
TransitScheduleWriterV2(final CoordinateTransformation coordinateTransformation, final TransitSchedule schedule)
final void writeStartTag(String tagname, List< Tuple< String, String >> attributes)
static Tuple< String, String > createTuple(String one, String two)
Map< Id< TransitLine >, TransitLine > getTransitLines()
final void writeDoctype(String rootTag, String dtdUrl)