GeographicLib  1.35
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-2012) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  *
9  * This implementation follows closely
10  * <a href="http://www.jhs-suositukset.fi/suomi/jhs154"> JHS 154, ETRS89 -
11  * j&auml;rjestelm&auml;&auml;n liittyv&auml;t karttaprojektiot,
12  * tasokoordinaatistot ja karttalehtijako</a> (Map projections, plane
13  * coordinates, and map sheet index for ETRS89), published by JUHTA, Finnish
14  * Geodetic Institute, and the National Land Survey of Finland (2006).
15  *
16  * The relevant section is available as the 2008 PDF file
17  * http://docs.jhs-suositukset.fi/jhs-suositukset/JHS154/JHS154_liite1.pdf
18  *
19  * This is a straight transcription of the formulas in this paper with the
20  * following exceptions:
21  * - use of 6th order series instead of 4th order series. This reduces the
22  * error to about 5nm for the UTM range of coordinates (instead of 200nm),
23  * with a speed penalty of only 1%;
24  * - use Newton's method instead of plain iteration to solve for latitude in
25  * terms of isometric latitude in the Reverse method;
26  * - use of Horner's representation for evaluating polynomials and Clenshaw's
27  * method for summing trigonometric series;
28  * - several modifications of the formulas to improve the numerical accuracy;
29  * - evaluating the convergence and scale using the expression for the
30  * projection or its inverse.
31  *
32  * If the preprocessor variable GEOGRAPHICLIB_TRANSVERSEMERCATOR_ORDER is set
33  * to an integer between 4 and 8, then this specifies the order of the series
34  * used for the forward and reverse transformations. The default value is 6.
35  * (The series accurate to 12th order is given in \ref tmseries.)
36  *
37  * Other equivalent implementations are given in
38  * - http://www.ign.fr/DISPLAY/000/526/702/5267021/NTG_76.pdf
39  * - http://www.lantmateriet.se/upload/filer/kartor/geodesi_gps_och_detaljmatning/geodesi/Formelsamling/Gauss_Conformal_Projection.pdf
40  **********************************************************************/
41 
43 
44 namespace GeographicLib {
45 
46  using namespace std;
47 
48  const Math::real TransverseMercator::tol_ =
49  real(0.1)*sqrt(numeric_limits<real>::epsilon());
50  // Overflow value s.t. atan(overflow_) = pi/2
51  const Math::real TransverseMercator::overflow_ =
52  1 / Math::sq(numeric_limits<real>::epsilon());
53 
54  TransverseMercator::TransverseMercator(real a, real f, real k0)
55  : _a(a)
56  , _f(f <= 1 ? f : 1/f)
57  , _k0(k0)
58  , _e2(_f * (2 - _f))
59  , _e(sqrt(abs(_e2)))
60  , _e2m(1 - _e2)
61  // _c = sqrt( pow(1 + _e, 1 + _e) * pow(1 - _e, 1 - _e) ) )
62  // See, for example, Lee (1976), p 100.
63  , _c( sqrt(_e2m) * exp(eatanhe(real(1))) )
64  , _n(_f / (2 - _f))
65  {
66  if (!(Math::isfinite(_a) && _a > 0))
67  throw GeographicErr("Major radius is not positive");
68  if (!(Math::isfinite(_f) && _f < 1))
69  throw GeographicErr("Minor radius is not positive");
70  if (!(Math::isfinite(_k0) && _k0 > 0))
71  throw GeographicErr("Scale is not positive");
72  // If coefficents might overflow_ an int, convert them to double (and they
73  // are all exactly representable as doubles).
74  real nx = Math::sq(_n);
75  switch (maxpow_) {
76  case 4:
77  _b1 = 1/(1+_n)*(nx*(nx+16)+64)/64;
78  _alp[1] = _n*(_n*(_n*(164*_n+225)-480)+360)/720;
79  _bet[1] = _n*(_n*((555-4*_n)*_n-960)+720)/1440;
80  _alp[2] = nx*(_n*(557*_n-864)+390)/1440;
81  _bet[2] = nx*((96-437*_n)*_n+30)/1440;
82  nx *= _n;
83  _alp[3] = (427-1236*_n)*nx/1680;
84  _bet[3] = (119-148*_n)*nx/3360;
85  nx *= _n;
86  _alp[4] = 49561*nx/161280;
87  _bet[4] = 4397*nx/161280;
88  break;
89  case 5:
90  _b1 = 1/(1+_n)*(nx*(nx+16)+64)/64;
91  _alp[1] = _n*(_n*(_n*((328-635*_n)*_n+450)-960)+720)/1440;
92  _bet[1] = _n*(_n*(_n*((-3645*_n-64)*_n+8880)-15360)+11520)/23040;
93  _alp[2] = nx*(_n*(_n*(4496*_n+3899)-6048)+2730)/10080;
94  _bet[2] = nx*(_n*(_n*(4416*_n-3059)+672)+210)/10080;
95  nx *= _n;
96  _alp[3] = nx*(_n*(15061*_n-19776)+6832)/26880;
97  _bet[3] = nx*((-627*_n-592)*_n+476)/13440;
98  nx *= _n;
99  _alp[4] = (49561-171840*_n)*nx/161280;
100  _bet[4] = (4397-3520*_n)*nx/161280;
101  nx *= _n;
102  _alp[5] = 34729*nx/80640;
103  _bet[5] = 4583*nx/161280;
104  break;
105  case 6:
106  _b1 = 1/(1+_n)*(nx*(nx*(nx+4)+64)+256)/256;
107  _alp[1] = _n*(_n*(_n*(_n*(_n*(31564*_n-66675)+34440)+47250)-100800)+
108  75600)/151200;
109  _bet[1] = _n*(_n*(_n*(_n*(_n*(384796*_n-382725)-6720)+932400)-1612800)+
110  1209600)/2419200;
111  _alp[2] = nx*(_n*(_n*((863232-1983433*_n)*_n+748608)-1161216)+524160)/
112  1935360;
113  _bet[2] = nx*(_n*(_n*((1695744-1118711*_n)*_n-1174656)+258048)+80640)/
114  3870720;
115  nx *= _n;
116  _alp[3] = nx*(_n*(_n*(670412*_n+406647)-533952)+184464)/725760;
117  _bet[3] = nx*(_n*(_n*(22276*_n-16929)-15984)+12852)/362880;
118  nx *= _n;
119  _alp[4] = nx*(_n*(6601661*_n-7732800)+2230245)/7257600;
120  _bet[4] = nx*((-830251*_n-158400)*_n+197865)/7257600;
121  nx *= _n;
122  _alp[5] = (3438171-13675556*_n)*nx/7983360;
123  _bet[5] = (453717-435388*_n)*nx/15966720;
124  nx *= _n;
125  _alp[6] = 212378941*nx/319334400;
126  _bet[6] = 20648693*nx/638668800;
127  break;
128  case 7:
129  _b1 = 1/(1+_n)*(nx*(nx*(nx+4)+64)+256)/256;
130  _alp[1] = _n*(_n*(_n*(_n*(_n*(_n*(1804025*_n+2020096)-4267200)+2204160)+
131  3024000)-6451200)+4838400)/9676800;
132  _bet[1] = _n*(_n*(_n*(_n*(_n*((6156736-5406467*_n)*_n-6123600)-107520)+
133  14918400)-25804800)+19353600)/38707200;
134  _alp[2] = nx*(_n*(_n*(_n*(_n*(4626384*_n-9917165)+4316160)+3743040)-
135  5806080)+2620800)/9676800;
136  _bet[2] = nx*(_n*(_n*(_n*(_n*(829456*_n-5593555)+8478720)-5873280)+
137  1290240)+403200)/19353600;
138  nx *= _n;
139  _alp[3] = nx*(_n*(_n*((26816480-67102379*_n)*_n+16265880)-21358080)+
140  7378560)/29030400;
141  _bet[3] = nx*(_n*(_n*(_n*(9261899*_n+3564160)-2708640)-2557440)+
142  2056320)/58060800;
143  nx *= _n;
144  _alp[4] = nx*(_n*(_n*(155912000*_n+72618271)-85060800)+24532695)/
145  79833600;
146  _bet[4] = nx*(_n*(_n*(14928352*_n-9132761)-1742400)+2176515)/79833600;
147  nx *= _n;
148  _alp[5] = nx*(_n*(102508609*_n-109404448)+27505368)/63866880;
149  _bet[5] = nx*((-8005831*_n-1741552)*_n+1814868)/63866880;
150  nx *= _n;
151  _alp[6] = (2760926233.0-12282192400.0*_n)*nx/4151347200.0;
152  _bet[6] = (268433009-261810608*_n)*nx/8302694400.0;
153  nx *= _n;
154  _alp[7] = 1522256789.0*nx/1383782400.0;
155  _bet[7] = 219941297*nx/5535129600.0;
156  break;
157  case 8:
158  _b1 = 1/(1+_n)*(nx*(nx*(nx*(25*nx+64)+256)+4096)+16384)/16384;
159  _alp[1] = _n*(_n*(_n*(_n*(_n*(_n*((37884525-75900428*_n)*_n+42422016)-
160  89611200)+46287360)+63504000)-135475200)+
161  101606400)/203212800;
162  _bet[1] = _n*(_n*(_n*(_n*(_n*(_n*(_n*(31777436*_n-37845269)+43097152)-
163  42865200)-752640)+104428800)-180633600)+
164  135475200)/270950400;
165  _alp[2] = nx*(_n*(_n*(_n*(_n*(_n*(148003883*_n+83274912)-178508970)+
166  77690880)+67374720)-104509440)+47174400)/
167  174182400;
168  _bet[2] = nx*(_n*(_n*(_n*(_n*(_n*(24749483*_n+14930208)-100683990)+
169  152616960)-105719040)+23224320)+7257600)/
170  348364800;
171  nx *= _n;
172  _alp[3] = nx*(_n*(_n*(_n*(_n*(318729724*_n-738126169)+294981280)+
173  178924680)-234938880)+81164160)/319334400;
174  _bet[3] = nx*(_n*(_n*(_n*((101880889-232468668*_n)*_n+39205760)-
175  29795040)-28131840)+22619520)/638668800;
176  nx *= _n;
177  _alp[4] = nx*(_n*(_n*((14967552000.0-40176129013.0*_n)*_n+6971354016.0)-
178  8165836800.0)+2355138720.0)/7664025600.0;
179  _bet[4] = nx*(_n*(_n*(_n*(324154477*_n+1433121792.0)-876745056)-
180  167270400)+208945440)/7664025600.0;
181  nx *= _n;
182  _alp[5] = nx*(_n*(_n*(10421654396.0*_n+3997835751.0)-4266773472.0)+
183  1072709352.0)/2490808320.0;
184  _bet[5] = nx*(_n*(_n*(457888660*_n-312227409)-67920528)+70779852)/
185  2490808320.0;
186  nx *= _n;
187  _alp[6] = nx*(_n*(175214326799.0*_n-171950693600.0)+38652967262.0)/
188  58118860800.0;
189  _bet[6] = nx*((-19841813847.0*_n-3665348512.0)*_n+3758062126.0)/
190  116237721600.0;
191  nx *= _n;
192  _alp[7] = (13700311101.0-67039739596.0*_n)*nx/12454041600.0;
193  _bet[7] = (1979471673.0-1989295244.0*_n)*nx/49816166400.0;
194  nx *= _n;
195  _alp[8] = 1424729850961.0*nx/743921418240.0;
196  _bet[8] = 191773887257.0*nx/3719607091200.0;
197  break;
198  default:
199  STATIC_ASSERT(maxpow_ >= 4 && maxpow_ <= 8, "Bad value of maxpow_");
200  }
201  // _a1 is the equivalent radius for computing the circumference of
202  // ellipse.
203  _a1 = _b1 * _a;
204  }
205 
206  const TransverseMercator
207  TransverseMercator::UTM(Constants::WGS84_a<real>(),
208  Constants::WGS84_f<real>(),
209  Constants::UTM_k0<real>());
210 
211  // Engsager and Poder (2007) use trigonometric series to convert between phi
212  // and phip.
213 
214  // Conversion from phi to phip:
215  //
216  // phip = phi + sum(c[j] * sin(2*j*phi), j, 1, 6)
217  //
218  // c[1] = - 2 * n
219  // + 2/3 * n^2
220  // + 4/3 * n^3
221  // - 82/45 * n^4
222  // + 32/45 * n^5
223  // + 4642/4725 * n^6;
224  // c[2] = 5/3 * n^2
225  // - 16/15 * n^3
226  // - 13/9 * n^4
227  // + 904/315 * n^5
228  // - 1522/945 * n^6;
229  // c[3] = - 26/15 * n^3
230  // + 34/21 * n^4
231  // + 8/5 * n^5
232  // - 12686/2835 * n^6;
233  // c[4] = 1237/630 * n^4
234  // - 12/5 * n^5
235  // - 24832/14175 * n^6;
236  // c[5] = - 734/315 * n^5
237  // + 109598/31185 * n^6;
238  // c[6] = 444337/155925 * n^6;
239 
240  // Conversion from phip to phi:
241  //
242  // phi = phip + sum(d[j] * sin(2*j*phip), j, 1, 6)
243  //
244  // d[1] = 2 * n
245  // - 2/3 * n^2
246  // - 2 * n^3
247  // + 116/45 * n^4
248  // + 26/45 * n^5
249  // - 2854/675 * n^6;
250  // d[2] = 7/3 * n^2
251  // - 8/5 * n^3
252  // - 227/45 * n^4
253  // + 2704/315 * n^5
254  // + 2323/945 * n^6;
255  // d[3] = 56/15 * n^3
256  // - 136/35 * n^4
257  // - 1262/105 * n^5
258  // + 73814/2835 * n^6;
259  // d[4] = 4279/630 * n^4
260  // - 332/35 * n^5
261  // - 399572/14175 * n^6;
262  // d[5] = 4174/315 * n^5
263  // - 144838/6237 * n^6;
264  // d[6] = 601676/22275 * n^6;
265 
266  // In order to maintain sufficient relative accuracy close to the pole use
267  //
268  // S = sum(c[i]*sin(2*i*phi),i,1,6)
269  // taup = (tau + tan(S)) / (1 - tau * tan(S))
270 
271  // taupf and tauf are adapted from TransverseMercatorExact (taup and
272  // taupinv). tau = tan(phi), taup = sinh(psi)
273  Math::real TransverseMercator::taupf(real tau) const throw() {
274  if (!(abs(tau) < overflow_))
275  return tau;
276  real
277  tau1 = Math::hypot(real(1), tau),
278  sig = sinh( eatanhe(tau / tau1) );
279  return Math::hypot(real(1), sig) * tau - sig * tau1;
280  }
281 
282  Math::real TransverseMercator::tauf(real taup) const throw() {
283  if (!(abs(taup) < overflow_))
284  return taup;
285  real
286  // To lowest order in e^2, taup = (1 - e^2) * tau = _e2m * tau; so use
287  // tau = taup/_e2m as a starting guess. Only 1 iteration is needed for
288  // |lat| < 3.35 deg, otherwise 2 iterations are needed. If, instead, tau
289  // = taup is used the mean number of iterations increases to 1.99 (2
290  // iterations are needed except near tau = 0).
291  tau = taup/_e2m,
292  stol = tol_ * max(real(1), abs(taup));
293  // min iterations = 1, max iterations = 2; mean = 1.94
294  for (int i = 0; i < numit_; ++i) {
295  real
296  tau1 = Math::hypot(real(1), tau),
297  sig = sinh( eatanhe( tau / tau1 ) ),
298  taupa = Math::hypot(real(1), sig) * tau - sig * tau1,
299  dtau = (taup - taupa) * (1 + _e2m * Math::sq(tau)) /
300  ( _e2m * tau1 * Math::hypot(real(1), taupa) );
301  tau += dtau;
302  if (!(abs(dtau) >= stol))
303  break;
304  }
305  return tau;
306  }
307 
308  void TransverseMercator::Forward(real lon0, real lat, real lon,
309  real& x, real& y, real& gamma, real& k)
310  const throw() {
312  // Explicitly enforce the parity
313  int
314  latsign = lat < 0 ? -1 : 1,
315  lonsign = lon < 0 ? -1 : 1;
316  lon *= lonsign;
317  lat *= latsign;
318  bool backside = lon > 90;
319  if (backside) {
320  if (lat == 0)
321  latsign = -1;
322  lon = 180 - lon;
323  }
324  real
325  phi = lat * Math::degree<real>(),
326  lam = lon * Math::degree<real>();
327  // phi = latitude
328  // phi' = conformal latitude
329  // psi = isometric latitude
330  // tau = tan(phi)
331  // tau' = tan(phi')
332  // [xi', eta'] = Gauss-Schreiber TM coordinates
333  // [xi, eta] = Gauss-Krueger TM coordinates
334  //
335  // We use
336  // tan(phi') = sinh(psi)
337  // sin(phi') = tanh(psi)
338  // cos(phi') = sech(psi)
339  // denom^2 = 1-cos(phi')^2*sin(lam)^2 = 1-sech(psi)^2*sin(lam)^2
340  // sin(xip) = sin(phi')/denom = tanh(psi)/denom
341  // cos(xip) = cos(phi')*cos(lam)/denom = sech(psi)*cos(lam)/denom
342  // cosh(etap) = 1/denom = 1/denom
343  // sinh(etap) = cos(phi')*sin(lam)/denom = sech(psi)*sin(lam)/denom
344  real etap, xip;
345  if (lat != 90) {
346  real
347  c = max(real(0), cos(lam)), // cos(pi/2) might be negative
348  tau = tan(phi),
349  taup = taupf(tau);
350  xip = atan2(taup, c);
351  // Used to be
352  // etap = Math::atanh(sin(lam) / cosh(psi));
353  etap = Math::asinh(sin(lam) / Math::hypot(taup, c));
354  // convergence and scale for Gauss-Schreiber TM (xip, etap) -- gamma0 =
355  // atan(tan(xip) * tanh(etap)) = atan(tan(lam) * sin(phi'));
356  // sin(phi') = tau'/sqrt(1 + tau'^2)
357  gamma = atan(tanx(lam) *
358  taup / Math::hypot(real(1), taup)); // Krueger p 22 (44)
359  // k0 = sqrt(1 - _e2 * sin(phi)^2) * (cos(phi') / cos(phi)) * cosh(etap)
360  // Note 1/cos(phi) = cosh(psip);
361  // and cos(phi') * cosh(etap) = 1/hypot(sinh(psi), cos(lam))
362  //
363  // This form has cancelling errors. This property is lost if cosh(psip)
364  // is replaced by 1/cos(phi), even though it's using "primary" data (phi
365  // instead of psip).
366  k = sqrt(_e2m + _e2 * Math::sq(cos(phi))) * Math::hypot(real(1), tau)
367  / Math::hypot(taup, c);
368  } else {
369  xip = Math::pi<real>()/2;
370  etap = 0;
371  gamma = lam;
372  k = _c;
373  }
374  // {xi',eta'} is {northing,easting} for Gauss-Schreiber transverse Mercator
375  // (for eta' = 0, xi' = bet). {xi,eta} is {northing,easting} for transverse
376  // Mercator with constant scale on the central meridian (for eta = 0, xip =
377  // rectifying latitude). Define
378  //
379  // zeta = xi + i*eta
380  // zeta' = xi' + i*eta'
381  //
382  // The conversion from conformal to rectifying latitude can be expressed as
383  // a series in _n:
384  //
385  // zeta = zeta' + sum(h[j-1]' * sin(2 * j * zeta'), j = 1..maxpow_)
386  //
387  // where h[j]' = O(_n^j). The reversion of this series gives
388  //
389  // zeta' = zeta - sum(h[j-1] * sin(2 * j * zeta), j = 1..maxpow_)
390  //
391  // which is used in Reverse.
392  //
393  // Evaluate sums via Clenshaw method. See
394  // http://mathworld.wolfram.com/ClenshawRecurrenceFormula.html
395  //
396  // Let
397  //
398  // S = sum(c[k] * F[k](x), k = 0..N)
399  // F[n+1](x) = alpha(n,x) * F[n](x) + beta(n,x) * F[n-1](x)
400  //
401  // Evaluate S with
402  //
403  // y[N+2] = y[N+1] = 0
404  // y[k] = alpha(k,x) * y[k+1] + beta(k+1,x) * y[k+2] + c[k]
405  // S = c[0] * F[0](x) + y[1] * F[1](x) + beta(1,x) * F[0](x) * y[2]
406  //
407  // Here we have
408  //
409  // x = 2 * zeta'
410  // F[n](x) = sin(n * x)
411  // a(n, x) = 2 * cos(x)
412  // b(n, x) = -1
413  // [ sin(A+B) - 2*cos(B)*sin(A) + sin(A-B) = 0, A = n*x, B = x ]
414  // N = maxpow_
415  // c[k] = _alp[k]
416  // S = y[1] * sin(x)
417  //
418  // For the derivative we have
419  //
420  // x = 2 * zeta'
421  // F[n](x) = cos(n * x)
422  // a(n, x) = 2 * cos(x)
423  // b(n, x) = -1
424  // [ cos(A+B) - 2*cos(B)*cos(A) + cos(A-B) = 0, A = n*x, B = x ]
425  // c[0] = 1; c[k] = 2*k*_alp[k]
426  // S = (c[0] - y[2]) + y[1] * cos(x)
427  real
428  c0 = cos(2 * xip), ch0 = cosh(2 * etap),
429  s0 = sin(2 * xip), sh0 = sinh(2 * etap),
430  ar = 2 * c0 * ch0, ai = -2 * s0 * sh0; // 2 * cos(2*zeta')
431  int n = maxpow_;
432  real
433  xi0 = (n & 1 ? _alp[n] : 0), eta0 = 0,
434  xi1 = 0, eta1 = 0;
435  real // Accumulators for dzeta/dzeta'
436  yr0 = (n & 1 ? 2 * maxpow_ * _alp[n--] : 0), yi0 = 0,
437  yr1 = 0, yi1 = 0;
438  while (n) {
439  xi1 = ar * xi0 - ai * eta0 - xi1 + _alp[n];
440  eta1 = ai * xi0 + ar * eta0 - eta1;
441  yr1 = ar * yr0 - ai * yi0 - yr1 + 2 * n * _alp[n];
442  yi1 = ai * yr0 + ar * yi0 - yi1;
443  --n;
444  xi0 = ar * xi1 - ai * eta1 - xi0 + _alp[n];
445  eta0 = ai * xi1 + ar * eta1 - eta0;
446  yr0 = ar * yr1 - ai * yi1 - yr0 + 2 * n * _alp[n];
447  yi0 = ai * yr1 + ar * yi1 - yi0;
448  --n;
449  }
450  ar /= 2; ai /= 2; // cos(2*zeta')
451  yr1 = 1 - yr1 + ar * yr0 - ai * yi0;
452  yi1 = - yi1 + ai * yr0 + ar * yi0;
453  ar = s0 * ch0; ai = c0 * sh0; // sin(2*zeta')
454  real
455  xi = xip + ar * xi0 - ai * eta0,
456  eta = etap + ai * xi0 + ar * eta0;
457  // Fold in change in convergence and scale for Gauss-Schreiber TM to
458  // Gauss-Krueger TM.
459  gamma -= atan2(yi1, yr1);
460  k *= _b1 * Math::hypot(yr1, yi1);
461  gamma /= Math::degree<real>();
462  y = _a1 * _k0 * (backside ? Math::pi<real>() - xi : xi) * latsign;
463  x = _a1 * _k0 * eta * lonsign;
464  if (backside)
465  gamma = 180 - gamma;
466  gamma *= latsign * lonsign;
467  k *= _k0;
468  }
469 
470  void TransverseMercator::Reverse(real lon0, real x, real y,
471  real& lat, real& lon, real& gamma, real& k)
472  const throw() {
473  // This undoes the steps in Forward. The wrinkles are: (1) Use of the
474  // reverted series to express zeta' in terms of zeta. (2) Newton's method
475  // to solve for phi in terms of tan(phi).
476  real
477  xi = y / (_a1 * _k0),
478  eta = x / (_a1 * _k0);
479  // Explicitly enforce the parity
480  int
481  xisign = xi < 0 ? -1 : 1,
482  etasign = eta < 0 ? -1 : 1;
483  xi *= xisign;
484  eta *= etasign;
485  bool backside = xi > Math::pi<real>()/2;
486  if (backside)
487  xi = Math::pi<real>() - xi;
488  real
489  c0 = cos(2 * xi), ch0 = cosh(2 * eta),
490  s0 = sin(2 * xi), sh0 = sinh(2 * eta),
491  ar = 2 * c0 * ch0, ai = -2 * s0 * sh0; // 2 * cos(2*zeta)
492  int n = maxpow_;
493  real // Accumulators for zeta'
494  xip0 = (n & 1 ? -_bet[n] : 0), etap0 = 0,
495  xip1 = 0, etap1 = 0;
496  real // Accumulators for dzeta'/dzeta
497  yr0 = (n & 1 ? - 2 * maxpow_ * _bet[n--] : 0), yi0 = 0,
498  yr1 = 0, yi1 = 0;
499  while (n) {
500  xip1 = ar * xip0 - ai * etap0 - xip1 - _bet[n];
501  etap1 = ai * xip0 + ar * etap0 - etap1;
502  yr1 = ar * yr0 - ai * yi0 - yr1 - 2 * n * _bet[n];
503  yi1 = ai * yr0 + ar * yi0 - yi1;
504  --n;
505  xip0 = ar * xip1 - ai * etap1 - xip0 - _bet[n];
506  etap0 = ai * xip1 + ar * etap1 - etap0;
507  yr0 = ar * yr1 - ai * yi1 - yr0 - 2 * n * _bet[n];
508  yi0 = ai * yr1 + ar * yi1 - yi0;
509  --n;
510  }
511  ar /= 2; ai /= 2; // cos(2*zeta')
512  yr1 = 1 - yr1 + ar * yr0 - ai * yi0;
513  yi1 = - yi1 + ai * yr0 + ar * yi0;
514  ar = s0 * ch0; ai = c0 * sh0; // sin(2*zeta)
515  real
516  xip = xi + ar * xip0 - ai * etap0,
517  etap = eta + ai * xip0 + ar * etap0;
518  // Convergence and scale for Gauss-Schreiber TM to Gauss-Krueger TM.
519  gamma = atan2(yi1, yr1);
520  k = _b1 / Math::hypot(yr1, yi1);
521  // JHS 154 has
522  //
523  // phi' = asin(sin(xi') / cosh(eta')) (Krueger p 17 (25))
524  // lam = asin(tanh(eta') / cos(phi')
525  // psi = asinh(tan(phi'))
526  real lam, phi;
527  real
528  s = sinh(etap),
529  c = max(real(0), cos(xip)), // cos(pi/2) might be negative
530  r = Math::hypot(s, c);
531  if (r != 0) {
532  lam = atan2(s, c); // Krueger p 17 (25)
533  // Use Newton's method to solve for tau
534  real
535  taup = sin(xip)/r,
536  tau = tauf(taup);
537  phi = atan(tau);
538  gamma += atan(tanx(xip) * tanh(etap)); // Krueger p 19 (31)
539  // Note cos(phi') * cosh(eta') = r
540  k *= sqrt(_e2m + _e2 * Math::sq(cos(phi))) *
541  Math::hypot(real(1), tau) * r;
542  } else {
543  phi = Math::pi<real>()/2;
544  lam = 0;
545  k *= _c;
546  }
547  lat = phi / Math::degree<real>() * xisign;
548  lon = lam / Math::degree<real>();
549  if (backside)
550  lon = 180 - lon;
551  lon *= etasign;
552  lon = Math::AngNormalize(lon + Math::AngNormalize(lon0));
553  gamma /= Math::degree<real>();
554  if (backside)
555  gamma = 180 - gamma;
556  gamma *= xisign * etasign;
557  k *= _k0;
558  }
559 
560 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:388
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
static bool isfinite(T x)
Definition: Math.hpp:435
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Transverse Mercator projection.
Header for GeographicLib::TransverseMercator class.
static const TransverseMercator UTM
static T asinh(T x)
Definition: Math.hpp:288
TransverseMercator(real a, real f, real k0)
static T hypot(T x, T y)
Definition: Math.hpp:165
static T sq(T x)
Definition: Math.hpp:153
static T AngDiff(T x, T y)
Definition: Math.hpp:418
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
#define STATIC_ASSERT(cond, reason)
Definition: Constants.hpp:37
Exception handling for GeographicLib.
Definition: Constants.hpp:320