MATSIM
FacilitiesReaderMatsimV1.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * FacilitiesReaderMatsimV1.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 FacilitiesReaderMatsimV1 extends MatsimXmlParser {
48  private static final Logger log = LogManager.getLogger(FacilitiesReaderMatsimV1.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  FacilitiesReaderMatsimV1(
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(FacilitiesReaderMatsimV1.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(
153  new Coord(
154  Double.parseDouble(atts.getValue("x")),
155  Double.parseDouble(atts.getValue("y")))),
156  Id.create(atts.getValue("linkId"),Link.class));
157  } else { // only coord present
158  this.currfacility =
159  this.factory.createActivityFacility(
160  Id.create(atts.getValue("id"), ActivityFacility.class),
161  coordinateTransformation.transform(
162  new Coord(
163  Double.parseDouble(atts.getValue("x")),
164  Double.parseDouble(atts.getValue("y")))));
165  }
166  } else {
167  if (atts.getValue("linkId") !=null) { //only link present
168  this.currfacility =
169  this.factory.createActivityFacility(
170  Id.create(atts.getValue("id"), ActivityFacility.class),
171  Id.create(atts.getValue("linkId"),Link.class));
172  } else { //neither coord nor link present
173  throw new RuntimeException("Neither coordinate nor linkId are available for facility id "+ atts.getValue("id")+". Aborting....");
174  }
175  }
176 
177  ((ActivityFacilityImpl) this.currfacility).setDesc(atts.getValue("desc"));
178  }
179 
180  private void startActivity(final Attributes atts) {
181  this.curractivity = this.factory.createActivityOption(atts.getValue("type"));
182  this.currfacility.addActivityOption(this.curractivity);
183  }
184 
185  private void startCapacity(final Attributes atts) {
186  double cap = Double.parseDouble(atts.getValue("value"));
187  this.curractivity.setCapacity(cap);
188  }
189 
190  private void startOpentime(final Attributes atts) {
191  this.curractivity.addOpeningTime(OpeningTimeImpl.createFromOptionalTimes(
192  Time.parseOptionalTime(atts.getValue("start_time")),
193  Time.parseOptionalTime(atts.getValue("end_time"))));
194  }
195 
196 
197 }
abstract void startTag(String name, Attributes atts, Stack< String > context)