UTMConversion.java 6.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * Author: Sami Salkosuo, sami.salkosuo@fi.ibm.com
 *
 * (c) Copyright IBM Corp. 2007
 */
package de.tud.kom.p2psim.impl.util.geo.maps.osm;

import static java.lang.Math.cos;
import static java.lang.Math.pow;
import static java.lang.Math.sin;
import static java.lang.Math.tan;
12
13

import de.tud.kom.p2psim.impl.topology.util.PositionVector;
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

/**
 * This class can be used to convert lat/lon coordinates to
 * utm coordinates.
 * 
 * It is a stripped down version of the one found at
 * http://www.ibm.com/developerworks/java/library/j-coordconvert/
 * Original author: Sami Salkosuo, sami.salkosuo@fi.ibm.com
 * 
 * @author Fabio Zöllner
 * @version 1.0, 09.05.2012
 */
@SuppressWarnings("synthetic-access")
public class UTMConversion {
	private static final double r2d = 180 / Math.PI;
	private static final double d2r = Math.PI / 180;
	
	public UTMConversion() {

	}

	public static UTM latLon2UTM(double latitude, double longitude) {
		LatLon2UTM c = new LatLon2UTM();
		
		return c.convertLatLonToUTM(latitude, longitude);
	}

	private static void validate(double latitude, double longitude) {
		if (latitude < -90.0 || latitude > 90.0 || longitude < -180.0
				|| longitude >= 180.0) {
			throw new IllegalArgumentException(
					"Legal ranges: latitude [-90,90], longitude [-180,180).");
		}

	}

	public static double degreeToRadian(double degree) {
		return degree * d2r;
	}

	public double radianToDegree(double radian) {
		return radian * r2d;
	}

	private static class LatLon2UTM {
		public UTM convertLatLonToUTM(double latitude, double longitude) {
			validate(latitude, longitude);
			UTM utm = null;

			setVariables(latitude, longitude);

			String longZone = getLongZone(longitude);
			LatZones latZones = new LatZones();
			String latZone = latZones.getLatZone(latitude);

			double _easting = getEasting();
			double _northing = getNorthing(latitude);

			utm = new UTM(new PositionVector(_easting, _northing), longZone, latZone);

			return utm;
		}

		protected void setVariables(double latitude, double longitude) {
			latitude = degreeToRadian(latitude);
			
			rho = equatorialRadius * (1 - e * e) / pow(1 - pow(e * sin(latitude), 2), 3 / 2.0);
			nu = equatorialRadius / pow(1 - pow(e * sin(latitude), 2), (1 / 2.0));

			double var1;
			
			if (longitude < 0.0) {
				var1 = ((int) ((180 + longitude) / 6.0)) + 1;
			} else {
				var1 = ((int) (longitude / 6)) + 31;
			}
			
			double var2 = (6 * var1) - 183;
			double var3 = longitude - var2;
			
			p = var3 * 3600 / 10000;

			S = A0 * latitude - B0 * sin(2 * latitude) + C0 * sin(4 * latitude) - D0 * sin(6 * latitude) + E0 * sin(8 * latitude);

			K1 = S * k0;
			K2 = nu * sin(latitude) * cos(latitude) * pow(sin1, 2) * k0 * (100000000) / 2;
			K3 = ((pow(sin1, 4) * nu * sin(latitude) * Math.pow(cos(latitude),
					3)) / 24)
					* (5 - pow(tan(latitude), 2) + 9 * e1sq
							* pow(cos(latitude), 2) + 4 * pow(e1sq, 2)
							* pow(cos(latitude), 4))
					* k0
					* (10000000000000000L);

			K4 = nu * cos(latitude) * sin1 * k0 * 10000;

			K5 = pow(sin1 * cos(latitude), 3)
					* (nu / 6)
					* (1 - pow(tan(latitude), 2) + e1sq * pow(cos(latitude), 2))
					* k0 * 1000000000000L;

			A6 = (pow(p * sin1, 6) * nu * sin(latitude) * pow(cos(latitude), 5) / 720)
					* (61 - 58 * pow(tan(latitude), 2) + pow(tan(latitude), 4)
							+ 270 * e1sq * pow(cos(latitude), 2) - 330 * e1sq
							* pow(sin(latitude), 2)) * k0 * (1E+24);

		}

		protected String getLongZone(double longitude) {
			double longZone = 0;
			
			if (longitude < 0.0) {
				longZone = ((180.0 + longitude) / 6) + 1;
			} else {
				longZone = (longitude / 6) + 31;
			}
			
			String val = String.valueOf((int) longZone);
			
			if (val.length() == 1) {
				val = "0" + val;
			}
			
			return val;
		}

		protected double getNorthing(double latitude) {
			double northing = K1 + K2 * p * p + K3 * pow(p, 4);
			
			if (latitude < 0.0) {
				northing = 10000000 + northing;
			}
			
			return northing;
		}

		protected double getEasting() {
			return 500000 + (K4 * p + K5 * pow(p, 3));
		}

		// Lat Lon to UTM variables

		// equatorial radius
		double equatorialRadius = 6378137;

		// polar radius
		double polarRadius = 6356752.314;

		// scale factor
		double k0 = 0.9996;

		// eccentricity
		double e = Math.sqrt(1 - pow(polarRadius / equatorialRadius, 2));

		double e1sq = e * e / (1 - e * e);

		// r curv 1
		double rho = 6368573.744;

		// r curv 2
		double nu = 6389236.914;

		// Calculate Meridional Arc Length
		// Meridional Arc
		double S = 5103266.421;

		double A0 = 6367449.146;

		double B0 = 16038.42955;

		double C0 = 16.83261333;

		double D0 = 0.021984404;

		double E0 = 0.000312705;

		// Calculation Constants
		// Delta Long
		double p = -0.483084;

		double sin1 = 4.84814E-06;

		// Coefficients for UTM Coordinates
		double K1 = 5101225.115;

		double K2 = 3750.291596;

		double K3 = 1.397608151;

		double K4 = 214839.3105;

		double K5 = -2.995382942;

		double A6 = -1.00541E-07;

	}

	private static class LatZones {
		private char[] letters = { 'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
				'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z' };

		private int[] degrees = { -90, -84, -72, -64, -56, -48, -40, -32, -24,
				-16, -8, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 84 };

		private char[] negLetters = { 'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
				'K', 'L', 'M' };

		private int[] negDegrees = { -90, -84, -72, -64, -56, -48, -40, -32,
				-24, -16, -8 };

		private char[] posLetters = { 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
				'W', 'X', 'Z' };

		private int[] posDegrees = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 84 };

		private int arrayLength = 22;

		public LatZones() {
		}

		public String getLatZone(double latitude) {
			int latIndex = -2;
			int lat = (int) latitude;

			if (lat >= 0) {
				int len = posLetters.length;
				for (int i = 0; i < len; i++) {
					if (lat == posDegrees[i]) {
						latIndex = i;
						break;
					}

					if (lat > posDegrees[i]) {
						continue;
					} else {
						latIndex = i - 1;
						break;
					}
				}
			} else {
				int len = negLetters.length;
				for (int i = 0; i < len; i++) {
					if (lat == negDegrees[i]) {
						latIndex = i;
						break;
					}

					if (lat < negDegrees[i]) {
						latIndex = i - 1;
						break;
					} else {
						continue;
					}

				}

			}

			if (latIndex == -1) {
				latIndex = 0;
			}
			if (lat >= 0) {
				if (latIndex == -2) {
					latIndex = posLetters.length - 1;
				}
				return String.valueOf(posLetters[latIndex]);
			} else {
				if (latIndex == -2) {
					latIndex = negLetters.length - 1;
				}
				return String.valueOf(negLetters[latIndex]);

			}
		}

	}

}