GeographicLib: TransverseMercator.cpp Source File

GeographicLib 2.7

Loading...
Searching...
No Matches
TransverseMercator.cpp
Go to the documentation of this file.
1/**
2 * \file TransverseMercator.cpp
3 * \brief Implementation for GeographicLib::TransverseMercator class
4 *
5 * Copyright (c) Charles Karney (2008-2023) <karney@alum.mit.edu> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 *
9 * This implementation follows closely JHS 154, ETRS89 -
10 * j&auml;rjestelm&auml;&auml;n liittyv&auml;t karttaprojektiot,
11 * tasokoordinaatistot ja karttalehtijako</a> (Map projections, plane
12 * coordinates, and map sheet index for ETRS89), published by JUHTA, Finnish
13 * Geodetic Institute, and the National Land Survey of Finland (2006).
14 *
15 * The relevant section is available as the 2008 PDF file
16 * http://docs.jhs-suositukset.fi/jhs-suositukset/JHS154/JHS154_liite1.pdf
17 *
18 * This is a straight transcription of the formulas in this paper with the
19 * following exceptions:
20 * - use of 6th order series instead of 4th order series. This reduces the
21 * error to about 5nm for the UTM range of coordinates (instead of 200nm),
22 * with a speed penalty of only 1%;
23 * - use Newton's method instead of plain iteration to solve for latitude in
24 * terms of isometric latitude in the Reverse method;
25 * - use of Horner's representation for evaluating polynomials and Clenshaw's
26 * method for summing trigonometric series;
27 * - several modifications of the formulas to improve the numerical accuracy;
28 * - evaluating the convergence and scale using the expression for the
29 * projection or its inverse.
30 *
31 * If the preprocessor variable GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER is set
32 * to an integer between 4 and 8, then this specifies the order of the series
33 * used for the forward and reverse transformations. The default value is 6.
34 * (The series accurate to 12th order is given in \ref tmseries.)
35 **********************************************************************/
36
37#include <complex>
38#include <GeographicLib/TransverseMercator.hpp>
39
40namespace GeographicLib {
41
42 using namespace std;
43
44 TransverseMercator::TransverseMercator(real a, real f, real k0,
45 bool exact, bool extendp)
46 : _a(a)
47 , _f(f)
48 , _k0(k0)
49 , _exact(exact)
50 , _e2(_f * (2 - _f))
51 , _es((_f < 0 ? -1 : 1) * sqrt(fabs(_e2)))
52 , _e2m(1 - _e2)
53 // _c = sqrt( pow(1 + _e, 1 + _e) * pow(1 - _e, 1 - _e) ) )
54 // See, for example, Lee (1976), p 100.
55 , _c( sqrt(_e2m) * exp(Math::eatanhe(real(1), _es)) )
56 , _n(_f / (2 - _f))
57 , _tmexact(_exact ? TransverseMercatorExact(a, f, k0, extendp) :
58 TransverseMercatorExact())
59 {
60 if (_exact) return;
61 if (!(isfinite(_a) && _a > 0))
62 throw GeographicErr("Equatorial radius is not positive");
63 if (!(isfinite(_f) && _f < 1))
64 throw GeographicErr("Polar semiaxis is not positive");
65 if (!(isfinite(_k0) && _k0 > 0))
66 throw GeographicErr("Scale is not positive");
67 if (extendp)
68 throw GeographicErr("TransverseMercator extendp not allowed if !exact");
69
70 // Generated by Maxima on 2015-05-14 22:55:13-04:00
71#if GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER/2 == 2
72 static const real b1coeff[] = {
73 // b1*(n+1), polynomial in n2 of order 2
74 1, 16, 64, 64,
75 }; // count = 4
76#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER/2 == 3
77 static const real b1coeff[] = {
78 // b1*(n+1), polynomial in n2 of order 3
79 1, 4, 64, 256, 256,
80 }; // count = 5
81#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER/2 == 4
82 static const real b1coeff[] = {
83 // b1*(n+1), polynomial in n2 of order 4
84 25, 64, 256, 4096, 16384, 16384,
85 }; // count = 6
86#else
87#error "Bad value for GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER"
88#endif
89
90#if GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 4
91 static const real alpcoeff[] = {
92 // alp[1]/n^1, polynomial in n of order 3
93 164, 225, -480, 360, 720,
94 // alp[2]/n^2, polynomial in n of order 2
95 557, -864, 390, 1440,
96 // alp[3]/n^3, polynomial in n of order 1
97 -1236, 427, 1680,
98 // alp[4]/n^4, polynomial in n of order 0
99 49561, 161280,
100 }; // count = 14
101#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 5
102 static const real alpcoeff[] = {
103 // alp[1]/n^1, polynomial in n of order 4
104 -635, 328, 450, -960, 720, 1440,
105 // alp[2]/n^2, polynomial in n of order 3
106 4496, 3899, -6048, 2730, 10080,
107 // alp[3]/n^3, polynomial in n of order 2
108 15061, -19776, 6832, 26880,
109 // alp[4]/n^4, polynomial in n of order 1
110 -171840, 49561, 161280,
111 // alp[5]/n^5, polynomial in n of order 0
112 34729, 80640,
113 }; // count = 20
114#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 6
115 static const real alpcoeff[] = {
116 // alp[1]/n^1, polynomial in n of order 5
117 31564, -66675, 34440, 47250, -100800, 75600, 151200,
118 // alp[2]/n^2, polynomial in n of order 4
119 -1983433, 863232, 748608, -1161216, 524160, 1935360,
120 // alp[3]/n^3, polynomial in n of order 3
121 670412, 406647, -533952, 184464, 725760,
122 // alp[4]/n^4, polynomial in n of order 2
123 6601661, -7732800, 2230245, 7257600,
124 // alp[5]/n^5, polynomial in n of order 1
125 -13675556, 3438171, 7983360,
126 // alp[6]/n^6, polynomial in n of order 0
127 212378941, 319334400,
128 }; // count = 27
129#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 7
130 static const real alpcoeff[] = {
131 // alp[1]/n^1, polynomial in n of order 6
132 1804025, 2020096, -4267200, 2204160, 3024000, -6451200, 4838400, 9676800,
133 // alp[2]/n^2, polynomial in n of order 5
134 4626384, -9917165, 4316160, 3743040, -5806080, 2620800, 9676800,
135 // alp[3]/n^3, polynomial in n of order 4
136 -67102379, 26816480, 16265880, -21358080, 7378560, 29030400,
137 // alp[4]/n^4, polynomial in n of order 3
138 155912000, 72618271, -85060800, 24532695, 79833600,
139 // alp[5]/n^5, polynomial in n of order 2
140 102508609, -109404448, 27505368, 63866880,
141 // alp[6]/n^6, polynomial in n of order 1
142 -12282192400LL, 2760926233LL, 4151347200LL,
143 // alp[7]/n^7, polynomial in n of order 0
144 1522256789, 1383782400,
145 }; // count = 35
146#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 8
147 static const real alpcoeff[] = {
148 // alp[1]/n^1, polynomial in n of order 7
149 -75900428, 37884525, 42422016, -89611200, 46287360, 63504000, -135475200,
150 101606400, 203212800,
151 // alp[2]/n^2, polynomial in n of order 6
152 148003883, 83274912, -178508970, 77690880, 67374720, -104509440,
153 47174400, 174182400,
154 // alp[3]/n^3, polynomial in n of order 5
155 318729724, -738126169, 294981280, 178924680, -234938880, 81164160,
156 319334400,
157 // alp[4]/n^4, polynomial in n of order 4
158 -40176129013LL, 14967552000LL, 6971354016LL, -8165836800LL, 2355138720LL,
159 7664025600LL,
160 // alp[5]/n^5, polynomial in n of order 3
161 10421654396LL, 3997835751LL, -4266773472LL, 1072709352, 2490808320LL,
162 // alp[6]/n^6, polynomial in n of order 2
163 175214326799LL, -171950693600LL, 38652967262LL, 58118860800LL,
164 // alp[7]/n^7, polynomial in n of order 1
165 -67039739596LL, 13700311101LL, 12454041600LL,
166 // alp[8]/n^8, polynomial in n of order 0
167 1424729850961LL, 743921418240LL,
168 }; // count = 44
169#else
170#error "Bad value for GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER"
171#endif
172
173#if GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 4
174 static const real betcoeff[] = {
175 // bet[1]/n^1, polynomial in n of order 3
176 -4, 555, -960, 720, 1440,
177 // bet[2]/n^2, polynomial in n of order 2
178 -437, 96, 30, 1440,
179 // bet[3]/n^3, polynomial in n of order 1
180 -148, 119, 3360,
181 // bet[4]/n^4, polynomial in n of order 0
182 4397, 161280,
183 }; // count = 14
184#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 5
185 static const real betcoeff[] = {
186 // bet[1]/n^1, polynomial in n of order 4
187 -3645, -64, 8880, -15360, 11520, 23040,
188 // bet[2]/n^2, polynomial in n of order 3
189 4416, -3059, 672, 210, 10080,
190 // bet[3]/n^3, polynomial in n of order 2
191 -627, -592, 476, 13440,
192 // bet[4]/n^4, polynomial in n of order 1
193 -3520, 4397, 161280,
194 // bet[5]/n^5, polynomial in n of order 0
195 4583, 161280,
196 }; // count = 20
197#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 6
198 static const real betcoeff[] = {
199 // bet[1]/n^1, polynomial in n of order 5
200 384796, -382725, -6720, 932400, -1612800, 1209600, 2419200,
201 // bet[2]/n^2, polynomial in n of order 4
202 -1118711, 1695744, -1174656, 258048, 80640, 3870720,
203 // bet[3]/n^3, polynomial in n of order 3
204 22276, -16929, -15984, 12852, 362880,
205 // bet[4]/n^4, polynomial in n of order 2
206 -830251, -158400, 197865, 7257600,
207 // bet[5]/n^5, polynomial in n of order 1
208 -435388, 453717, 15966720,
209 // bet[6]/n^6, polynomial in n of order 0
210 20648693, 638668800,
211 }; // count = 27
212#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 7
213 static const real betcoeff[] = {
214 // bet[1]/n^1, polynomial in n of order 6
215 -5406467, 6156736, -6123600, -107520, 14918400, -25804800, 19353600,
216 38707200,
217 // bet[2]/n^2, polynomial in n of order 5
218 829456, -5593555, 8478720, -5873280, 1290240, 403200, 19353600,
219 // bet[3]/n^3, polynomial in n of order 4
220 9261899, 3564160, -2708640, -2557440, 2056320, 58060800,
221 // bet[4]/n^4, polynomial in n of order 3
222 14928352, -9132761, -1742400, 2176515, 79833600,
223 // bet[5]/n^5, polynomial in n of order 2
224 -8005831, -1741552, 1814868, 63866880,
225 // bet[6]/n^6, polynomial in n of order 1
226 -261810608, 268433009, 8302694400LL,
227 // bet[7]/n^7, polynomial in n of order 0
228 219941297, 5535129600LL,
229 }; // count = 35
230#elif GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER == 8
231 static const real betcoeff[] = {
232 // bet[1]/n^1, polynomial in n of order 7
233 31777436, -37845269, 43097152, -42865200, -752640, 104428800, -180633600,
234 135475200, 270950400,
235 // bet[2]/n^2, polynomial in n of order 6
236 24749483, 14930208, -100683990, 152616960, -105719040, 23224320, 7257600,
237 348364800,
238 // bet[3]/n^3, polynomial in n of order 5
239 -232468668, 101880889, 39205760, -29795040, -28131840, 22619520,
240 638668800,
241 // bet[4]/n^4, polynomial in n of order 4
242 324154477, 1433121792, -876745056, -167270400, 208945440, 7664025600LL,
243 // bet[5]/n^5, polynomial in n of order 3
244 457888660, -312227409, -67920528, 70779852, 2490808320LL,
245 // bet[6]/n^6, polynomial in n of order 2
246 -19841813847LL, -3665348512LL, 3758062126LL, 116237721600LL,
247 // bet[7]/n^7, polynomial in n of order 1
248 -1989295244, 1979471673, 49816166400LL,
249 // bet[8]/n^8, polynomial in n of order 0
250 191773887257LL, 3719607091200LL,
251 }; // count = 44
252#else
253#error "Bad value for GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER"
254#endif
255
256 static_assert(sizeof(b1coeff) / sizeof(real) == maxpow_/2 + 2,
257 "Coefficient array size mismatch for b1");
258 static_assert(sizeof(alpcoeff) / sizeof(real) ==
259 (maxpow_ * (maxpow_ + 3))/2,
260 "Coefficient array size mismatch for alp");
261 static_assert(sizeof(betcoeff) / sizeof(real) ==
262 (maxpow_ * (maxpow_ + 3))/2,
263 "Coefficient array size mismatch for bet");
264 int m = maxpow_/2;
265 _b1 = Math::polyval(m, b1coeff, Math::sq(_n)) / (b1coeff[m + 1] * (1+_n));
266 // _a1 is the equivalent radius for computing the circumference of
267 // ellipse.
268 _a1 = _b1 * _a;
269 int o = 0;
270 real d = _n;
271 for (int l = 1; l <= maxpow_; ++l) {
272 m = maxpow_ - l;
273 _alp[l] = d * Math::polyval(m, alpcoeff + o, _n) / alpcoeff[o + m + 1];
274 _bet[l] = d * Math::polyval(m, betcoeff + o, _n) / betcoeff[o + m + 1];
275 o += m + 2;
276 d *= _n;
277 }
278 // Post condition: o == sizeof(alpcoeff) / sizeof(real) &&
279 // o == sizeof(betcoeff) / sizeof(real)
280 }
281
282 const TransverseMercator& TransverseMercator::UTM() {
283 static const TransverseMercator utm(Constants::WGS84_a(),
284 Constants::WGS84_f(),
285 Constants::UTM_k0());
286 return utm;
287 }
288
289 // Engsager and Poder (2007) use trigonometric series to convert between phi
290 // and phip. Here are the series...
291 //
292 // Conversion from phi to phip:
293 //
294 // phip = phi + sum(c[j] * sin(2*j*phi), j, 1, 6)
295 //
296 // c[1] = - 2 * n
297 // + 2/3 * n^2
298 // + 4/3 * n^3
299 // - 82/45 * n^4
300 // + 32/45 * n^5
301 // + 4642/4725 * n^6;
302 // c[2] = 5/3 * n^2
303 // - 16/15 * n^3
304 // - 13/9 * n^4
305 // + 904/315 * n^5
306 // - 1522/945 * n^6;
307 // c[3] = - 26/15 * n^3
308 // + 34/21 * n^4
309 // + 8/5 * n^5
310 // - 12686/2835 * n^6;
311 // c[4] = 1237/630 * n^4
312 // - 12/5 * n^5
313 // - 24832/14175 * n^6;
314 // c[5] = - 734/315 * n^5
315 // + 109598/31185 * n^6;
316 // c[6] = 444337/155925 * n^6;
317 //
318 // Conversion from phip to phi:
319 //
320 // phi = phip + sum(d[j] * sin(2*j*phip), j, 1, 6)
321 //
322 // d[1] = 2 * n
323 // - 2/3 * n^2
324 // - 2 * n^3
325 // + 116/45 * n^4
326 // + 26/45 * n^5
327 // - 2854/675 * n^6;
328 // d[2] = 7/3 * n^2
329 // - 8/5 * n^3
330 // - 227/45 * n^4
331 // + 2704/315 * n^5
332 // + 2323/945 * n^6;
333 // d[3] = 56/15 * n^3
334 // - 136/35 * n^4
335 // - 1262/105 * n^5
336 // + 73814/2835 * n^6;
337 // d[4] = 4279/630 * n^4
338 // - 332/35 * n^5
339 // - 399572/14175 * n^6;
340 // d[5] = 4174/315 * n^5
341 // - 144838/6237 * n^6;
342 // d[6] = 601676/22275 * n^6;
343 //
344 // In order to maintain sufficient relative accuracy close to the pole use
345 //
346 // S = sum(c[i]*sin(2*i*phi),i,1,6)
347 // taup = (tau + tan(S)) / (1 - tau * tan(S))
348
349 // In Math::taupf and Math::tauf we evaluate the forward transform explicitly
350 // and solve the reverse one by Newton's method.
351 //
352 // There are adapted from TransverseMercatorExact (taup and taupinv). tau =
353 // tan(phi), taup = sinh(psi)
354
355 void TransverseMercator::Forward(real lon0, real lat, real lon,
356 real& x, real& y,
357 real& gamma, real& k) const {
358 if (_exact)
359 return _tmexact.Forward(lon0, lat, lon, x, y, gamma, k);
360 lat = Math::LatFix(lat);
361 lon = Math::AngDiff(lon0, lon);
362 // Explicitly enforce the parity
363 int
364 latsign = signbit(lat) ? -1 : 1,
365 lonsign = signbit(lon) ? -1 : 1;
366 lon *= lonsign;
367 lat *= latsign;
368 bool backside = lon > Math::qd;
369 if (backside) {
370 if (lat == 0)
371 latsign = -1;
372 lon = Math::hd - lon;
373 }
374 real sphi, cphi, slam, clam;
375 Math::sincosd(lat, sphi, cphi);
376 Math::sincosd(lon, slam, clam);
377 // phi = latitude
378 // phi' = conformal latitude
379 // psi = isometric latitude
380 // tau = tan(phi)
381 // tau' = tan(phi')
382 // [xi', eta'] = Gauss-Schreiber TM coordinates
383 // [xi, eta] = Gauss-Krueger TM coordinates
384 //
385 // We use
386 // tan(phi') = sinh(psi)
387 // sin(phi') = tanh(psi)
388 // cos(phi') = sech(psi)
389 // denom^2 = 1-cos(phi')^2*sin(lam)^2 = 1-sech(psi)^2*sin(lam)^2
390 // sin(xip) = sin(phi')/denom = tanh(psi)/denom
391 // cos(xip) = cos(phi')*cos(lam)/denom = sech(psi)*cos(lam)/denom
392 // cosh(etap) = 1/denom = 1/denom
393 // sinh(etap) = cos(phi')*sin(lam)/denom = sech(psi)*sin(lam)/denom
394 real etap, xip;
395 if (lat != Math::qd) {
396 real
397 tau = sphi / cphi,
398 taup = Math::taupf(tau, _es);
399 xip = atan2(taup, clam);
400 // Used to be
401 // etap = Math::atanh(sin(lam) / cosh(psi));
402 etap = asinh(slam / hypot(taup, clam));
403 // convergence and scale for Gauss-Schreiber TM (xip, etap) -- gamma0 =
404 // atan(tan(xip) * tanh(etap)) = atan(tan(lam) * sin(phi'));
405 // sin(phi') = tau'/sqrt(1 + tau'^2)
406 // Krueger p 22 (44)
407 gamma = Math::atan2d(slam * taup, clam * hypot(real(1), taup));
408 // k0 = sqrt(1 - _e2 * sin(phi)^2) * (cos(phi') / cos(phi)) * cosh(etap)
409 // Note 1/cos(phi) = cosh(psip);
410 // and cos(phi') * cosh(etap) = 1/hypot(sinh(psi), cos(lam))
411 //
412 // This form has cancelling errors. This property is lost if cosh(psip)
413 // is replaced by 1/cos(phi), even though it's using "primary" data (phi
414 // instead of psip).
415 k = sqrt(_e2m + _e2 * Math::sq(cphi)) * hypot(real(1), tau)
416 / hypot(taup, clam);
417 } else {
418 xip = Math::pi()/2;
419 etap = 0;
420 gamma = lon;
421 k = _c;
422 }
423 // {xi',eta'} is {northing,easting} for Gauss-Schreiber transverse Mercator
424 // (for eta' = 0, xi' = bet). {xi,eta} is {northing,easting} for transverse
425 // Mercator with constant scale on the central meridian (for eta = 0, xip =
426 // rectifying latitude). Define
427 //
428 // zeta = xi + i*eta
429 // zeta' = xi' + i*eta'
430 //
431 // The conversion from conformal to rectifying latitude can be expressed as
432 // a series in _n:
433 //
434 // zeta = zeta' + sum(h[j-1]' * sin(2 * j * zeta'), j = 1..maxpow_)
435 //
436 // where h[j]' = O(_n^j). The reversion of this series gives
437 //
438 // zeta' = zeta - sum(h[j-1] * sin(2 * j * zeta), j = 1..maxpow_)
439 //
440 // which is used in Reverse.
441 //
442 // Evaluate sums via Clenshaw method. See
443 // https://en.wikipedia.org/wiki/Clenshaw_algorithm
444 //
445 // Let
446 //
447 // S = sum(a[k] * phi[k](x), k = 0..n)
448 // phi[k+1](x) = alpha[k](x) * phi[k](x) + beta[k](x) * phi[k-1](x)
449 //
450 // Evaluate S with
451 //
452 // b[n+2] = b[n+1] = 0
453 // b[k] = alpha[k](x) * b[k+1] + beta[k+1](x) * b[k+2] + a[k]
454 // S = (a[0] + beta[1](x) * b[2]) * phi[0](x) + b[1] * phi[1](x)
455 //
456 // Here we have
457 //
458 // x = 2 * zeta'
459 // phi[k](x) = sin(k * x)
460 // alpha[k](x) = 2 * cos(x)
461 // beta[k](x) = -1
462 // [ sin(A+B) - 2*cos(B)*sin(A) + sin(A-B) = 0, A = k*x, B = x ]
463 // n = maxpow_
464 // a[k] = _alp[k]
465 // S = b[1] * sin(x)
466 //
467 // For the derivative we have
468 //
469 // x = 2 * zeta'
470 // phi[k](x) = cos(k * x)
471 // alpha[k](x) = 2 * cos(x)
472 // beta[k](x) = -1
473 // [ cos(A+B) - 2*cos(B)*cos(A) + cos(A-B) = 0, A = k*x, B = x ]
474 // a[0] = 1; a[k] = 2*k*_alp[k]
475 // S = (a[0] - b[2]) + b[1] * cos(x)
476 //
477 // Matrix formulation (not used here):
478 // phi[k](x) = [sin(k * x); k * cos(k * x)]
479 // alpha[k](x) = 2 * [cos(x), 0; -sin(x), cos(x)]
480 // beta[k](x) = -1 * [1, 0; 0, 1]
481 // a[k] = _alp[k] * [1, 0; 0, 1]
482 // b[n+2] = b[n+1] = [0, 0; 0, 0]
483 // b[k] = alpha[k](x) * b[k+1] + beta[k+1](x) * b[k+2] + a[k]
484 // N.B., for all k: b[k](1,2) = 0; b[k](1,1) = b[k](2,2)
485 // S = (a[0] + beta[1](x) * b[2]) * phi[0](x) + b[1] * phi[1](x)
486 // phi[0](x) = [0; 0]
487 // phi[1](x) = [sin(x); cos(x)]
488 real
489 c0 = cos(2 * xip), ch0 = cosh(2 * etap),
490 s0 = sin(2 * xip), sh0 = sinh(2 * etap);
491 complex<real> a(2 * c0 * ch0, -2 * s0 * sh0); // 2 * cos(2*zeta')
492 int n = maxpow_;
493 complex<real>
494 y0(n & 1 ? _alp[n] : 0), y1, // default initializer is 0+i0
495 z0(n & 1 ? 2*n * _alp[n] : 0), z1;
496 if (n & 1) --n;
497 while (n) {
498 y1 = a * y0 - y1 + _alp[n];
499 z1 = a * z0 - z1 + 2*n * _alp[n];
500 --n;
501 y0 = a * y1 - y0 + _alp[n];
502 z0 = a * z1 - z0 + 2*n * _alp[n];
503 --n;
504 }
505 a /= real(2); // cos(2*zeta')
506 z1 = real(1) - z1 + a * z0;
507 a = complex<real>(s0 * ch0, c0 * sh0); // sin(2*zeta')
508 y1 = complex<real>(xip, etap) + a * y0;
509 // Fold in change in convergence and scale for Gauss-Schreiber TM to
510 // Gauss-Krueger TM.
511 gamma -= Math::atan2d(z1.imag(), z1.real());
512 k *= _b1 * abs(z1);
513 real xi = y1.real(), eta = y1.imag();
514 y = _a1 * _k0 * (backside ? Math::pi() - xi : xi) * latsign;
515 x = _a1 * _k0 * eta * lonsign;
516 if (backside)
517 gamma = Math::hd - gamma;
518 gamma *= latsign * lonsign;
519 gamma = Math::AngNormalize(gamma);
520 k *= _k0;
521 }
522
523 void TransverseMercator::Reverse(real lon0, real x, real y,
524 real& lat, real& lon,
525 real& gamma, real& k) const {
526 if (_exact)
527 return _tmexact.Reverse(lon0, x, y, lat, lon, gamma, k);
528 // This undoes the steps in Forward. The wrinkles are: (1) Use of the
529 // reverted series to express zeta' in terms of zeta. (2) Newton's method
530 // to solve for phi in terms of tan(phi).
531 real
532 xi = y / (_a1 * _k0),
533 eta = x / (_a1 * _k0);
534 // Explicitly enforce the parity
535 int
536 xisign = signbit(xi) ? -1 : 1,
537 etasign = signbit(eta) ? -1 : 1;
538 xi *= xisign;
539 eta *= etasign;
540 bool backside = xi > Math::pi()/2;
541 if (backside)
542 xi = Math::pi() - xi;
543 real
544 c0 = cos(2 * xi), ch0 = cosh(2 * eta),
545 s0 = sin(2 * xi), sh0 = sinh(2 * eta);
546 complex<real> a(2 * c0 * ch0, -2 * s0 * sh0); // 2 * cos(2*zeta)
547 int n = maxpow_;
548 complex<real>
549 y0(n & 1 ? -_bet[n] : 0), y1, // default initializer is 0+i0
550 z0(n & 1 ? -2*n * _bet[n] : 0), z1;
551 if (n & 1) --n;
552 while (n) {
553 y1 = a * y0 - y1 - _bet[n];
554 z1 = a * z0 - z1 - 2*n * _bet[n];
555 --n;
556 y0 = a * y1 - y0 - _bet[n];
557 z0 = a * z1 - z0 - 2*n * _bet[n];
558 --n;
559 }
560 a /= real(2); // cos(2*zeta)
561 z1 = real(1) - z1 + a * z0;
562 a = complex<real>(s0 * ch0, c0 * sh0); // sin(2*zeta)
563 y1 = complex<real>(xi, eta) + a * y0;
564 // Convergence and scale for Gauss-Schreiber TM to Gauss-Krueger TM.
565 gamma = Math::atan2d(z1.imag(), z1.real());
566 k = _b1 / abs(z1);
567 // JHS 154 has
568 //
569 // phi' = asin(sin(xi') / cosh(eta')) (Krueger p 17 (25))
570 // lam = asin(tanh(eta') / cos(phi')
571 // psi = asinh(tan(phi'))
572 real
573 xip = y1.real(), etap = y1.imag(),
574 s = sinh(etap),
575 c = fmax(real(0), cos(xip)), // cos(pi/2) might be negative
576 r = hypot(s, c);
577 if (r != 0) {
578 lon = Math::atan2d(s, c); // Krueger p 17 (25)
579 // Use Newton's method to solve for tau
580 real
581 sxip = sin(xip),
582 tau = Math::tauf(sxip/r, _es);
583 gamma += Math::atan2d(sxip * tanh(etap), c); // Krueger p 19 (31)
584 lat = Math::atand(tau);
585 // Note cos(phi') * cosh(eta') = r
586 k *= sqrt(_e2m + _e2 / (1 + Math::sq(tau))) *
587 hypot(real(1), tau) * r;
588 } else {
589 lat = Math::qd;
590 lon = 0;
591 k *= _c;
592 }
593 lat *= xisign;
594 if (backside)
595 lon = Math::hd - lon;
596 lon *= etasign;
597 lon = Math::AngNormalize(lon + lon0);
598 if (backside)
599 gamma = Math::hd - gamma;
600 gamma *= xisign * etasign;
601 gamma = Math::AngNormalize(gamma);
602 k *= _k0;
603 }
604
605} // namespace GeographicLib
real
GeographicLib::Math::real real
Definition Geod3Solve.cpp:25
TransverseMercator.hpp
Header for GeographicLib::TransverseMercator class.
GeographicLib::Constants::UTM_k0
static T UTM_k0()
Definition Constants.hpp:176
GeographicLib::Constants::WGS84_f
static T WGS84_f()
Definition Constants.hpp:118
GeographicLib::Constants::WGS84_a
static T WGS84_a()
Definition Constants.hpp:112
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:344
GeographicLib::Math
Mathematical functions needed by GeographicLib.
Definition Math.hpp:82
GeographicLib::Math::LatFix
static T LatFix(T x)
Definition Math.hpp:303
GeographicLib::Math::sincosd
static void sincosd(T x, T &sinx, T &cosx)
Definition Math.cpp:104
GeographicLib::Math::atan2d
static T atan2d(T y, T x)
Definition Math.cpp:212
GeographicLib::Math::sq
static T sq(T x)
Definition Math.hpp:209
GeographicLib::Math::qd
static constexpr int qd
degrees per quarter turn
Definition Math.hpp:142
GeographicLib::Math::tauf
static T tauf(T taup, T es)
Definition Math.cpp:251
GeographicLib::Math::AngNormalize
static T AngNormalize(T x)
Definition Math.cpp:69
GeographicLib::Math::atand
static T atand(T x)
Definition Math.cpp:234
GeographicLib::Math::taupf
static T taupf(T tau, T es)
Definition Math.cpp:241
GeographicLib::Math::pi
static T pi()
Definition Math.hpp:187
GeographicLib::Math::polyval
static T polyval(int N, const T p[], T x)
Definition Math.hpp:270
GeographicLib::Math::AngDiff
static T AngDiff(T x, T y, T &e)
Definition Math.cpp:80
GeographicLib::Math::hd
static constexpr int hd
degrees per half turn
Definition Math.hpp:145
GeographicLib::TransverseMercatorExact
An exact implementation of the transverse Mercator projection.
Definition TransverseMercatorExact.hpp:85
GeographicLib::TransverseMercator::TransverseMercator
TransverseMercator(real a, real f, real k0, bool exact=false, bool extendp=false)
Definition TransverseMercator.cpp:44
GeographicLib::TransverseMercator::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
Definition TransverseMercator.cpp:523
GeographicLib::TransverseMercator::UTM
static const TransverseMercator & UTM()
Definition TransverseMercator.cpp:282
GeographicLib::TransverseMercator::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Definition TransverseMercator.cpp:355
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12