21 package org.matsim.core.network.io;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
36 import org.xml.sax.Attributes;
38 import java.util.HashSet;
40 import java.util.Stack;
47 final class NetworkReaderMatsimV1
extends MatsimXmlParser {
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";
55 private final Network network;
57 private boolean hasElevation =
false;
58 private boolean hasNoElevation =
false;
61 private final CoordinateTransformation transformation;
63 private final static Logger log = LogManager.getLogger(NetworkReaderMatsimV1.class);
65 public NetworkReaderMatsimV1(Network network) {
66 this(
new IdentityTransformation() , network );
69 public NetworkReaderMatsimV1(
70 final CoordinateTransformation transformation,
71 final Network network) {
72 super(ValidationType.DTD_ONLY);
73 this.transformation = transformation;
74 this.network = network;
78 public void startTag(
final String name,
final Attributes atts,
final Stack<String> context) {
79 if (NODE.equals(name)) {
81 }
else if (LINK.equals(name)) {
83 }
else if (NETWORK.equals(name)) {
85 }
else if (LINKS.equals(name)) {
91 public void endTag(
final String name,
final String content,
final Stack<String> context) {
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.");
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)");
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));
117 private void startLinks(
final Attributes atts) {
118 double capacityPeriod = 3600.0;
119 String capperiod = atts.getValue(
"capperiod");
120 if (capperiod != null) {
121 capacityPeriod = Time.parseTime(capperiod);
124 log.warn(
"capperiod was not defined. Using default value of " + Time.writeTime(capacityPeriod) +
".");
126 this.network.setCapacityPeriod(capacityPeriod);
128 String effectivecellsize = atts.getValue(
"effectivecellsize");
129 if (effectivecellsize == null){
130 this.network.setEffectiveCellSize(7.5);
132 this.network.setEffectiveCellSize(Double.parseDouble(effectivecellsize));
135 String effectivelanewidth = atts.getValue(
"effectivelanewidth");
136 if (effectivelanewidth == null) {
137 this.network.setEffectiveLaneWidth(3.75);
139 this.network.setEffectiveLaneWidth(Double.parseDouble(effectivelanewidth));
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") ;
151 private void startNode(
final Attributes atts) {
152 boolean hasZ = atts.getValue(
"z") == null ? false :
true;
155 c = transformation.transform(
new Coord(
156 Double.parseDouble(atts.getValue(
"x")),
157 Double.parseDouble(atts.getValue(
"y")),
158 Double.parseDouble(atts.getValue(
"z"))));
161 c = transformation.transform(
new Coord(
162 Double.parseDouble(atts.getValue(
"x")),
163 Double.parseDouble(atts.getValue(
"y"))));
164 hasNoElevation =
true;
168 this.network.getFactory().createNode(
169 Id.create(atts.getValue(
"id"), Node.class), c);
170 this.network.addNode(node);
172 NetworkUtils.setType(node,atts.getValue(
"type"));
175 if (atts.getValue(
"origid") != null) {
176 NetworkUtils.setOrigId( node, atts.getValue(
"origid") ) ;
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 ) ;
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 ) ;
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<>());
204 Set<String> modes =
new HashSet<>();
205 for (String strMode : strModes) {
206 modes.add(strMode.trim().intern());
208 l.setAllowedModes(modes);
211 if (atts.getValue(
"volume") != null) {
212 log.info(
"Attribute volume for element link is deprecated.");
214 if (atts.getValue(
"nt_category") != null) {
215 log.info(
"Attribute nt_category for element link is deprecated.");
217 if (atts.getValue(
"nt_type") != null) {
218 log.info(
"Attribute nt_type for element link is deprecated.");