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.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   
9   import org.djunits.unit.LengthUnit;
10  import org.djunits.value.vdouble.scalar.Length;
11  import org.opentrafficsim.core.network.NetworkException;
12  import org.opentrafficsim.road.network.lane.Lane;
13  import org.w3c.dom.NamedNodeMap;
14  import org.w3c.dom.Node;
15  import org.xml.sax.SAXException;
16  
17  /**
18   * <p>
19   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * <p>
22   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
23   * initial version Jul 23, 2015 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   */
26  class LaneSectionTag implements Serializable
27  {
28  
29      /** */
30      private static final long serialVersionUID = 20150723L;
31  
32      /** Sequence of the laneSection. */
33      @SuppressWarnings("checkstyle:visibilitymodifier")
34      int id = 0;
35  
36      /** Start position (s-coordinate). */
37      @SuppressWarnings("checkstyle:visibilitymodifier")
38      Length s = null;
39  
40      /** Left lanes */
41      @SuppressWarnings("checkstyle:visibilitymodifier")
42      Map<Integer, LaneTag> leftLaneTags = new HashMap<Integer, LaneTag>();
43  
44      /** Center lanes */
45      @SuppressWarnings("checkstyle:visibilitymodifier")
46      Map<Integer, LaneTag> centerLaneTags = new HashMap<Integer, LaneTag>();
47  
48      /** Right lanes */
49      @SuppressWarnings("checkstyle:visibilitymodifier")
50      Map<Integer, LaneTag> rightLaneTags = new HashMap<Integer, LaneTag>();
51  
52      /** All lanes */
53      @SuppressWarnings("checkstyle:visibilitymodifier")
54      Map<Integer, Lane> lanes = new HashMap<Integer, Lane>();
55  
56      /**
57       * Parse the attributes of the road tag. The sub-elements are parsed in separate classes.
58       * @param node Node; the top-level road node
59       * @param parser OpenDriveNetworkLaneParser; the parser with the lists of information
60       * @return the generated RoadTag for further reference
61       * @throws SAXException when parsing of the tag fails
62       * @throws NetworkException when parsing of the tag fails
63       */
64      @SuppressWarnings("checkstyle:needbraces")
65      static LaneSectionTag parseLaneSection(final Node node, final OpenDriveNetworkLaneParser parser)
66              throws SAXException, NetworkException
67      {
68          NamedNodeMap attributes = node.getAttributes();
69          LaneSectionTag laneSectionTag = new LaneSectionTag();
70  
71          Node s = attributes.getNamedItem("s");
72          if (s == null)
73              throw new SAXException("LaneSection: missing attribute s");
74          laneSectionTag.s = new Length(Double.parseDouble(s.getNodeValue().trim()), LengthUnit.METER);
75  
76          for (Node leftNode : XMLParser.getNodes(node.getChildNodes(), "left"))
77              for (Node laneNode : XMLParser.getNodes(leftNode.getChildNodes(), "lane"))
78              {
79                  LaneTag laneTag = LaneTag.parseLane(laneNode, parser);
80                  laneSectionTag.leftLaneTags.put(laneTag.id, laneTag);
81              }
82  
83          for (Node centerNode : XMLParser.getNodes(node.getChildNodes(), "center"))
84              for (Node laneNode : XMLParser.getNodes(centerNode.getChildNodes(), "lane"))
85              {
86                  LaneTag laneTag = LaneTag.parseLane(laneNode, parser);
87                  laneSectionTag.centerLaneTags.put(laneTag.id, laneTag);
88              }
89  
90          for (Node rightNode : XMLParser.getNodes(node.getChildNodes(), "right"))
91              for (Node laneNode : XMLParser.getNodes(rightNode.getChildNodes(), "lane"))
92              {
93                  LaneTag laneTag = LaneTag.parseLane(laneNode, parser);
94                  laneSectionTag.rightLaneTags.put(laneTag.id, laneTag);
95              }
96  
97          return laneSectionTag;
98      }
99  
100     /**
101      * @param orientation String; Plus or minus orientation, indicated by '+' or '-'
102      * @return lanes a list of lanes in the given orientation
103      */
104     public List<Lane> findLanes(String orientation)
105     {
106         List<Lane> lanes1 = new ArrayList<Lane>();
107         for (int key : this.lanes.keySet())
108         {
109             Lane lane = this.lanes.get(key);
110             if (key < 0)
111             {
112                 if (orientation.equals("+") && this.rightLaneTags.get(key).type.equals("driving"))
113                     lanes1.add(lane);
114             }
115             else if (key > 0)
116             {
117                 if (orientation.equals("-") && this.leftLaneTags.get(key).type.equals("driving"))
118                     lanes1.add(lane);
119             }
120 
121         }
122         if (lanes1.size() != 1)
123             System.err.println("Exception in finding lanes");
124         return lanes1;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public final String toString()
130     {
131         return "LaneSectionTag [id=" + this.id + ", s=" + this.s + ", leftLaneTags=" + this.leftLaneTags + ", centerLaneTags="
132                 + this.centerLaneTags + ", rightLaneTags=" + this.rightLaneTags + ", lanes=" + this.lanes + "]";
133     }
134 }