GeographicLib 2.1.2
AlbersEqualArea.cpp
Go to the documentation of this file.
1/**
2 * \file AlbersEqualArea.cpp
3 * \brief Implementation for GeographicLib::AlbersEqualArea class
4 *
5 * Copyright (c) Charles Karney (2010-2022) <charles@karney.com> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 **********************************************************************/
9
11
12#if defined(_MSC_VER)
13// Squelch warnings about constant conditional and enum-float expressions
14# pragma warning (disable: 4127 5055)
15#endif
16
17namespace GeographicLib {
18
19 using namespace std;
20
21 AlbersEqualArea::AlbersEqualArea(real a, real f, real stdlat, real k0)
22 : eps_(numeric_limits<real>::epsilon())
23 , epsx_(Math::sq(eps_))
24 , epsx2_(Math::sq(epsx_))
25 , tol_(sqrt(eps_))
26 , tol0_(tol_ * sqrt(sqrt(eps_)))
27 , _a(a)
28 , _f(f)
29 , _fm(1 - _f)
30 , _e2(_f * (2 - _f))
31 , _e(sqrt(fabs(_e2)))
32 , _e2m(1 - _e2)
33 , _qZ(1 + _e2m * atanhee(real(1)))
34 , _qx(_qZ / ( 2 * _e2m ))
35 {
36 if (!(isfinite(_a) && _a > 0))
37 throw GeographicErr("Equatorial radius is not positive");
38 if (!(isfinite(_f) && _f < 1))
39 throw GeographicErr("Polar semi-axis is not positive");
40 if (!(isfinite(k0) && k0 > 0))
41 throw GeographicErr("Scale is not positive");
42 if (!(fabs(stdlat) <= Math::qd))
43 throw GeographicErr("Standard latitude not in [-" + to_string(Math::qd)
44 + "d, " + to_string(Math::qd) + "d]");
45 real sphi, cphi;
46 Math::sincosd(stdlat, sphi, cphi);
47 Init(sphi, cphi, sphi, cphi, k0);
48 }
49
50 AlbersEqualArea::AlbersEqualArea(real a, real f, real stdlat1, real stdlat2,
51 real k1)
52 : eps_(numeric_limits<real>::epsilon())
53 , epsx_(Math::sq(eps_))
54 , epsx2_(Math::sq(epsx_))
55 , tol_(sqrt(eps_))
56 , tol0_(tol_ * sqrt(sqrt(eps_)))
57 , _a(a)
58 , _f(f)
59 , _fm(1 - _f)
60 , _e2(_f * (2 - _f))
61 , _e(sqrt(fabs(_e2)))
62 , _e2m(1 - _e2)
63 , _qZ(1 + _e2m * atanhee(real(1)))
64 , _qx(_qZ / ( 2 * _e2m ))
65 {
66 if (!(isfinite(_a) && _a > 0))
67 throw GeographicErr("Equatorial radius is not positive");
68 if (!(isfinite(_f) && _f < 1))
69 throw GeographicErr("Polar semi-axis is not positive");
70 if (!(isfinite(k1) && k1 > 0))
71 throw GeographicErr("Scale is not positive");
72 if (!(fabs(stdlat1) <= Math::qd))
73 throw GeographicErr("Standard latitude 1 not in [-"
74 + to_string(Math::qd) + "d, "
75 + to_string(Math::qd) + "d]");
76 if (!(fabs(stdlat2) <= Math::qd))
77 throw GeographicErr("Standard latitude 2 not in [-"
78 + to_string(Math::qd) + "d, "
79 + to_string(Math::qd) + "d]");
80 real sphi1, cphi1, sphi2, cphi2;
81 Math::sincosd(stdlat1, sphi1, cphi1);
82 Math::sincosd(stdlat2, sphi2, cphi2);
83 Init(sphi1, cphi1, sphi2, cphi2, k1);
84 }
85
87 real sinlat1, real coslat1,
88 real sinlat2, real coslat2,
89 real k1)
90 : eps_(numeric_limits<real>::epsilon())
91 , epsx_(Math::sq(eps_))
92 , epsx2_(Math::sq(epsx_))
93 , tol_(sqrt(eps_))
94 , tol0_(tol_ * sqrt(sqrt(eps_)))
95 , _a(a)
96 , _f(f)
97 , _fm(1 - _f)
98 , _e2(_f * (2 - _f))
99 , _e(sqrt(fabs(_e2)))
100 , _e2m(1 - _e2)
101 , _qZ(1 + _e2m * atanhee(real(1)))
102 , _qx(_qZ / ( 2 * _e2m ))
103 {
104 if (!(isfinite(_a) && _a > 0))
105 throw GeographicErr("Equatorial radius is not positive");
106 if (!(isfinite(_f) && _f < 1))
107 throw GeographicErr("Polar semi-axis is not positive");
108 if (!(isfinite(k1) && k1 > 0))
109 throw GeographicErr("Scale is not positive");
110 if (signbit(coslat1))
111 throw GeographicErr("Standard latitude 1 not in [-"
112 + to_string(Math::qd) + "d, "
113 + to_string(Math::qd) + "d]");
114 if (signbit(coslat2))
115 throw GeographicErr("Standard latitude 2 not in [-"
116 + to_string(Math::qd) + "d, "
117 + to_string(Math::qd) + "d]");
118 if (!(fabs(sinlat1) <= 1 && coslat1 <= 1) || (coslat1 == 0 && sinlat1 == 0))
119 throw GeographicErr("Bad sine/cosine of standard latitude 1");
120 if (!(fabs(sinlat2) <= 1 && coslat2 <= 1) || (coslat2 == 0 && sinlat2 == 0))
121 throw GeographicErr("Bad sine/cosine of standard latitude 2");
122 if (coslat1 == 0 && coslat2 == 0 && sinlat1 * sinlat2 <= 0)
123 throw GeographicErr
124 ("Standard latitudes cannot be opposite poles");
125 Init(sinlat1, coslat1, sinlat2, coslat2, k1);
126 }
127
128 void AlbersEqualArea::Init(real sphi1, real cphi1,
129 real sphi2, real cphi2, real k1) {
130 {
131 real r;
132 r = hypot(sphi1, cphi1);
133 sphi1 /= r; cphi1 /= r;
134 r = hypot(sphi2, cphi2);
135 sphi2 /= r; cphi2 /= r;
136 }
137 bool polar = (cphi1 == 0);
138 cphi1 = fmax(epsx_, cphi1); // Avoid singularities at poles
139 cphi2 = fmax(epsx_, cphi2);
140 // Determine hemisphere of tangent latitude
141 _sign = sphi1 + sphi2 >= 0 ? 1 : -1;
142 // Internally work with tangent latitude positive
143 sphi1 *= _sign; sphi2 *= _sign;
144 if (sphi1 > sphi2) {
145 swap(sphi1, sphi2); swap(cphi1, cphi2); // Make phi1 < phi2
146 }
147 real
148 tphi1 = sphi1/cphi1, tphi2 = sphi2/cphi2;
149
150 // q = (1-e^2)*(sphi/(1-e^2*sphi^2) - atanhee(sphi))
151 // qZ = q(pi/2) = (1 + (1-e^2)*atanhee(1))
152 // atanhee(x) = atanh(e*x)/e
153 // q = sxi * qZ
154 // dq/dphi = 2*(1-e^2)*cphi/(1-e^2*sphi^2)^2
155 //
156 // n = (m1^2-m2^2)/(q2-q1) -> sin(phi0) for phi1, phi2 -> phi0
157 // C = m1^2 + n*q1 = (m1^2*q2-m2^2*q1)/(q2-q1)
158 // let
159 // rho(pi/2)/rho(-pi/2) = (1-s)/(1+s)
160 // s = n*qZ/C
161 // = qZ * (m1^2-m2^2)/(m1^2*q2-m2^2*q1)
162 // = qZ * (scbet2^2 - scbet1^2)/(scbet2^2*q2 - scbet1^2*q1)
163 // = (scbet2^2 - scbet1^2)/(scbet2^2*sxi2 - scbet1^2*sxi1)
164 // = (tbet2^2 - tbet1^2)/(scbet2^2*sxi2 - scbet1^2*sxi1)
165 // 1-s = -((1-sxi2)*scbet2^2 - (1-sxi1)*scbet1^2)/
166 // (scbet2^2*sxi2 - scbet1^2*sxi1)
167 //
168 // Define phi0 to give same value of s, i.e.,
169 // s = sphi0 * qZ / (m0^2 + sphi0*q0)
170 // = sphi0 * scbet0^2 / (1/qZ + sphi0 * scbet0^2 * sxi0)
171
172 real tphi0, C;
173 if (polar || tphi1 == tphi2) {
174 tphi0 = tphi2;
175 C = 1; // ignored
176 } else {
177 real
178 tbet1 = _fm * tphi1, scbet12 = 1 + Math::sq(tbet1),
179 tbet2 = _fm * tphi2, scbet22 = 1 + Math::sq(tbet2),
180 txi1 = txif(tphi1), cxi1 = 1/hyp(txi1), sxi1 = txi1 * cxi1,
181 txi2 = txif(tphi2), cxi2 = 1/hyp(txi2), sxi2 = txi2 * cxi2,
182 dtbet2 = _fm * (tbet1 + tbet2),
183 es1 = 1 - _e2 * Math::sq(sphi1), es2 = 1 - _e2 * Math::sq(sphi2),
184 /*
185 dsxi = ( (_e2 * sq(sphi2 + sphi1) + es2 + es1) / (2 * es2 * es1) +
186 Datanhee(sphi2, sphi1) ) * Dsn(tphi2, tphi1, sphi2, sphi1) /
187 ( 2 * _qx ),
188 */
189 dsxi = ( (1 + _e2 * sphi1 * sphi2) / (es2 * es1) +
190 Datanhee(sphi2, sphi1) ) * Dsn(tphi2, tphi1, sphi2, sphi1) /
191 ( 2 * _qx ),
192 den = (sxi2 + sxi1) * dtbet2 + (scbet22 + scbet12) * dsxi,
193 // s = (sq(tbet2) - sq(tbet1)) / (scbet22*sxi2 - scbet12*sxi1)
194 s = 2 * dtbet2 / den,
195 // 1-s = -(sq(scbet2)*(1-sxi2) - sq(scbet1)*(1-sxi1)) /
196 // (scbet22*sxi2 - scbet12*sxi1)
197 // Write
198 // sq(scbet)*(1-sxi) = sq(scbet)*(1-sphi) * (1-sxi)/(1-sphi)
199 sm1 = -Dsn(tphi2, tphi1, sphi2, sphi1) *
200 ( -( ((sphi2 <= 0 ? (1 - sxi2) / (1 - sphi2) :
201 Math::sq(cxi2/cphi2) * (1 + sphi2) / (1 + sxi2)) +
202 (sphi1 <= 0 ? (1 - sxi1) / (1 - sphi1) :
203 Math::sq(cxi1/cphi1) * (1 + sphi1) / (1 + sxi1))) ) *
204 (1 + _e2 * (sphi1 + sphi2 + sphi1 * sphi2)) /
205 (1 + (sphi1 + sphi2 + sphi1 * sphi2)) +
206 (scbet22 * (sphi2 <= 0 ? 1 - sphi2 :
207 Math::sq(cphi2) / ( 1 + sphi2)) +
208 scbet12 * (sphi1 <= 0 ? 1 - sphi1 : Math::sq(cphi1) / ( 1 + sphi1)))
209 * (_e2 * (1 + sphi1 + sphi2 + _e2 * sphi1 * sphi2)/(es1 * es2)
210 +_e2m * DDatanhee(sphi1, sphi2) ) / _qZ ) / den;
211 // C = (scbet22*sxi2 - scbet12*sxi1) / (scbet22 * scbet12 * (sx2 - sx1))
212 C = den / (2 * scbet12 * scbet22 * dsxi);
213 tphi0 = (tphi2 + tphi1)/2;
214 real stol = tol0_ * fmax(real(1), fabs(tphi0));
215 for (int i = 0; i < 2*numit0_ || GEOGRAPHICLIB_PANIC; ++i) {
216 // Solve (scbet0^2 * sphi0) / (1/qZ + scbet0^2 * sphi0 * sxi0) = s
217 // for tphi0 by Newton's method on
218 // v(tphi0) = (scbet0^2 * sphi0) - s * (1/qZ + scbet0^2 * sphi0 * sxi0)
219 // = 0
220 // Alt:
221 // (scbet0^2 * sphi0) / (1/qZ - scbet0^2 * sphi0 * (1-sxi0))
222 // = s / (1-s)
223 // w(tphi0) = (1-s) * (scbet0^2 * sphi0)
224 // - s * (1/qZ - scbet0^2 * sphi0 * (1-sxi0))
225 // = (1-s) * (scbet0^2 * sphi0)
226 // - S/qZ * (1 - scbet0^2 * sphi0 * (qZ-q0))
227 // Now
228 // qZ-q0 = (1+e2*sphi0)*(1-sphi0)/(1-e2*sphi0^2) +
229 // (1-e2)*atanhee((1-sphi0)/(1-e2*sphi0))
230 // In limit sphi0 -> 1, qZ-q0 -> 2*(1-sphi0)/(1-e2), so wrte
231 // qZ-q0 = 2*(1-sphi0)/(1-e2) + A + B
232 // A = (1-sphi0)*( (1+e2*sphi0)/(1-e2*sphi0^2) - (1+e2)/(1-e2) )
233 // = -e2 *(1-sphi0)^2 * (2+(1+e2)*sphi0) / ((1-e2)*(1-e2*sphi0^2))
234 // B = (1-e2)*atanhee((1-sphi0)/(1-e2*sphi0)) - (1-sphi0)
235 // = (1-sphi0)*(1-e2)/(1-e2*sphi0)*
236 // ((atanhee(x)/x-1) - e2*(1-sphi0)/(1-e2))
237 // x = (1-sphi0)/(1-e2*sphi0), atanhee(x)/x = atanh(e*x)/(e*x)
238 //
239 // 1 - scbet0^2 * sphi0 * (qZ-q0)
240 // = 1 - scbet0^2 * sphi0 * (2*(1-sphi0)/(1-e2) + A + B)
241 // = D - scbet0^2 * sphi0 * (A + B)
242 // D = 1 - scbet0^2 * sphi0 * 2*(1-sphi0)/(1-e2)
243 // = (1-sphi0)*(1-e2*(1+2*sphi0*(1+sphi0)))/((1-e2)*(1+sphi0))
244 // dD/dsphi0 = -2*(1-e2*sphi0^2*(2*sphi0+3))/((1-e2)*(1+sphi0)^2)
245 // d(A+B)/dsphi0 = 2*(1-sphi0^2)*e2*(2-e2*(1+sphi0^2))/
246 // ((1-e2)*(1-e2*sphi0^2)^2)
247
248 real
249 scphi02 = 1 + Math::sq(tphi0), scphi0 = sqrt(scphi02),
250 // sphi0m = 1-sin(phi0) = 1/( sec(phi0) * (tan(phi0) + sec(phi0)) )
251 sphi0 = tphi0 / scphi0, sphi0m = 1/(scphi0 * (tphi0 + scphi0)),
252 // scbet0^2 * sphi0
253 g = (1 + Math::sq( _fm * tphi0 )) * sphi0,
254 // dg/dsphi0 = dg/dtphi0 * scphi0^3
255 dg = _e2m * scphi02 * (1 + 2 * Math::sq(tphi0)) + _e2,
256 D = sphi0m * (1 - _e2*(1 + 2*sphi0*(1+sphi0))) / (_e2m * (1+sphi0)),
257 // dD/dsphi0
258 dD = -2 * (1 - _e2*Math::sq(sphi0) * (2*sphi0+3)) /
259 (_e2m * Math::sq(1+sphi0)),
260 A = -_e2 * Math::sq(sphi0m) * (2+(1+_e2)*sphi0) /
261 (_e2m*(1-_e2*Math::sq(sphi0))),
262 B = (sphi0m * _e2m / (1 - _e2*sphi0) *
263 (atanhxm1(_e2 *
264 Math::sq(sphi0m / (1-_e2*sphi0))) - _e2*sphi0m/_e2m)),
265 // d(A+B)/dsphi0
266 dAB = (2 * _e2 * (2 - _e2 * (1 + Math::sq(sphi0))) /
267 (_e2m * Math::sq(1 - _e2*Math::sq(sphi0)) * scphi02)),
268 u = sm1 * g - s/_qZ * ( D - g * (A + B) ),
269 // du/dsphi0
270 du = sm1 * dg - s/_qZ * (dD - dg * (A + B) - g * dAB),
271 dtu = -u/du * (scphi0 * scphi02);
272 tphi0 += dtu;
273 if (!(fabs(dtu) >= stol))
274 break;
275 }
276 }
277 _txi0 = txif(tphi0); _scxi0 = hyp(_txi0); _sxi0 = _txi0 / _scxi0;
278 _n0 = tphi0/hyp(tphi0);
279 _m02 = 1 / (1 + Math::sq(_fm * tphi0));
280 _nrho0 = polar ? 0 : _a * sqrt(_m02);
281 _k0 = sqrt(tphi1 == tphi2 ? 1 : C / (_m02 + _n0 * _qZ * _sxi0)) * k1;
282 _k2 = Math::sq(_k0);
283 _lat0 = _sign * atan(tphi0)/Math::degree();
284 }
285
287 static const AlbersEqualArea
288 cylindricalequalarea(Constants::WGS84_a(), Constants::WGS84_f(),
289 real(0), real(1), real(0), real(1), real(1));
290 return cylindricalequalarea;
291 }
292
294 static const AlbersEqualArea
295 azimuthalequalareanorth(Constants::WGS84_a(), Constants::WGS84_f(),
296 real(1), real(0), real(1), real(0), real(1));
297 return azimuthalequalareanorth;
298 }
299
301 static const AlbersEqualArea
302 azimuthalequalareasouth(Constants::WGS84_a(), Constants::WGS84_f(),
303 real(-1), real(0), real(-1), real(0), real(1));
304 return azimuthalequalareasouth;
305 }
306
307 Math::real AlbersEqualArea::txif(real tphi) const {
308 // sxi = ( sphi/(1-e2*sphi^2) + atanhee(sphi) ) /
309 // ( 1/(1-e2) + atanhee(1) )
310 //
311 // txi = ( sphi/(1-e2*sphi^2) + atanhee(sphi) ) /
312 // sqrt( ( (1+e2*sphi)*(1-sphi)/( (1-e2*sphi^2) * (1-e2) ) +
313 // atanhee((1-sphi)/(1-e2*sphi)) ) *
314 // ( (1-e2*sphi)*(1+sphi)/( (1-e2*sphi^2) * (1-e2) ) +
315 // atanhee((1+sphi)/(1+e2*sphi)) ) )
316 // = ( tphi/(1-e2*sphi^2) + atanhee(sphi, e2)/cphi ) /
317 // sqrt(
318 // ( (1+e2*sphi)/( (1-e2*sphi^2) * (1-e2) ) + Datanhee(1, sphi) ) *
319 // ( (1-e2*sphi)/( (1-e2*sphi^2) * (1-e2) ) + Datanhee(1, -sphi) ) )
320 //
321 // This function maintains odd parity
322 real
323 cphi = 1 / sqrt(1 + Math::sq(tphi)),
324 sphi = tphi * cphi,
325 es1 = _e2 * sphi,
326 es2m1 = 1 - es1 * sphi, // 1 - e2 * sphi^2
327 es2m1a = _e2m * es2m1; // (1 - e2 * sphi^2) * (1 - e2)
328 return ( tphi / es2m1 + atanhee(sphi) / cphi ) /
329 sqrt( ( (1 + es1) / es2m1a + Datanhee(1, sphi) ) *
330 ( (1 - es1) / es2m1a + Datanhee(1, -sphi) ) );
331 }
332
333 Math::real AlbersEqualArea::tphif(real txi) const {
334 real
335 tphi = txi,
336 stol = tol_ * fmax(real(1), fabs(txi));
337 // CHECK: min iterations = 1, max iterations = 2; mean = 1.99
338 for (int i = 0; i < numit_ || GEOGRAPHICLIB_PANIC; ++i) {
339 // dtxi/dtphi = (scxi/scphi)^3 * 2*(1-e^2)/(qZ*(1-e^2*sphi^2)^2)
340 real
341 txia = txif(tphi),
342 tphi2 = Math::sq(tphi),
343 scphi2 = 1 + tphi2,
344 scterm = scphi2/(1 + Math::sq(txia)),
345 dtphi = (txi - txia) * scterm * sqrt(scterm) *
346 _qx * Math::sq(1 - _e2 * tphi2 / scphi2);
347 tphi += dtphi;
348 if (!(fabs(dtphi) >= stol))
349 break;
350 }
351 return tphi;
352 }
353
354 // return atanh(sqrt(x))/sqrt(x) - 1 = x/3 + x^2/5 + x^3/7 + ...
355 // typical x < e^2 = 2*f
356 Math::real AlbersEqualArea::atanhxm1(real x) {
357 real s = 0;
358 if (fabs(x) < real(0.5)) {
359 static const real lg2eps_ = -log2(numeric_limits<real>::epsilon() / 2);
360 int e;
361 frexp(x, &e);
362 e = -e;
363 // x = [0.5,1) * 2^(-e)
364 // estimate n s.t. x^n/(2*n+1) < x/3 * epsilon/2
365 // a stronger condition is x^(n-1) < epsilon/2
366 // taking log2 of both sides, a stronger condition is
367 // (n-1)*(-e) < -lg2eps or (n-1)*e > lg2eps or n > ceiling(lg2eps/e)+1
368 int n = x == 0 ? 1 : int(ceil(lg2eps_ / e)) + 1;
369 while (n--) // iterating from n-1 down to 0
370 s = x * s + (n ? 1 : 0)/Math::real(2*n + 1);
371 } else {
372 real xs = sqrt(fabs(x));
373 s = (x > 0 ? atanh(xs) : atan(xs)) / xs - 1;
374 }
375 return s;
376 }
377
378 // return (Datanhee(1,y) - Datanhee(1,x))/(y-x)
379 Math::real AlbersEqualArea::DDatanhee(real x, real y) const {
380 // This function is called with x = sphi1, y = sphi2, phi1 <= phi2, sphi2
381 // >= 0, abs(sphi1) <= phi2. However for safety's sake we enforce x <= y.
382 if (y < x) swap(x, y); // ensure that x <= y
383 real q1 = fabs(_e2),
384 q2 = fabs(2 * _e / _e2m * (1 - x));
385 return
386 x <= 0 || !(fmin(q1, q2) < real(0.75)) ? DDatanhee0(x, y) :
387 (q1 < q2 ? DDatanhee1(x, y) : DDatanhee2(x, y));
388 }
389
390 // Rearrange difference so that 1 - x is in the denominator, then do a
391 // straight divided difference.
392 Math::real AlbersEqualArea::DDatanhee0(real x, real y) const {
393 return (Datanhee(1, y) - Datanhee(x, y))/(1 - x);
394 }
395
396 // The expansion for e2 small
397 Math::real AlbersEqualArea::DDatanhee1(real x, real y) const {
398 // The series in e2 is
399 // sum( c[l] * e2^l, l, 1, N)
400 // where
401 // c[l] = sum( x^i * y^j; i >= 0, j >= 0, i+j < 2*l) / (2*l + 1)
402 // = ( (x-y) - (1-y) * x^(2*l+1) + (1-x) * y^(2*l+1) ) /
403 // ( (2*l+1) * (x-y) * (1-y) * (1-x) )
404 // For x = y = 1, c[l] = l
405 //
406 // In the limit x,y -> 1,
407 //
408 // DDatanhee -> e2/(1-e2)^2 = sum(l * e2^l, l, 1, inf)
409 //
410 // Use if e2 is sufficiently small.
411 real s = 0;
412 real z = 1, k = 1, t = 0, c = 0, en = 1;
413 while (true) {
414 t = y * t + z; c += t; z *= x;
415 t = y * t + z; c += t; z *= x;
416 k += 2; en *= _e2;
417 // Here en[l] = e2^l, k[l] = 2*l + 1,
418 // c[l] = sum( x^i * y^j; i >= 0, j >= 0, i+j < 2*l) / (2*l + 1)
419 // Taylor expansion is
420 // s = sum( c[l] * e2^l, l, 1, N)
421 real ds = en * c / k;
422 s += ds;
423 if (!(fabs(ds) > fabs(s) * eps_/2))
424 break; // Iterate until the added term is sufficiently small
425 }
426 return s;
427 }
428
429 // The expansion for x (and y) close to 1
430 Math::real AlbersEqualArea::DDatanhee2(real x, real y) const {
431 // If x and y are both close to 1, expand in Taylor series in dx = 1-x and
432 // dy = 1-y:
433 //
434 // DDatanhee = sum(C_m * (dx^(m+1) - dy^(m+1)) / (dx - dy), m, 0, inf)
435 //
436 // where
437 //
438 // C_m = sum( (m+2)!! / (m+2-2*k)!! *
439 // ((m+1)/2)! / ((m+1)/2-k)! /
440 // (k! * (2*k-1)!!) *
441 // e2^((m+1)/2+k),
442 // k, 0, (m+1)/2) * (-1)^m / ((m+2) * (1-e2)^(m+2))
443 // for m odd, and
444 //
445 // C_m = sum( 2 * (m+1)!! / (m+1-2*k)!! *
446 // (m/2+1)! / (m/2-k)! /
447 // (k! * (2*k+1)!!) *
448 // e2^(m/2+1+k),
449 // k, 0, m/2)) * (-1)^m / ((m+2) * (1-e2)^(m+2))
450 // for m even.
451 //
452 // Here i!! is the double factorial extended to negative i with
453 // i!! = (i+2)!!/(i+2).
454 //
455 // Note that
456 // (dx^(m+1) - dy^(m+1)) / (dx - dy) =
457 // dx^m + dx^(m-1)*dy ... + dx*dy^(m-1) + dy^m
458 //
459 // Leading (m = 0) term is e2 / (1 - e2)^2
460 //
461 // Magnitude of mth term relative to the leading term scales as
462 //
463 // 2*(2*e/(1-e2)*dx)^m
464 //
465 // So use series if (2*e/(1-e2)*dx) is sufficiently small
466 real s, dx = 1 - x, dy = 1 - y, xy = 1, yy = 1, ee = _e2 / Math::sq(_e2m);
467 s = ee;
468 for (int m = 1; ; ++m) {
469 real c = m + 2, t = c;
470 yy *= dy; // yy = dy^m
471 xy = dx * xy + yy;
472 // Now xy = dx^m + dx^(m-1)*dy ... + dx*dy^(m-1) + dy^m
473 // = (dx^(m+1) - dy^(m+1)) / (dx - dy)
474 // max value = (m+1) * max(dx,dy)^m
475 ee /= -_e2m;
476 if (m % 2 == 0) ee *= _e2;
477 // Now ee = (-1)^m * e2^(floor(m/2)+1) / (1-e2)^(m+2)
478 int kmax = (m+1)/2;
479 for (int k = kmax - 1; k >= 0; --k) {
480 // max coeff is less than 2^(m+1)
481 c *= (k + 1) * (2 * (k + m - 2*kmax) + 3);
482 c /= (kmax - k) * (2 * (kmax - k) + 1);
483 // Horner sum for inner _e2 series
484 t = _e2 * t + c;
485 }
486 // Straight sum for outer m series
487 real ds = t * ee * xy / (m + 2);
488 s = s + ds;
489 if (!(fabs(ds) > fabs(s) * eps_/2))
490 break; // Iterate until the added term is sufficiently small
491 }
492 return s;
493 }
494
495 void AlbersEqualArea::Forward(real lon0, real lat, real lon,
496 real& x, real& y, real& gamma, real& k) const {
497 lon = Math::AngDiff(lon0, lon);
498 lat *= _sign;
499 real sphi, cphi;
500 Math::sincosd(Math::LatFix(lat) * _sign, sphi, cphi);
501 cphi = fmax(epsx_, cphi);
502 real
503 lam = lon * Math::degree(),
504 tphi = sphi/cphi, txi = txif(tphi), sxi = txi/hyp(txi),
505 dq = _qZ * Dsn(txi, _txi0, sxi, _sxi0) * (txi - _txi0),
506 drho = - _a * dq / (sqrt(_m02 - _n0 * dq) + _nrho0 / _a),
507 theta = _k2 * _n0 * lam, stheta = sin(theta), ctheta = cos(theta),
508 t = _nrho0 + _n0 * drho;
509 x = t * (_n0 != 0 ? stheta / _n0 : _k2 * lam) / _k0;
510 y = (_nrho0 *
511 (_n0 != 0 ?
512 (ctheta < 0 ? 1 - ctheta : Math::sq(stheta)/(1 + ctheta)) / _n0 :
513 0)
514 - drho * ctheta) / _k0;
515 k = _k0 * (t != 0 ? t * hyp(_fm * tphi) / _a : 1);
516 y *= _sign;
517 gamma = _sign * theta / Math::degree();
518 }
519
520 void AlbersEqualArea::Reverse(real lon0, real x, real y,
521 real& lat, real& lon,
522 real& gamma, real& k) const {
523 y *= _sign;
524 real
525 nx = _k0 * _n0 * x, ny = _k0 * _n0 * y, y1 = _nrho0 - ny,
526 den = hypot(nx, y1) + _nrho0, // 0 implies origin with polar aspect
527 drho = den != 0 ? (_k0*x*nx - 2*_k0*y*_nrho0 + _k0*y*ny) / den : 0,
528 // dsxia = scxi0 * dsxi
529 dsxia = - _scxi0 * (2 * _nrho0 + _n0 * drho) * drho /
530 (Math::sq(_a) * _qZ),
531 txi = (_txi0 + dsxia) / sqrt(fmax(1 - dsxia * (2*_txi0 + dsxia), epsx2_)),
532 tphi = tphif(txi),
533 theta = atan2(nx, y1),
534 lam = _n0 != 0 ? theta / (_k2 * _n0) : x / (y1 * _k0);
535 gamma = _sign * theta / Math::degree();
536 lat = Math::atand(_sign * tphi);
537 lon = lam / Math::degree();
538 lon = Math::AngNormalize(lon + Math::AngNormalize(lon0));
539 k = _k0 * (den != 0 ? (_nrho0 + _n0 * drho) * hyp(_fm * tphi) / _a : 1);
540 }
541
542 void AlbersEqualArea::SetScale(real lat, real k) {
543 if (!(isfinite(k) && k > 0))
544 throw GeographicErr("Scale is not positive");
545 if (!(fabs(lat) < Math::qd))
546 throw GeographicErr("Latitude for SetScale not in (-"
547 + to_string(Math::qd) + "d, "
548 + to_string(Math::qd) + "d)");
549 real x, y, gamma, kold;
550 Forward(0, lat, 0, x, y, gamma, kold);
551 k /= kold;
552 _k0 *= k;
553 _k2 = Math::sq(_k0);
554 }
555
556} // namespace GeographicLib
Header for GeographicLib::AlbersEqualArea class.
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
#define GEOGRAPHICLIB_PANIC
Definition: Math.hpp:61
Albers equal area conic projection.
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
AlbersEqualArea(real a, real f, real stdlat, real k0)
void SetScale(real lat, real k=real(1))
static const AlbersEqualArea & CylindricalEqualArea()
static const AlbersEqualArea & AzimuthalEqualAreaNorth()
static const AlbersEqualArea & AzimuthalEqualAreaSouth()
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
Exception handling for GeographicLib.
Definition: Constants.hpp:316
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:76
static T degree()
Definition: Math.hpp:200
static T LatFix(T x)
Definition: Math.hpp:300
static void sincosd(T x, T &sinx, T &cosx)
Definition: Math.cpp:106
static T sq(T x)
Definition: Math.hpp:212
static T AngNormalize(T x)
Definition: Math.cpp:71
static T atand(T x)
Definition: Math.cpp:202
static T AngDiff(T x, T y, T &e)
Definition: Math.cpp:82
@ qd
degrees per quarter turn
Definition: Math.hpp:141
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)