View Javadoc
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   * <p>
35   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
37   * <p>
38   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
39   * initial version Jul 23, 2015 <br>
40   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
41   */
42  public class OpenDriveNetworkLaneParser implements Serializable
43  {
44      /** */
45      private static final long serialVersionUID = 20150723L;
46  
47      /** Header tag. */
48      @SuppressWarnings("visibilitymodifier")
49      protected HeaderTag headerTag = null;
50  
51      /** Junction tags. */
52      @SuppressWarnings("visibilitymodifier")
53      protected Map<String, ControllerTag> controllerTags = new HashMap<>();
54  
55      /** Controller tags. */
56      @SuppressWarnings("visibilitymodifier")
57      protected Map<String, JunctionTag> junctionTags = new HashMap<>();
58  
59      /** Road tags. */
60      @SuppressWarnings("visibilitymodifier")
61      protected Map<String, RoadTag> roadTags = new HashMap<>();
62  
63      /** The GTUTypes that have been created. */
64      @SuppressWarnings("visibilitymodifier")
65      protected Map<String, GTUType> gtuTypes = new HashMap<>();
66  
67      /** The LaneTypes that have been created. */
68      @SuppressWarnings("visibilitymodifier")
69      protected Map<String, LaneType> laneTypes = new HashMap<>();
70  
71      /** The simulator for creating the animation. Null if no animation needed. */
72      @SuppressWarnings("visibilitymodifier")
73      protected OTSSimulatorInterface simulator;
74  
75      /** OTS network */
76      @SuppressWarnings("visibilitymodifier")
77      protected OTSNetwork network = null;
78  
79      /** The signalTags that have been created. */
80      @SuppressWarnings("visibilitymodifier")
81      protected Map<String, SignalTag> signalTags = new HashMap<>();
82  
83      /** The trafficLights that have been created, organized by signals */
84      @SuppressWarnings("visibilitymodifier")
85      protected Map<String, Set<SimpleTrafficLight>> trafficLightsBySignals = new HashMap<>();
86  
87      /** The trafficLights that have been created, organized by lanes */
88      @SuppressWarnings("visibilitymodifier")
89      protected Map<String, Set<SimpleTrafficLight>> trafficLightsByLanes = new HashMap<>();
90  
91      /** The generated animation per object. */
92      @SuppressWarnings("checkstyle:visibilitymodifier")
93      public Map<Object, Renderable2D> animationMap = new HashMap<>();
94  
95      /**
96       * @param simulator OTSSimulatorInterface; the simulator for creating the animation. Null if no animation needed.
97       */
98      public OpenDriveNetworkLaneParser(final OTSSimulatorInterface simulator)
99      {
100         this.simulator = simulator;
101     }
102 
103     /**
104      * @param url URL; the file with the network in the agreed xml-grammar.
105      * @return the network with Nodes, Links, and Lanes.
106      * @throws NetworkException in case of parsing problems.
107      * @throws SAXException in case of parsing problems.
108      * @throws ParserConfigurationException in case of parsing problems.
109      * @throws IOException in case of file reading problems.
110      * @throws NamingException in case the animation context cannot be found
111      * @throws GTUException in case of a problem with creating the LaneBlock (which is a GTU right now)
112      * @throws OTSGeometryException when construction of a lane contour or offset design line fails
113      * @throws SimRuntimeException when simulator cannot be used to schedule GTU generation
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         // there should be a header tag
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         // parse the junction tags
143         List<Node> junctionNodes = XMLParser.getNodes(networkNodeList, "junction");
144         for (Node junctionNode : junctionNodes)
145             JunctionTag.parseJunction(junctionNode, this);
146 
147         // parse the controller tags
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         // parse the road tags
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             // ObjectsTag.parseObjects(roadNode.getChildNodes(), this, roadTag);
172             SignalsTag.parseSignals(roadNode.getChildNodes(), this, roadTag);
173             /*-SurfaceTag.parseSurface(roadNode.getChildNodes(), this, roadTag);
174             RailroadTag.parseRailroad(roadNode.getChildNodes(), this, roadTag);
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             // System.err.println("RoadTag " + roadTag.id);
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         // store the structure information in the network
208         return this.network;
209     }
210 
211     /**
212      * @param name String; the name of the network
213      * @return the OTSNetwork with the static information about the network
214      * @throws NetworkException if items cannot be added to the Network
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      * @return headerTag
225      */
226     public HeaderTag getHeaderTag()
227     {
228         return this.headerTag;
229     }
230 
231     /** {@inheritDoc} */
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 }