MATSIM
CountsWriterV2.java
Go to the documentation of this file.
1 package org.matsim.counts;
2 
3 import it.unimi.dsi.fastutil.ints.Int2DoubleMap;
4 import org.apache.logging.log4j.LogManager;
5 import org.apache.logging.log4j.Logger;
6 import org.matsim.api.core.v01.Coord;
11 
12 import java.io.IOException;
13 import java.nio.file.Path;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Map;
17 
21 final class CountsWriterV2 extends MatsimXmlWriter {
22 
23  private static final Logger logger = LogManager.getLogger(CountsWriterV2.class);
24 
25  private final Counts<?> counts;
26 
27  private final AttributesXmlWriterDelegate attributesWriter = new AttributesXmlWriterDelegate();
28  private final CoordinateTransformation coordinateTransformation;
29 
30 
34  public CountsWriterV2(CoordinateTransformation coordinateTransformation, Counts<?> counts) {
35  this.coordinateTransformation = coordinateTransformation;
36  this.counts = counts;
37  }
38 
44  public void write(String filename) throws IOException {
45  logger.info("Write Counts to {}", filename);
46 
47  this.openFile(filename);
48 
49  this.writeXmlHead();
50  this.writeRootElement();
51 
52  super.close();
53  }
54 
60  public void write(Path filename) throws IOException {
61  write(filename.toString());
62  }
63 
64  private void writeRootElement() {
65 
66  List<Tuple<String, String>> atts = new ArrayList<>();
67 
68  atts.add(createTuple(XMLNS, MatsimXmlWriter.MATSIM_NAMESPACE));
69  atts.add(createTuple(XMLNS + ":xsi", DEFAULTSCHEMANAMESPACELOCATION));
70  atts.add(createTuple("xsi:schemaLocation", MATSIM_NAMESPACE + " " + DEFAULT_DTD_LOCATION + "counts_v2.xsd"));
71 
72  if (counts.getSource() != null)
73  atts.add(createTuple("source", counts.getSource()));
74 
75  if (counts.getName() != null)
76  atts.add(createTuple("name", counts.getName()));
77 
78  if (counts.getDescription() != null)
79  atts.add(createTuple("description", counts.getDescription()));
80 
81  atts.add(createTuple("year", String.valueOf(counts.getYear())));
82 
83  this.writeStartTag(Counts.ELEMENT_NAME, atts, false, true);
84 
85  attributesWriter.writeAttributes("\t", this.writer, counts.getAttributes());
86 
87  writeCounts();
88 
89  this.writeContent("\n", true);
90  this.writeEndTag(Counts.ELEMENT_NAME);
91  }
92 
93  private void writeCounts() {
94 
95  for (MeasurementLocation<?> count : counts.getMeasureLocations().values()) {
96 
97  List<Tuple<String, String>> attributes = new ArrayList<>();
98 
99  attributes.add(createTuple("refId", count.getRefId().toString()));
100 
101  if (count.getId() != null)
102  attributes.add(createTuple("id", count.getId()));
103 
104  if (count.getStationName() != null)
105  attributes.add(createTuple("name", count.getStationName()));
106 
107  if (count.getDescription() != null)
108  attributes.add(createTuple("description", counts.getDescription()));
109 
110  if (count.getCoordinates() != null) {
111  Coord c = coordinateTransformation.transform(count.getCoordinates());
112  attributes.add(createTuple("x", c.getX()));
113  attributes.add(createTuple("y", c.getY()));
114  }
115 
116  writeStartTag(MeasurementLocation.ELEMENT_NAME, attributes, false, true);
117 
118  attributesWriter.writeAttributes("\t\t", this.writer, count.getAttributes());
119 
120  //write volumes for each mode
121  writeMeasurables(count);
122 
123  writeEndTag(MeasurementLocation.ELEMENT_NAME);
124  writeEmptyLine();
125  }
126 
127  }
128 
129  private void writeMeasurables(MeasurementLocation<?> count) {
130 
131  Map<MeasurementLocation.TypeAndMode, Measurable> measurables = count.getMeasurables();
132 
133  for (Map.Entry<MeasurementLocation.TypeAndMode, Measurable> entry : measurables.entrySet()) {
134 
135  Measurable m = entry.getValue();
136 
137  int interval = m.getInterval();
138 
139  writeStartTag(Measurable.ELEMENT_NAME, List.of(
140  new Tuple<>("type", m.getMeasurableType()),
141  new Tuple<>("networkMode", m.getMode()),
142  new Tuple<>("interval", String.valueOf(interval)))
143  );
144 
145  //write values
146  Int2DoubleMap values = m.getValues();
147  try {
148  for (int second : values.keySet()) {
149  double v = values.get(second);
150  writeEmptyLine();
151 
152  //start tag
153  super.writer.write("\t\t\t\t<value ");
154 
155  super.writer.write("t=\"" + second + "\" ");
156 
157  super.writer.write("val=\"" + v + "\" ");
158 
159  //end tag
160  super.writer.write("/>");
161  }
162  } catch (IOException e) {
163  logger.error("Error writing Measurables", e);
164  }
165 
166  writeEndTag(Measurable.ELEMENT_NAME);
167  }
168  }
169 
170  private void writeEmptyLine() {
171  try {
172  this.writer.write(NL);
173  } catch (IOException e) {
174  logger.warn("Error writing counts", e);
175  }
176  }
177 }
Attributes getAttributes()
Definition: Counts.java:146
Map< Id< T >, MeasurementLocation< T > > getMeasureLocations()
Definition: Counts.java:92
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)