GeographicLib  1.42
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-2015) <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  TransverseMercator::TransverseMercator(real a, real f, real k0)
49  : _a(a)
50  , _f(f <= 1 ? f : 1/f)
51  , _k0(k0)
52  , _e2(_f * (2 - _f))
53  , _es((f < 0 ? -1 : 1) * sqrt(abs(_e2)))
54  , _e2m(1 - _e2)
55  // _c = sqrt( pow(1 + _e, 1 + _e) * pow(1 - _e, 1 - _e) ) )
56  // See, for example, Lee (1976), p 100.
57  , _c( sqrt(_e2m) * exp(Math::eatanhe(real(1), _es)) )
58  , _n(_f / (2 - _f))
59  {
60  if (!(Math::isfinite(_a) && _a > 0))
61  throw GeographicErr("Major radius is not positive");
62  if (!(Math::isfinite(_f) && _f < 1))
63  throw GeographicErr("Minor radius is not positive");
64  if (!(Math::isfinite(_k0) && _k0 > 0))
65  throw GeographicErr("Scale is not positive");
66  // If coefficents might overflow_ an int, convert them to double (and they
67  // are all exactly representable as doubles).
68  real nx = Math::sq(_n);
69  switch (maxpow_) {
70  case 4:
71  _b1 = 1/(1+_n)*(nx*(nx+16)+64)/64;
72  _alp[1] = _n*(_n*(_n*(164*_n+225)-480)+360)/720;
73  _bet[1] = _n*(_n*((555-4*_n)*_n-960)+720)/1440;
74  _alp[2] = nx*(_n*(557*_n-864)+390)/1440;
75  _bet[2] = nx*((96-437*_n)*_n+30)/1440;
76  nx *= _n;
77  _alp[3] = (427-1236*_n)*nx/1680;
78  _bet[3] = (119-148*_n)*nx/3360;
79  nx *= _n;
80  _alp[4] = 49561*nx/161280;
81  _bet[4] = 4397*nx/161280;
82  break;
83  case 5:
84  _b1 = 1/(1+_n)*(nx*(nx+16)+64)/64;
85  _alp[1] = _n*(_n*(_n*((328-635*_n)*_n+450)-960)+720)/1440;
86  _bet[1] = _n*(_n*(_n*((-3645*_n-64)*_n+8880)-15360)+11520)/23040;
87  _alp[2] = nx*(_n*(_n*(4496*_n+3899)-6048)+2730)/10080;
88  _bet[2] = nx*(_n*(_n*(4416*_n-3059)+672)+210)/10080;
89  nx *= _n;
90  _alp[3] = nx*(_n*(15061*_n-19776)+6832)/26880;
91  _bet[3] = nx*((-627*_n-592)*_n+476)/13440;
92  nx *= _n;
93  _alp[4] = (49561-171840*_n)*nx/161280;
94  _bet[4] = (4397-3520*_n)*nx/161280;
95  nx *= _n;
96  _alp[5] = 34729*nx/80640;
97  _bet[5] = 4583*nx/161280;
98  break;
99  case 6:
100  _b1 = 1/(1+_n)*(nx*(nx*(nx+4)+64)+256)/256;
101  _alp[1] = _n*(_n*(_n*(_n*(_n*(31564*_n-66675)+34440)+47250)-100800)+
102  75600)/151200;
103  _bet[1] = _n*(_n*(_n*(_n*(_n*(384796*_n-382725)-6720)+932400)-1612800)+
104  1209600)/2419200;
105  _alp[2] = nx*(_n*(_n*((863232-1983433*_n)*_n+748608)-1161216)+524160)/
106  1935360;
107  _bet[2] = nx*(_n*(_n*((1695744-1118711*_n)*_n-1174656)+258048)+80640)/
108  3870720;
109  nx *= _n;
110  _alp[3] = nx*(_n*(_n*(670412*_n+406647)-533952)+184464)/725760;
111  _bet[3] = nx*(_n*(_n*(22276*_n-16929)-15984)+12852)/362880;
112  nx *= _n;
113  _alp[4] = nx*(_n*(6601661*_n-7732800)+2230245)/7257600;
114  _bet[4] = nx*((-830251*_n-158400)*_n+197865)/7257600;
115  nx *= _n;
116  _alp[5] = (3438171-13675556*_n)*nx/7983360;
117  _bet[5] = (453717-435388*_n)*nx/15966720;
118  nx *= _n;
119  _alp[6] = 212378941*nx/319334400;
120  _bet[6] = 20648693*nx/638668800;
121  break;
122  case 7:
123  _b1 = 1/(1+_n)*(nx*(nx*(nx+4)+64)+256)/256;
124  _alp[1] = _n*(_n*(_n*(_n*(_n*(_n*(1804025*_n+2020096)-4267200)+2204160)+
125  3024000)-6451200)+4838400)/9676800;
126  _bet[1] = _n*(_n*(_n*(_n*(_n*((6156736-5406467*_n)*_n-6123600)-107520)+
127  14918400)-25804800)+19353600)/38707200;
128  _alp[2] = nx*(_n*(_n*(_n*(_n*(4626384*_n-9917165)+4316160)+3743040)-
129  5806080)+2620800)/9676800;
130  _bet[2] = nx*(_n*(_n*(_n*(_n*(829456*_n-5593555)+8478720)-5873280)+
131  1290240)+403200)/19353600;
132  nx *= _n;
133  _alp[3] = nx*(_n*(_n*((26816480-67102379*_n)*_n+16265880)-21358080)+
134  7378560)/29030400;
135  _bet[3] = nx*(_n*(_n*(_n*(9261899*_n+3564160)-2708640)-2557440)+
136  2056320)/58060800;
137  nx *= _n;
138  _alp[4] = nx*(_n*(_n*(155912000*_n+72618271)-85060800)+24532695)/
139  79833600;
140  _bet[4] = nx*(_n*(_n*(14928352*_n-9132761)-1742400)+2176515)/79833600;
141  nx *= _n;
142  _alp[5] = nx*(_n*(102508609*_n-109404448)+27505368)/63866880;
143  _bet[5] = nx*((-8005831*_n-1741552)*_n+1814868)/63866880;
144  nx *= _n;
145  _alp[6] = (2760926233LL-12282192400LL*_n)*nx/4151347200LL;
146  _bet[6] = (268433009-261810608*_n)*nx/8302694400LL;
147  nx *= _n;
148  _alp[7] = 1522256789LL*nx/1383782400LL;
149  _bet[7] = 219941297*nx/5535129600LL;
150  break;
151  case 8:
152  _b1 = 1/(1+_n)*(nx*(nx*(nx*(25*nx+64)+256)+4096)+16384)/16384;
153  _alp[1] = _n*(_n*(_n*(_n*(_n*(_n*((37884525-75900428*_n)*_n+42422016)-
154  89611200)+46287360)+63504000)-135475200)+
155  101606400)/203212800;
156  _bet[1] = _n*(_n*(_n*(_n*(_n*(_n*(_n*(31777436*_n-37845269)+43097152)-
157  42865200)-752640)+104428800)-180633600)+
158  135475200)/270950400;
159  _alp[2] = nx*(_n*(_n*(_n*(_n*(_n*(148003883*_n+83274912)-178508970)+
160  77690880)+67374720)-104509440)+47174400)/
161  174182400;
162  _bet[2] = nx*(_n*(_n*(_n*(_n*(_n*(24749483*_n+14930208)-100683990)+
163  152616960)-105719040)+23224320)+7257600)/
164  348364800;
165  nx *= _n;
166  _alp[3] = nx*(_n*(_n*(_n*(_n*(318729724*_n-738126169)+294981280)+
167  178924680)-234938880)+81164160)/319334400;
168  _bet[3] = nx*(_n*(_n*(_n*((101880889-232468668*_n)*_n+39205760)-
169  29795040)-28131840)+22619520)/638668800;
170  nx *= _n;
171  _alp[4] = nx*(_n*(_n*((14967552000LL-40176129013LL*_n)*_n+6971354016LL)-
172  8165836800LL)+2355138720LL)/7664025600LL;
173  _bet[4] = nx*(_n*(_n*(_n*(324154477*_n+1433121792LL)-876745056)-
174  167270400)+208945440)/7664025600LL;
175  nx *= _n;
176  _alp[5] = nx*(_n*(_n*(10421654396LL*_n+3997835751LL)-4266773472LL)+
177  1072709352LL)/2490808320LL;
178  _bet[5] = nx*(_n*(_n*(457888660*_n-312227409)-67920528)+70779852)/
179  2490808320LL;
180  nx *= _n;
181  _alp[6] = nx*(_n*(175214326799LL*_n-171950693600LL)+38652967262LL)/
182  58118860800LL;
183  _bet[6] = nx*((-19841813847LL*_n-3665348512LL)*_n+3758062126LL)/
184  116237721600LL;
185  nx *= _n;
186  _alp[7] = (13700311101LL-67039739596LL*_n)*nx/12454041600LL;
187  _bet[7] = (1979471673LL-1989295244LL*_n)*nx/49816166400LL;
188  nx *= _n;
189  _alp[8] = 1424729850961LL*nx/743921418240LL;
190  _bet[8] = 191773887257LL*nx/3719607091200LL;
191  break;
192  default:
193  GEOGRAPHICLIB_STATIC_ASSERT(maxpow_ >= 4 && maxpow_ <= 8,
194  "Bad value of maxpow_");
195  }
196  // _a1 is the equivalent radius for computing the circumference of
197  // ellipse.
198  _a1 = _b1 * _a;
199  }
200 
202  static const TransverseMercator utm(Constants::WGS84_a(),
205  return utm;
206  }
207 
208  // Engsager and Poder (2007) use trigonometric series to convert between phi
209  // and phip. Here are the series...
210  //
211  // Conversion from phi to phip:
212  //
213  // phip = phi + sum(c[j] * sin(2*j*phi), j, 1, 6)
214  //
215  // c[1] = - 2 * n
216  // + 2/3 * n^2
217  // + 4/3 * n^3
218  // - 82/45 * n^4
219  // + 32/45 * n^5
220  // + 4642/4725 * n^6;
221  // c[2] = 5/3 * n^2
222  // - 16/15 * n^3
223  // - 13/9 * n^4
224  // + 904/315 * n^5
225  // - 1522/945 * n^6;
226  // c[3] = - 26/15 * n^3
227  // + 34/21 * n^4
228  // + 8/5 * n^5
229  // - 12686/2835 * n^6;
230  // c[4] = 1237/630 * n^4
231  // - 12/5 * n^5
232  // - 24832/14175 * n^6;
233  // c[5] = - 734/315 * n^5
234  // + 109598/31185 * n^6;
235  // c[6] = 444337/155925 * n^6;
236  //
237  // Conversion from phip to phi:
238  //
239  // phi = phip + sum(d[j] * sin(2*j*phip), j, 1, 6)
240  //
241  // d[1] = 2 * n
242  // - 2/3 * n^2
243  // - 2 * n^3
244  // + 116/45 * n^4
245  // + 26/45 * n^5
246  // - 2854/675 * n^6;
247  // d[2] = 7/3 * n^2
248  // - 8/5 * n^3
249  // - 227/45 * n^4
250  // + 2704/315 * n^5
251  // + 2323/945 * n^6;
252  // d[3] = 56/15 * n^3
253  // - 136/35 * n^4
254  // - 1262/105 * n^5
255  // + 73814/2835 * n^6;
256  // d[4] = 4279/630 * n^4
257  // - 332/35 * n^5
258  // - 399572/14175 * n^6;
259  // d[5] = 4174/315 * n^5
260  // - 144838/6237 * n^6;
261  // d[6] = 601676/22275 * n^6;
262  //
263  // In order to maintain sufficient relative accuracy close to the pole use
264  //
265  // S = sum(c[i]*sin(2*i*phi),i,1,6)
266  // taup = (tau + tan(S)) / (1 - tau * tan(S))
267 
268  // In Math::taupf and Math::tauf we evaluate the forward transform explicitly
269  // and solve the reverse one by Newton's method.
270  //
271  // There are adapted from TransverseMercatorExact (taup and taupinv). tau =
272  // tan(phi), taup = sinh(psi)
273 
274  void TransverseMercator::Forward(real lon0, real lat, real lon,
275  real& x, real& y, real& gamma, real& k)
276  const {
278  // Explicitly enforce the parity
279  int
280  latsign = lat < 0 ? -1 : 1,
281  lonsign = lon < 0 ? -1 : 1;
282  lon *= lonsign;
283  lat *= latsign;
284  bool backside = lon > 90;
285  if (backside) {
286  if (lat == 0)
287  latsign = -1;
288  lon = 180 - lon;
289  }
290  real
291  phi = lat * Math::degree(),
292  lam = lon * Math::degree();
293  // phi = latitude
294  // phi' = conformal latitude
295  // psi = isometric latitude
296  // tau = tan(phi)
297  // tau' = tan(phi')
298  // [xi', eta'] = Gauss-Schreiber TM coordinates
299  // [xi, eta] = Gauss-Krueger TM coordinates
300  //
301  // We use
302  // tan(phi') = sinh(psi)
303  // sin(phi') = tanh(psi)
304  // cos(phi') = sech(psi)
305  // denom^2 = 1-cos(phi')^2*sin(lam)^2 = 1-sech(psi)^2*sin(lam)^2
306  // sin(xip) = sin(phi')/denom = tanh(psi)/denom
307  // cos(xip) = cos(phi')*cos(lam)/denom = sech(psi)*cos(lam)/denom
308  // cosh(etap) = 1/denom = 1/denom
309  // sinh(etap) = cos(phi')*sin(lam)/denom = sech(psi)*sin(lam)/denom
310  real etap, xip;
311  if (lat != 90) {
312  real
313  c = max(real(0), cos(lam)), // cos(pi/2) might be negative
314  tau = tan(phi),
315  taup = Math::taupf(tau, _es);
316  xip = atan2(taup, c);
317  // Used to be
318  // etap = Math::atanh(sin(lam) / cosh(psi));
319  etap = Math::asinh(sin(lam) / Math::hypot(taup, c));
320  // convergence and scale for Gauss-Schreiber TM (xip, etap) -- gamma0 =
321  // atan(tan(xip) * tanh(etap)) = atan(tan(lam) * sin(phi'));
322  // sin(phi') = tau'/sqrt(1 + tau'^2)
323  gamma = atan(Math::tand(lon) *
324  taup / Math::hypot(real(1), taup)); // Krueger p 22 (44)
325  // k0 = sqrt(1 - _e2 * sin(phi)^2) * (cos(phi') / cos(phi)) * cosh(etap)
326  // Note 1/cos(phi) = cosh(psip);
327  // and cos(phi') * cosh(etap) = 1/hypot(sinh(psi), cos(lam))
328  //
329  // This form has cancelling errors. This property is lost if cosh(psip)
330  // is replaced by 1/cos(phi), even though it's using "primary" data (phi
331  // instead of psip).
332  k = sqrt(_e2m + _e2 * Math::sq(cos(phi))) * Math::hypot(real(1), tau)
333  / Math::hypot(taup, c);
334  } else {
335  xip = Math::pi()/2;
336  etap = 0;
337  gamma = lam;
338  k = _c;
339  }
340  // {xi',eta'} is {northing,easting} for Gauss-Schreiber transverse Mercator
341  // (for eta' = 0, xi' = bet). {xi,eta} is {northing,easting} for transverse
342  // Mercator with constant scale on the central meridian (for eta = 0, xip =
343  // rectifying latitude). Define
344  //
345  // zeta = xi + i*eta
346  // zeta' = xi' + i*eta'
347  //
348  // The conversion from conformal to rectifying latitude can be expressed as
349  // a series in _n:
350  //
351  // zeta = zeta' + sum(h[j-1]' * sin(2 * j * zeta'), j = 1..maxpow_)
352  //
353  // where h[j]' = O(_n^j). The reversion of this series gives
354  //
355  // zeta' = zeta - sum(h[j-1] * sin(2 * j * zeta), j = 1..maxpow_)
356  //
357  // which is used in Reverse.
358  //
359  // Evaluate sums via Clenshaw method. See
360  // http://mathworld.wolfram.com/ClenshawRecurrenceFormula.html
361  //
362  // Let
363  //
364  // S = sum(c[k] * F[k](x), k = 0..N)
365  // F[n+1](x) = alpha(n,x) * F[n](x) + beta(n,x) * F[n-1](x)
366  //
367  // Evaluate S with
368  //
369  // y[N+2] = y[N+1] = 0
370  // y[k] = alpha(k,x) * y[k+1] + beta(k+1,x) * y[k+2] + c[k]
371  // S = c[0] * F[0](x) + y[1] * F[1](x) + beta(1,x) * F[0](x) * y[2]
372  //
373  // Here we have
374  //
375  // x = 2 * zeta'
376  // F[n](x) = sin(n * x)
377  // a(n, x) = 2 * cos(x)
378  // b(n, x) = -1
379  // [ sin(A+B) - 2*cos(B)*sin(A) + sin(A-B) = 0, A = n*x, B = x ]
380  // N = maxpow_
381  // c[k] = _alp[k]
382  // S = y[1] * sin(x)
383  //
384  // For the derivative we have
385  //
386  // x = 2 * zeta'
387  // F[n](x) = cos(n * x)
388  // a(n, x) = 2 * cos(x)
389  // b(n, x) = -1
390  // [ cos(A+B) - 2*cos(B)*cos(A) + cos(A-B) = 0, A = n*x, B = x ]
391  // c[0] = 1; c[k] = 2*k*_alp[k]
392  // S = (c[0] - y[2]) + y[1] * cos(x)
393  real
394  c0 = cos(2 * xip), ch0 = cosh(2 * etap),
395  s0 = sin(2 * xip), sh0 = sinh(2 * etap),
396  ar = 2 * c0 * ch0, ai = -2 * s0 * sh0; // 2 * cos(2*zeta')
397  int n = maxpow_;
398  real
399  xi0 = (n & 1 ? _alp[n] : 0), eta0 = 0,
400  xi1 = 0, eta1 = 0;
401  real // Accumulators for dzeta/dzeta'
402  yr0 = (n & 1 ? 2 * maxpow_ * _alp[n--] : 0), yi0 = 0,
403  yr1 = 0, yi1 = 0;
404  while (n) {
405  xi1 = ar * xi0 - ai * eta0 - xi1 + _alp[n];
406  eta1 = ai * xi0 + ar * eta0 - eta1;
407  yr1 = ar * yr0 - ai * yi0 - yr1 + 2 * n * _alp[n];
408  yi1 = ai * yr0 + ar * yi0 - yi1;
409  --n;
410  xi0 = ar * xi1 - ai * eta1 - xi0 + _alp[n];
411  eta0 = ai * xi1 + ar * eta1 - eta0;
412  yr0 = ar * yr1 - ai * yi1 - yr0 + 2 * n * _alp[n];
413  yi0 = ai * yr1 + ar * yi1 - yi0;
414  --n;
415  }
416  ar /= 2; ai /= 2; // cos(2*zeta')
417  yr1 = 1 - yr1 + ar * yr0 - ai * yi0;
418  yi1 = - yi1 + ai * yr0 + ar * yi0;
419  ar = s0 * ch0; ai = c0 * sh0; // sin(2*zeta')
420  real
421  xi = xip + ar * xi0 - ai * eta0,
422  eta = etap + ai * xi0 + ar * eta0;
423  // Fold in change in convergence and scale for Gauss-Schreiber TM to
424  // Gauss-Krueger TM.
425  gamma -= atan2(yi1, yr1);
426  k *= _b1 * Math::hypot(yr1, yi1);
427  gamma /= Math::degree();
428  y = _a1 * _k0 * (backside ? Math::pi() - xi : xi) * latsign;
429  x = _a1 * _k0 * eta * lonsign;
430  if (backside)
431  gamma = 180 - gamma;
432  gamma *= latsign * lonsign;
433  k *= _k0;
434  }
435 
436  void TransverseMercator::Reverse(real lon0, real x, real y,
437  real& lat, real& lon, real& gamma, real& k)
438  const {
439  // This undoes the steps in Forward. The wrinkles are: (1) Use of the
440  // reverted series to express zeta' in terms of zeta. (2) Newton's method
441  // to solve for phi in terms of tan(phi).
442  real
443  xi = y / (_a1 * _k0),
444  eta = x / (_a1 * _k0);
445  // Explicitly enforce the parity
446  int
447  xisign = xi < 0 ? -1 : 1,
448  etasign = eta < 0 ? -1 : 1;
449  xi *= xisign;
450  eta *= etasign;
451  bool backside = xi > Math::pi()/2;
452  if (backside)
453  xi = Math::pi() - xi;
454  real
455  c0 = cos(2 * xi), ch0 = cosh(2 * eta),
456  s0 = sin(2 * xi), sh0 = sinh(2 * eta),
457  ar = 2 * c0 * ch0, ai = -2 * s0 * sh0; // 2 * cos(2*zeta)
458  int n = maxpow_;
459  real // Accumulators for zeta'
460  xip0 = (n & 1 ? -_bet[n] : 0), etap0 = 0,
461  xip1 = 0, etap1 = 0;
462  real // Accumulators for dzeta'/dzeta
463  yr0 = (n & 1 ? - 2 * maxpow_ * _bet[n--] : 0), yi0 = 0,
464  yr1 = 0, yi1 = 0;
465  while (n) {
466  xip1 = ar * xip0 - ai * etap0 - xip1 - _bet[n];
467  etap1 = ai * xip0 + ar * etap0 - etap1;
468  yr1 = ar * yr0 - ai * yi0 - yr1 - 2 * n * _bet[n];
469  yi1 = ai * yr0 + ar * yi0 - yi1;
470  --n;
471  xip0 = ar * xip1 - ai * etap1 - xip0 - _bet[n];
472  etap0 = ai * xip1 + ar * etap1 - etap0;
473  yr0 = ar * yr1 - ai * yi1 - yr0 - 2 * n * _bet[n];
474  yi0 = ai * yr1 + ar * yi1 - yi0;
475  --n;
476  }
477  ar /= 2; ai /= 2; // cos(2*zeta')
478  yr1 = 1 - yr1 + ar * yr0 - ai * yi0;
479  yi1 = - yi1 + ai * yr0 + ar * yi0;
480  ar = s0 * ch0; ai = c0 * sh0; // sin(2*zeta)
481  real
482  xip = xi + ar * xip0 - ai * etap0,
483  etap = eta + ai * xip0 + ar * etap0;
484  // Convergence and scale for Gauss-Schreiber TM to Gauss-Krueger TM.
485  gamma = atan2(yi1, yr1);
486  k = _b1 / Math::hypot(yr1, yi1);
487  // JHS 154 has
488  //
489  // phi' = asin(sin(xi') / cosh(eta')) (Krueger p 17 (25))
490  // lam = asin(tanh(eta') / cos(phi')
491  // psi = asinh(tan(phi'))
492  real lam, phi;
493  real
494  s = sinh(etap),
495  c = max(real(0), cos(xip)), // cos(pi/2) might be negative
496  r = Math::hypot(s, c);
497  if (r != 0) {
498  lam = atan2(s, c); // Krueger p 17 (25)
499  // Use Newton's method to solve for tau
500  real
501  sxip = sin(xip),
502  tau = Math::tauf(sxip/r, _es);
503  gamma += atan2(sxip * tanh(etap), c); // Krueger p 19 (31)
504  phi = atan(tau);
505  // Note cos(phi') * cosh(eta') = r
506  k *= sqrt(_e2m + _e2 * Math::sq(cos(phi))) *
507  Math::hypot(real(1), tau) * r;
508  } else {
509  phi = Math::pi()/2;
510  lam = 0;
511  k *= _c;
512  }
513  lat = phi / Math::degree() * xisign;
514  lon = lam / Math::degree();
515  if (backside)
516  lon = 180 - lon;
517  lon *= etasign;
518  lon = Math::AngNormalize(lon + Math::AngNormalize(lon0));
519  gamma /= Math::degree();
520  if (backside)
521  gamma = 180 - gamma;
522  gamma *= xisign * etasign;
523  k *= _k0;
524  }
525 
526 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:428
static T pi()
Definition: Math.hpp:214
static bool isfinite(T x)
Definition: Math.hpp:596
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:102
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Transverse Mercator projection.
static const TransverseMercator & UTM()
Header for GeographicLib::TransverseMercator class.
static T asinh(T x)
Definition: Math.hpp:323
TransverseMercator(real a, real f, real k0)
static T hypot(T x, T y)
Definition: Math.hpp:255
static T sq(T x)
Definition: Math.hpp:244
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T degree()
Definition: Math.hpp:228
static T AngDiff(T x, T y)
Definition: Math.hpp:458
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
static T tand(T x)
Definition: Math.hpp:500
static T tauf(T taup, T es)
Exception handling for GeographicLib.
Definition: Constants.hpp:382
static T taupf(T tau, T es)