MATSIM
CountsReaderMatsimV2.java
Go to the documentation of this file.
1 package org.matsim.counts;
2 
3 import org.apache.logging.log4j.LogManager;
4 import org.apache.logging.log4j.Logger;
5 import org.matsim.api.core.v01.Coord;
6 import org.matsim.api.core.v01.Id;
14 import org.xml.sax.Attributes;
15 
16 import java.util.Stack;
17 
20 
24 public class CountsReaderMatsimV2 extends MatsimXmlParser {
25  private static final Logger log = LogManager.getLogger( CountsReaderMatsimV2.class );
26  private final static String VALUE = "value";
27  private final Class<? extends Identifiable<?>> idClass;
29  private final Counts<?> counts;
31  private final String externalInputCRS;
32  private final String targetCRS;
36 
37  public CountsReaderMatsimV2(Counts<?> counts, Class<? extends Identifiable<?>> idClass) {
38  this( null, null, counts, idClass );
39  }
40 
41  public CountsReaderMatsimV2( String externalInputCRS, String targetCRS, Counts<?> counts, Class<? extends Identifiable<?>> idClass ) {
43  this.externalInputCRS = externalInputCRS;
44  this.targetCRS = targetCRS;
45  this.counts = counts;
46  this.idClass = idClass;
47 
48  if (externalInputCRS != null && targetCRS != null) {
49  this.coordinateTransformation = TransformationFactory.getCoordinateTransformation(externalInputCRS, targetCRS);
50  ProjectionUtils.putCRS(this.counts, targetCRS);
51  } else if ( externalInputCRS==null && targetCRS==null ){
52  this.coordinateTransformation = new IdentityTransformation();
53  } else {
54  log.warn("finding a coordinate spec on one side but not on the other: inputCRS=" + externalInputCRS + "; targetCRS=" + targetCRS + ". We are assuming that things are consistent, and are continuing anyways." );
55  this.coordinateTransformation = new IdentityTransformation();
56  // yy this is the logic that I fould. One could alternatively fail here. kai, feb'24
57  }
58 
59  }
60 
61  @Override
62  public void startTag(String name, Attributes atts, Stack<String> context) {
63  switch (name) {
65  case MeasurementLocation.ELEMENT_NAME -> startMeasurementLocation(atts);
66  case Measurable.ELEMENT_NAME -> startMeasurable(name, atts);
67  case TAG_ATTRIBUTES, TAG_ATTRIBUTE -> attributesDelegate.startTag(name, atts, context, currAttributes);
68  case VALUE -> addValuesToMeasurable(atts);
69  }
70  }
71 
72  @Override
73  public void endTag(String name, String content, Stack<String> context) {
74  switch( name ) {
75  case TAG_ATTRIBUTES:
76  if (context.peek().equals(Counts.ELEMENT_NAME)) {
77  String inputCRS = (String) counts.getAttributes().getAttribute( ProjectionUtils.INPUT_CRS_ATT );
78  if (inputCRS != null && targetCRS != null) {
79  if (externalInputCRS != null) {
80  // warn or crash?
81  log.warn("coordinate transformation defined both in config and in input file: setting from input file will be used");
82  }
83  coordinateTransformation = TransformationFactory.getCoordinateTransformation(inputCRS, targetCRS );
84  ProjectionUtils.putCRS(counts, targetCRS);
85  }
86  }
87  /* fall-through */
88  case TAG_ATTRIBUTE:
89  attributesDelegate.endTag(name, content, context);
90  break;
91  }
92  }
93 
94  private void addValuesToMeasurable(Attributes atts) {
95  int t = Integer.parseInt(atts.getValue("t"));
96  double val = Double.parseDouble(atts.getValue("val"));
97  this.currMeasurable.setAtSecond(t, val);
98  }
99 
100  private void startMeasurable(String tag, Attributes atts) {
101  int interval = Integer.parseInt(atts.getValue("interval"));
102  currMeasurable = currLocation.createMeasurable(atts.getValue("type"), atts.getValue("networkMode"), interval);
103  }
104 
106  String idString = atts.getValue("refId");
107  Id id = Id.create(idString, this.idClass);
108  String stationName = atts.getValue("name");
109 
110  currLocation = counts.createAndAddMeasureLocation(id, stationName);
111  currLocation.setId(atts.getValue("id"));
112 
113  String x = atts.getValue("x");
114  String y = atts.getValue("y");
115  if (x != null && y != null) {
116  currLocation.setCoordinates(coordinateTransformation.transform(
117  new Coord(Double.parseDouble(x), Double.parseDouble(y))
118  ));
119  }
120 
121  currAttributes = currLocation.getAttributes();
122  }
123 
124  private void startMultiModeCounts(Attributes atts) {
125  currAttributes = counts.getAttributes();
126 
127  for (int i = 0; i < atts.getLength(); i++) {
128  String name = atts.getQName(i);
129  String value = atts.getValue(i);
130 
131  switch (name) {
132  case "name" -> counts.setName(value);
133  case "source" -> counts.setSource(value);
134  case "year" -> counts.setYear(Integer.parseInt(value));
135  case "description" -> counts.setDescription(value);
136  }
137  }
138  }
139 }
Measurable createMeasurable(String typeOfMeasurableData, String mode, int interval)
org.matsim.utils.objectattributes.attributable.Attributes currAttributes
void setSource(String source)
Definition: Counts.java:69
MeasurementLocation< T > createAndAddMeasureLocation(final Id< T > id, String stationName)
Definition: Counts.java:37
void startMeasurable(String tag, Attributes atts)
static CoordinateTransformation getCoordinateTransformation(final String fromSystem, final String toSystem)
CountsReaderMatsimV2(Counts<?> counts, Class<? extends Identifiable<?>> idClass)
static< T extends MatsimToplevelContainer &Attributable > void putCRS(T container, String CRS)
CountsReaderMatsimV2(String externalInputCRS, String targetCRS, Counts<?> counts, Class<? extends Identifiable<?>> idClass)
Attributes getAttributes()
Definition: Counts.java:146
void setName(String name)
Definition: Counts.java:53
void startTag(String name, Attributes atts, Stack< String > context)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
void endTag(String name, String content, Stack< String > context)
void setDescription(String description)
Definition: Counts.java:61
void setYear(int year)
Definition: Counts.java:77
void setAtSecond(int seconds, double value)
Definition: Measurable.java:76
static final String ELEMENT_NAME
Definition: Counts.java:23
final Class<? extends Identifiable<?> > idClass
final AttributesXmlReaderDelegate attributesDelegate
CoordinateTransformation coordinateTransformation
void startTag(String name, org.xml.sax.Attributes atts, Stack< String > context, Attributes currentAttributes)