12 namespace GeographicLib {
16 const Math::real AlbersEqualArea::eps_ = numeric_limits<real>::epsilon();
19 const Math::real AlbersEqualArea::tol_ = sqrt(eps_);
20 const Math::real AlbersEqualArea::tol0_ = tol_ * sqrt(sqrt(eps_));
22 real(numeric_limits<real>::digits) * log(
real(numeric_limits<real>::radix))
27 , _f(f <= 1 ? f : 1/f)
32 , _qZ(1 + _e2m * atanhee(real(1)))
33 , _qx(_qZ / ( 2 * _e2m ))
41 if (!(abs(stdlat) <= 90))
44 phi = stdlat * Math::degree<real>(),
46 cphi = abs(stdlat) != 90 ? cos(phi) : 0;
47 Init(sphi, cphi, sphi, cphi, k0);
53 , _f(f <= 1 ? f : 1/f)
58 , _qZ(1 + _e2m * atanhee(real(1)))
59 , _qx(_qZ / ( 2 * _e2m ))
67 if (!(abs(stdlat1) <= 90))
68 throw GeographicErr(
"Standard latitude 1 not in [-90d, 90d]");
69 if (!(abs(stdlat2) <= 90))
70 throw GeographicErr(
"Standard latitude 2 not in [-90d, 90d]");
72 phi1 = stdlat1 * Math::degree<real>(),
73 phi2 = stdlat2 * Math::degree<real>();
74 Init(sin(phi1), abs(stdlat1) != 90 ? cos(phi1) : 0,
75 sin(phi2), abs(stdlat2) != 90 ? cos(phi2) : 0, k1);
79 real sinlat1, real coslat1,
80 real sinlat2, real coslat2,
83 , _f(f <= 1 ? f : 1/f)
88 , _qZ(1 + _e2m * atanhee(real(1)))
89 , _qx(_qZ / ( 2 * _e2m ))
98 throw GeographicErr(
"Standard latitude 1 not in [-90d, 90d]");
100 throw GeographicErr(
"Standard latitude 2 not in [-90d, 90d]");
101 if (!(abs(sinlat1) <= 1 && coslat1 <= 1) || (coslat1 == 0 && sinlat1 == 0))
102 throw GeographicErr(
"Bad sine/cosine of standard latitude 1");
103 if (!(abs(sinlat2) <= 1 && coslat2 <= 1) || (coslat2 == 0 && sinlat2 == 0))
104 throw GeographicErr(
"Bad sine/cosine of standard latitude 2");
105 if (coslat1 == 0 && coslat2 == 0 && sinlat1 * sinlat2 <= 0)
107 (
"Standard latitudes cannot be opposite poles");
108 Init(sinlat1, coslat1, sinlat2, coslat2, k1);
111 void AlbersEqualArea::Init(
real sphi1,
real cphi1,
116 sphi1 /= r; cphi1 /= r;
118 sphi2 /= r; cphi2 /= r;
120 bool polar = (cphi1 == 0);
121 cphi1 = max(epsx_, cphi1);
122 cphi2 = max(epsx_, cphi2);
124 _sign = sphi1 + sphi2 >= 0 ? 1 : -1;
126 sphi1 *= _sign; sphi2 *= _sign;
128 swap(sphi1, sphi2); swap(cphi1, cphi2);
131 tphi1 = sphi1/cphi1, tphi2 = sphi2/cphi2;
156 if (polar || tphi1 == tphi2) {
161 tbet1 = _fm * tphi1, scbet12 = 1 +
Math::sq(tbet1),
162 tbet2 = _fm * tphi2, scbet22 = 1 +
Math::sq(tbet2),
163 txi1 = txif(tphi1), cxi1 = 1/hyp(txi1), sxi1 = txi1 * cxi1,
164 txi2 = txif(tphi2), cxi2 = 1/hyp(txi2), sxi2 = txi2 * cxi2,
165 dtbet2 = _fm * (tbet1 + tbet2),
172 dsxi = ( (1 + _e2 * sphi1 * sphi2) / (es2 * es1) +
173 Datanhee(sphi2, sphi1) ) * Dsn(tphi2, tphi1, sphi2, sphi1) /
175 den = (sxi2 + sxi1) * dtbet2 + (scbet22 + scbet12) * dsxi,
177 s = 2 * dtbet2 / den,
182 sm1 = -Dsn(tphi2, tphi1, sphi2, sphi1) *
183 ( -( ((sphi2 <= 0 ? (1 - sxi2) / (1 - sphi2) :
184 Math::sq(cxi2/cphi2) * (1 + sphi2) / (1 + sxi2)) +
185 (sphi1 <= 0 ? (1 - sxi1) / (1 - sphi1) :
186 Math::sq(cxi1/cphi1) * (1 + sphi1) / (1 + sxi1))) ) *
187 (1 + _e2 * (sphi1 + sphi2 + sphi1 * sphi2)) /
188 (1 + (sphi1 + sphi2 + sphi1 * sphi2)) +
189 (scbet22 * (sphi2 <= 0 ? 1 - sphi2 :
Math::sq(cphi2) / ( 1 + sphi2)) +
190 scbet12 * (sphi1 <= 0 ? 1 - sphi1 :
Math::sq(cphi1) / ( 1 + sphi1)))
191 * (_e2 * (1 + sphi1 + sphi2 + _e2 * sphi1 * sphi2)/(es1 * es2)
192 +_e2m * DDatanhee(sphi1, sphi2) ) / _qZ ) / den;
194 C = den / (2 * scbet12 * scbet22 * dsxi);
195 tphi0 = (tphi2 + tphi1)/2;
196 real stol = tol0_ * max(
real(1), abs(tphi0));
197 for (
int i = 0; i < 2*numit0_; ++i) {
230 scphi02 = 1 +
Math::sq(tphi0), scphi0 = sqrt(scphi02),
232 sphi0 = tphi0 / scphi0, sphi0m = 1/(scphi0 * (tphi0 + scphi0)),
234 g = (1 +
Math::sq( _fm * tphi0 )) * sphi0,
236 dg = _e2m * scphi02 * (1 + 2 *
Math::sq(tphi0)) + _e2,
237 D = sphi0m * (1 - _e2*(1 + 2*sphi0*(1+sphi0))) / (_e2m * (1+sphi0)),
239 dD = -2 * (1 - _e2*
Math::sq(sphi0) * (2*sphi0+3)) /
241 A = -_e2 *
Math::sq(sphi0m) * (2+(1+_e2)*sphi0) /
243 B = (sphi0m * _e2m / (1 - _e2*sphi0) *
245 Math::sq(sphi0m / (1-_e2*sphi0))) - _e2*sphi0m/_e2m)),
247 dAB = (2 * _e2 * (2 - _e2 * (1 +
Math::sq(sphi0))) /
249 u = sm1 * g - s/_qZ * ( D - g * (A + B) ),
251 du = sm1 * dg - s/_qZ * (dD - dg * (A + B) - g * dAB),
252 dtu = -u/du * (scphi0 * scphi02);
254 if (!(abs(dtu) >= stol))
258 _txi0 = txif(tphi0); _scxi0 = hyp(_txi0); _sxi0 = _txi0 / _scxi0;
259 _n0 = tphi0/hyp(tphi0);
260 _m02 = 1 / (1 +
Math::sq(_fm * tphi0));
261 _nrho0 = polar ? 0 : _a * sqrt(_m02);
262 _k0 = sqrt(tphi1 == tphi2 ? 1 : C / (_m02 + _n0 * _qZ * _sxi0)) * k1;
264 _lat0 = _sign * atan(tphi0)/Math::degree<real>();
267 const AlbersEqualArea
269 Constants::WGS84_f<real>(),
273 const AlbersEqualArea
275 Constants::WGS84_f<real>(),
279 const AlbersEqualArea
281 Constants::WGS84_f<real>(),
296 int s = tphi < 0 ? -1 : 1;
300 sphi = tphi * sqrt(cphi2),
302 es2m1 = 1 - es1 * sphi,
304 es1m1 = (1 - es1) * sp1,
305 es2m1a = _e2m * es2m1,
306 es1p1 = sp1 / (1 + es1);
307 return s * ( sphi / es2m1 + atanhee(sphi) ) /
308 sqrt( ( cphi2 / (es1p1 * es2m1a) + atanhee(cphi2 / es1m1) ) *
309 ( es1m1 / es2m1a + atanhee(es1p1) ) );
315 stol = tol_ * max(
real(1), abs(txi));
317 for (
int i = 0; i < numit_; ++i) {
323 scterm = scphi2/(1 +
Math::sq(txia)),
324 dtphi = (txi - txia) * scterm * sqrt(scterm) *
325 _qx *
Math::sq(1 - _e2 * tphi2 / scphi2);
327 if (!(abs(dtphi) >= stol))
337 if (abs(x) <
real(0.5)) {
338 real os = -1, y = 1, k = 1;
346 real xs = sqrt(abs(x));
355 if (_e2 * (abs(x) + abs(y)) <
real(0.5)) {
356 real os = -1, z = 1, k = 1, t = 0, c = 0, en = 1;
359 t = y * t + z; c += t; z *= x;
360 t = y * t + z; c += t; z *= x;
369 s = (Datanhee(1, y) - Datanhee(x, y))/(1 - x);
374 real& x, real& y, real& gamma, real& k)
379 lam = lon * Math::degree<real>(),
380 phi = lat * Math::degree<real>(),
381 sphi = sin(phi), cphi = abs(lat) != 90 ? cos(phi) : epsx_,
382 tphi = sphi/cphi, txi = txif(tphi), sxi = txi/hyp(txi),
383 dq = _qZ * Dsn(txi, _txi0, sxi, _sxi0) * (txi - _txi0),
384 drho = - _a * dq / (sqrt(_m02 - _n0 * dq) + _nrho0 / _a),
385 theta = _k2 * _n0 * lam, stheta = sin(theta), ctheta = cos(theta),
386 t = _nrho0 + _n0 * drho;
387 x = t * (_n0 != 0 ? stheta / _n0 : _k2 * lam) / _k0;
390 (ctheta < 0 ? 1 - ctheta :
Math::sq(stheta)/(1 + ctheta)) / _n0 :
392 - drho * ctheta) / _k0;
393 k = _k0 * (t != 0 ? t * hyp(_fm * tphi) / _a : 1);
395 gamma = _sign * theta / Math::degree<real>();
399 real& lat, real& lon,
400 real& gamma, real& k)
404 nx = _k0 * _n0 * x, ny = _k0 * _n0 * y, y1 = _nrho0 - ny,
406 drho = den != 0 ? (_k0*x*nx - 2*_k0*y*_nrho0 + _k0*y*ny) / den : 0,
408 dsxia = - _scxi0 * (2 * _nrho0 + _n0 * drho) * drho /
410 txi = (_txi0 + dsxia) / sqrt(max(1 - dsxia * (2*_txi0 + dsxia), epsx2_)),
412 phi = _sign * atan(tphi),
413 theta = atan2(nx, y1),
414 lam = _n0 != 0 ? theta / (_k2 * _n0) : x / (y1 * _k0);
415 gamma = _sign * theta / Math::degree<real>();
416 lat = phi / Math::degree<real>();
417 lon = lam / Math::degree<real>();
419 k = _k0 * (den != 0 ? (_nrho0 + _n0 * drho) * hyp(_fm * tphi) / _a : 1);
425 if (!(abs(lat) < 90))
426 throw GeographicErr(
"Latitude for SetScale not in (-90d, 90d)");
427 real x, y, gamma, kold;
428 Forward(0, lat, 0, x, y, gamma, kold);
static T AngNormalize(T x)
void Reverse(real lon0, real x, real y, real &lat, real &lon, real &gamma, real &k) const
static const AlbersEqualArea AzimuthalEqualAreaSouth
GeographicLib::Math::real real
AlbersEqualArea(real a, real f, real stdlat, real k0)
static bool isfinite(T x)
static const AlbersEqualArea CylindricalEqualArea
static const AlbersEqualArea AzimuthalEqualAreaNorth
Header for GeographicLib::AlbersEqualArea class.
static T AngDiff(T x, T y)
Exception handling for GeographicLib.
void Forward(real lon0, real lat, real lon, real &x, real &y, real &gamma, real &k) const
void SetScale(real lat, real k=real(1))