1 package tim.prune.data;
4 * Class to hold an altitude and provide conversion functions
8 private boolean _valid = false;
9 private int _value = 0;
10 private int _format = -1;
11 private String _stringValue = null;
13 /** Altitude formats */
14 public static final int FORMAT_NONE = -1;
15 public static final int FORMAT_METRES = 0;
16 public static final int FORMAT_FEET = 1;
18 /** Constants for conversion */
19 private static final double CONVERT_FEET_TO_METRES = 0.3048;
20 private static final double CONVERT_METRES_TO_FEET = 3.28084;
22 /** Constant for no altitude value */
23 public static final Altitude NONE = new Altitude(null, FORMAT_NONE);
27 * Constructor using String
28 * @param inString string to parse
29 * @param inFormat format of altitude, either metres or feet
31 public Altitude(String inString, int inFormat)
33 if (inString != null && !inString.equals(""))
37 _stringValue = inString;
38 _value = (int) Double.parseDouble(inString.trim());
42 catch (NumberFormatException nfe) {}
48 * Constructor with int vaue
49 * @param inValue int value of altitude
50 * @param inFormat format of altitude, either metres or feet
52 public Altitude(int inValue, int inFormat)
61 * @return true if the value could be parsed
63 public boolean isValid()
70 * @return raw value as int
79 * @return format of number
81 public int getFormat()
88 * Get the altitude value in the specified format
89 * @param inFormat desired format, either FORMAT_METRES or FORMAT_FEET
90 * @return value as an int
92 public int getValue(int inFormat)
94 // Note possible rounding errors here if converting to/from units
95 if (inFormat == _format)
97 if (inFormat == FORMAT_METRES)
98 return (int) (_value * CONVERT_FEET_TO_METRES);
99 if (inFormat == FORMAT_FEET)
100 return (int) (_value * CONVERT_METRES_TO_FEET);
105 * Get a string version of the value
106 * @param inFormat specified format
107 * @return string value, if possible the original one
109 public String getStringValue(int inFormat)
111 if (inFormat == _format && _stringValue != null && !_stringValue.equals("")) {
114 return "" + getValue(inFormat);
119 * Interpolate a new Altitude object between the given ones
120 * @param inStart start altitude
121 * @param inEnd end altitude
122 * @param inIndex index of interpolated point
123 * @param inNumSteps number of steps to interpolate
124 * @return Interpolated Altitude object
126 public static Altitude interpolate(Altitude inStart, Altitude inEnd, int inIndex, int inNumSteps)
128 return interpolate(inStart, inEnd, 1.0 * (inIndex + 1) / (inNumSteps + 1));
133 * Interpolate a new Altitude object between the given ones
134 * @param inStart start altitude
135 * @param inEnd end altitude
136 * @param inFrac fraction of distance from first point
137 * @return Interpolated Altitude object
139 public static Altitude interpolate(Altitude inStart, Altitude inEnd, double inFrac)
141 // Check if altitudes are valid
142 if (inStart == null || inEnd == null || !inStart.isValid() || !inEnd.isValid())
143 return new Altitude(null, FORMAT_NONE);
144 // Use altitude format of first point
145 int altFormat = inStart.getFormat();
146 int startValue = inStart.getValue();
147 int endValue = inEnd.getValue(altFormat);
148 // interpolate between start and end
149 int newValue = startValue + (int) ((endValue - startValue) * inFrac);
150 return new Altitude(newValue, altFormat);