View Javadoc
1   package org.opentrafficsim.road.network.factory.vissim;
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.w3c.dom.NamedNodeMap;
16  import org.w3c.dom.Node;
17  import org.w3c.dom.NodeList;
18  import org.xml.sax.SAXException;
19  
20  /**
21   * LANETYPE Tag.
22   *
23   * <pre>
24   * {@code
25    <xsd:element name="LANETYPE">
26      <xsd:complexType>
27        <xsd:sequence>
28          <xsd:element name="GTUTYPE" minOccurs="1" maxOccurs="unbounded">
29            <xsd:complexType>
30              <xsd:attribute name="NAME" type="xsd:string" use="required" />
31              <xsd:attribute name="LEGALSPEEDLIMIT" type="SPEEDTYPE" use="optional" />
32            </xsd:complexType>
33          </xsd:element>
34        </xsd:sequence>
35        <xsd:attribute name="NAME" type="xsd:string" use="required" />
36        <xsd:attribute name="DEFAULTLANEWIDTH" type="LENGTHTYPE" use="optional" />
37        <xsd:attribute name="DEFAULTLANEKEEPING" type="LANEKEEPINGTYPE" use="optional" />
38        <xsd:attribute ref="xml:base" />
39      </xsd:complexType>
40    </xsd:element>
41   * }
42   * </pre>
43   * <p>
44   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
45   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
46   * <p>
47   * $LastChangedDate: 2019-01-06 01:39:32 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4833 $, by $Author: averbraeck $,
48   * initial version Jul 23, 2015 <br>
49   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
50   */
51  class LaneTypeTag implements Serializable
52  {
53      /** */
54      private static final long serialVersionUID = 20150723L;
55  
56      /** Name. */
57      @SuppressWarnings("checkstyle:visibilitymodifier")
58      String name = null;
59  
60      /** Speed limits. */
61      @SuppressWarnings("checkstyle:visibilitymodifier")
62      Map<GTUType, Speed> legalSpeedLimits = new LinkedHashMap<>();
63  
64      /** Default lane width. */
65      @SuppressWarnings("checkstyle:visibilitymodifier")
66      Length defaultLaneWidth = null;
67  
68      /** The lane keeping policy, i.e., keep left, keep right or keep lane. */
69      @SuppressWarnings("checkstyle:visibilitymodifier")
70      LaneKeepingPolicy defaultLaneKeepingPolicy = null;
71  
72      /**
73       * Parse the LANETYPE tags.
74       * @param nodeList NodeList; nodeList the top-level nodes of the XML-file
75       * @param parser VissimNetworkLaneParser; the parser with the lists of information
76       * @throws SAXException when parsing of the tag fails
77       * @throws NetworkException when parsing of the tag fails
78       */
79      @SuppressWarnings("checkstyle:needbraces")
80      static void parseLaneTypes(final NodeList nodeList, final VissimNetworkLaneParser parser)
81              throws SAXException, NetworkException
82      {
83          for (Node node : XMLParser.getNodes(nodeList, "LANETYPE"))
84          {
85              NamedNodeMap attributes = node.getAttributes();
86              LaneTypeTag laneTypeTag = new LaneTypeTag();
87  
88              Node name = attributes.getNamedItem("NAME");
89              if (name == null)
90              {
91                  throw new SAXException("LANETYPE: missing attribute NAME");
92              }
93              laneTypeTag.name = name.getNodeValue().trim();
94              if (parser.getLaneTypeTags().keySet().contains(laneTypeTag.name))
95              {
96                  throw new SAXException("LANETYPE: NAME " + laneTypeTag.name + " defined twice");
97              }
98  
99              Node width = attributes.getNamedItem("DEFAULTLANEWIDTH");
100             if (width != null)
101             {
102                 laneTypeTag.defaultLaneWidth = LengthUnits.parseLength(width.getNodeValue());
103             }
104 
105             Node lkp = attributes.getNamedItem("DEFAULTLANEKEEPING");
106             if (lkp != null)
107             {
108                 laneTypeTag.defaultLaneKeepingPolicy = org.opentrafficsim.road.network.factory.vissim.units.LaneAttributes
109                         .parseLaneKeepingPolicy(lkp.getNodeValue().trim());
110             }
111 
112             List<Node> speedLimitList = XMLParser.getNodes(node.getChildNodes(), "SPEEDLIMIT");
113             if (speedLimitList.size() == 0)
114             {
115                 throw new SAXException("LANETYPE: missing tag SPEEDLIMIT");
116             }
117             for (Node speedLimitNode : speedLimitList)
118             {
119                 NamedNodeMap speedLimitAttributes = speedLimitNode.getAttributes();
120 
121                 Node gtuTypeName = speedLimitAttributes.getNamedItem("GTUTYPE");
122                 if (gtuTypeName == null)
123                 {
124                     throw new NetworkException("LANETYPE: No GTUTYPE defined");
125                 }
126                 if (!parser.getGtuTypes().containsKey(gtuTypeName.getNodeValue().trim()))
127                 {
128                     throw new NetworkException(
129                             "LANETYPE: " + laneTypeTag.name + " GTUTYPE " + gtuTypeName.getNodeValue().trim() + " not defined");
130                 }
131                 GTUType gtuType = parser.getGtuTypes().get(gtuTypeName.getNodeValue().trim());
132 
133                 Node speedNode = speedLimitAttributes.getNamedItem("LEGALSPEEDLIMIT");
134                 if (speedNode == null)
135                 {
136                     throw new NetworkException(
137                             "LANETYPE: " + laneTypeTag.name + " GTUTYPE " + gtuType.getId() + ": LEGALSPEEDLIMIT not defined");
138                 }
139                 Speed speed = SpeedUnits.parseSpeed(speedNode.getNodeValue().trim());
140 
141                 laneTypeTag.legalSpeedLimits.put(gtuType, speed);
142             }
143             parser.getLaneTypeTags().put(laneTypeTag.name, laneTypeTag);
144         }
145     }
146 
147 }