source: josm/trunk/src/org/openstreetmap/josm/data/projection/UTM.java@ 1823

Last change on this file since 1823 was 1823, checked in by stoecker, 15 years ago

some projection and zoom cleanups - projection classes still need better handling of outside-world coordinates

File size: 12.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.projection;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import org.openstreetmap.josm.data.coor.LatLon;
7import org.openstreetmap.josm.data.coor.EastNorth;
8import org.openstreetmap.josm.data.Bounds;
9import org.openstreetmap.josm.data.ProjectionBounds;
10
11/**
12 * Directly use latitude / longitude values as x/y.
13 *
14 * @author Dirk Stöcker
15 * code based on JavaScript from Chuck Taylor
16 */
17public class UTM implements Projection {
18
19 final private double UTMScaleFactor = 0.9996;
20
21 /* Ellipsoid model constants (WGS84) - TODO Use Elliposid class here too */
22 final private double sm_EccSquared = 6.69437999013e-03;
23
24 /*
25 * ArcLengthOfMeridian
26 *
27 * Computes the ellipsoidal distance from the equator to a point at a
28 * given latitude.
29 *
30 * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
31 * GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
32 *
33 * Inputs:
34 * phi - Latitude of the point, in radians.
35 *
36 * Globals:
37 * Ellipsoid.GRS80.a - Ellipsoid model major axis.
38 * Ellipsoid.GRS80.b - Ellipsoid model minor axis.
39 *
40 * Returns:
41 * The ellipsoidal distance of the point from the equator, in meters.
42 *
43 */
44 private double ArcLengthOfMeridian(double phi)
45 {
46 /* Precalculate n */
47 double n = (Ellipsoid.GRS80.a - Ellipsoid.GRS80.b) / (Ellipsoid.GRS80.a + Ellipsoid.GRS80.b);
48
49 /* Precalculate alpha */
50 double alpha = ((Ellipsoid.GRS80.a + Ellipsoid.GRS80.b) / 2.0)
51 * (1.0 + (Math.pow (n, 2.0) / 4.0) + (Math.pow (n, 4.0) / 64.0));
52
53 /* Precalculate beta */
54 double beta = (-3.0 * n / 2.0) + (9.0 * Math.pow (n, 3.0) / 16.0)
55 + (-3.0 * Math.pow (n, 5.0) / 32.0);
56
57 /* Precalculate gamma */
58 double gamma = (15.0 * Math.pow (n, 2.0) / 16.0)
59 + (-15.0 * Math.pow (n, 4.0) / 32.0);
60
61 /* Precalculate delta */
62 double delta = (-35.0 * Math.pow (n, 3.0) / 48.0)
63 + (105.0 * Math.pow (n, 5.0) / 256.0);
64
65 /* Precalculate epsilon */
66 double epsilon = (315.0 * Math.pow (n, 4.0) / 512.0);
67
68 /* Now calculate the sum of the series and return */
69 return alpha
70 * (phi + (beta * Math.sin (2.0 * phi))
71 + (gamma * Math.sin (4.0 * phi))
72 + (delta * Math.sin (6.0 * phi))
73 + (epsilon * Math.sin (8.0 * phi)));
74 }
75
76 /*
77 * UTMCentralMeridian
78 *
79 * Determines the central meridian for the given UTM zone.
80 *
81 * Inputs:
82 * zone - An integer value designating the UTM zone, range [1,60].
83 *
84 * Returns:
85 * The central meridian for the given UTM zone, in radians, or zero
86 * if the UTM zone parameter is outside the range [1,60].
87 * Range of the central meridian is the radian equivalent of [-177,+177].
88 *
89 */
90 private double UTMCentralMeridian(int zone)
91 {
92 return Math.toRadians(-183.0 + (zone * 6.0));
93 }
94 private double UTMCentralMeridianDeg(int zone)
95 {
96 return -183.0 + (zone * 6.0);
97 }
98
99 /*
100 * FootpointLatitude
101 *
102 * Computes the footpoint latitude for use in converting transverse
103 * Mercator coordinates to ellipsoidal coordinates.
104 *
105 * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
106 * GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
107 *
108 * Inputs:
109 * y - The UTM northing coordinate, in meters.
110 *
111 * Returns:
112 * The footpoint latitude, in radians.
113 *
114 */
115 private double FootpointLatitude(double y)
116 {
117 /* Precalculate n (Eq. 10.18) */
118 double n = (Ellipsoid.GRS80.a - Ellipsoid.GRS80.b) / (Ellipsoid.GRS80.a + Ellipsoid.GRS80.b);
119
120 /* Precalculate alpha_ (Eq. 10.22) */
121 /* (Same as alpha in Eq. 10.17) */
122 double alpha_ = ((Ellipsoid.GRS80.a + Ellipsoid.GRS80.b) / 2.0)
123 * (1 + (Math.pow (n, 2.0) / 4) + (Math.pow (n, 4.0) / 64));
124
125 /* Precalculate y_ (Eq. 10.23) */
126 double y_ = y / alpha_;
127
128 /* Precalculate beta_ (Eq. 10.22) */
129 double beta_ = (3.0 * n / 2.0) + (-27.0 * Math.pow (n, 3.0) / 32.0)
130 + (269.0 * Math.pow (n, 5.0) / 512.0);
131
132 /* Precalculate gamma_ (Eq. 10.22) */
133 double gamma_ = (21.0 * Math.pow (n, 2.0) / 16.0)
134 + (-55.0 * Math.pow (n, 4.0) / 32.0);
135
136 /* Precalculate delta_ (Eq. 10.22) */
137 double delta_ = (151.0 * Math.pow (n, 3.0) / 96.0)
138 + (-417.0 * Math.pow (n, 5.0) / 128.0);
139
140 /* Precalculate epsilon_ (Eq. 10.22) */
141 double epsilon_ = (1097.0 * Math.pow (n, 4.0) / 512.0);
142
143 /* Now calculate the sum of the series (Eq. 10.21) */
144 return y_ + (beta_ * Math.sin (2.0 * y_))
145 + (gamma_ * Math.sin (4.0 * y_))
146 + (delta_ * Math.sin (6.0 * y_))
147 + (epsilon_ * Math.sin (8.0 * y_));
148 }
149
150 /*
151 * MapLatLonToXY
152 *
153 * Converts a latitude/longitude pair to x and y coordinates in the
154 * Transverse Mercator projection. Note that Transverse Mercator is not
155 * the same as UTM; a scale factor is required to convert between them.
156 *
157 * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
158 * GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
159 *
160 * Inputs:
161 * phi - Latitude of the point, in radians.
162 * lambda - Longitude of the point, in radians.
163 * lambda0 - Longitude of the central meridian to be used, in radians.
164 *
165 * Outputs:
166 * xy - A 2-element array containing the x and y coordinates
167 * of the computed point.
168 *
169 * Returns:
170 * The function does not return a value.
171 *
172 */
173 public EastNorth MapLatLonToXY(double phi, double lambda, double lambda0)
174 {
175 /* Precalculate ep2 */
176 double ep2 = (Math.pow (Ellipsoid.GRS80.a, 2.0) - Math.pow (Ellipsoid.GRS80.b, 2.0)) / Math.pow (Ellipsoid.GRS80.b, 2.0);
177
178 /* Precalculate nu2 */
179 double nu2 = ep2 * Math.pow (Math.cos (phi), 2.0);
180
181 /* Precalculate N */
182 double N = Math.pow (Ellipsoid.GRS80.a, 2.0) / (Ellipsoid.GRS80.b * Math.sqrt (1 + nu2));
183
184 /* Precalculate t */
185 double t = Math.tan (phi);
186 double t2 = t * t;
187 double tmp = (t2 * t2 * t2) - Math.pow (t, 6.0);
188
189 /* Precalculate l */
190 double l = lambda - lambda0;
191
192 /* Precalculate coefficients for l**n in the equations below
193 so a normal human being can read the expressions for easting
194 and northing
195 -- l**1 and l**2 have coefficients of 1.0 */
196 double l3coef = 1.0 - t2 + nu2;
197
198 double l4coef = 5.0 - t2 + 9 * nu2 + 4.0 * (nu2 * nu2);
199
200 double l5coef = 5.0 - 18.0 * t2 + (t2 * t2) + 14.0 * nu2
201 - 58.0 * t2 * nu2;
202
203 double l6coef = 61.0 - 58.0 * t2 + (t2 * t2) + 270.0 * nu2
204 - 330.0 * t2 * nu2;
205
206 double l7coef = 61.0 - 479.0 * t2 + 179.0 * (t2 * t2) - (t2 * t2 * t2);
207
208 double l8coef = 1385.0 - 3111.0 * t2 + 543.0 * (t2 * t2) - (t2 * t2 * t2);
209
210 return new EastNorth(
211 /* Calculate easting (x) */
212 N * Math.cos (phi) * l
213 + (N / 6.0 * Math.pow (Math.cos (phi), 3.0) * l3coef * Math.pow (l, 3.0))
214 + (N / 120.0 * Math.pow (Math.cos (phi), 5.0) * l5coef * Math.pow (l, 5.0))
215 + (N / 5040.0 * Math.pow (Math.cos (phi), 7.0) * l7coef * Math.pow (l, 7.0)),
216 /* Calculate northing (y) */
217 ArcLengthOfMeridian (phi)
218 + (t / 2.0 * N * Math.pow (Math.cos (phi), 2.0) * Math.pow (l, 2.0))
219 + (t / 24.0 * N * Math.pow (Math.cos (phi), 4.0) * l4coef * Math.pow (l, 4.0))
220 + (t / 720.0 * N * Math.pow (Math.cos (phi), 6.0) * l6coef * Math.pow (l, 6.0))
221 + (t / 40320.0 * N * Math.pow (Math.cos (phi), 8.0) * l8coef * Math.pow (l, 8.0)));
222 }
223
224 /*
225 * MapXYToLatLon
226 *
227 * Converts x and y coordinates in the Transverse Mercator projection to
228 * a latitude/longitude pair. Note that Transverse Mercator is not
229 * the same as UTM; a scale factor is required to convert between them.
230 *
231 * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
232 * GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
233 *
234 * Inputs:
235 * x - The easting of the point, in meters.
236 * y - The northing of the point, in meters.
237 * lambda0 - Longitude of the central meridian to be used, in radians.
238 *
239 * Outputs:
240 * philambda - A 2-element containing the latitude and longitude
241 * in radians.
242 *
243 * Returns:
244 * The function does not return a value.
245 *
246 * Remarks:
247 * The local variables Nf, nuf2, tf, and tf2 serve the same purpose as
248 * N, nu2, t, and t2 in MapLatLonToXY, but they are computed with respect
249 * to the footpoint latitude phif.
250 *
251 * x1frac, x2frac, x2poly, x3poly, etc. are to enhance readability and
252 * to optimize computations.
253 *
254 */
255 public LatLon MapXYToLatLon(double x, double y, double lambda0)
256 {
257 /* Get the value of phif, the footpoint latitude. */
258 double phif = FootpointLatitude (y);
259
260 /* Precalculate ep2 */
261 double ep2 = (Math.pow (Ellipsoid.GRS80.a, 2.0) - Math.pow (Ellipsoid.GRS80.b, 2.0))
262 / Math.pow (Ellipsoid.GRS80.b, 2.0);
263
264 /* Precalculate cos (phif) */
265 double cf = Math.cos (phif);
266
267 /* Precalculate nuf2 */
268 double nuf2 = ep2 * Math.pow (cf, 2.0);
269
270 /* Precalculate Nf and initialize Nfpow */
271 double Nf = Math.pow (Ellipsoid.GRS80.a, 2.0) / (Ellipsoid.GRS80.b * Math.sqrt (1 + nuf2));
272 double Nfpow = Nf;
273
274 /* Precalculate tf */
275 double tf = Math.tan (phif);
276 double tf2 = tf * tf;
277 double tf4 = tf2 * tf2;
278
279 /* Precalculate fractional coefficients for x**n in the equations
280 below to simplify the expressions for latitude and longitude. */
281 double x1frac = 1.0 / (Nfpow * cf);
282
283 Nfpow *= Nf; /* now equals Nf**2) */
284 double x2frac = tf / (2.0 * Nfpow);
285
286 Nfpow *= Nf; /* now equals Nf**3) */
287 double x3frac = 1.0 / (6.0 * Nfpow * cf);
288
289 Nfpow *= Nf; /* now equals Nf**4) */
290 double x4frac = tf / (24.0 * Nfpow);
291
292 Nfpow *= Nf; /* now equals Nf**5) */
293 double x5frac = 1.0 / (120.0 * Nfpow * cf);
294
295 Nfpow *= Nf; /* now equals Nf**6) */
296 double x6frac = tf / (720.0 * Nfpow);
297
298 Nfpow *= Nf; /* now equals Nf**7) */
299 double x7frac = 1.0 / (5040.0 * Nfpow * cf);
300
301 Nfpow *= Nf; /* now equals Nf**8) */
302 double x8frac = tf / (40320.0 * Nfpow);
303
304 /* Precalculate polynomial coefficients for x**n.
305 -- x**1 does not have a polynomial coefficient. */
306 double x2poly = -1.0 - nuf2;
307 double x3poly = -1.0 - 2 * tf2 - nuf2;
308 double x4poly = 5.0 + 3.0 * tf2 + 6.0 * nuf2 - 6.0 * tf2 * nuf2 - 3.0 * (nuf2 *nuf2) - 9.0 * tf2 * (nuf2 * nuf2);
309 double x5poly = 5.0 + 28.0 * tf2 + 24.0 * tf4 + 6.0 * nuf2 + 8.0 * tf2 * nuf2;
310 double x6poly = -61.0 - 90.0 * tf2 - 45.0 * tf4 - 107.0 * nuf2 + 162.0 * tf2 * nuf2;
311 double x7poly = -61.0 - 662.0 * tf2 - 1320.0 * tf4 - 720.0 * (tf4 * tf2);
312 double x8poly = 1385.0 + 3633.0 * tf2 + 4095.0 * tf4 + 1575 * (tf4 * tf2);
313
314 return new LatLon(
315 /* Calculate latitude */
316 Math.toDegrees(
317 phif + x2frac * x2poly * (x * x)
318 + x4frac * x4poly * Math.pow (x, 4.0)
319 + x6frac * x6poly * Math.pow (x, 6.0)
320 + x8frac * x8poly * Math.pow (x, 8.0)),
321 Math.toDegrees(
322 /* Calculate longitude */
323 lambda0 + x1frac * x
324 + x3frac * x3poly * Math.pow (x, 3.0)
325 + x5frac * x5poly * Math.pow (x, 5.0)
326 + x7frac * x7poly * Math.pow (x, 7.0)));
327 }
328
329 public EastNorth latlon2eastNorth(LatLon p) {
330 EastNorth a = MapLatLonToXY(Math.toRadians(p.lat()), Math.toRadians(p.lon()), UTMCentralMeridian(getzone()));
331 return new EastNorth(a.east() * UTMScaleFactor + 3500000.0, a.north() * UTMScaleFactor);
332 }
333
334 public LatLon eastNorth2latlon(EastNorth p) {
335 return MapXYToLatLon((p.east()-3500000.0)/UTMScaleFactor, p.north()/UTMScaleFactor, UTMCentralMeridian(getzone()));
336 }
337
338 @Override public String toString() {
339 return tr("UTM Zone {0}", getzone());
340 }
341
342 /* TODO - support all UTM's not only zone 33 */
343 public int getzone()
344 {
345 return 33;
346 }
347
348 public String toCode() {
349 return "EPSG:325833";
350 }
351
352 public String getCacheDirectoryName() {
353 return "epsg325833";
354 }
355
356 public Bounds getWorldBoundsLatLon()
357 {
358 return new Bounds(
359 new LatLon(-85.0, UTMCentralMeridianDeg(getzone())-5.0),
360 new LatLon(85.0, UTMCentralMeridianDeg(getzone())+5.0));
361 }
362}
Note: See TracBrowser for help on using the repository browser.