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.ExtendedDataType;
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
16
17
18
19
20
21
22
23
24
25 public class ReferenceSpeed extends ExtendedDataType<FloatSpeed>
26 {
27
28
29
30
31 public ReferenceSpeed()
32 {
33 super("referenceSpeed");
34 }
35
36
37 @Override
38 public final FloatSpeed getValue(final GtuDataInterface gtu)
39 {
40 Throw.when(!(gtu instanceof GtuData), IllegalArgumentException.class,
41 "Extended data type ReferenceSpeed can only be used with GtuData.");
42 LaneBasedGTU gtuObj = ((GtuData) gtu).getGtu();
43 try
44 {
45 double v1 = gtuObj.getReferencePosition().getLane().getSpeedLimit(gtuObj.getGTUType()).si;
46 double v2 = gtuObj.getMaximumSpeed().si;
47 return new FloatSpeed(v1 < v2 ? v1 : v2, SpeedUnit.SI);
48 }
49 catch (GTUException exception)
50 {
51
52 return new FloatSpeed(Double.NaN, SpeedUnit.SI);
53 }
54 catch (NetworkException exception)
55 {
56 throw new RuntimeException("Could not obtain reference speed from GTU " + gtuObj, exception);
57 }
58 }
59
60 }