14# pragma warning (disable: 4127)
22 : eps_(numeric_limits<real>::epsilon())
23 , epsx_(
Math::sq(eps_))
24 , epsx2_(
Math::sq(epsx_))
26 , tol0_(tol_ * sqrt(sqrt(eps_)))
33 , _qZ(1 + _e2m * atanhee(real(1)))
34 , _qx(_qZ / ( 2 * _e2m ))
36 if (!(isfinite(_a) && _a > 0))
38 if (!(isfinite(_f) && _f < 1))
40 if (!(isfinite(k0) && k0 > 0))
42 if (!(abs(stdlat) <= 90))
46 Init(sphi, cphi, sphi, cphi, k0);
51 : eps_(numeric_limits<real>::epsilon())
52 , epsx_(
Math::sq(eps_))
53 , epsx2_(
Math::sq(epsx_))
55 , tol0_(tol_ * sqrt(sqrt(eps_)))
62 , _qZ(1 + _e2m * atanhee(real(1)))
63 , _qx(_qZ / ( 2 * _e2m ))
65 if (!(isfinite(_a) && _a > 0))
67 if (!(isfinite(_f) && _f < 1))
69 if (!(isfinite(k1) && k1 > 0))
71 if (!(abs(stdlat1) <= 90))
72 throw GeographicErr(
"Standard latitude 1 not in [-90d, 90d]");
73 if (!(abs(stdlat2) <= 90))
74 throw GeographicErr(
"Standard latitude 2 not in [-90d, 90d]");
75 real sphi1, cphi1, sphi2, cphi2;
78 Init(sphi1, cphi1, sphi2, cphi2, k1);
82 real sinlat1, real coslat1,
83 real sinlat2, real coslat2,
85 : eps_(numeric_limits<real>::epsilon())
86 , epsx_(
Math::sq(eps_))
87 , epsx2_(
Math::sq(epsx_))
89 , tol0_(tol_ * sqrt(sqrt(eps_)))
96 , _qZ(1 + _e2m * atanhee(real(1)))
97 , _qx(_qZ / ( 2 * _e2m ))
99 if (!(isfinite(_a) && _a > 0))
101 if (!(isfinite(_f) && _f < 1))
103 if (!(isfinite(k1) && k1 > 0))
106 throw GeographicErr(
"Standard latitude 1 not in [-90d, 90d]");
108 throw GeographicErr(
"Standard latitude 2 not in [-90d, 90d]");
109 if (!(abs(sinlat1) <= 1 && coslat1 <= 1) || (coslat1 == 0 && sinlat1 == 0))
110 throw GeographicErr(
"Bad sine/cosine of standard latitude 1");
111 if (!(abs(sinlat2) <= 1 && coslat2 <= 1) || (coslat2 == 0 && sinlat2 == 0))
112 throw GeographicErr(
"Bad sine/cosine of standard latitude 2");
113 if (coslat1 == 0 && coslat2 == 0 && sinlat1 * sinlat2 <= 0)
115 (
"Standard latitudes cannot be opposite poles");
116 Init(sinlat1, coslat1, sinlat2, coslat2, k1);
119 void AlbersEqualArea::Init(
real sphi1,
real cphi1,
123 r = hypot(sphi1, cphi1);
124 sphi1 /= r; cphi1 /= r;
125 r = hypot(sphi2, cphi2);
126 sphi2 /= r; cphi2 /= r;
128 bool polar = (cphi1 == 0);
129 cphi1 = max(epsx_, cphi1);
130 cphi2 = max(epsx_, cphi2);
132 _sign = sphi1 + sphi2 >= 0 ? 1 : -1;
134 sphi1 *= _sign; sphi2 *= _sign;
136 swap(sphi1, sphi2);
swap(cphi1, cphi2);
139 tphi1 = sphi1/cphi1, tphi2 = sphi2/cphi2;
164 if (polar || tphi1 == tphi2) {
169 tbet1 = _fm * tphi1, scbet12 = 1 +
Math::sq(tbet1),
170 tbet2 = _fm * tphi2, scbet22 = 1 +
Math::sq(tbet2),
171 txi1 = txif(tphi1), cxi1 = 1/hyp(txi1), sxi1 = txi1 * cxi1,
172 txi2 = txif(tphi2), cxi2 = 1/hyp(txi2), sxi2 = txi2 * cxi2,
173 dtbet2 = _fm * (tbet1 + tbet2),
180 dsxi = ( (1 + _e2 * sphi1 * sphi2) / (es2 * es1) +
181 Datanhee(sphi2, sphi1) ) * Dsn(tphi2, tphi1, sphi2, sphi1) /
183 den = (sxi2 + sxi1) * dtbet2 + (scbet22 + scbet12) * dsxi,
185 s = 2 * dtbet2 / den,
190 sm1 = -Dsn(tphi2, tphi1, sphi2, sphi1) *
191 ( -( ((sphi2 <= 0 ? (1 - sxi2) / (1 - sphi2) :
192 Math::sq(cxi2/cphi2) * (1 + sphi2) / (1 + sxi2)) +
193 (sphi1 <= 0 ? (1 - sxi1) / (1 - sphi1) :
194 Math::sq(cxi1/cphi1) * (1 + sphi1) / (1 + sxi1))) ) *
195 (1 + _e2 * (sphi1 + sphi2 + sphi1 * sphi2)) /
196 (1 + (sphi1 + sphi2 + sphi1 * sphi2)) +
197 (scbet22 * (sphi2 <= 0 ? 1 - sphi2 :
199 scbet12 * (sphi1 <= 0 ? 1 - sphi1 :
Math::sq(cphi1) / ( 1 + sphi1)))
200 * (_e2 * (1 + sphi1 + sphi2 + _e2 * sphi1 * sphi2)/(es1 * es2)
201 +_e2m * DDatanhee(sphi1, sphi2) ) / _qZ ) / den;
203 C = den / (2 * scbet12 * scbet22 * dsxi);
204 tphi0 = (tphi2 + tphi1)/2;
205 real stol = tol0_ * max(
real(1), abs(tphi0));
240 scphi02 = 1 +
Math::sq(tphi0), scphi0 = sqrt(scphi02),
242 sphi0 = tphi0 / scphi0, sphi0m = 1/(scphi0 * (tphi0 + scphi0)),
244 g = (1 +
Math::sq( _fm * tphi0 )) * sphi0,
246 dg = _e2m * scphi02 * (1 + 2 *
Math::sq(tphi0)) + _e2,
247 D = sphi0m * (1 - _e2*(1 + 2*sphi0*(1+sphi0))) / (_e2m * (1+sphi0)),
249 dD = -2 * (1 - _e2*
Math::sq(sphi0) * (2*sphi0+3)) /
251 A = -_e2 *
Math::sq(sphi0m) * (2+(1+_e2)*sphi0) /
253 B = (sphi0m * _e2m / (1 - _e2*sphi0) *
255 Math::sq(sphi0m / (1-_e2*sphi0))) - _e2*sphi0m/_e2m)),
257 dAB = (2 * _e2 * (2 - _e2 * (1 +
Math::sq(sphi0))) /
259 u = sm1 * g - s/_qZ * ( D - g * (A + B) ),
261 du = sm1 * dg - s/_qZ * (dD - dg * (A + B) - g * dAB),
262 dtu = -u/du * (scphi0 * scphi02);
264 if (!(abs(dtu) >= stol))
268 _txi0 = txif(tphi0); _scxi0 = hyp(_txi0); _sxi0 = _txi0 / _scxi0;
269 _n0 = tphi0/hyp(tphi0);
270 _m02 = 1 / (1 +
Math::sq(_fm * tphi0));
271 _nrho0 = polar ? 0 : _a * sqrt(_m02);
272 _k0 = sqrt(tphi1 == tphi2 ? 1 : C / (_m02 + _n0 * _qZ * _sxi0)) * k1;
280 real(0), real(1), real(0), real(1), real(1));
281 return cylindricalequalarea;
287 real(1), real(0), real(1), real(0), real(1));
288 return azimuthalequalareanorth;
294 real(-1), real(0), real(-1), real(0), real(1));
295 return azimuthalequalareasouth;
314 cphi = 1 / sqrt(1 +
Math::sq(tphi)),
317 es2m1 = 1 - es1 * sphi,
318 es2m1a = _e2m * es2m1;
319 return ( tphi / es2m1 + atanhee(sphi) / cphi ) /
320 sqrt( ( (1 + es1) / es2m1a + Datanhee(1, sphi) ) *
321 ( (1 - es1) / es2m1a + Datanhee(1, -sphi) ) );
327 stol = tol_ * max(
real(1), abs(txi));
335 scterm = scphi2/(1 +
Math::sq(txia)),
336 dtphi = (txi - txia) * scterm * sqrt(scterm) *
337 _qx *
Math::sq(1 - _e2 * tphi2 / scphi2);
339 if (!(abs(dtphi) >= stol))
349 if (abs(x) <
real(0.5)) {
350 static const real lg2eps_ = -log2(numeric_limits<real>::epsilon() / 2);
359 int n = x == 0 ? 1 : int(ceil(lg2eps_ / e)) + 1;
363 real xs = sqrt(abs(x));
364 s = (x > 0 ? atanh(xs) : atan(xs)) / xs - 1;
373 if (y < x)
swap(x, y);
375 q2 = abs(2 * _e / _e2m * (1 - x));
377 x <= 0 || !(min(q1, q2) <
real(0.75)) ? DDatanhee0(x, y) :
378 (q1 < q2 ? DDatanhee1(x, y) : DDatanhee2(x, y));
384 return (Datanhee(1, y) - Datanhee(x, y))/(1 - x);
403 real z = 1, k = 1, t = 0, c = 0, en = 1;
405 t = y * t + z; c += t; z *= x;
406 t = y * t + z; c += t; z *= x;
412 real ds = en * c / k;
414 if (!(abs(ds) > abs(s) * eps_/2))
457 real s, dx = 1 - x, dy = 1 - y, xy = 1, yy = 1, ee = _e2 /
Math::sq(_e2m);
459 for (
int m = 1; ; ++m) {
460 real c = m + 2, t = c;
467 if (m % 2 == 0) ee *= _e2;
470 for (
int k = kmax - 1; k >= 0; --k) {
472 c *= (k + 1) * (2 * (k + m - 2*kmax) + 3);
473 c /= (kmax - k) * (2 * (kmax - k) + 1);
478 real ds = t * ee * xy / (m + 2);
480 if (!(abs(ds) > abs(s) * eps_/2))
487 real& x, real& y, real& gamma, real& k)
const {
492 cphi = max(epsx_, cphi);
495 tphi = sphi/cphi, txi = txif(tphi), sxi = txi/hyp(txi),
496 dq = _qZ * Dsn(txi, _txi0, sxi, _sxi0) * (txi - _txi0),
497 drho = - _a * dq / (sqrt(_m02 - _n0 * dq) + _nrho0 / _a),
498 theta = _k2 * _n0 * lam, stheta = sin(theta), ctheta = cos(theta),
499 t = _nrho0 + _n0 * drho;
500 x = t * (_n0 != 0 ? stheta / _n0 : _k2 * lam) / _k0;
503 (ctheta < 0 ? 1 - ctheta :
Math::sq(stheta)/(1 + ctheta)) / _n0 :
505 - drho * ctheta) / _k0;
506 k = _k0 * (t != 0 ? t * hyp(_fm * tphi) / _a : 1);
512 real& lat, real& lon,
513 real& gamma, real& k)
const {
516 nx = _k0 * _n0 * x, ny = _k0 * _n0 * y, y1 = _nrho0 - ny,
517 den = hypot(nx, y1) + _nrho0,
518 drho = den != 0 ? (_k0*x*nx - 2*_k0*y*_nrho0 + _k0*y*ny) / den : 0,
520 dsxia = - _scxi0 * (2 * _nrho0 + _n0 * drho) * drho /
522 txi = (_txi0 + dsxia) / sqrt(max(1 - dsxia * (2*_txi0 + dsxia), epsx2_)),
524 theta = atan2(nx, y1),
525 lam = _n0 != 0 ? theta / (_k2 * _n0) : x / (y1 * _k0);
530 k = _k0 * (den != 0 ? (_nrho0 + _n0 * drho) * hyp(_fm * tphi) / _a : 1);
534 if (!(isfinite(k) && k > 0))
536 if (!(abs(lat) < 90))
537 throw GeographicErr(
"Latitude for SetScale not in (-90d, 90d)");
538 real x, y, gamma, kold;
539 Forward(0, lat, 0, x, y, gamma, kold);
Header for GeographicLib::AlbersEqualArea class.
GeographicLib::Math::real real
#define GEOGRAPHICLIB_PANIC
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.
Mathematical functions needed by GeographicLib.
static T AngNormalize(T x)
static void sincosd(T x, T &sinx, T &cosx)
static T AngDiff(T x, T y, T &e)
Namespace for GeographicLib.
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)