]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/data/Altitude.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / data / Altitude.java
index 495e84491746f4c1c011cb4b0d64cb1c7b0bde91..ac71d759cb4a45a8a7af971b8d6adc830010bf83 100644 (file)
@@ -21,8 +21,8 @@ public class Altitude
        }
 
        /** Constants for conversion */
-       private static final double CONVERT_FEET_TO_METRES = 0.3048;
-       private static final double CONVERT_METRES_TO_FEET = 3.28084;
+       private static final double CONVERT_METRES_TO_FEET = UnitSetLibrary.UNITS_FEET.getMultFactorFromStd();
+       private static final double CONVERT_FEET_TO_METRES = 1 / CONVERT_METRES_TO_FEET;
 
        /** Constant for no altitude value */
        public static final Altitude NONE = new Altitude(null, Format.NO_FORMAT);
@@ -50,7 +50,7 @@ public class Altitude
 
 
        /**
-        * Constructor with int vaue
+        * Constructor with int value
         * @param inValue int value of altitude
         * @param inFormat format of altitude, either metres or feet
         */
@@ -61,6 +61,25 @@ public class Altitude
                _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)
+       {
+               _stringValue = inClone._stringValue;
+               _value = inClone._value;
+               _format = inClone._format;
+               _valid = inClone._valid;
+       }
 
        /**
         * @return true if the value could be parsed
@@ -79,6 +98,14 @@ public class Altitude
                return _value;
        }
 
+       /**
+        * @param inAltUnit altitude units to use
+        * @return rounded value in specified units
+        */
+       public int getValue(Unit inAltUnit)
+       {
+               return (int) (getMetricValue() * inAltUnit.getMultFactorFromStd());
+       }
 
        /**
         * @return format of number
@@ -106,6 +133,17 @@ public class Altitude
                return _value;
        }
 
+       /**
+        * @return value of altitude in metres, used for calculations and charts
+        */
+       public double getMetricValue()
+       {
+               if (_format == Format.FEET) {
+                       return _value / UnitSetLibrary.UNITS_FEET.getMultFactorFromStd();
+               }
+               return _value;
+       }
+
        /**
         * Get a string version of the value
         * @param inFormat specified format
@@ -114,7 +152,9 @@ public class Altitude
        public String getStringValue(Format inFormat)
        {
                if (!_valid) {return "";}
-               if (inFormat == _format && _stringValue != null && !_stringValue.equals("")) {
+               // 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);
@@ -155,4 +195,32 @@ public class Altitude
                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;
+               }
+               // FIXME: The following will fail if _stringValue is null - not sure how it can get in that state!
+               if (_stringValue == null) System.err.println("*** Altitude.addOffset - how did the string value get to be null?");
+               // Add the offset
+               double newValue = Double.parseDouble(_stringValue.trim()) + offset;
+               _value = (int) newValue;
+               _stringValue = NumberUtils.formatNumber(newValue, numDecimals);
+       }
 }