1 | |
---|
2 | |
---|
3 | package gov.noaa.pmel.util; |
---|
4 | |
---|
5 | /** |
---|
6 | * Base class used by LatitudeValue and LongitudeValue for translating between |
---|
7 | * a float and deg/min/sec. |
---|
8 | */ |
---|
9 | public class GeographicValue { |
---|
10 | |
---|
11 | protected int degrees; |
---|
12 | protected int minutes; |
---|
13 | protected int seconds; |
---|
14 | protected int sign; |
---|
15 | protected float decimalValue; |
---|
16 | |
---|
17 | // --------------------------------------------- |
---|
18 | // |
---|
19 | public GeographicValue( ) { |
---|
20 | this.decimalValue = 0; |
---|
21 | decimalToDegMinSec(); |
---|
22 | } |
---|
23 | |
---|
24 | // --------------------------------------------- |
---|
25 | // |
---|
26 | public GeographicValue( float decVal ) { |
---|
27 | this.decimalValue = decVal; |
---|
28 | decimalToDegMinSec(); |
---|
29 | } |
---|
30 | |
---|
31 | // --------------------------------------------- |
---|
32 | // |
---|
33 | public void decimalToDegMinSec() { |
---|
34 | sign = 1; |
---|
35 | if (decimalValue < 0) sign = -1; |
---|
36 | |
---|
37 | float num1 = Math.abs( decimalValue ); |
---|
38 | degrees = (new Double( num1)).intValue(); |
---|
39 | float f1 = Math.abs( num1 - degrees); |
---|
40 | float f2 = (f1 * 60); |
---|
41 | |
---|
42 | float num2 = Math.abs( f2 ); |
---|
43 | minutes = new Double( Math.floor( (new Double(num2)).doubleValue() )).intValue(); |
---|
44 | float f3 = Math.abs( num2 - minutes); |
---|
45 | double dd = f3 * 60; |
---|
46 | seconds = new Long( Math.round( dd )).intValue(); |
---|
47 | if (seconds == 60) { |
---|
48 | seconds = 0; |
---|
49 | minutes++; |
---|
50 | } |
---|
51 | /* |
---|
52 | System.out.println("======================================================"); |
---|
53 | System.out.println(" num1: " + num1 |
---|
54 | + " \n degrees: " + degrees |
---|
55 | + " \n f1: " + f1 |
---|
56 | + " \n f2: " + f2 |
---|
57 | + " \n num2: " + num2 |
---|
58 | + " \n minutes: " + minutes |
---|
59 | + " \n f3: " + f3 |
---|
60 | + " \n dd: " + dd |
---|
61 | + " \n seconds: " + seconds); |
---|
62 | System.out.println(""); |
---|
63 | */ |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | // --------------------------------------------- |
---|
68 | // |
---|
69 | public void degMinSecToDecimal() { |
---|
70 | float minVal = minutes; |
---|
71 | float degVal = degrees; |
---|
72 | float secVal = seconds; |
---|
73 | if (seconds == 0) secVal = 0.1f; |
---|
74 | minVal += (secVal/60f); |
---|
75 | degVal += (minVal/60f); |
---|
76 | degVal = degVal * sign; |
---|
77 | decimalValue = degVal; |
---|
78 | } |
---|
79 | |
---|
80 | // --------------------------------------------- |
---|
81 | // |
---|
82 | public void setDegrees( int degrees ) { |
---|
83 | this.degrees = degrees; |
---|
84 | } |
---|
85 | // --------------------------------------------- |
---|
86 | // |
---|
87 | public void setMinutes( int minutes ) { |
---|
88 | this.minutes = minutes; |
---|
89 | } |
---|
90 | // --------------------------------------------- |
---|
91 | // |
---|
92 | public void setSeconds( int seconds ) { |
---|
93 | this.seconds = seconds; |
---|
94 | } |
---|
95 | // --------------------------------------------- |
---|
96 | // |
---|
97 | public void setSign( int sign ) { |
---|
98 | this.sign = sign; |
---|
99 | } |
---|
100 | // --------------------------------------------- |
---|
101 | // |
---|
102 | public void setDecimalValue( float decimalValue ) { |
---|
103 | this.decimalValue = decimalValue; |
---|
104 | decimalToDegMinSec(); |
---|
105 | } |
---|
106 | |
---|
107 | // --------------------------------------------- |
---|
108 | // |
---|
109 | public int getDegrees( ) { |
---|
110 | return degrees; |
---|
111 | } |
---|
112 | // --------------------------------------------- |
---|
113 | // |
---|
114 | public int getMinutes( ) { |
---|
115 | return minutes; |
---|
116 | } |
---|
117 | // --------------------------------------------- |
---|
118 | // |
---|
119 | public int getSeconds( ) { |
---|
120 | return seconds; |
---|
121 | } |
---|
122 | // --------------------------------------------- |
---|
123 | // |
---|
124 | public int getSign( ) { |
---|
125 | return sign; |
---|
126 | } |
---|
127 | // --------------------------------------------- |
---|
128 | // |
---|
129 | public float getDecimalValue( ) { |
---|
130 | return decimalValue; |
---|
131 | } |
---|
132 | |
---|
133 | // --------------------------------------------- |
---|
134 | // |
---|
135 | public String toString( ) { |
---|
136 | StringBuffer str = new StringBuffer(); |
---|
137 | str.append( " ----------------------------: " |
---|
138 | + " \n degrees: " + degrees |
---|
139 | + " \n minutes: " + minutes |
---|
140 | + " \n seconds: " + seconds |
---|
141 | + " \n decimalValue: " + decimalValue); |
---|
142 | return str.toString(); |
---|
143 | } |
---|
144 | |
---|
145 | // --------------------------------------------- |
---|
146 | // |
---|
147 | public static void main( String args[] ) { |
---|
148 | GeographicValue l1 = new GeographicValue( -154.7002716064453f); |
---|
149 | System.out.println(" toString: " + l1.toString()); |
---|
150 | } |
---|
151 | |
---|
152 | } |
---|