-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversionUtility.java
197 lines (161 loc) · 4.22 KB
/
ConversionUtility.java
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
/**
* A collection of methods for doing various conversions.
* <p>
*
* @version 1.3, 16 Nov 98
* @author Tommy Jasmin, SSEC
*/
public class ConversionUtility
{
/**
* Find the distance in km between two points given the lats/lons.
*
* @param lat1 - latitude of first point
* @param lat2 - latitude of second point
* @param lon1 - longitude of first point
* @param lon2 - longitude of second point
* @return - the distance in km between the two input points
*/
public static float LatLonToDistance (
float lat1,
float lon1,
float lat2,
float lon2
)
{
double arcl;
double cplat;
double cplon;
double crlat;
double crlon;
double dist;
double plat;
double plon;
double rlat;
double rlon;
double srlat;
double srlon;
double splat;
double splon;
double xx;
double yy;
double zz;
float r = 6371.0f;
float z = 0.017453292f;
rlat = (double) (lat1) * z;
rlon = (double) (lon1) * z;
plat = (double) (lat2) * z;
plon = (double) (lon2) * z;
crlat = Math.cos(rlat);
crlon = Math.cos(rlon);
srlat = Math.sin(rlat);
srlon = Math.sin(rlon);
cplat = Math.cos(plat);
cplon = Math.cos(plon);
splat = Math.sin(plat);
splon = Math.sin(plon);
xx = (cplat * cplon) - (crlat * crlon);
yy = (cplat * splon) - (crlat * srlon);
zz = splat - srlat;
dist = Math.sqrt((xx * xx) + (yy * yy) + (zz * zz));
arcl = 2.0 * Math.asin(dist / 2.0) * r;
return (float) (arcl);
}
/**
* Convert a latitude or longitude in dddmmss format to floating point.
*
* @param dddmmss - latitude or longitude
* @return - floating point representation of the input parameter.
*/
public static float FloatLatLon (
int dddmmss
)
{
int inVal;
float negVal;
float retVal;
if (dddmmss < 0) {
inVal = -dddmmss;
negVal = -1.0f;
} else {
inVal = dddmmss;
negVal = 1.0f;
}
retVal = (float) (inVal / 10000) +
(float) ((inVal / 100) % 100) / 60.0f +
(float) (inVal % 100) / 3600.0f;
retVal = negVal * retVal;
return (retVal);
}
/**
* Convert a Gould format floating point number to native double format
*
* @param inVal - input Gould value
* @return - the input value converted to double floating point
*/
public static double GouldToNative(int inVal) {
int sign;
int exponent;
float mant;
int byte0;
int byte1;
int byte2;
double dblMant;
double tempVal;
double nativeVal;
/*
* an example conversion:
*
* input value (hex): BE DA 4D 07
* a) convert to binary:
* 1011 1110 1101 1010 0100 1101 0000 0111
* b) sign bit is set, so take twos complement:
* 0100 0001 0010 0101 1011 0010 1111 1001
* c) convert this back to hex: 41 25 B2 F9
* d) mantissa = 2470649
* e) exponent = 65
* f) tempVal = mantissa / 16 exp (70 - 65)
* g) outputVal = tempVal * sign = -2.3561944
*
*/
// set up munging bytes
byte0 = (inVal & 0x000000FF);
byte1 = ((inVal >> 8) & 0x000000FF);
byte2 = ((inVal >> 16) & 0x000000FF);
sign = 1;
if ((inVal & 0x80000000) != 0) {
sign = -1;
inVal = -inVal;
}
exponent = ((inVal & 0x7F000000) >> 24);
if (exponent == 0) {
exponent = 64;
}
// determine the value of the mantissa, load into a double
mant = (float) (byte0 + (byte1 * 256) + (byte2 * 65536));
mant = Math.abs(mant);
dblMant = (double) mant;
// now adjust the mantissa according to the exponent
tempVal = Math.pow((double) 16, (double) (70 - exponent));
nativeVal = dblMant / tempVal;
nativeVal = nativeVal * sign;
return nativeVal;
}
/**
* swap the bytes of an integer array
*
* @param array[] array of integers to be flipped
* @param first starting element of the array
* @param last last element of array to flip
*
*/
public static void swap (int array[], int first, int last)
{
int i, k;
for (i = first; i <= last; i++) {
k = array[i];
array[i] = ( (k >>> 24) & 0xff) | ( (k >>> 8) & 0xff00) |
( (k & 0xff) << 24 ) | ( (k & 0xff00) << 8);
}
}
}