1 package org.matsim.counts;
3 import it.unimi.dsi.fastutil.ints.Int2DoubleMap;
4 import org.apache.logging.log4j.LogManager;
5 import org.apache.logging.log4j.Logger;
12 import java.io.IOException;
13 import java.nio.file.Path;
14 import java.util.ArrayList;
15 import java.util.List;
21 final class CountsWriterV2
extends MatsimXmlWriter {
23 private static final Logger logger = LogManager.getLogger(CountsWriterV2.class);
27 private final AttributesXmlWriterDelegate attributesWriter =
new AttributesXmlWriterDelegate();
28 private final CoordinateTransformation coordinateTransformation;
34 public CountsWriterV2(CoordinateTransformation coordinateTransformation,
Counts<?> counts) {
35 this.coordinateTransformation = coordinateTransformation;
44 public void write(String filename)
throws IOException {
45 logger.info(
"Write Counts to {}", filename);
50 this.writeRootElement();
60 public void write(Path filename)
throws IOException {
61 write(filename.toString());
64 private void writeRootElement() {
66 List<Tuple<String, String>> atts =
new ArrayList<>();
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"));
85 attributesWriter.writeAttributes(
"\t", this.writer, counts.
getAttributes());
93 private void writeCounts() {
97 List<Tuple<String, String>> attributes =
new ArrayList<>();
99 attributes.add(
createTuple(
"refId", count.getRefId().toString()));
101 if (count.getId() != null)
104 if (count.getStationName() != null)
105 attributes.add(
createTuple(
"name", count.getStationName()));
107 if (count.getDescription() != null)
110 if (count.getCoordinates() != null) {
111 Coord c = coordinateTransformation.transform(count.getCoordinates());
116 writeStartTag(MeasurementLocation.ELEMENT_NAME, attributes,
false,
true);
118 attributesWriter.writeAttributes(
"\t\t", this.writer, count.getAttributes());
121 writeMeasurables(count);
131 Map<MeasurementLocation.TypeAndMode, Measurable> measurables = count.getMeasurables();
133 for (Map.Entry<MeasurementLocation.TypeAndMode, Measurable> entry : measurables.entrySet()) {
135 Measurable m = entry.getValue();
137 int interval = m.getInterval();
140 new Tuple<>(
"type", m.getMeasurableType()),
141 new Tuple<>(
"networkMode", m.getMode()),
142 new Tuple<>(
"interval", String.valueOf(interval)))
146 Int2DoubleMap values = m.getValues();
148 for (
int second : values.keySet()) {
149 double v = values.get(second);
153 super.writer.write(
"\t\t\t\t<value ");
155 super.writer.write(
"t=\"" + second +
"\" ");
157 super.writer.write(
"val=\"" + v +
"\" ");
160 super.writer.write(
"/>");
162 }
catch (IOException e) {
163 logger.error(
"Error writing Measurables", e);
170 private void writeEmptyLine() {
173 }
catch (IOException e) {
174 logger.warn(
"Error writing counts", e);
final void writeXmlHead()
Attributes getAttributes()
Map< Id< T >, MeasurementLocation< T > > getMeasureLocations()
final void writeContent(String content, boolean allowWhitespaces)
final void openFile(final String filename)
final void writeStartTag(String tagname, List< Tuple< String, String >> attributes)
static Tuple< String, String > createTuple(String one, String two)
final void writeEndTag(String tagname)