MATSIM
TransitScheduleWriterV1.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * TransitScheduleWriter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 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.pt.transitSchedule;
22 
23 import java.io.UncheckedIOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 
28 import org.matsim.api.core.v01.Coord;
29 import org.matsim.api.core.v01.Id;
43 
49 public class TransitScheduleWriterV1 extends MatsimXmlWriter implements MatsimSomeWriter {
50 
52 
53  private final TransitSchedule schedule;
54 
55  public TransitScheduleWriterV1(final TransitSchedule schedule) {
56  this( new IdentityTransformation() , schedule );
57  }
58 
60  final CoordinateTransformation coordinateTransformation,
61  final TransitSchedule schedule) {
62  this.coordinateTransformation = coordinateTransformation;
63  this.schedule = schedule;
64  }
65 
66  public void write(final String filename) throws UncheckedIOException {
67  this.openFile(filename);
68  this.writeXmlHead();
69  this.writeDoctype(Constants.TRANSIT_SCHEDULE, "http://www.matsim.org/files/dtd/transitSchedule_v1.dtd");
70  this.writeStartTag(Constants.TRANSIT_SCHEDULE, null);
71 
72  this.writeTransitStops();
73  for (TransitLine line : this.schedule.getTransitLines().values()) {
74  writeTransitLine(line);
75  }
76  this.writeEndTag(Constants.TRANSIT_SCHEDULE);
77  this.close();
78  }
79 
80  private void writeTransitStops() throws UncheckedIOException {
81  this.writeStartTag(Constants.TRANSIT_STOPS, null);
82 
83  List<Tuple<String, String>> attributes = new ArrayList<Tuple<String, String>>(5);
84  for (TransitStopFacility stop : this.schedule.getFacilities().values()) {
85  attributes.clear();
86  attributes.add(createTuple(Constants.ID, stop.getId().toString()));
87  final Coord coord = coordinateTransformation.transform( stop.getCoord() );
88  attributes.add(createTuple("x", coord.getX()));
89  attributes.add(createTuple("y", coord.getY()));
90  if (stop.getLinkId() != null) {
91  attributes.add(createTuple("linkRefId", stop.getLinkId().toString()));
92  }
93  if (stop.getName() != null) {
94  attributes.add(createTuple("name", stop.getName()));
95  }
96  attributes.add(createTuple("isBlocking", stop.getIsBlockingLane()));
97  this.writeStartTag(Constants.STOP_FACILITY, attributes, true);
98  }
99 
100  this.writeEndTag(Constants.TRANSIT_STOPS);
101  }
102 
103  private void writeTransitLine(final TransitLine line) throws UncheckedIOException {
104  List<Tuple<String, String>> attributes = new ArrayList<Tuple<String, String>>(1);
105  attributes.add(createTuple(Constants.ID, line.getId().toString()));
106  if (line.getName() != null) {
107  attributes.add(createTuple(Constants.NAME, line.getName()));
108  }
109  this.writeStartTag(Constants.TRANSIT_LINE, attributes);
110 
111  for (TransitRoute route : line.getRoutes().values()) {
112  writeTransitRoute(route);
113  }
114 
115  this.writeEndTag(Constants.TRANSIT_LINE);
116  }
117 
118  private void writeTransitRoute(final TransitRoute route) throws UncheckedIOException {
119  List<Tuple<String, String>> attributes = new ArrayList<Tuple<String, String>>(1);
120  attributes.add(createTuple(Constants.ID, route.getId().toString()));
121  this.writeStartTag(Constants.TRANSIT_ROUTE, attributes);
122 
123  if (route.getDescription() != null) {
124  this.writeStartTag(Constants.DESCRIPTION, null);
125  this.writeContent(route.getDescription(), false);
126  this.writeEndTag(Constants.DESCRIPTION);
127  }
128 
129  this.writeStartTag(Constants.TRANSPORT_MODE, null);
130  this.writeContent(route.getTransportMode(), false);
131  this.writeEndTag(Constants.TRANSPORT_MODE);
132 
133  this.writeRouteProfile(route.getStops());
134  this.writeRoute(route.getRoute());
135  this.writeDepartures(route.getDepartures());
136 
137  this.writeEndTag(Constants.TRANSIT_ROUTE);
138  }
139 
140  private void writeRouteProfile(final List<TransitRouteStop> stops) throws UncheckedIOException {
141  this.writeStartTag(Constants.ROUTE_PROFILE, null);
142 
143  // optimization: only create one List for multiple departures
144  List<Tuple<String, String>> attributes = new ArrayList<Tuple<String, String>>(4);
145  for (TransitRouteStop stop : stops) {
146  attributes.clear();
147  attributes.add(createTuple(Constants.REF_ID, stop.getStopFacility().getId().toString()));
148  stop.getArrivalOffset()
149  .ifDefined(offset -> attributes.add(createTimeTuple(Constants.ARRIVAL_OFFSET, offset)));
150  stop.getDepartureOffset().ifDefined(offset->
151  attributes.add(createTimeTuple(Constants.DEPARTURE_OFFSET, offset)));
152  attributes.add(createTuple(Constants.AWAIT_DEPARTURE, String.valueOf(stop.isAwaitDepartureTime())));
153  this.writeStartTag(Constants.STOP, attributes, true);
154  }
155 
156  this.writeEndTag(Constants.ROUTE_PROFILE);
157  }
158 
159  private void writeRoute(final NetworkRoute route) throws UncheckedIOException {
160  if (route != null) {
161  this.writeStartTag(Constants.ROUTE, null);
162 
163  // optimization: only create one List for multiple departures
164  List<Tuple<String, String>> attributes = new ArrayList<Tuple<String, String>>(1);
165  attributes.add(createTuple(Constants.REF_ID, route.getStartLinkId().toString()));
166  this.writeStartTag(Constants.LINK, attributes, true);
167 
168  for (Id<Link> linkId : route.getLinkIds()) {
169  attributes.clear();
170  attributes.add(createTuple(Constants.REF_ID, linkId.toString()));
171  this.writeStartTag(Constants.LINK, attributes, true);
172  }
173 
174  attributes.clear();
175  attributes.add(createTuple(Constants.REF_ID, route.getEndLinkId().toString()));
176  this.writeStartTag(Constants.LINK, attributes, true);
177 
178  this.writeEndTag(Constants.ROUTE);
179  }
180  }
181 
182  private void writeDepartures(final Map<Id<Departure>, Departure> departures) throws UncheckedIOException {
183  this.writeStartTag(Constants.DEPARTURES, null);
184 
185  // optimization: only create one List for multiple departures
186  List<Tuple<String, String>> attributes = new ArrayList<Tuple<String, String>>(3);
187 
188  for (Departure dep : departures.values()) {
189  attributes.clear();
190  attributes.add(createTuple(Constants.ID, dep.getId().toString()));
191  attributes.add(createTimeTuple(Constants.DEPARTURE_TIME, dep.getDepartureTime()));
192  if (dep.getVehicleId() != null) {
193  attributes.add(createTuple(Constants.VEHICLE_REF_ID, dep.getVehicleId().toString()));
194  }
195  this.writeStartTag(Constants.DEPARTURE, attributes, true);
196  }
197 
198  this.writeEndTag(Constants.DEPARTURES);
199  }
200 }
void writeDepartures(final Map< Id< Departure >, Departure > departures)
Map< Id< TransitStopFacility >, TransitStopFacility > getFacilities()
void writeRouteProfile(final List< TransitRouteStop > stops)
TransitScheduleWriterV1(final CoordinateTransformation coordinateTransformation, final TransitSchedule schedule)
static Tuple< String, String > createTimeTuple(String one, double sec)
final void writeContent(String content, boolean allowWhitespaces)
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)