1 package org.opentrafficsim.road.network.factory.opendrive;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.Serializable;
6 import java.net.URL;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
12 import javax.naming.NamingException;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16
17 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
18 import org.opentrafficsim.core.geometry.OTSGeometryException;
19 import org.opentrafficsim.core.gtu.GTUException;
20 import org.opentrafficsim.core.gtu.GTUType;
21 import org.opentrafficsim.core.network.NetworkException;
22 import org.opentrafficsim.core.network.OTSNetwork;
23 import org.opentrafficsim.road.network.lane.LaneType;
24 import org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Node;
27 import org.w3c.dom.NodeList;
28 import org.xml.sax.SAXException;
29
30 import nl.tudelft.simulation.dsol.SimRuntimeException;
31 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
32
33
34
35
36
37
38
39
40
41
42 public class OpenDriveNetworkLaneParser implements Serializable
43 {
44
45 private static final long serialVersionUID = 20150723L;
46
47
48 @SuppressWarnings("visibilitymodifier")
49 protected HeaderTag headerTag = null;
50
51
52 @SuppressWarnings("visibilitymodifier")
53 protected Map<String, ControllerTag> controllerTags = new HashMap<>();
54
55
56 @SuppressWarnings("visibilitymodifier")
57 protected Map<String, JunctionTag> junctionTags = new HashMap<>();
58
59
60 @SuppressWarnings("visibilitymodifier")
61 protected Map<String, RoadTag> roadTags = new HashMap<>();
62
63
64 @SuppressWarnings("visibilitymodifier")
65 protected Map<String, GTUType> gtuTypes = new HashMap<>();
66
67
68 @SuppressWarnings("visibilitymodifier")
69 protected Map<String, LaneType> laneTypes = new HashMap<>();
70
71
72 @SuppressWarnings("visibilitymodifier")
73 protected OTSSimulatorInterface simulator;
74
75
76 @SuppressWarnings("visibilitymodifier")
77 protected OTSNetwork network = null;
78
79
80 @SuppressWarnings("visibilitymodifier")
81 protected Map<String, SignalTag> signalTags = new HashMap<>();
82
83
84 @SuppressWarnings("visibilitymodifier")
85 protected Map<String, Set<SimpleTrafficLight>> trafficLightsBySignals = new HashMap<>();
86
87
88 @SuppressWarnings("visibilitymodifier")
89 protected Map<String, Set<SimpleTrafficLight>> trafficLightsByLanes = new HashMap<>();
90
91
92 @SuppressWarnings("checkstyle:visibilitymodifier")
93 public Map<Object, Renderable2D> animationMap = new HashMap<>();
94
95
96
97
98 public OpenDriveNetworkLaneParser(final OTSSimulatorInterface simulator)
99 {
100 this.simulator = simulator;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115 @SuppressWarnings("checkstyle:needbraces")
116 public final OTSNetwork build(final URL url) throws NetworkException, ParserConfigurationException, SAXException,
117 IOException, NamingException, GTUException, OTSGeometryException, SimRuntimeException
118 {
119 if (url.getFile().length() > 0 && !(new File(url.getFile()).exists()))
120 throw new SAXException("OpenDriveNetworkLaneParser.build: File url.getFile() does not exist");
121
122 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
123 factory.setNamespaceAware(true);
124 factory.setXIncludeAware(true);
125 DocumentBuilder builder = factory.newDocumentBuilder();
126 Document document = builder.parse(url.openStream());
127 NodeList networkNodeList = document.getDocumentElement().getChildNodes();
128
129 if (!document.getDocumentElement().getNodeName().equals("OpenDRIVE"))
130 throw new SAXException("OpenDriveNetworkLaneParser.build: XML document does not start with an OpenDRIVE tag, found "
131 + document.getDocumentElement().getNodeName() + " instead");
132
133 this.network = new OTSNetwork(url.toString());
134
135
136 List<Node> headerNodes = XMLParser.getNodes(networkNodeList, "header");
137 if (headerNodes.size() != 1)
138 throw new SAXException("OpenDriveNetworkLaneParser.build: XML document does not have a header tag");
139 else
140 HeaderTag.parseHeader(headerNodes.get(0), this);
141
142
143 List<Node> junctionNodes = XMLParser.getNodes(networkNodeList, "junction");
144 for (Node junctionNode : junctionNodes)
145 JunctionTag.parseJunction(junctionNode, this);
146
147
148 List<Node> controllerNodes = XMLParser.getNodes(networkNodeList, "controller");
149 for (Node controllerNode : controllerNodes)
150 {
151 ControllerTag controllerTag = ControllerTag.parseController(controllerNode, this);
152 this.controllerTags.put(controllerTag.id, controllerTag);
153 }
154
155
156 List<Node> roadNodes = XMLParser.getNodes(networkNodeList, "road");
157 if (roadNodes.size() == 0)
158 throw new SAXException("OpenDriveNetworkLaneParser.build: XML document does not have a road tag");
159 for (Node roadNode : roadNodes)
160 {
161 RoadTag roadTag = RoadTag.parseRoad(roadNode, this);
162 LinkTag.parseLink(roadNode.getChildNodes(), this, roadTag);
163 TypeTag.parseType(roadNode.getChildNodes(), this, roadTag);
164
165 ElevationProfileTag.parseElevationProfile(roadNode.getChildNodes(), this, roadTag);
166
167 PlanViewTag.parsePlanView(roadNode.getChildNodes(), this, roadTag);
168
169 LateralProfileTag.parseElevationProfile(roadNode.getChildNodes(), this, roadTag);
170 LanesTag.parseLanes(roadNode.getChildNodes(), this, roadTag);
171
172 SignalsTag.parseSignals(roadNode.getChildNodes(), this, roadTag);
173
174
175
176 }
177
178 for (RoadTag roadTag : this.roadTags.values())
179 {
180 RoadTag.buildLink(roadTag, this);
181 }
182
183 for (RoadTag roadTag : this.roadTags.values())
184 {
185 RoadTag.buildSubLinks(roadTag, this.simulator, this);
186 }
187
188 for (RoadTag roadTag : this.roadTags.values())
189 {
190
191 RoadTag.generateRegularRoads(roadTag, this.simulator, this);
192 }
193
194 for (RoadTag roadTag : this.roadTags.values())
195 {
196 RoadTag.generateTrafficLightsbySignal(roadTag, this.simulator, this);
197 }
198
199 for (RoadTag roadTag : this.roadTags.values())
200 {
201 RoadTag.generateTrafficLightsbySignalReference(roadTag, this.simulator, this);
202 }
203
204 for (JunctionTag juncTag : this.junctionTags.values())
205 JunctionTag.createController(juncTag, this.simulator, this);
206
207
208 return this.network;
209 }
210
211
212
213
214
215
216 @SuppressWarnings({ "unchecked", "rawtypes" })
217 private OTSNetwork makeNetwork(final String name) throws NetworkException
218 {
219 this.network = new OTSNetwork(name);
220 return this.network;
221 }
222
223
224
225
226 public HeaderTag getHeaderTag()
227 {
228 return this.headerTag;
229 }
230
231
232 @Override
233 public final String toString()
234 {
235 return "OpenDriveNetworkLaneParser [headerTag=" + this.headerTag + ", controllerTags.size=" + this.controllerTags.size()
236 + ", junctionTags.size=" + this.junctionTags.size() + ", roadTags.size=" + this.roadTags.size()
237 + ", gtuTypes.size=" + this.gtuTypes.size() + ", laneTypes.size=" + this.laneTypes.size() + ", simulator="
238 + this.simulator + ", network=" + this.network + ", signalTags.size=" + this.signalTags.size()
239 + ", trafficLightsBySignals.size=" + this.trafficLightsBySignals.size() + ", trafficLightsByLanes.size="
240 + this.trafficLightsByLanes.size() + ", animationMap.size=" + this.animationMap.size() + "]";
241 }
242
243 }