File: SpeedCalculator.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (197 lines) | stat: -rw-r--r-- 6,305 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package tim.prune.data;

import tim.prune.config.Config;

/**
 * Abstract class to hold static calculation functions
 * for speed (and vertical speed)
 */
public abstract class SpeedCalculator
{
	/**
	 * Calculate the horizontal speed value of the track at the specified index
	 * @param inTrack track object
	 * @param inIndex index of point to calculate speed for
	 * @param inValue object in which to place result of calculation
	 */
	public static void calculateSpeed(Track inTrack, int inIndex, SpeedValue inValue)
	{
		inValue.setInvalid();
		if (inTrack == null || inIndex < 0 || inValue == null)
		{
			System.err.println("Cannot calculate speed for index " + inIndex);
			return;
		}

		DataPoint point = inTrack.getPoint(inIndex);
		if (point == null) {return;}
		boolean pointHasSpeed = false;
		double  speedValue = 0.0;

		// First, see if point has a speed value already
		if (point.hasHSpeed()) {
			speedValue = point.getHSpeed().getValue(Config.getUnitSet().getSpeedUnit());
			pointHasSpeed = true;
		}

		// otherwise, see if we can calculate it from the timestamps
		if (!pointHasSpeed && point.hasTimestamp() && !point.isWaypoint())
		{
			double totalRadians = 0.0;
			int index = inIndex-1;
			DataPoint p = null;
			DataPoint q = point;
			Timestamp earlyStamp = point.getTimestamp();
			boolean stop = false;

			// Count backwards until timestamp earlier than now; total distances back to this point
			if (!point.getSegmentStart())
			{
				do
				{
					p = inTrack.getPoint(index);
					boolean timeOk = p != null && p.hasTimestamp() && !p.getTimestamp().isAfter(point.getTimestamp());
					boolean pValid = timeOk && !p.isWaypoint();
					if (pValid) {
						totalRadians += DataPoint.calculateRadiansBetween(p, q);
						earlyStamp = p.getTimestamp();
					}
					stop = (p == null) || (p.hasTimestamp() && !p.getTimestamp().isEqual(point.getTimestamp())
						|| p.getSegmentStart());
					index--;
					if (p != null && !p.isWaypoint()) {
						q = p;
					}
				}
				while (!stop);
			}
			// Count forwards until timestamp later than now; total distances forward to this point
			Timestamp lateStamp = point.getTimestamp();
			q = point;
			index = inIndex+1;
			do
			{
				p = inTrack.getPoint(index);
				boolean timeOk = p != null && p.hasTimestamp() && !p.getTimestamp().isBefore(point.getTimestamp());
				boolean pValid = timeOk && !p.isWaypoint() && !p.getSegmentStart();
				if (pValid) {
					totalRadians += DataPoint.calculateRadiansBetween(p, q);
					lateStamp = p.getTimestamp();
				}
				stop = (p == null) || (p.hasTimestamp() && !p.getTimestamp().isEqual(point.getTimestamp())
					|| p.getSegmentStart());
				index++;
				if (p != null && !p.isWaypoint()) {
					q = p;
				}
			}
			while (!stop);

			// See if we've managed to get a time range of at least a second
			long milliseconds = lateStamp.getMillisecondsSince(earlyStamp);
			if (milliseconds >= 1000L)
			{
				double dist = Distance.convertRadiansToDistance(totalRadians);
				// Store the value and maintain max and min values
				speedValue = dist / milliseconds * 1000.0 * 60.0 * 60.0; // convert from per millisec to per hour
				pointHasSpeed = true;
			}
		}
		// Did we get a value?
		if (pointHasSpeed)
		{
			inValue.setValue(speedValue);
		}
		// otherwise, just leave value as invalid
	}


	/**
	 * Calculate the vertical speed value of the track at the specified index
	 * @param inTrack track object
	 * @param inIndex index of point to calculate speed for
	 * @param inValue object in which to place the result of calculation
	 */
	public static void calculateVerticalSpeed(Track inTrack, int inIndex, SpeedValue inValue)
	{
		if (inTrack == null || inIndex < 0 || inValue == null) {
			System.err.println("Cannot calculate vert speed for index " + inIndex);
			return;
		}
		inValue.setInvalid();

		DataPoint point = inTrack.getPoint(inIndex);
		boolean pointHasSpeed = false;
		double  speedValue = 0.0;

		// First, see if point has a speed value already
		if (point != null && point.hasVSpeed())
		{
			speedValue = point.getVSpeed().getValue(Config.getUnitSet().getVerticalSpeedUnit());
			pointHasSpeed = true;
		}
		// otherwise, see if we can calculate it from the heights and timestamps
		if (!pointHasSpeed
			&& point != null && point.hasTimestamp() && point.hasAltitude() && !point.isWaypoint())
		{
			int index = inIndex-1;
			DataPoint p = null;
			Timestamp earlyStamp = point.getTimestamp();
			Altitude firstAlt = point.getAltitude();
			boolean stop = false;

			// Count backwards until timestamp earlier than now
			if (!point.getSegmentStart())
			{
				do
				{
					p = inTrack.getPoint(index);
					boolean timeOk = p != null && p.hasTimestamp() && !p.getTimestamp().isAfter(point.getTimestamp());
					boolean pValid = timeOk && !p.isWaypoint();
					if (pValid) {
						earlyStamp = p.getTimestamp();
						if (p.hasAltitude()) firstAlt = p.getAltitude();
					}
					stop = (p == null) || (p.hasTimestamp() && !p.getTimestamp().isEqual(point.getTimestamp())
						|| p.getSegmentStart());
					index--;
				}
				while (!stop);
			}

			// Count forwards until timestamp later than now
			Timestamp lateStamp = point.getTimestamp();
			Altitude lastAlt = point.getAltitude();
			index = inIndex+1;
			do
			{
				p = inTrack.getPoint(index);
				boolean timeOk = p != null && p.hasTimestamp() && !p.getTimestamp().isBefore(point.getTimestamp());
				boolean pValid = timeOk && !p.isWaypoint() && !p.getSegmentStart();
				if (pValid) {
					lateStamp = p.getTimestamp();
					if (p.hasAltitude()) lastAlt = p.getAltitude();
				}
				stop = (p == null) || (p.hasTimestamp() && !p.getTimestamp().isEqual(point.getTimestamp())
					|| p.getSegmentStart());
				index++;
			}
			while (!stop);

			// See if we've managed to get a non-zero time range
			long milliseconds = lateStamp.getMillisecondsSince(earlyStamp);
			if (milliseconds >= 1000L)
			{
				double altDiff = (lastAlt.getMetricValue() - firstAlt.getMetricValue())
				 * Config.getUnitSet().getVerticalSpeedUnit().getMultFactorFromStd();
				speedValue = altDiff / milliseconds * 1000.0; // units are feet/sec or metres/sec
				pointHasSpeed = true;
			}
		}
		// Check whether we got a value from either method
		if (pointHasSpeed)
		{
			inValue.setValue(speedValue);
		}
	}
}