GeographicLib  1.40
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LambertConformalConic.cpp
Go to the documentation of this file.
1 /**
2  * \file LambertConformalConic.cpp
3  * \brief Implementation for GeographicLib::LambertConformalConic class
4  *
5  * Copyright (c) Charles Karney (2010-2014) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
11 
12 #if defined(_MSC_VER)
13 // Squelch warnings about constant conditional expressions
14 # pragma warning (disable: 4127)
15 #endif
16 
17 namespace GeographicLib {
18 
19  using namespace std;
20 
22  real stdlat, real k0)
23  : eps_(numeric_limits<real>::epsilon())
24  , epsx_(Math::sq(eps_))
25  , tol_(real(0.1) * sqrt(eps_))
26  , ahypover_(Math::digits() * log(real(numeric_limits<real>::radix)) + 2)
27  , _a(a)
28  , _f(f <= 1 ? f : 1/f)
29  , _fm(1 - _f)
30  , _e2(_f * (2 - _f))
31  , _e(sqrt(abs(_e2)))
32  , _e2m(1 - _e2)
33  {
34  if (!(Math::isfinite(_a) && _a > 0))
35  throw GeographicErr("Major radius is not positive");
36  if (!(Math::isfinite(_f) && _f < 1))
37  throw GeographicErr("Minor radius is not positive");
38  if (!(Math::isfinite(k0) && k0 > 0))
39  throw GeographicErr("Scale is not positive");
40  if (!(abs(stdlat) <= 90))
41  throw GeographicErr("Standard latitude not in [-90d, 90d]");
42  real
43  phi = stdlat * Math::degree(),
44  sphi = sin(phi),
45  cphi = abs(stdlat) != 90 ? cos(phi) : 0;
46  Init(sphi, cphi, sphi, cphi, k0);
47  }
48 
50  real stdlat1, real stdlat2,
51  real k1)
52  : eps_(numeric_limits<real>::epsilon())
53  , epsx_(Math::sq(eps_))
54  , tol_(real(0.1) * sqrt(eps_))
55  , ahypover_(Math::digits() * log(real(numeric_limits<real>::radix)) + 2)
56  , _a(a)
57  , _f(f <= 1 ? f : 1/f)
58  , _fm(1 - _f)
59  , _e2(_f * (2 - _f))
60  , _e(sqrt(abs(_e2)))
61  , _e2m(1 - _e2)
62  {
63  if (!(Math::isfinite(_a) && _a > 0))
64  throw GeographicErr("Major radius is not positive");
65  if (!(Math::isfinite(_f) && _f < 1))
66  throw GeographicErr("Minor radius is not positive");
67  if (!(Math::isfinite(k1) && k1 > 0))
68  throw GeographicErr("Scale is not positive");
69  if (!(abs(stdlat1) <= 90))
70  throw GeographicErr("Standard latitude 1 not in [-90d, 90d]");
71  if (!(abs(stdlat2) <= 90))
72  throw GeographicErr("Standard latitude 2 not in [-90d, 90d]");
73  real
74  phi1 = stdlat1 * Math::degree(),
75  phi2 = stdlat2 * Math::degree();
76  Init(sin(phi1), abs(stdlat1) != 90 ? cos(phi1) : 0,
77  sin(phi2), abs(stdlat2) != 90 ? cos(phi2) : 0, k1);
78  }
79 
81  real sinlat1, real coslat1,
82  real sinlat2, real coslat2,
83  real k1)
84  : eps_(numeric_limits<real>::epsilon())
85  , epsx_(Math::sq(eps_))
86  , tol_(real(0.1) * sqrt(eps_))
87  , ahypover_(Math::digits() * log(real(numeric_limits<real>::radix)) + 2)
88  , _a(a)
89  , _f(f <= 1 ? f : 1/f)
90  , _fm(1 - _f)
91  , _e2(_f * (2 - _f))
92  , _e(sqrt(abs(_e2)))
93  , _e2m(1 - _e2)
94  {
95  if (!(Math::isfinite(_a) && _a > 0))
96  throw GeographicErr("Major radius is not positive");
97  if (!(Math::isfinite(_f) && _f < 1))
98  throw GeographicErr("Minor radius is not positive");
99  if (!(Math::isfinite(k1) && k1 > 0))
100  throw GeographicErr("Scale is not positive");
101  if (!(coslat1 >= 0))
102  throw GeographicErr("Standard latitude 1 not in [-90d, 90d]");
103  if (!(coslat2 >= 0))
104  throw GeographicErr("Standard latitude 2 not in [-90d, 90d]");
105  if (!(abs(sinlat1) <= 1 && coslat1 <= 1) || (coslat1 == 0 && sinlat1 == 0))
106  throw GeographicErr("Bad sine/cosine of standard latitude 1");
107  if (!(abs(sinlat2) <= 1 && coslat2 <= 1) || (coslat2 == 0 && sinlat2 == 0))
108  throw GeographicErr("Bad sine/cosine of standard latitude 2");
109  if (coslat1 == 0 || coslat2 == 0)
110  if (!(coslat1 == coslat2 && sinlat1 == sinlat2))
111  throw GeographicErr
112  ("Standard latitudes must be equal is either is a pole");
113  Init(sinlat1, coslat1, sinlat2, coslat2, k1);
114  }
115 
116  void LambertConformalConic::Init(real sphi1, real cphi1,
117  real sphi2, real cphi2, real k1) {
118  {
119  real r;
120  r = Math::hypot(sphi1, cphi1);
121  sphi1 /= r; cphi1 /= r;
122  r = Math::hypot(sphi2, cphi2);
123  sphi2 /= r; cphi2 /= r;
124  }
125  bool polar = (cphi1 == 0);
126  cphi1 = max(epsx_, cphi1); // Avoid singularities at poles
127  cphi2 = max(epsx_, cphi2);
128  // Determine hemisphere of tangent latitude
129  _sign = sphi1 + sphi2 >= 0 ? 1 : -1;
130  // Internally work with tangent latitude positive
131  sphi1 *= _sign; sphi2 *= _sign;
132  if (sphi1 > sphi2) {
133  swap(sphi1, sphi2); swap(cphi1, cphi2); // Make phi1 < phi2
134  }
135  real
136  tphi1 = sphi1/cphi1, tphi2 = sphi2/cphi2, tphi0;
137  //
138  // Snyder: 15-8: n = (log(m1) - log(m2))/(log(t1)-log(t2))
139  //
140  // m = cos(bet) = 1/sec(bet) = 1/sqrt(1+tan(bet)^2)
141  // bet = parametric lat, tan(bet) = (1-f)*tan(phi)
142  //
143  // t = tan(pi/4-chi/2) = 1/(sec(chi) + tan(chi)) = sec(chi) - tan(chi)
144  // log(t) = -asinh(tan(chi)) = -psi
145  // chi = conformal lat
146  // tan(chi) = tan(phi)*cosh(xi) - sinh(xi)*sec(phi)
147  // xi = eatanhe(sin(phi)), eatanhe(x) = e * atanh(e*x)
148  //
149  // n = (log(sec(bet2))-log(sec(bet1)))/(asinh(tan(chi2))-asinh(tan(chi1)))
150  //
151  // Let log(sec(bet)) = b(tphi), asinh(tan(chi)) = c(tphi)
152  // Then n = Db(tphi2, tphi1)/Dc(tphi2, tphi1)
153  // In limit tphi2 -> tphi1, n -> sphi1
154  //
155  real
156  tbet1 = _fm * tphi1, scbet1 = hyp(tbet1),
157  tbet2 = _fm * tphi2, scbet2 = hyp(tbet2);
158  real
159  scphi1 = 1/cphi1,
160  xi1 = eatanhe(sphi1), shxi1 = sinh(xi1), chxi1 = hyp(shxi1),
161  tchi1 = chxi1 * tphi1 - shxi1 * scphi1, scchi1 = hyp(tchi1),
162  scphi2 = 1/cphi2,
163  xi2 = eatanhe(sphi2), shxi2 = sinh(xi2), chxi2 = hyp(shxi2),
164  tchi2 = chxi2 * tphi2 - shxi2 * scphi2, scchi2 = hyp(tchi2),
165  psi1 = Math::asinh(tchi1);
166  if (tphi2 - tphi1 != 0) {
167  // Db(tphi2, tphi1)
168  real num = Dlog1p(Math::sq(tbet2)/(1 + scbet2),
169  Math::sq(tbet1)/(1 + scbet1))
170  * Dhyp(tbet2, tbet1, scbet2, scbet1) * _fm;
171  // Dc(tphi2, tphi1)
172  real den = Dasinh(tphi2, tphi1, scphi2, scphi1)
173  - Deatanhe(sphi2, sphi1) * Dsn(tphi2, tphi1, sphi2, sphi1);
174  _n = num/den;
175 
176  if (_n < 0.25)
177  _nc = sqrt((1 - _n) * (1 + _n));
178  else {
179  // Compute nc = cos(phi0) = sqrt((1 - n) * (1 + n)), evaluating 1 - n
180  // carefully. First write
181  //
182  // Dc(tphi2, tphi1) * (tphi2 - tphi1)
183  // = log(tchi2 + scchi2) - log(tchi1 + scchi1)
184  //
185  // then den * (1 - n) =
186  // (log((tchi2 + scchi2)/(2*scbet2)) - log((tchi1 + scchi1)/(2*scbet1)))
187  // / (tphi2 - tphi1)
188  // = Dlog1p(a2, a1) * (tchi2+scchi2 + tchi1+scchi1)/(4*scbet1*scbet2)
189  // * fm * Q
190  //
191  // where
192  // a1 = ( (tchi1 - scbet1) + (scchi1 - scbet1) ) / (2 * scbet1)
193  // Q = ((scbet2 + scbet1)/fm)/((scchi2 + scchi1)/D(tchi2, tchi1))
194  // - (tbet2 + tbet1)/(scbet2 + scbet1)
195  real t;
196  {
197  real
198  // s1 = (scbet1 - scchi1) * (scbet1 + scchi1)
199  s1 = (tphi1 * (2 * shxi1 * chxi1 * scphi1 - _e2 * tphi1) -
200  Math::sq(shxi1) * (1 + 2 * Math::sq(tphi1))),
201  s2 = (tphi2 * (2 * shxi2 * chxi2 * scphi2 - _e2 * tphi2) -
202  Math::sq(shxi2) * (1 + 2 * Math::sq(tphi2))),
203  // t1 = scbet1 - tchi1
204  t1 = tchi1 < 0 ? scbet1 - tchi1 : (s1 + 1)/(scbet1 + tchi1),
205  t2 = tchi2 < 0 ? scbet2 - tchi2 : (s2 + 1)/(scbet2 + tchi2),
206  a2 = -(s2 / (scbet2 + scchi2) + t2) / (2 * scbet2),
207  a1 = -(s1 / (scbet1 + scchi1) + t1) / (2 * scbet1);
208  t = Dlog1p(a2, a1) / den;
209  }
210  // multiply by (tchi2 + scchi2 + tchi1 + scchi1)/(4*scbet1*scbet2) * fm
211  t *= ( ( (tchi2 >= 0 ? scchi2 + tchi2 : 1/(scchi2 - tchi2)) +
212  (tchi1 >= 0 ? scchi1 + tchi1 : 1/(scchi1 - tchi1)) ) /
213  (4 * scbet1 * scbet2) ) * _fm;
214 
215  // Rewrite
216  // Q = (1 - (tbet2 + tbet1)/(scbet2 + scbet1)) -
217  // (1 - ((scbet2 + scbet1)/fm)/((scchi2 + scchi1)/D(tchi2, tchi1)))
218  // = tbm - tam
219  // where
220  real tbm = ( ((tbet1 > 0 ? 1/(scbet1+tbet1) : scbet1 - tbet1) +
221  (tbet2 > 0 ? 1/(scbet2+tbet2) : scbet2 - tbet2)) /
222  (scbet1+scbet2) );
223 
224  // tam = (1 - ((scbet2+scbet1)/fm)/((scchi2+scchi1)/D(tchi2, tchi1)))
225  //
226  // Let
227  // (scbet2 + scbet1)/fm = scphi2 + scphi1 + dbet
228  // (scchi2 + scchi1)/D(tchi2, tchi1) = scphi2 + scphi1 + dchi
229  // then
230  // tam = D(tchi2, tchi1) * (dchi - dbet) / (scchi1 + scchi2)
231  real
232  // D(tchi2, tchi1)
233  dtchi = den / Dasinh(tchi2, tchi1, scchi2, scchi1),
234  // (scbet2 + scbet1)/fm - (scphi2 + scphi1)
235  dbet = (_e2/_fm) * ( 1 / (scbet2 + _fm * scphi2) +
236  1 / (scbet1 + _fm * scphi1) );
237 
238  // dchi = (scchi2 + scchi1)/D(tchi2, tchi1) - (scphi2 + scphi1)
239  // Let
240  // tzet = chxiZ * tphi - shxiZ * scphi
241  // tchi = tzet + nu
242  // scchi = sczet + mu
243  // where
244  // xiZ = eatanhe(1), shxiZ = sinh(xiZ), chxiZ = cosh(xiZ)
245  // nu = scphi * (shxiZ - shxi) - tphi * (chxiZ - chxi)
246  // mu = - scphi * (chxiZ - chxi) + tphi * (shxiZ - shxi)
247  // then
248  // dchi = ((mu2 + mu1) - D(nu2, nu1) * (scphi2 + scphi1)) /
249  // D(tchi2, tchi1)
250  real
251  xiZ = eatanhe(real(1)), shxiZ = sinh(xiZ), chxiZ = hyp(shxiZ),
252  // These are differences not divided differences
253  // dxiZ1 = xiZ - xi1; dshxiZ1 = shxiZ - shxi; dchxiZ1 = chxiZ - chxi
254  dxiZ1 = Deatanhe(real(1), sphi1)/(scphi1*(tphi1+scphi1)),
255  dxiZ2 = Deatanhe(real(1), sphi2)/(scphi2*(tphi2+scphi2)),
256  dshxiZ1 = Dsinh(xiZ, xi1, shxiZ, shxi1, chxiZ, chxi1) * dxiZ1,
257  dshxiZ2 = Dsinh(xiZ, xi2, shxiZ, shxi2, chxiZ, chxi2) * dxiZ2,
258  dchxiZ1 = Dhyp(shxiZ, shxi1, chxiZ, chxi1) * dshxiZ1,
259  dchxiZ2 = Dhyp(shxiZ, shxi2, chxiZ, chxi2) * dshxiZ2,
260  // mu1 + mu2
261  amu12 = (- scphi1 * dchxiZ1 + tphi1 * dshxiZ1
262  - scphi2 * dchxiZ2 + tphi2 * dshxiZ2),
263  // D(xi2, xi1)
264  dxi = Deatanhe(sphi1, sphi2) * Dsn(tphi2, tphi1, sphi2, sphi1),
265  // D(nu2, nu1)
266  dnu12 =
267  ( (_f * 4 * scphi2 * dshxiZ2 > _f * scphi1 * dshxiZ1 ?
268  // Use divided differences
269  (dshxiZ1 + dshxiZ2)/2 * Dhyp(tphi1, tphi2, scphi1, scphi2)
270  - ( (scphi1 + scphi2)/2
271  * Dsinh(xi1, xi2, shxi1, shxi2, chxi1, chxi2) * dxi ) :
272  // Use ratio of differences
273  (scphi2 * dshxiZ2 - scphi1 * dshxiZ1)/(tphi2 - tphi1))
274  + ( (tphi1 + tphi2)/2 * Dhyp(shxi1, shxi2, chxi1, chxi2)
275  * Dsinh(xi1, xi2, shxi1, shxi2, chxi1, chxi2) * dxi )
276  - (dchxiZ1 + dchxiZ2)/2 ),
277  // dtchi * dchi
278  dchia = (amu12 - dnu12 * (scphi2 + scphi1)),
279  tam = (dchia - dtchi * dbet) / (scchi1 + scchi2);
280  t *= tbm - tam;
281  _nc = sqrt(max(real(0), t) * (1 + _n));
282  }
283  {
284  real r = Math::hypot(_n, _nc);
285  _n /= r;
286  _nc /= r;
287  }
288  tphi0 = _n / _nc;
289  } else {
290  tphi0 = tphi1;
291  _nc = 1/hyp(tphi0);
292  _n = tphi0 * _nc;
293  if (polar)
294  _nc = 0;
295  }
296 
297  _scbet0 = hyp(_fm * tphi0);
298  real shxi0 = sinh(eatanhe(_n));
299  _tchi0 = tphi0 * hyp(shxi0) - shxi0 * hyp(tphi0); _scchi0 = hyp(_tchi0);
300  _psi0 = Math::asinh(_tchi0);
301 
302  _lat0 = atan(_sign * tphi0) / Math::degree();
303  _t0nm1 = Math::expm1(- _n * _psi0); // Snyder's t0^n - 1
304  // a * k1 * m1/t1^n = a * k1 * m2/t2^n = a * k1 * n * (Snyder's F)
305  // = a * k1 / (scbet1 * exp(-n * psi1))
306  _scale = _a * k1 / scbet1 *
307  // exp(n * psi1) = exp(- (1 - n) * psi1) * exp(psi1)
308  // with (1-n) = nc^2/(1+n) and exp(-psi1) = scchi1 + tchi1
309  exp( - (Math::sq(_nc)/(1 + _n)) * psi1 )
310  * (tchi1 >= 0 ? scchi1 + tchi1 : 1 / (scchi1 - tchi1));
311  // Scale at phi0 = k0 = k1 * (scbet0*exp(-n*psi0))/(scbet1*exp(-n*psi1))
312  // = k1 * scbet0/scbet1 * exp(n * (psi1 - psi0))
313  // psi1 - psi0 = Dasinh(tchi1, tchi0) * (tchi1 - tchi0)
314  _k0 = k1 * (_scbet0/scbet1) *
315  exp( - (Math::sq(_nc)/(1 + _n)) *
316  Dasinh(tchi1, _tchi0, scchi1, _scchi0) * (tchi1 - _tchi0))
317  * (tchi1 >= 0 ? scchi1 + tchi1 : 1 / (scchi1 - tchi1)) /
318  (_scchi0 + _tchi0);
319  _nrho0 = polar ? 0 : _a * _k0 / _scbet0;
320  {
321  // Figure _drhomax using code at beginning of Forward with lat = -90
322  real
323  sphi = -1, cphi = epsx_,
324  tphi = sphi/cphi,
325  scphi = 1/cphi, shxi = sinh(eatanhe(sphi)),
326  tchi = hyp(shxi) * tphi - shxi * scphi, scchi = hyp(tchi),
327  psi = Math::asinh(tchi),
328  dpsi = Dasinh(tchi, _tchi0, scchi, _scchi0) * (tchi - _tchi0);
329  _drhomax = - _scale * (2 * _nc < 1 && dpsi != 0 ?
330  (exp(Math::sq(_nc)/(1 + _n) * psi ) *
331  (tchi > 0 ? 1/(scchi + tchi) : (scchi - tchi))
332  - (_t0nm1 + 1))/(-_n) :
333  Dexp(-_n * psi, -_n * _psi0) * dpsi);
334  }
335  }
336 
338  static const LambertConformalConic mercator(Constants::WGS84_a(),
340  real(0), real(1));
341  return mercator;
342  }
343 
344  void LambertConformalConic::Forward(real lon0, real lat, real lon,
345  real& x, real& y, real& gamma, real& k)
346  const {
348  // From Snyder, we have
349  //
350  // theta = n * lambda
351  // x = rho * sin(theta)
352  // = (nrho0 + n * drho) * sin(theta)/n
353  // y = rho0 - rho * cos(theta)
354  // = nrho0 * (1-cos(theta))/n - drho * cos(theta)
355  //
356  // where nrho0 = n * rho0, drho = rho - rho0
357  // and drho is evaluated with divided differences
358  real
359  lam = lon * Math::degree(),
360  phi = _sign * lat * Math::degree(),
361  sphi = sin(phi), cphi = abs(lat) != 90 ? cos(phi) : epsx_,
362  tphi = sphi/cphi, scbet = hyp(_fm * tphi),
363  scphi = 1/cphi, shxi = sinh(eatanhe(sphi)),
364  tchi = hyp(shxi) * tphi - shxi * scphi, scchi = hyp(tchi),
365  psi = Math::asinh(tchi),
366  theta = _n * lam, stheta = sin(theta), ctheta = cos(theta),
367  dpsi = Dasinh(tchi, _tchi0, scchi, _scchi0) * (tchi - _tchi0),
368  drho = - _scale * (2 * _nc < 1 && dpsi != 0 ?
369  (exp(Math::sq(_nc)/(1 + _n) * psi ) *
370  (tchi > 0 ? 1/(scchi + tchi) : (scchi - tchi))
371  - (_t0nm1 + 1))/(-_n) :
372  Dexp(-_n * psi, -_n * _psi0) * dpsi);
373  x = (_nrho0 + _n * drho) * (_n ? stheta / _n : lam);
374  y = _nrho0 *
375  (_n ?
376  (ctheta < 0 ? 1 - ctheta : Math::sq(stheta)/(1 + ctheta)) / _n : 0)
377  - drho * ctheta;
378  k = _k0 * (scbet/_scbet0) /
379  (exp( - (Math::sq(_nc)/(1 + _n)) * dpsi )
380  * (tchi >= 0 ? scchi + tchi : 1 / (scchi - tchi)) / (_scchi0 + _tchi0));
381  y *= _sign;
382  gamma = _sign * theta / Math::degree();
383  }
384 
385  void LambertConformalConic::Reverse(real lon0, real x, real y,
386  real& lat, real& lon,
387  real& gamma, real& k)
388  const {
389  // From Snyder, we have
390  //
391  // x = rho * sin(theta)
392  // rho0 - y = rho * cos(theta)
393  //
394  // rho = hypot(x, rho0 - y)
395  // drho = (n*x^2 - 2*y*nrho0 + n*y^2)/(hypot(n*x, nrho0-n*y) + nrho0)
396  // theta = atan2(n*x, nrho0-n*y)
397  //
398  // From drho, obtain t^n-1
399  // psi = -log(t), so
400  // dpsi = - Dlog1p(t^n-1, t0^n-1) * drho / scale
401  y *= _sign;
402  real
403  // Guard against 0 * inf in computation of ny
404  nx = _n * x, ny = _n ? _n * y : 0, y1 = _nrho0 - ny,
405  den = Math::hypot(nx, y1) + _nrho0, // 0 implies origin with polar aspect
406  // isfinite test is to avoid inf/inf
407  drho = ((den != 0 && Math::isfinite(den))
408  ? (x*nx + y * (ny - 2*_nrho0)) / den
409  : den);
410  drho = min(drho, _drhomax);
411  if (_n == 0)
412  drho = max(drho, -_drhomax);
413  real
414  tnm1 = _t0nm1 + _n * drho/_scale,
415  dpsi = (den == 0 ? 0 :
416  (tnm1 + 1 != 0 ? - Dlog1p(tnm1, _t0nm1) * drho / _scale :
417  ahypover_));
418  real tchi;
419  if (2 * _n <= 1) {
420  // tchi = sinh(psi)
421  real
422  psi = _psi0 + dpsi, tchia = sinh(psi), scchi = hyp(tchia),
423  dtchi = Dsinh(psi, _psi0, tchia, _tchi0, scchi, _scchi0) * dpsi;
424  tchi = _tchi0 + dtchi; // Update tchi using divided difference
425  } else {
426  // tchi = sinh(-1/n * log(tn))
427  // = sinh((1-1/n) * log(tn) - log(tn))
428  // = + sinh((1-1/n) * log(tn)) * cosh(log(tn))
429  // - cosh((1-1/n) * log(tn)) * sinh(log(tn))
430  // (1-1/n) = - nc^2/(n*(1+n))
431  // cosh(log(tn)) = (tn + 1/tn)/2; sinh(log(tn)) = (tn - 1/tn)/2
432  real
433  tn = tnm1 + 1 == 0 ? epsx_ : tnm1 + 1,
434  sh = sinh( -Math::sq(_nc)/(_n * (1 + _n)) *
435  (2 * tn > 1 ? Math::log1p(tnm1) : log(tn)) );
436  tchi = sh * (tn + 1/tn)/2 - hyp(sh) * (tnm1 * (tn + 1)/tn)/2;
437  }
438 
439  // Use Newton's method to solve for tphi
440  real
441  // See comment in TransverseMercator.cpp about the initial guess
442  tphi = tchi/_e2m,
443  stol = tol_ * max(real(1), abs(tchi));
444  // min iterations = 1, max iterations = 2; mean = 1.94
445  for (int i = 0; i < numit_ || GEOGRAPHICLIB_PANIC; ++i) {
446  real
447  scphi = hyp(tphi),
448  shxi = sinh( eatanhe( tphi / scphi ) ),
449  tchia = hyp(shxi) * tphi - shxi * scphi,
450  dtphi = (tchi - tchia) * (1 + _e2m * Math::sq(tphi)) /
451  ( _e2m * scphi * hyp(tchia) );
452  tphi += dtphi;
453  if (!(abs(dtphi) >= stol))
454  break;
455  }
456  // log(t) = -asinh(tan(chi)) = -psi
457  gamma = atan2(nx, y1);
458  real
459  phi = _sign * atan(tphi),
460  scbet = hyp(_fm * tphi), scchi = hyp(tchi),
461  lam = _n ? gamma / _n : x / y1;
462  lat = phi / Math::degree();
463  lon = lam / Math::degree();
464  lon = Math::AngNormalize(lon + Math::AngNormalize(lon0));
465  k = _k0 * (scbet/_scbet0) /
466  (exp(_nc ? - (Math::sq(_nc)/(1 + _n)) * dpsi : 0)
467  * (tchi >= 0 ? scchi + tchi : 1 / (scchi - tchi)) / (_scchi0 + _tchi0));
468  gamma /= _sign * Math::degree();
469  }
470 
471  void LambertConformalConic::SetScale(real lat, real k) {
472  if (!(Math::isfinite(k) && k > 0))
473  throw GeographicErr("Scale is not positive");
474  if (!(abs(lat) <= 90))
475  throw GeographicErr("Latitude for SetScale not in [-90d, 90d]");
476  if (abs(lat) == 90 && !(_nc == 0 && lat * _n > 0))
477  throw GeographicErr("Incompatible polar latitude in SetScale");
478  real x, y, gamma, kold;
479  Forward(0, lat, 0, x, y, gamma, kold);
480  k /= kold;
481  _scale *= k;
482  _k0 *= k;
483  }
484 
485 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:400
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
LambertConformalConic(real a, real f, real stdlat, real k0)
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
static bool isfinite(T x)
Definition: Math.hpp:446
Lambert conformal conic projection.
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:102
static T expm1(T x)
Definition: Math.hpp:277
void SetScale(real lat, real k=real(1))
static T asinh(T x)
Definition: Math.hpp:323
static T hypot(T x, T y)
Definition: Math.hpp:255
Header for GeographicLib::LambertConformalConic class.
static T sq(T x)
Definition: Math.hpp:244
static const LambertConformalConic & Mercator()
static T degree()
Definition: Math.hpp:228
static T AngDiff(T x, T y)
Definition: Math.hpp:430
static T log1p(T x)
Definition: Math.hpp:300
Exception handling for GeographicLib.
Definition: Constants.hpp:361
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
#define GEOGRAPHICLIB_PANIC
Definition: Math.hpp:87