]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Altitude.java
Version 6, October 2008
[GpsPrune.git] / tim / prune / data / Altitude.java
1 package tim.prune.data;
2
3 /**
4  * Class to hold an altitude and provide conversion functions
5  */
6 public class Altitude
7 {
8         private boolean _valid = false;
9         private int _value = 0;
10         private int _format = -1;
11         private String _stringValue = null;
12
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;
17
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;
21
22         /** Constant for no altitude value */
23         public static final Altitude NONE = new Altitude(null, FORMAT_NONE);
24
25
26         /**
27          * Constructor using String
28          * @param inString string to parse
29          * @param inFormat format of altitude, either metres or feet
30          */
31         public Altitude(String inString, int inFormat)
32         {
33                 if (inString != null && !inString.equals(""))
34                 {
35                         try
36                         {
37                                 _stringValue = inString;
38                                 _value = (int) Double.parseDouble(inString.trim());
39                                 _format = inFormat;
40                                 _valid = true;
41                         }
42                         catch (NumberFormatException nfe) {}
43                 }
44         }
45
46
47         /**
48          * Constructor with int vaue
49          * @param inValue int value of altitude
50          * @param inFormat format of altitude, either metres or feet
51          */
52         public Altitude(int inValue, int inFormat)
53         {
54                 _value = inValue;
55                 _format = inFormat;
56                 _valid = true;
57         }
58
59
60         /**
61          * @return true if the value could be parsed
62          */
63         public boolean isValid()
64         {
65                 return _valid;
66         }
67
68
69         /**
70          * @return raw value as int
71          */
72         public int getValue()
73         {
74                 return _value;
75         }
76
77
78         /**
79          * @return format of number
80          */
81         public int getFormat()
82         {
83                 return _format;
84         }
85
86
87         /**
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
91          */
92         public int getValue(int inFormat)
93         {
94                 // Note possible rounding errors here if converting to/from units
95                 if (inFormat == _format)
96                         return _value;
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);
101                 return _value;
102         }
103
104         /**
105          * Get a string version of the value
106          * @param inFormat specified format
107          * @return string value, if possible the original one
108          */
109         public String getStringValue(int inFormat)
110         {
111                 if (inFormat == _format && _stringValue != null && !_stringValue.equals("")) {
112                         return _stringValue;
113                 }
114                 return "" + getValue(inFormat);
115         }
116
117
118         /**
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
125          */
126         public static Altitude interpolate(Altitude inStart, Altitude inEnd, int inIndex, int inNumSteps)
127         {
128                 return interpolate(inStart, inEnd, 1.0 * (inIndex + 1) / (inNumSteps + 1));
129         }
130
131
132         /**
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
138          */
139         public static Altitude interpolate(Altitude inStart, Altitude inEnd, double inFrac)
140         {
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);
151         }
152 }