GeographicLib  1.38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TransverseMercatorExact.cpp
Go to the documentation of this file.
1 /**
2  * \file TransverseMercatorExact.cpp
3  * \brief Implementation for GeographicLib::TransverseMercatorExact class
4  *
5  * Copyright (c) Charles Karney (2008-2014) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  *
9  * The relevant section of Lee's paper is part V, pp 67--101,
10  * <a href="http://dx.doi.org/10.3138/X687-1574-4325-WM62">Conformal
11  * Projections Based On Jacobian Elliptic Functions</a>.
12  *
13  * The method entails using the Thompson Transverse Mercator as an
14  * intermediate projection. The projections from the intermediate
15  * coordinates to [\e phi, \e lam] and [\e x, \e y] are given by elliptic
16  * functions. The inverse of these projections are found by Newton's method
17  * with a suitable starting guess.
18  *
19  * This implementation and notation closely follows Lee, with the following
20  * exceptions:
21  * <center><table>
22  * <tr><th>Lee <th>here <th>Description
23  * <tr><td>x/a <td>xi <td>Northing (unit Earth)
24  * <tr><td>y/a <td>eta <td>Easting (unit Earth)
25  * <tr><td>s/a <td>sigma <td>xi + i * eta
26  * <tr><td>y <td>x <td>Easting
27  * <tr><td>x <td>y <td>Northing
28  * <tr><td>k <td>e <td>eccentricity
29  * <tr><td>k^2 <td>mu <td>elliptic function parameter
30  * <tr><td>k'^2 <td>mv <td>elliptic function complementary parameter
31  * <tr><td>m <td>k <td>scale
32  * <tr><td>zeta <td>zeta <td>complex longitude = Mercator = chi in paper
33  * <tr><td>s <td>sigma <td>complex GK = zeta in paper
34  * </table></center>
35  *
36  * Minor alterations have been made in some of Lee's expressions in an
37  * attempt to control round-off. For example atanh(sin(phi)) is replaced by
38  * asinh(tan(phi)) which maintains accuracy near phi = pi/2. Such changes
39  * are noted in the code.
40  **********************************************************************/
41 
43 
44 #if defined(_MSC_VER)
45 // Squelch warnings about constant conditional expressions
46 # pragma warning (disable: 4127)
47 #endif
48 
49 namespace GeographicLib {
50 
51  using namespace std;
52 
54  bool extendp)
55  : tol_(numeric_limits<real>::epsilon())
56  , tol1_(real(0.1) * sqrt(tol_))
57  , tol2_(real(0.1) * tol_)
58  , taytol_(pow(tol_, real(0.6)))
59  , _a(a)
60  , _f(f <= 1 ? f : 1/f)
61  , _k0(k0)
62  , _mu(_f * (2 - _f)) // e^2
63  , _mv(1 - _mu) // 1 - e^2
64  , _e(sqrt(_mu))
65  , _extendp(extendp)
66  , _Eu(_mu)
67  , _Ev(_mv)
68  {
69  if (!(Math::isfinite(_a) && _a > 0))
70  throw GeographicErr("Major radius is not positive");
71  if (!(_f > 0))
72  throw GeographicErr("Flattening is not positive");
73  if (!(_f < 1))
74  throw GeographicErr("Minor radius is not positive");
75  if (!(Math::isfinite(_k0) && _k0 > 0))
76  throw GeographicErr("Scale is not positive");
77  }
78 
83  return utm;
84  }
85 
86  // tau = tan(phi), taup = sinh(psi)
87  Math::real TransverseMercatorExact::taup(real tau) const {
88  real
89  tau1 = Math::hypot(real(1), tau),
90  sig = sinh( _e * Math::atanh(_e * tau / tau1) );
91  return Math::hypot(real(1), sig) * tau - sig * tau1;
92  }
93 
94  Math::real TransverseMercatorExact::taupinv(real taup) const {
95  real
96  // See comment in TransverseMercator.cpp about the initial guess
97  tau = taup/_mv,
98  stol = tol_ * max(real(1), abs(taup));
99  // min iterations = 1, max iterations = 2; mean = 1.94
100  for (int i = 0; i < numit_ || GEOGRAPHICLIB_PANIC; ++i) {
101  real
102  tau1 = Math::hypot(real(1), tau),
103  sig = sinh( _e * Math::atanh(_e * tau / tau1 ) ),
104  taupa = Math::hypot(real(1), sig) * tau - sig * tau1,
105  dtau = (taup - taupa) * (1 + _mv * Math::sq(tau)) /
106  ( _mv * tau1 * Math::hypot(real(1), taupa) );
107  tau += dtau;
108  if (!(abs(dtau) >= stol))
109  break;
110  }
111  return tau;
112  }
113 
114  void TransverseMercatorExact::zeta(real /*u*/, real snu, real cnu, real dnu,
115  real /*v*/, real snv, real cnv, real dnv,
116  real& taup, real& lam) const {
117  // Lee 54.17 but write
118  // atanh(snu * dnv) = asinh(snu * dnv / sqrt(cnu^2 + _mv * snu^2 * snv^2))
119  // atanh(_e * snu / dnv) =
120  // asinh(_e * snu / sqrt(_mu * cnu^2 + _mv * cnv^2))
121  real
122  d1 = sqrt(Math::sq(cnu) + _mv * Math::sq(snu * snv)),
123  d2 = sqrt(_mu * Math::sq(cnu) + _mv * Math::sq(cnv)),
124  t1 = (d1 ? snu * dnv / d1 : (snu < 0 ? -overflow() : overflow())),
125  t2 = (d2 ? sinh( _e * Math::asinh(_e * snu / d2) ) :
126  (snu < 0 ? -overflow() : overflow()));
127  // psi = asinh(t1) - asinh(t2)
128  // taup = sinh(psi)
129  taup = t1 * Math::hypot(real(1), t2) - t2 * Math::hypot(real(1), t1);
130  lam = (d1 != 0 && d2 != 0) ?
131  atan2(dnu * snv, cnu * cnv) - _e * atan2(_e * cnu * snv, dnu * cnv) :
132  0;
133  }
134 
135  void TransverseMercatorExact::dwdzeta(real /*u*/,
136  real snu, real cnu, real dnu,
137  real /*v*/,
138  real snv, real cnv, real dnv,
139  real& du, real& dv) const {
140  // Lee 54.21 but write (1 - dnu^2 * snv^2) = (cnv^2 + _mu * snu^2 * snv^2)
141  // (see A+S 16.21.4)
142  real d = _mv * Math::sq(Math::sq(cnv) + _mu * Math::sq(snu * snv));
143  du = cnu * dnu * dnv * (Math::sq(cnv) - _mu * Math::sq(snu * snv)) / d;
144  dv = -snu * snv * cnv * (Math::sq(dnu * dnv) + _mu * Math::sq(cnu)) / d;
145  }
146 
147  // Starting point for zetainv
148  bool TransverseMercatorExact::zetainv0(real psi, real lam, real& u, real& v)
149  const {
150  bool retval = false;
151  if (psi < -_e * Math::pi()/4 &&
152  lam > (1 - 2 * _e) * Math::pi()/2 &&
153  psi < lam - (1 - _e) * Math::pi()/2) {
154  // N.B. this branch is normally not taken because psi < 0 is converted
155  // psi > 0 by Forward.
156  //
157  // There's a log singularity at w = w0 = Eu.K() + i * Ev.K(),
158  // corresponding to the south pole, where we have, approximately
159  //
160  // psi = _e + i * pi/2 - _e * atanh(cos(i * (w - w0)/(1 + _mu/2)))
161  //
162  // Inverting this gives:
163  real
164  psix = 1 - psi / _e,
165  lamx = (Math::pi()/2 - lam) / _e;
166  u = Math::asinh(sin(lamx) / Math::hypot(cos(lamx), sinh(psix))) *
167  (1 + _mu/2);
168  v = atan2(cos(lamx), sinh(psix)) * (1 + _mu/2);
169  u = _Eu.K() - u;
170  v = _Ev.K() - v;
171  } else if (psi < _e * Math::pi()/2 &&
172  lam > (1 - 2 * _e) * Math::pi()/2) {
173  // At w = w0 = i * Ev.K(), we have
174  //
175  // zeta = zeta0 = i * (1 - _e) * pi/2
176  // zeta' = zeta'' = 0
177  //
178  // including the next term in the Taylor series gives:
179  //
180  // zeta = zeta0 - (_mv * _e) / 3 * (w - w0)^3
181  //
182  // When inverting this, we map arg(w - w0) = [-90, 0] to
183  // arg(zeta - zeta0) = [-90, 180]
184  real
185  dlam = lam - (1 - _e) * Math::pi()/2,
186  rad = Math::hypot(psi, dlam),
187  // atan2(dlam-psi, psi+dlam) + 45d gives arg(zeta - zeta0) in range
188  // [-135, 225). Subtracting 180 (since multiplier is negative) makes
189  // range [-315, 45). Multiplying by 1/3 (for cube root) gives range
190  // [-105, 15). In particular the range [-90, 180] in zeta space maps
191  // to [-90, 0] in w space as required.
192  ang = atan2(dlam-psi, psi+dlam) - real(0.75) * Math::pi();
193  // Error using this guess is about 0.21 * (rad/e)^(5/3)
194  retval = rad < _e * taytol_;
195  rad = Math::cbrt(3 / (_mv * _e) * rad);
196  ang /= 3;
197  u = rad * cos(ang);
198  v = rad * sin(ang) + _Ev.K();
199  } else {
200  // Use spherical TM, Lee 12.6 -- writing atanh(sin(lam) / cosh(psi)) =
201  // asinh(sin(lam) / hypot(cos(lam), sinh(psi))). This takes care of the
202  // log singularity at zeta = Eu.K() (corresponding to the north pole)
203  v = Math::asinh(sin(lam) / Math::hypot(cos(lam), sinh(psi)));
204  u = atan2(sinh(psi), cos(lam));
205  // But scale to put 90,0 on the right place
206  u *= _Eu.K() / (Math::pi()/2);
207  v *= _Eu.K() / (Math::pi()/2);
208  }
209  return retval;
210  }
211 
212  // Invert zeta using Newton's method
213  void TransverseMercatorExact::zetainv(real taup, real lam, real& u, real& v)
214  const {
215  real
216  psi = Math::asinh(taup),
217  scal = 1/Math::hypot(real(1), taup);
218  if (zetainv0(psi, lam, u, v))
219  return;
220  real stol2 = tol2_ / Math::sq(max(psi, real(1)));
221  // min iterations = 2, max iterations = 6; mean = 4.0
222  for (int i = 0, trip = 0; i < numit_ || GEOGRAPHICLIB_PANIC; ++i) {
223  real snu, cnu, dnu, snv, cnv, dnv;
224  _Eu.sncndn(u, snu, cnu, dnu);
225  _Ev.sncndn(v, snv, cnv, dnv);
226  real tau1, lam1, du1, dv1;
227  zeta(u, snu, cnu, dnu, v, snv, cnv, dnv, tau1, lam1);
228  dwdzeta(u, snu, cnu, dnu, v, snv, cnv, dnv, du1, dv1);
229  tau1 -= taup;
230  lam1 -= lam;
231  tau1 *= scal;
232  real
233  delu = tau1 * du1 - lam1 * dv1,
234  delv = tau1 * dv1 + lam1 * du1;
235  u -= delu;
236  v -= delv;
237  if (trip)
238  break;
239  real delw2 = Math::sq(delu) + Math::sq(delv);
240  if (!(delw2 >= stol2))
241  ++trip;
242  }
243  }
244 
245  void TransverseMercatorExact::sigma(real /*u*/, real snu, real cnu, real dnu,
246  real v, real snv, real cnv, real dnv,
247  real& xi, real& eta) const {
248  // Lee 55.4 writing
249  // dnu^2 + dnv^2 - 1 = _mu * cnu^2 + _mv * cnv^2
250  real d = _mu * Math::sq(cnu) + _mv * Math::sq(cnv);
251  xi = _Eu.E(snu, cnu, dnu) - _mu * snu * cnu * dnu / d;
252  eta = v - _Ev.E(snv, cnv, dnv) + _mv * snv * cnv * dnv / d;
253  }
254 
255  void TransverseMercatorExact::dwdsigma(real /*u*/,
256  real snu, real cnu, real dnu,
257  real /*v*/,
258  real snv, real cnv, real dnv,
259  real& du, real& dv) const {
260  // Reciprocal of 55.9: dw/ds = dn(w)^2/_mv, expanding complex dn(w) using
261  // A+S 16.21.4
262  real d = _mv * Math::sq(Math::sq(cnv) + _mu * Math::sq(snu * snv));
263  real
264  dnr = dnu * cnv * dnv,
265  dni = - _mu * snu * cnu * snv;
266  du = (Math::sq(dnr) - Math::sq(dni)) / d;
267  dv = 2 * dnr * dni / d;
268  }
269 
270  // Starting point for sigmainv
271  bool TransverseMercatorExact::sigmainv0(real xi, real eta, real& u, real& v)
272  const {
273  bool retval = false;
274  if (eta > real(1.25) * _Ev.KE() ||
275  (xi < -real(0.25) * _Eu.E() && xi < eta - _Ev.KE())) {
276  // sigma as a simple pole at w = w0 = Eu.K() + i * Ev.K() and sigma is
277  // approximated by
278  //
279  // sigma = (Eu.E() + i * Ev.KE()) + 1/(w - w0)
280  real
281  x = xi - _Eu.E(),
282  y = eta - _Ev.KE(),
283  r2 = Math::sq(x) + Math::sq(y);
284  u = _Eu.K() + x/r2;
285  v = _Ev.K() - y/r2;
286  } else if ((eta > real(0.75) * _Ev.KE() && xi < real(0.25) * _Eu.E())
287  || eta > _Ev.KE()) {
288  // At w = w0 = i * Ev.K(), we have
289  //
290  // sigma = sigma0 = i * Ev.KE()
291  // sigma' = sigma'' = 0
292  //
293  // including the next term in the Taylor series gives:
294  //
295  // sigma = sigma0 - _mv / 3 * (w - w0)^3
296  //
297  // When inverting this, we map arg(w - w0) = [-pi/2, -pi/6] to
298  // arg(sigma - sigma0) = [-pi/2, pi/2]
299  // mapping arg = [-pi/2, -pi/6] to [-pi/2, pi/2]
300  real
301  deta = eta - _Ev.KE(),
302  rad = Math::hypot(xi, deta),
303  // Map the range [-90, 180] in sigma space to [-90, 0] in w space. See
304  // discussion in zetainv0 on the cut for ang.
305  ang = atan2(deta-xi, xi+deta) - real(0.75) * Math::pi();
306  // Error using this guess is about 0.068 * rad^(5/3)
307  retval = rad < 2 * taytol_;
308  rad = Math::cbrt(3 / _mv * rad);
309  ang /= 3;
310  u = rad * cos(ang);
311  v = rad * sin(ang) + _Ev.K();
312  } else {
313  // Else use w = sigma * Eu.K/Eu.E (which is correct in the limit _e -> 0)
314  u = xi * _Eu.K()/_Eu.E();
315  v = eta * _Eu.K()/_Eu.E();
316  }
317  return retval;
318  }
319 
320  // Invert sigma using Newton's method
321  void TransverseMercatorExact::sigmainv(real xi, real eta, real& u, real& v)
322  const {
323  if (sigmainv0(xi, eta, u, v))
324  return;
325  // min iterations = 2, max iterations = 7; mean = 3.9
326  for (int i = 0, trip = 0; i < numit_ || GEOGRAPHICLIB_PANIC; ++i) {
327  real snu, cnu, dnu, snv, cnv, dnv;
328  _Eu.sncndn(u, snu, cnu, dnu);
329  _Ev.sncndn(v, snv, cnv, dnv);
330  real xi1, eta1, du1, dv1;
331  sigma(u, snu, cnu, dnu, v, snv, cnv, dnv, xi1, eta1);
332  dwdsigma(u, snu, cnu, dnu, v, snv, cnv, dnv, du1, dv1);
333  xi1 -= xi;
334  eta1 -= eta;
335  real
336  delu = xi1 * du1 - eta1 * dv1,
337  delv = xi1 * dv1 + eta1 * du1;
338  u -= delu;
339  v -= delv;
340  if (trip)
341  break;
342  real delw2 = Math::sq(delu) + Math::sq(delv);
343  if (!(delw2 >= tol2_))
344  ++trip;
345  }
346  }
347 
348  void TransverseMercatorExact::Scale(real tau, real /*lam*/,
349  real snu, real cnu, real dnu,
350  real snv, real cnv, real dnv,
351  real& gamma, real& k) const {
352  real sec2 = 1 + Math::sq(tau); // sec(phi)^2
353  // Lee 55.12 -- negated for our sign convention. gamma gives the bearing
354  // (clockwise from true north) of grid north
355  gamma = atan2(_mv * snu * snv * cnv, cnu * dnu * dnv);
356  // Lee 55.13 with nu given by Lee 9.1 -- in sqrt change the numerator
357  // from
358  //
359  // (1 - snu^2 * dnv^2) to (_mv * snv^2 + cnu^2 * dnv^2)
360  //
361  // to maintain accuracy near phi = 90 and change the denomintor from
362  //
363  // (dnu^2 + dnv^2 - 1) to (_mu * cnu^2 + _mv * cnv^2)
364  //
365  // to maintain accuracy near phi = 0, lam = 90 * (1 - e). Similarly
366  // rewrite sqrt term in 9.1 as
367  //
368  // _mv + _mu * c^2 instead of 1 - _mu * sin(phi)^2
369  k = sqrt(_mv + _mu / sec2) * sqrt(sec2) *
370  sqrt( (_mv * Math::sq(snv) + Math::sq(cnu * dnv)) /
371  (_mu * Math::sq(cnu) + _mv * Math::sq(cnv)) );
372  }
373 
374  void TransverseMercatorExact::Forward(real lon0, real lat, real lon,
375  real& x, real& y, real& gamma, real& k)
376  const {
378  // Explicitly enforce the parity
379  int
380  latsign = (!_extendp && lat < 0) ? -1 : 1,
381  lonsign = (!_extendp && lon < 0) ? -1 : 1;
382  lon *= lonsign;
383  lat *= latsign;
384  bool backside = !_extendp && lon > 90;
385  if (backside) {
386  if (lat == 0)
387  latsign = -1;
388  lon = 180 - lon;
389  }
390  real
391  phi = lat * Math::degree(),
392  lam = lon * Math::degree(),
393  tau = tanx(phi);
394 
395  // u,v = coordinates for the Thompson TM, Lee 54
396  real u, v;
397  if (lat == 90) {
398  u = _Eu.K();
399  v = 0;
400  } else if (lat == 0 && lon == 90 * (1 - _e)) {
401  u = 0;
402  v = _Ev.K();
403  } else
404  zetainv(taup(tau), lam, u, v);
405 
406  real snu, cnu, dnu, snv, cnv, dnv;
407  _Eu.sncndn(u, snu, cnu, dnu);
408  _Ev.sncndn(v, snv, cnv, dnv);
409 
410  real xi, eta;
411  sigma(u, snu, cnu, dnu, v, snv, cnv, dnv, xi, eta);
412  if (backside)
413  xi = 2 * _Eu.E() - xi;
414  y = xi * _a * _k0 * latsign;
415  x = eta * _a * _k0 * lonsign;
416 
417  if (lat == 90) {
418  gamma = lon;
419  k = 1;
420  } else {
421  // Recompute (tau, lam) from (u, v) to improve accuracy of Scale
422  zeta(u, snu, cnu, dnu, v, snv, cnv, dnv, tau, lam);
423  tau=taupinv(tau);
424  Scale(tau, lam, snu, cnu, dnu, snv, cnv, dnv, gamma, k);
425  gamma /= Math::degree();
426  }
427  if (backside)
428  gamma = 180 - gamma;
429  gamma *= latsign * lonsign;
430  k *= _k0;
431  }
432 
433  void TransverseMercatorExact::Reverse(real lon0, real x, real y,
434  real& lat, real& lon,
435  real& gamma, real& k)
436  const {
437  // This undoes the steps in Forward.
438  real
439  xi = y / (_a * _k0),
440  eta = x / (_a * _k0);
441  // Explicitly enforce the parity
442  int
443  latsign = !_extendp && y < 0 ? -1 : 1,
444  lonsign = !_extendp && x < 0 ? -1 : 1;
445  xi *= latsign;
446  eta *= lonsign;
447  bool backside = !_extendp && xi > _Eu.E();
448  if (backside)
449  xi = 2 * _Eu.E()- xi;
450 
451  // u,v = coordinates for the Thompson TM, Lee 54
452  real u, v;
453  if (xi == 0 && eta == _Ev.KE()) {
454  u = 0;
455  v = _Ev.K();
456  } else
457  sigmainv(xi, eta, u, v);
458 
459  real snu, cnu, dnu, snv, cnv, dnv;
460  _Eu.sncndn(u, snu, cnu, dnu);
461  _Ev.sncndn(v, snv, cnv, dnv);
462  real phi, lam, tau;
463  if (v != 0 || u != _Eu.K()) {
464  zeta(u, snu, cnu, dnu, v, snv, cnv, dnv, tau, lam);
465  tau = taupinv(tau);
466  phi = atan(tau);
467  lat = phi / Math::degree();
468  lon = lam / Math::degree();
469  Scale(tau, lam, snu, cnu, dnu, snv, cnv, dnv, gamma, k);
470  gamma /= Math::degree();
471  } else {
472  lat = 90;
473  lon = lam = gamma = 0;
474  k = 1;
475  }
476 
477  if (backside)
478  lon = 180 - lon;
479  lon *= lonsign;
480  lon = Math::AngNormalize(lon + Math::AngNormalize(lon0));
481  lat *= latsign;
482  if (backside)
483  gamma = 180 - gamma;
484  gamma *= latsign * lonsign;
485  k *= _k0;
486  }
487 
488 } // namespace GeographicLib