1 package org.opentrafficsim.road.network.factory.xml.old;
2
3 import java.io.Serializable;
4 import java.util.LinkedHashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.djunits.value.vdouble.scalar.Length;
9 import org.djunits.value.vdouble.scalar.Speed;
10 import org.opentrafficsim.core.gtu.GTUType;
11 import org.opentrafficsim.core.network.NetworkException;
12 import org.opentrafficsim.core.network.factory.xml.units.LengthUnits;
13 import org.opentrafficsim.core.network.factory.xml.units.SpeedUnits;
14 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
15 import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
16 import org.w3c.dom.NamedNodeMap;
17 import org.w3c.dom.Node;
18 import org.w3c.dom.NodeList;
19 import org.xml.sax.SAXException;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 class RoadLayoutTag implements Serializable
108 {
109
110 private static final long serialVersionUID = 20150723L;
111
112
113 @SuppressWarnings("checkstyle:visibilitymodifier")
114 String name = null;
115
116
117 @SuppressWarnings("checkstyle:visibilitymodifier")
118 RoadTypeTag roadTypeTag = null;
119
120
121 @SuppressWarnings("checkstyle:visibilitymodifier")
122 Map<GTUType, Speed> legalSpeedLimits = null;
123
124
125 @SuppressWarnings("checkstyle:visibilitymodifier")
126 LaneKeepingPolicy laneKeepingPolicy = null;
127
128
129 @SuppressWarnings("checkstyle:visibilitymodifier")
130 OvertakingConditions overtakingConditions = null;
131
132
133 @SuppressWarnings("checkstyle:visibilitymodifier")
134 Length defaultLaneWidth = null;
135
136
137 @SuppressWarnings("checkstyle:visibilitymodifier")
138 Map<String, CrossSectionElementTag> cseTags = new LinkedHashMap<>();
139
140
141
142
143
144
145
146
147
148
149 @SuppressWarnings("checkstyle:needbraces")
150 static void parseRoadTypes(final NodeList nodeList, final XmlNetworkLaneParserOld parser)
151 throws SAXException, NetworkException
152 {
153 for (Node node : XMLParser.getNodes(nodeList, "ROADLAYOUT"))
154 {
155 RoadLayoutTag roadLayoutTag = parseRoadType(node, parser);
156 parser.roadLayoutTags.put(roadLayoutTag.name, roadLayoutTag);
157 }
158 }
159
160
161
162
163
164
165
166
167
168 @SuppressWarnings("checkstyle:needbraces")
169 static RoadLayoutTag parseRoadType(final Node node, final XmlNetworkLaneParserOld parser)
170 throws SAXException, NetworkException
171 {
172 NamedNodeMap attributes = node.getAttributes();
173 RoadLayoutTag roadLayoutTag = new RoadLayoutTag();
174
175 Node name = attributes.getNamedItem("NAME");
176 if (name == null)
177 throw new SAXException("ROADLAYOUT: missing attribute NAME");
178 roadLayoutTag.name = name.getNodeValue().trim();
179 if (parser.roadLayoutTags.keySet().contains(roadLayoutTag.name))
180 throw new SAXException("ROADLAYOUT: NAME " + roadLayoutTag.name + " defined twice");
181
182 Node roadType = attributes.getNamedItem("ROADTYPE");
183 if (roadType == null)
184 throw new SAXException("ROADLAYOUT: missing attribute ROADTYPE");
185 if (!parser.roadTypeTags.containsKey(roadType.getNodeValue().trim()))
186 throw new SAXException("ROADLAYOUT: ROADTYPE " + roadType.getNodeValue().trim() + " not defined");
187 roadLayoutTag.roadTypeTag = parser.roadTypeTags.get(roadType.getNodeValue().trim());
188
189 Node width = attributes.getNamedItem("WIDTH");
190 if (width != null)
191 roadLayoutTag.defaultLaneWidth = LengthUnits.parseLength(width.getNodeValue());
192
193 Node lkp = attributes.getNamedItem("LANEKEEPING");
194 if (lkp != null)
195 roadLayoutTag.laneKeepingPolicy = LaneAttributes.parseLaneKeepingPolicy(lkp.getNodeValue().trim());
196
197 Node oc = attributes.getNamedItem("OVERTAKING");
198 if (oc != null)
199 roadLayoutTag.overtakingConditions = LaneAttributes.parseOvertakingConditions(oc.getNodeValue().trim(), parser);
200
201 List<Node> speedLimitList = XMLParser.getNodes(node.getChildNodes(), "SPEEDLIMIT");
202 if (speedLimitList.size() > 0)
203 roadLayoutTag.legalSpeedLimits = new LinkedHashMap<>();
204 for (Node speedLimitNode : speedLimitList)
205 {
206 NamedNodeMap speedLimitAttributes = speedLimitNode.getAttributes();
207
208 Node gtuTypeName = speedLimitAttributes.getNamedItem("GTUTYPE");
209 if (gtuTypeName == null)
210 throw new NetworkException("ROADLAYOUT.SPEEDLIMIT: No GTUTYPE defined");
211 if (!parser.gtuTypes.containsKey(gtuTypeName.getNodeValue().trim()))
212 throw new NetworkException("ROADLAYOUT.SPEEDLIMIT: " + roadLayoutTag.name + " GTUTYPE "
213 + gtuTypeName.getNodeValue().trim() + " not defined");
214 GTUType gtuType = parser.gtuTypes.get(gtuTypeName.getNodeValue().trim());
215
216 Node speedNode = speedLimitAttributes.getNamedItem("LEGALSPEEDLIMIT");
217 if (speedNode == null)
218 throw new NetworkException("ROADLAYOUT.SPEEDLIMIT: " + roadLayoutTag.name + " GTUTYPE " + gtuType.getId()
219 + ": LEGALSPEEDLIMIT not defined");
220 Speed speed = SpeedUnits.parseSpeed(speedNode.getNodeValue().trim());
221
222 roadLayoutTag.legalSpeedLimits.put(gtuType, speed);
223 }
224
225 int cseCount = 0;
226
227 for (Node laneNode : XMLParser.getNodes(node.getChildNodes(), "LANE"))
228 {
229 CrossSectionElementTag.parseLane(laneNode, parser, roadLayoutTag);
230 cseCount++;
231 }
232
233 for (Node ntlNode : XMLParser.getNodes(node.getChildNodes(), "NOTRAFFICLANE"))
234 {
235 CrossSectionElementTag.parseNoTrafficLane(ntlNode, parser, roadLayoutTag);
236 cseCount++;
237 }
238
239 for (Node stripeNode : XMLParser.getNodes(node.getChildNodes(), "STRIPE"))
240 {
241 CrossSectionElementTag.parseStripe(stripeNode, parser, roadLayoutTag);
242 cseCount++;
243 }
244
245 for (Node shoulderNode : XMLParser.getNodes(node.getChildNodes(), "SHOULDER"))
246 {
247 CrossSectionElementTag.parseShoulder(shoulderNode, parser, roadLayoutTag);
248 cseCount++;
249 }
250
251 if (cseCount == 0)
252 throw new NetworkException("ROADLAYOUT: No elements defined for road type " + roadLayoutTag.name);
253
254 return roadLayoutTag;
255 }
256
257
258 @Override
259 public String toString()
260 {
261 return "RoadLayoutTag [name=" + this.name + ", roadTypeTag=" + this.roadTypeTag + ", legalSpeedLimits="
262 + this.legalSpeedLimits + ", laneKeepingPolicy=" + this.laneKeepingPolicy + ", overtakingConditions="
263 + this.overtakingConditions + ", defaultLaneWidth=" + this.defaultLaneWidth + ", cseTags=" + this.cseTags + "]";
264 }
265
266 }