MATSIM
FacilitiesReaderMatsimV2.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * FacilitiesReaderMatsimV2.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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.facilities;
22 
23 import java.util.Map;
24 import java.util.Stack;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.matsim.api.core.v01.Coord;
29 import org.matsim.api.core.v01.Id;
36 import org.matsim.core.utils.misc.Time;
39 import org.xml.sax.Attributes;
40 
47 final class FacilitiesReaderMatsimV2 extends MatsimXmlParser {
48  private static final Logger log = LogManager.getLogger(FacilitiesReaderMatsimV2.class);
49 
50  private final static String FACILITIES = "facilities";
51  private final static String FACILITY = "facility";
52  private final static String ACTIVITY = "activity";
53  private final static String CAPACITY = "capacity";
54  private final static String OPENTIME = "opentime";
55  private static final String ATTRIBUTES = "attributes";
56  private static final String ATTRIBUTE = "attribute";
57 
58  private final ActivityFacilities facilities;
59  private final ActivityFacilitiesFactory factory;
60  private final AttributesXmlReaderDelegate attributesReader = new AttributesXmlReaderDelegate();
61  private ActivityFacility currfacility = null;
62  private ActivityOption curractivity = null;
63  private org.matsim.utils.objectattributes.attributable.Attributes currAttributes = null;
64 
65  private final String externalInputCRS;
66  private final String targetCRS;
67  private CoordinateTransformation coordinateTransformation = new IdentityTransformation();
68 
69  FacilitiesReaderMatsimV2(
70  final String externalInputCRS,
71  final String targetCRS,
72  final ActivityFacilities facilities) {
73  super(ValidationType.DTD_ONLY);
74  this.externalInputCRS = externalInputCRS;
75  this.targetCRS = targetCRS;
76  this.facilities = facilities;
77  this.factory = this.facilities.getFactory();
78  if (externalInputCRS != null && targetCRS != null) {
79  this.coordinateTransformation = TransformationFactory.getCoordinateTransformation(externalInputCRS, targetCRS);
80  ProjectionUtils.putCRS(this.facilities, targetCRS);
81  }
82  }
83 
84  public void putAttributeConverter(Class<?> clazz, AttributeConverter<?> converter) {
85  this.attributesReader.putAttributeConverter(clazz, converter);
86  }
87 
88  public void putAttributeConverters(Map<Class<?>, AttributeConverter<?>> converters) {
89  this.attributesReader.putAttributeConverters(converters);
90  }
91 
92  @Override
93  public void startTag(final String name, final org.xml.sax.Attributes atts, final Stack<String> context) {
94  if (FACILITIES.equals(name)) {
95  startFacilities(atts);
96  } else if (FACILITY.equals(name)) {
97  startFacility(atts);
98  } else if (ACTIVITY.equals(name)) {
99  startActivity(atts);
100  } else if (CAPACITY.equals(name)) {
101  startCapacity(atts);
102  } else if (OPENTIME.equals(name)) {
103  startOpentime(atts);
104  } else if (ATTRIBUTE.equals(name)) {
105  this.attributesReader.startTag(name, atts, context, this.currAttributes);
106  } else if (ATTRIBUTES.equals(name)) {
107  currAttributes = context.peek().equals(FACILITIES) ? this.facilities.getAttributes() : this.currfacility.getAttributes();
108  attributesReader.startTag(name, atts, context, currAttributes);
109  }
110  }
111 
112  @Override
113  public void endTag(final String name, final String content, final Stack<String> context) {
114  if (FACILITY.equals(name)) {
115  this.facilities.addActivityFacility(this.currfacility);
116  this.currfacility = null;
117  } else if (ACTIVITY.equals(name)) {
118  this.curractivity = null;
119  } else if (ATTRIBUTES.equalsIgnoreCase(name)) {
120  if (context.peek().equals(FACILITIES)) {
121  String inputCRS = (String) currAttributes.getAttribute(ProjectionUtils.INPUT_CRS_ATT);
122 
123  if (inputCRS != null && targetCRS != null) {
124  if (externalInputCRS != null) {
125  // warn or crash?
126  log.warn("coordinate transformation defined both in config and in input file: setting from input file will be used");
127  }
128  coordinateTransformation = TransformationFactory.getCoordinateTransformation(inputCRS, targetCRS);
129  currAttributes.putAttribute(ProjectionUtils.INPUT_CRS_ATT, targetCRS);
130  }
131  }
132  this.currAttributes = null;
133  } else if (ATTRIBUTE.equalsIgnoreCase(name)) {
134  this.attributesReader.endTag(name, content, context);
135  }
136  }
137 
138  private void startFacilities(final Attributes atts) {
139  this.facilities.setName(atts.getValue("name"));
140  this.currAttributes = facilities.getAttributes();
141  if (atts.getValue("aggregation_layer") != null) {
142  LogManager.getLogger(FacilitiesReaderMatsimV2.class).warn("aggregation_layer is deprecated.");
143  }
144  }
145 
146  private void startFacility(final Attributes atts) {
147  if ( atts.getValue("x") !=null && atts.getValue("y") !=null ) {
148  if (atts.getValue("linkId") !=null) { //both coord and link present
149  this.currfacility =
150  this.factory.createActivityFacility(
151  Id.create(atts.getValue("id"), ActivityFacility.class),
152  coordinateTransformation.transform(coordFromAtts(atts)),
153  Id.create(atts.getValue("linkId"),Link.class));
154  } else { // only coord present
155  this.currfacility =
156  this.factory.createActivityFacility(
157  Id.create(atts.getValue("id"), ActivityFacility.class),
158  coordinateTransformation.transform(coordFromAtts(atts)));
159  }
160  } else {
161  if (atts.getValue("linkId") !=null) { //only link present
162  this.currfacility =
163  this.factory.createActivityFacility(
164  Id.create(atts.getValue("id"), ActivityFacility.class),
165  Id.create(atts.getValue("linkId"),Link.class));
166  } else { //neither coord nor link present
167  throw new RuntimeException("Neither coordinate nor linkId are available for facility id "+ atts.getValue("id")+". Aborting....");
168  }
169  }
170 
171  ((ActivityFacilityImpl) this.currfacility).setDesc(atts.getValue("desc"));
172  }
173 
174  private static Coord coordFromAtts(final Attributes atts) {
175  if (atts.getValue("z") != null) {
176  return new Coord(
177  Double.parseDouble(atts.getValue("x")),
178  Double.parseDouble(atts.getValue("y")),
179  Double.parseDouble(atts.getValue("z")));
180  } else {
181  return new Coord(
182  Double.parseDouble(atts.getValue("x")),
183  Double.parseDouble(atts.getValue("y")));
184  }
185  }
186 
187  private void startActivity(final Attributes atts) {
188  this.curractivity = this.factory.createActivityOption(atts.getValue("type"));
189  this.currfacility.addActivityOption(this.curractivity);
190  }
191 
192  private void startCapacity(final Attributes atts) {
193  double cap = Double.parseDouble(atts.getValue("value"));
194  this.curractivity.setCapacity(cap);
195  }
196 
197  private void startOpentime(final Attributes atts) {
198  this.curractivity.addOpeningTime(OpeningTimeImpl.createFromOptionalTimes(
199  Time.parseOptionalTime(atts.getValue("start_time")),
200  Time.parseOptionalTime(atts.getValue("end_time"))));
201  }
202 
203 
204 }
abstract void startTag(String name, Attributes atts, Stack< String > context)