]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/profile/SpeedData.java
d836543420a9e55578363981f685d10224232eca
[GpsPrune.git] / tim / prune / gui / profile / SpeedData.java
1 package tim.prune.gui.profile;
2
3 import tim.prune.I18nManager;
4 import tim.prune.config.Config;
5 import tim.prune.data.DataPoint;
6 import tim.prune.data.Distance;
7 import tim.prune.data.Track;
8 import tim.prune.data.Distance.Units;
9
10 /**
11  * Class to provide a source of speed data for the profile chart
12  */
13 public class SpeedData extends ProfileData
14 {
15         /** Flag for metric units */
16         private boolean _metric = true;
17
18         /**
19          * Constructor
20          * @param inTrack track object
21          */
22         public SpeedData(Track inTrack) {
23                 super(inTrack);
24         }
25
26         /**
27          * Get the data and populate the instance arrays
28          */
29         public void init()
30         {
31                 initArrays();
32                 _metric = Config.getConfigBoolean(Config.KEY_METRIC_UNITS);
33                 _hasData = false;
34                 _minValue = _maxValue = 0.0;
35                 if (_track != null) {
36                         DataPoint prevPrevPoint = null, prevPoint = null, point = null;
37                         for (int i=0; i<_track.getNumPoints(); i++)
38                         {
39                                 point = _track.getPoint(i);
40                                 if (prevPrevPoint != null && prevPrevPoint.hasTimestamp()
41                                         && prevPoint != null && prevPoint.hasTimestamp()
42                                         && point != null && point.hasTimestamp())
43                                 {
44                                         // All three points have timestamps
45                                         double seconds = point.getTimestamp().getSecondsSince(prevPrevPoint.getTimestamp());
46                                         if (seconds > 0)
47                                         {
48                                                 double distInRads = DataPoint.calculateRadiansBetween(prevPrevPoint, prevPoint)
49                                                         + DataPoint.calculateRadiansBetween(prevPoint, point);
50                                                 double dist = Distance.convertRadiansToDistance(distInRads, _metric?Units.KILOMETRES:Units.MILES);
51                                                 // Store the value and maintain max and min values
52                                                 double value = dist / seconds * 60.0 * 60.0;
53                                                 _pointValues[i-1] = value;
54                                                 if (value < _minValue || _minValue == 0.0) {_minValue = value;}
55                                                 if (value > _maxValue) {_maxValue = value;}
56
57                                                 _hasData = true;
58                                                 _pointHasData[i-1] = true;
59                                         }
60                                 }
61                                 // Exchange points
62                                 prevPrevPoint = prevPoint;
63                                 prevPoint = point;
64                         }
65                 }
66         }
67
68         /**
69          * @return text description including units
70          */
71         public String getLabel()
72         {
73                 return I18nManager.getText("fieldname.speed") + " ("
74                         + I18nManager.getText(_metric?"units.kmh":"units.mph") + ")";
75         }
76
77         /**
78          * @return key for message when no speeds present
79          */
80         public String getNoDataKey() {
81                 return "display.notimestamps";
82         }
83 }