MATSIM
NetworkReaderMatsimV1.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * NetworkReaderMatsimV1.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.core.network.io;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.matsim.api.core.v01.Coord;
26 import org.matsim.api.core.v01.Id;
35 import org.matsim.core.utils.misc.Time;
36 import org.xml.sax.Attributes;
37 
38 import java.util.HashSet;
39 import java.util.Set;
40 import java.util.Stack;
41 
47 final class NetworkReaderMatsimV1 extends MatsimXmlParser {
48 
49  private final static String NETWORK = "network";
50  private final static String LINKS = "links";
51  private final static String NODES = "nodes";
52  private final static String NODE = "node";
53  private final static String LINK = "link";
54 
55  private final Network network;
56 
57  private boolean hasElevation = false;
58  private boolean hasNoElevation = false;
59 
60  // final or settable?
61  private final CoordinateTransformation transformation;
62 
63  private final static Logger log = LogManager.getLogger(NetworkReaderMatsimV1.class);
64 
65  public NetworkReaderMatsimV1(Network network) {
66  this( new IdentityTransformation() , network );
67  }
68 
69  public NetworkReaderMatsimV1(
70  final CoordinateTransformation transformation,
71  final Network network) {
72  super(ValidationType.DTD_ONLY);
73  this.transformation = transformation;
74  this.network = network;
75  }
76 
77  @Override
78  public void startTag(final String name, final Attributes atts, final Stack<String> context) {
79  if (NODE.equals(name)) {
80  startNode(atts);
81  } else if (LINK.equals(name)) {
82  startLink(atts);
83  } else if (NETWORK.equals(name)) {
84  startNetwork(atts);
85  } else if (LINKS.equals(name)) {
86  startLinks(atts);
87  }
88  }
89 
90  @Override
91  public void endTag(final String name, final String content, final Stack<String> context) {
92  /* Check if there is a mixture of nodes WITH and WITHOUT elevation. */
93  /* FIXME Note: I would like this to rather crash here (jwjoubert, Sep'16) */
94  if(NODES.equals(name)){
95  if(hasElevation && hasNoElevation){
96  log.warn("There is a mixture of nodes WITH and WITHOUT elevation! " +
97  "You will likely run into problems when doing coordinate calculations.");
98  }
99  }
100  }
101 
102  private void startNetwork(final Attributes atts) {
103  if (atts.getValue("type") != null) {
104  log.info("Attribute 'type' is deprecated. There's always only ONE network, where the links and nodes define, which " +
105  "transportation mode is allowed to use it (for the future)");
106  }
107  this.network.setName(atts.getValue("name"));
108  if (atts.getValue("capDivider") != null) {
109  log.warn("capDivider defined. it will be used but should be gone eventually. " +
110  "-- This is a weird comment, since the matsim public api tells to put this into the network rather than" +
111  " into the ``links''. kai, jun'11");
112  String capperiod = atts.getValue("capDivider") + ":00:00";
113  this.network.setCapacityPeriod(Time.parseTime(capperiod));
114  }
115  }
116 
117  private void startLinks(final Attributes atts) {
118  double capacityPeriod = 3600.0; //the default of one hour
119  String capperiod = atts.getValue("capperiod");
120  if (capperiod != null) {
121  capacityPeriod = Time.parseTime(capperiod);
122  }
123  else {
124  log.warn("capperiod was not defined. Using default value of " + Time.writeTime(capacityPeriod) + ".");
125  }
126  this.network.setCapacityPeriod(capacityPeriod);
127 
128  String effectivecellsize = atts.getValue("effectivecellsize");
129  if (effectivecellsize == null){
130  this.network.setEffectiveCellSize(7.5); // we use a default cell size of 7.5 meters
131  } else {
132  this.network.setEffectiveCellSize(Double.parseDouble(effectivecellsize));
133  }
134 
135  String effectivelanewidth = atts.getValue("effectivelanewidth");
136  if (effectivelanewidth == null) {
137  this.network.setEffectiveLaneWidth(3.75); // the default lane width is 3.75
138  } else {
139  this.network.setEffectiveLaneWidth(Double.parseDouble(effectivelanewidth));
140  }
141 
142  if ((atts.getValue("capPeriod") != null) || (atts.getValue("capDivider") != null) || (atts.getValue("capdivider") != null)) {
143  log.warn("Found capPeriod, capDivider and/or capdivider in the links element. They will be ignored, since they " +
144  "should be set in the network element. -- This is a weird warning, since setting them in the " +
145  "network element also produces a warning.");
146  log.warn("At this point, it seems that, in network.xml, one sets capperiod in the `links' section, but in the " +
147  "matsim api, the corresponding entry belongs into the `network' object. kai, jun'11") ;
148  }
149  }
150 
151  private void startNode(final Attributes atts) {
152  boolean hasZ = atts.getValue("z") == null ? false : true;
153  Coord c;
154  if(hasZ){
155  c = transformation.transform(new Coord(
156  Double.parseDouble(atts.getValue("x")),
157  Double.parseDouble(atts.getValue("y")),
158  Double.parseDouble(atts.getValue("z"))));
159  hasElevation = true;
160  } else{
161  c = transformation.transform(new Coord(
162  Double.parseDouble(atts.getValue("x")),
163  Double.parseDouble(atts.getValue("y"))));
164  hasNoElevation = true;
165  }
166 
167  final Node node =
168  this.network.getFactory().createNode(
169  Id.create(atts.getValue("id"), Node.class), c);
170  this.network.addNode(node);
171 
172  NetworkUtils.setType(node,atts.getValue("type"));
173  // (did not have a null check when I found it. kai, jul'16)
174 
175  if (atts.getValue("origid") != null) {
176  NetworkUtils.setOrigId( node, atts.getValue("origid") ) ;
177  }
178  }
179 
180  private void startLink(final Attributes atts) {
181  final String fromNodeStr = atts.getValue("from");
182  Node fromNode = this.network.getNodes().get(Id.create(fromNodeStr, Node.class));
183  if ( fromNode==null ) {
184  throw new RuntimeException("node id given by link cannot be dereferenced; node label=" + fromNodeStr ) ;
185  }
186  final String toNodeStr = atts.getValue("to");
187  Node toNode = this.network.getNodes().get(Id.create(toNodeStr, Node.class));
188  if ( toNode==null ) {
189  throw new RuntimeException("node id given by link cannot be dereferenced; node label=" + toNodeStr ) ;
190  }
191  Link l = this.network.getFactory().createLink(Id.create(atts.getValue("id"), Link.class), fromNode, toNode);
192  l.setLength(Double.parseDouble(atts.getValue("length")));
193  l.setFreespeed(Double.parseDouble(atts.getValue("freespeed")));
194  l.setCapacity(Double.parseDouble(atts.getValue("capacity")));
195  l.setNumberOfLanes(Double.parseDouble(atts.getValue("permlanes")));
196  this.network.addLink(l);
197  NetworkUtils.setOrigId(l, atts.getValue("origid") ) ;
198  NetworkUtils.setType(l, atts.getValue("type"));
199  if (atts.getValue("modes") != null) {
200  String[] strModes = StringUtils.explode(atts.getValue("modes"), ',');
201  if ((strModes.length == 1) && strModes[0].isEmpty()) {
202  l.setAllowedModes(new HashSet<>());
203  } else {
204  Set<String> modes = new HashSet<>();
205  for (String strMode : strModes) {
206  modes.add(strMode.trim().intern());
207  }
208  l.setAllowedModes(modes);
209  }
210  }
211  if (atts.getValue("volume") != null) {
212  log.info("Attribute volume for element link is deprecated.");
213  }
214  if (atts.getValue("nt_category") != null) {
215  log.info("Attribute nt_category for element link is deprecated.");
216  }
217  if (atts.getValue("nt_type") != null) {
218  log.info("Attribute nt_type for element link is deprecated.");
219  }
220  }
221 
222 }