View Javadoc
1   package org.opentrafficsim.road.network.sampling.data;
2   
3   import org.djunits.unit.SpeedUnit;
4   import org.djunits.value.vfloat.scalar.FloatSpeed;
5   import org.opentrafficsim.core.gtu.GTUException;
6   import org.opentrafficsim.core.network.NetworkException;
7   import org.opentrafficsim.kpi.interfaces.GtuDataInterface;
8   import org.opentrafficsim.kpi.sampling.data.ExtendedDataTypeSpeed;
9   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
10  import org.opentrafficsim.road.network.sampling.GtuData;
11  
12  import nl.tudelft.simulation.language.Throw;
13  
14  /**
15   * <p>
16   * Copyright (c) 2013-2017 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/node/13">OpenTrafficSim License</a>.
18   * <p>
19   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 21 nov. 2016 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   */
24  public class ReferenceSpeed extends ExtendedDataTypeSpeed
25  {
26  
27      /**
28       * 
29       */
30      public ReferenceSpeed()
31      {
32          super("referenceSpeed");
33      }
34  
35      /** {@inheritDoc} */
36      @Override
37      public final FloatSpeed getValue(final GtuDataInterface gtu)
38      {
39          Throw.when(!(gtu instanceof GtuData), IllegalArgumentException.class,
40                  "Extended data type ReferenceSpeed can only be used with GtuData.");
41          LaneBasedGTU gtuObj = ((GtuData) gtu).getGtu();
42          try
43          {
44              double v1 = gtuObj.getReferencePosition().getLane().getSpeedLimit(gtuObj.getGTUType()).si;
45              double v2 = gtuObj.getMaximumSpeed().si;
46              return new FloatSpeed(v1 < v2 ? v1 : v2, SpeedUnit.SI);
47          }
48          catch (GTUException exception)
49          {
50              // GTU was destroyed and is without a reference location
51              return new FloatSpeed(Double.NaN, SpeedUnit.SI);
52          }
53          catch (NetworkException exception)
54          {
55              throw new RuntimeException("Could not obtain reference speed from GTU " + gtuObj, exception);
56          }
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public String toString()
62      {
63          return "Reference Speed";
64      }
65  
66  }