source: josm/trunk/src/org/openstreetmap/josm/data/projection/proj/TransverseMercator.java@ 5926

Last change on this file since 5926 was 5926, checked in by bastiK, 11 years ago

clean up imports

  • Property svn:eol-style set to native
File size: 10.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.proj;
3
4import static java.lang.Math.*;
5
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
9
10/**
11 * Transverse Mercator projection.
12 *
13 * @author Dirk Stöcker
14 * code based on JavaScript from Chuck Taylor
15 *
16 */
17public class TransverseMercator implements Proj {
18
19 protected double a, b;
20
21 @Override
22 public String getName() {
23 return tr("Transverse Mercator");
24 }
25
26 @Override
27 public String getProj4Id() {
28 return "tmerc";
29 }
30
31 @Override
32 public void initialize(ProjParameters params) throws ProjectionConfigurationException {
33 this.a = params.ellps.a;
34 this.b = params.ellps.b;
35 }
36
37 /**
38 * Converts a latitude/longitude pair to x and y coordinates in the
39 * Transverse Mercator projection. Note that Transverse Mercator is not
40 * the same as UTM; a scale factor is required to convert between them.
41 *
42 * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
43 * GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
44 *
45 * @param phi Latitude of the point, in radians
46 * @param lambda Longitude of the point, in radians
47 * @return A 2-element array containing the x and y coordinates
48 * of the computed point
49 */
50 @Override
51 public double[] project(double phi, double lambda) {
52
53 /* Precalculate ep2 */
54 double ep2 = (pow(a, 2.0) - pow(b, 2.0)) / pow(b, 2.0);
55
56 /* Precalculate nu2 */
57 double nu2 = ep2 * pow(cos(phi), 2.0);
58
59 /* Precalculate N / a */
60 double N_a = a / (b * sqrt(1 + nu2));
61
62 /* Precalculate t */
63 double t = tan(phi);
64 double t2 = t * t;
65
66 /* Precalculate l */
67 double l = lambda;
68
69 /* Precalculate coefficients for l**n in the equations below
70 so a normal human being can read the expressions for easting
71 and northing
72 -- l**1 and l**2 have coefficients of 1.0 */
73 double l3coef = 1.0 - t2 + nu2;
74
75 double l4coef = 5.0 - t2 + 9 * nu2 + 4.0 * (nu2 * nu2);
76
77 double l5coef = 5.0 - 18.0 * t2 + (t2 * t2) + 14.0 * nu2
78 - 58.0 * t2 * nu2;
79
80 double l6coef = 61.0 - 58.0 * t2 + (t2 * t2) + 270.0 * nu2
81 - 330.0 * t2 * nu2;
82
83 double l7coef = 61.0 - 479.0 * t2 + 179.0 * (t2 * t2) - (t2 * t2 * t2);
84
85 double l8coef = 1385.0 - 3111.0 * t2 + 543.0 * (t2 * t2) - (t2 * t2 * t2);
86
87 return new double[] {
88 /* Calculate easting (x) */
89 N_a * cos(phi) * l
90 + (N_a / 6.0 * pow(cos(phi), 3.0) * l3coef * pow(l, 3.0))
91 + (N_a / 120.0 * pow(cos(phi), 5.0) * l5coef * pow(l, 5.0))
92 + (N_a / 5040.0 * pow(cos(phi), 7.0) * l7coef * pow(l, 7.0)),
93 /* Calculate northing (y) */
94 ArcLengthOfMeridian (phi) / a
95 + (t / 2.0 * N_a * pow(cos(phi), 2.0) * pow(l, 2.0))
96 + (t / 24.0 * N_a * pow(cos(phi), 4.0) * l4coef * pow(l, 4.0))
97 + (t / 720.0 * N_a * pow(cos(phi), 6.0) * l6coef * pow(l, 6.0))
98 + (t / 40320.0 * N_a * pow(cos(phi), 8.0) * l8coef * pow(l, 8.0)) };
99 }
100
101 /**
102 * Converts x and y coordinates in the Transverse Mercator projection to
103 * a latitude/longitude pair. Note that Transverse Mercator is not
104 * the same as UTM; a scale factor is required to convert between them.
105 *
106 * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
107 * GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
108 *
109 * Remarks:
110 * The local variables Nf, nuf2, tf, and tf2 serve the same purpose as
111 * N, nu2, t, and t2 in MapLatLonToXY, but they are computed with respect
112 * to the footpoint latitude phif.
113 *
114 * x1frac, x2frac, x2poly, x3poly, etc. are to enhance readability and
115 * to optimize computations.
116 *
117 * @param x The easting of the point, in meters, divided by the semi major axis of the ellipsoid
118 * @param y The northing of the point, in meters, divided by the semi major axis of the ellipsoid
119 * @return A 2-element containing the latitude and longitude
120 * in radians
121 */
122 @Override
123 public double[] invproject(double x, double y) {
124 /* Get the value of phif, the footpoint latitude. */
125 double phif = footpointLatitude(y);
126
127 /* Precalculate ep2 */
128 double ep2 = (a*a - b*b)
129 / (b*b);
130
131 /* Precalculate cos(phif) */
132 double cf = cos(phif);
133
134 /* Precalculate nuf2 */
135 double nuf2 = ep2 * pow(cf, 2.0);
136
137 /* Precalculate Nf / a and initialize Nfpow */
138 double Nf_a = a / (b * sqrt(1 + nuf2));
139 double Nfpow = Nf_a;
140
141 /* Precalculate tf */
142 double tf = tan(phif);
143 double tf2 = tf * tf;
144 double tf4 = tf2 * tf2;
145
146 /* Precalculate fractional coefficients for x**n in the equations
147 below to simplify the expressions for latitude and longitude. */
148 double x1frac = 1.0 / (Nfpow * cf);
149
150 Nfpow *= Nf_a; /* now equals Nf**2) */
151 double x2frac = tf / (2.0 * Nfpow);
152
153 Nfpow *= Nf_a; /* now equals Nf**3) */
154 double x3frac = 1.0 / (6.0 * Nfpow * cf);
155
156 Nfpow *= Nf_a; /* now equals Nf**4) */
157 double x4frac = tf / (24.0 * Nfpow);
158
159 Nfpow *= Nf_a; /* now equals Nf**5) */
160 double x5frac = 1.0 / (120.0 * Nfpow * cf);
161
162 Nfpow *= Nf_a; /* now equals Nf**6) */
163 double x6frac = tf / (720.0 * Nfpow);
164
165 Nfpow *= Nf_a; /* now equals Nf**7) */
166 double x7frac = 1.0 / (5040.0 * Nfpow * cf);
167
168 Nfpow *= Nf_a; /* now equals Nf**8) */
169 double x8frac = tf / (40320.0 * Nfpow);
170
171 /* Precalculate polynomial coefficients for x**n.
172 -- x**1 does not have a polynomial coefficient. */
173 double x2poly = -1.0 - nuf2;
174 double x3poly = -1.0 - 2 * tf2 - nuf2;
175 double x4poly = 5.0 + 3.0 * tf2 + 6.0 * nuf2 - 6.0 * tf2 * nuf2 - 3.0 * (nuf2 *nuf2) - 9.0 * tf2 * (nuf2 * nuf2);
176 double x5poly = 5.0 + 28.0 * tf2 + 24.0 * tf4 + 6.0 * nuf2 + 8.0 * tf2 * nuf2;
177 double x6poly = -61.0 - 90.0 * tf2 - 45.0 * tf4 - 107.0 * nuf2 + 162.0 * tf2 * nuf2;
178 double x7poly = -61.0 - 662.0 * tf2 - 1320.0 * tf4 - 720.0 * (tf4 * tf2);
179 double x8poly = 1385.0 + 3633.0 * tf2 + 4095.0 * tf4 + 1575 * (tf4 * tf2);
180
181 return new double[] {
182 /* Calculate latitude */
183 phif + x2frac * x2poly * (x * x)
184 + x4frac * x4poly * pow(x, 4.0)
185 + x6frac * x6poly * pow(x, 6.0)
186 + x8frac * x8poly * pow(x, 8.0),
187 /* Calculate longitude */
188 x1frac * x
189 + x3frac * x3poly * pow(x, 3.0)
190 + x5frac * x5poly * pow(x, 5.0)
191 + x7frac * x7poly * pow(x, 7.0) };
192 }
193
194 /**
195 * ArcLengthOfMeridian
196 *
197 * Computes the ellipsoidal distance from the equator to a point at a
198 * given latitude.
199 *
200 * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
201 * GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
202 *
203 * @param phi Latitude of the point, in radians
204 * @return The ellipsoidal distance of the point from the equator
205 * (in meters, divided by the semi major axis of the ellipsoid)
206 */
207 private double ArcLengthOfMeridian(double phi) {
208 /* Precalculate n */
209 double n = (a - b) / (a + b);
210
211 /* Precalculate alpha */
212 double alpha = ((a + b) / 2.0)
213 * (1.0 + (pow(n, 2.0) / 4.0) + (pow(n, 4.0) / 64.0));
214
215 /* Precalculate beta */
216 double beta = (-3.0 * n / 2.0) + (9.0 * pow(n, 3.0) / 16.0)
217 + (-3.0 * pow(n, 5.0) / 32.0);
218
219 /* Precalculate gamma */
220 double gamma = (15.0 * pow(n, 2.0) / 16.0)
221 + (-15.0 * pow(n, 4.0) / 32.0);
222
223 /* Precalculate delta */
224 double delta = (-35.0 * pow(n, 3.0) / 48.0)
225 + (105.0 * pow(n, 5.0) / 256.0);
226
227 /* Precalculate epsilon */
228 double epsilon = (315.0 * pow(n, 4.0) / 512.0);
229
230 /* Now calculate the sum of the series and return */
231 return alpha
232 * (phi + (beta * sin(2.0 * phi))
233 + (gamma * sin(4.0 * phi))
234 + (delta * sin(6.0 * phi))
235 + (epsilon * sin(8.0 * phi)));
236 }
237
238 /**
239 * FootpointLatitude
240 *
241 * Computes the footpoint latitude for use in converting transverse
242 * Mercator coordinates to ellipsoidal coordinates.
243 *
244 * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
245 * GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
246 *
247 * @param y northing coordinate, in meters, divided by the semi major axis of the ellipsoid
248 * @return The footpoint latitude, in radians
249 */
250 private double footpointLatitude(double y) {
251 /* Precalculate n (Eq. 10.18) */
252 double n = (a - b) / (a + b);
253
254 /* Precalculate alpha_ (Eq. 10.22) */
255 /* (Same as alpha in Eq. 10.17) */
256 double alpha_ = ((a + b) / 2.0)
257 * (1 + (pow(n, 2.0) / 4) + (pow(n, 4.0) / 64));
258
259 /* Precalculate y_ (Eq. 10.23) */
260 double y_ = y / alpha_ * a;
261
262 /* Precalculate beta_ (Eq. 10.22) */
263 double beta_ = (3.0 * n / 2.0) + (-27.0 * pow(n, 3.0) / 32.0)
264 + (269.0 * pow(n, 5.0) / 512.0);
265
266 /* Precalculate gamma_ (Eq. 10.22) */
267 double gamma_ = (21.0 * pow(n, 2.0) / 16.0)
268 + (-55.0 * pow(n, 4.0) / 32.0);
269
270 /* Precalculate delta_ (Eq. 10.22) */
271 double delta_ = (151.0 * pow(n, 3.0) / 96.0)
272 + (-417.0 * pow(n, 5.0) / 128.0);
273
274 /* Precalculate epsilon_ (Eq. 10.22) */
275 double epsilon_ = (1097.0 * pow(n, 4.0) / 512.0);
276
277 /* Now calculate the sum of the series (Eq. 10.21) */
278 return y_ + (beta_ * sin(2.0 * y_))
279 + (gamma_ * sin(4.0 * y_))
280 + (delta_ * sin(6.0 * y_))
281 + (epsilon_ * sin(8.0 * y_));
282 }
283
284}
Note: See TracBrowser for help on using the repository browser.