File: Altitude.java

package info (click to toggle)
gpsprune 10-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,220 kB
  • ctags: 3,013
  • sloc: java: 22,662; sh: 23; makefile: 16; python: 15
file content (204 lines) | stat: -rw-r--r-- 5,144 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
198
199
200
201
202
203
204
package tim.prune.data;

/**
 * Class to hold an altitude and provide conversion functions
 */
public class Altitude
{
	private boolean _valid = false;
	private int _value = 0;
	private Format _format = Format.NO_FORMAT;
	private String _stringValue = null;

	/** Altitude formats */
	public enum Format {
		/** No format */
		NO_FORMAT,
		/** Metres */
		METRES,
		/** Feet */
		FEET
	}

	/** Constants for conversion */
	private static final double CONVERT_FEET_TO_METRES = 0.3048;
	private static final double CONVERT_METRES_TO_FEET = 3.28084;

	/** Constant for no altitude value */
	public static final Altitude NONE = new Altitude(null, Format.NO_FORMAT);


	/**
	 * Constructor using String
	 * @param inString string to parse
	 * @param inFormat format of altitude, either metres or feet
	 */
	public Altitude(String inString, Format inFormat)
	{
		if (inString != null && !inString.equals(""))
		{
			try
			{
				_stringValue = inString;
				_value = (int) Double.parseDouble(inString.trim());
				_format = inFormat;
				_valid = true;
			}
			catch (NumberFormatException nfe) {}
		}
	}


	/**
	 * Constructor with int vaue
	 * @param inValue int value of altitude
	 * @param inFormat format of altitude, either metres or feet
	 */
	public Altitude(int inValue, Format inFormat)
	{
		_value = inValue;
		_format = inFormat;
		_valid = true;
	}

	/**
	 * @return an exact copy of this Altitude object
	 */
	public Altitude clone()
	{
		return new Altitude(_stringValue, _format);
	}

	/**
	 * Reset the altitude parameters to the same as the given object
	 * @param inClone clone object to copy
	 */
	public void reset(Altitude inClone)
	{
		_value = inClone._value;
		_format = inClone._format;
		_valid = inClone._valid;
	}

	/**
	 * @return true if the value could be parsed
	 */
	public boolean isValid()
	{
		return _valid;
	}


	/**
	 * @return raw value as int
	 */
	public int getValue()
	{
		return _value;
	}


	/**
	 * @return format of number
	 */
	public Format getFormat()
	{
		return _format;
	}


	/**
	 * Get the altitude value in the specified format
	 * @param inFormat desired format, either FORMAT_METRES or FORMAT_FEET
	 * @return value as an int
	 */
	public int getValue(Format inFormat)
	{
		// Note possible rounding errors here if converting to/from units
		if (inFormat == _format)
			return _value;
		if (inFormat == Format.METRES)
			return (int) (_value * CONVERT_FEET_TO_METRES);
		if (inFormat == Format.FEET)
			return (int) (_value * CONVERT_METRES_TO_FEET);
		return _value;
	}

	/**
	 * Get a string version of the value
	 * @param inFormat specified format
	 * @return string value, if possible the original one
	 */
	public String getStringValue(Format inFormat)
	{
		if (!_valid) {return "";}
		// Return string value if the same format or "no format" was requested
		if ((inFormat == _format || inFormat == Format.NO_FORMAT)
		 && _stringValue != null && !_stringValue.equals("")) {
			return _stringValue;
		}
		return "" + getValue(inFormat);
	}


	/**
	 * Interpolate a new Altitude object between the given ones
	 * @param inStart start altitude
	 * @param inEnd end altitude
	 * @param inIndex index of interpolated point
	 * @param inNumSteps number of steps to interpolate
	 * @return Interpolated Altitude object
	 */
	public static Altitude interpolate(Altitude inStart, Altitude inEnd, int inIndex, int inNumSteps)
	{
		return interpolate(inStart, inEnd, 1.0 * (inIndex + 1) / (inNumSteps + 1));
	}


	/**
	 * Interpolate a new Altitude object between the given ones
	 * @param inStart start altitude
	 * @param inEnd end altitude
	 * @param inFrac fraction of distance from first point
	 * @return Interpolated Altitude object
	 */
	public static Altitude interpolate(Altitude inStart, Altitude inEnd, double inFrac)
	{
		// Check if altitudes are valid
		if (inStart == null || inEnd == null || !inStart.isValid() || !inEnd.isValid())
			return Altitude.NONE;
		// Use altitude format of first point
		Format altFormat = inStart.getFormat();
		int startValue = inStart.getValue();
		int endValue = inEnd.getValue(altFormat);
		// interpolate between start and end
		int newValue = startValue + (int) ((endValue - startValue) * inFrac);
		return new Altitude(newValue, altFormat);
	}

	/**
	 * Add the given offset to the current altitude
	 * @param inOffset offset as double
	 * @param inFormat format of offset, feet or metres
	 * @param inDecimals number of decimal places
	 */
	public void addOffset(double inOffset, Format inFormat, int inDecimals)
	{
		// Use the maximum number of decimal places from current value and offset
		int numDecimals = NumberUtils.getDecimalPlaces(_stringValue);
		if (numDecimals < inDecimals) {numDecimals = inDecimals;}
		// Convert offset to correct units
		double offset = inOffset;
		if (inFormat != _format)
		{
			if (inFormat == Format.FEET)
				offset = inOffset * CONVERT_FEET_TO_METRES;
			else
				offset = inOffset * CONVERT_METRES_TO_FEET;
		}
		// Add the offset
		double newValue = Double.parseDouble(_stringValue.trim()) + offset;
		_value = (int) newValue;
		_stringValue = NumberUtils.formatNumber(newValue, numDecimals);
	}
}