View Javadoc
1   package org.opentrafficsim.road.network.factory.opendrive;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.opentrafficsim.core.network.NetworkException;
9   import org.w3c.dom.Node;
10  import org.w3c.dom.NodeList;
11  import org.xml.sax.SAXException;
12  
13  /**
14   * Parser for lanes.
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * <p>
19   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
20   * initial version Jul 23, 2015 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   */
23  class LanesTag implements Serializable
24  {
25  
26      /** */
27      private static final long serialVersionUID = 20150723L;
28  
29      /** GeometryTags */
30      @SuppressWarnings("checkstyle:visibilitymodifier")
31      List<LaneSectionTag> laneSectionTags = new ArrayList<LaneSectionTag>();
32  
33      /**
34       * Parse the attributes of the road tag. The sub-elements are parsed in separate classes.
35       * @param nodeList NodeList; the list of subnodes of the road node
36       * @param parser OpenDriveNetworkLaneParser; the parser with the lists of information
37       * @param roadTag RoadTag; the RoadTag to which this element belongs
38       * @throws SAXException when parsing of the tag fails
39       * @throws NetworkException when parsing of the tag fails
40       */
41      @SuppressWarnings("checkstyle:needbraces")
42      static void parseLanes(final NodeList nodeList, final OpenDriveNetworkLaneParser parser, final RoadTag roadTag)
43              throws SAXException, NetworkException
44      {
45          int laneSectionCount = 0;
46          LanesTag lanesTag = new LanesTag();
47  
48          for (Node node0 : XMLParser.getNodes(nodeList, "lanes"))
49              for (Node node : XMLParser.getNodes(node0.getChildNodes(), "laneSection"))
50              {
51                  LaneSectionTag laneSectionTag = LaneSectionTag.parseLaneSection(node, parser);
52                  laneSectionTag.id = laneSectionCount;
53                  laneSectionCount++;
54  
55                  lanesTag.laneSectionTags.add(laneSectionTag);
56              }
57          roadTag.lanesTag = lanesTag;
58  
59      }
60  
61      /**
62       * @param s Length; progression on the lane in the design direction
63       * @return laneSection the section belonging to 's' progression
64       */
65      public LaneSectionTag findDrivingLaneSec(Length s)
66      {
67          for (int i = 0; i < this.laneSectionTags.size(); i++)
68          {
69              if (i < this.laneSectionTags.size() - 1)
70              {
71                  LaneSectionTag currentSec = this.laneSectionTags.get(i);
72                  LaneSectionTag nextSec = this.laneSectionTags.get(i + 1);
73  
74                  if (s.si <= nextSec.s.si && s.si >= currentSec.s.si)
75                  {
76                      return currentSec;
77                  }
78              }
79              else
80              {
81                  LaneSectionTag currentSec = this.laneSectionTags.get(i);
82                  if (s.si >= currentSec.s.si)
83                  {
84                      return currentSec;
85                  }
86              }
87          }
88          return null;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public final String toString()
94      {
95          return "LanesTag [laneSectionTags=" + this.laneSectionTags + "]";
96      }
97  }