Coverage for pygeodesy/albers.py: 97%
420 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-28 15:52 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-28 15:52 -0400
2# -*- coding: utf-8 -*-
4u'''Albers Equal-Area projections.
6Classes L{AlbersEqualArea}, L{AlbersEqualArea2}, L{AlbersEqualArea4},
7L{AlbersEqualAreaCylindrical}, L{AlbersEqualAreaNorth}, L{AlbersEqualAreaSouth}
8and L{AlbersError}, transcoded from I{Charles Karney}'s C++ class U{AlbersEqualArea
9<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AlbersEqualArea.html>}.
11See also I{Albers Equal-Area Conic Projection} in U{John P. Snyder, "Map Projections
12-- A Working Manual", 1987<https://Pubs.USGS.gov/pp/1395/report.pdf>}, pp 98-106
13and the Albers Conical Equal-Area examples on pp 291-294.
14'''
15# make sure int/int division yields float quotient, see .basics
16from __future__ import division as _; del _ # PYCHOK semicolon
18from pygeodesy.basics import neg, neg_
19from pygeodesy.constants import EPS0, EPS02, _EPSqrt as _TOL, \
20 _0_0, _0_5, _1_0, _N_1_0, _2_0, \
21 _N_2_0, _4_0, _6_0, _90_0, _N_90_0
22from pygeodesy.datums import _ellipsoidal_datum, _WGS84
23from pygeodesy.errors import _ValueError, _xkwds
24from pygeodesy.fmath import hypot, hypot1, sqrt3
25from pygeodesy.fsums import Fsum, fsum1f_
26from pygeodesy.interns import NN, _COMMASPACE_, _datum_, _gamma_, _k0_, \
27 _lat_, _lat1_, _lat2_, _lon_, _name_, _not_, \
28 _negative_, _scale_, _SPACE_, _x_, _y_
29from pygeodesy.karney import _diff182, _norm180, _signBit
30from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY
31from pygeodesy.named import _NamedBase, _NamedTuple, _Pass
32from pygeodesy.props import deprecated_Property_RO, Property_RO, _update_all
33from pygeodesy.streprs import Fmt, strs, unstr
34from pygeodesy.units import Bearing, Float_, Lat, Lat_, Lon, Meter, Scalar_
35from pygeodesy.utily import atand, atan2d, degrees360, sincos2, \
36 sincos2d, sincos2d_
38from math import atan, atan2, atanh, degrees, fabs, radians, sqrt
40__all__ = _ALL_LAZY.albers
41__version__ = '23.08.18'
43_k1_ = 'k1'
44_NUMIT = 8 # XXX 4?
45_NUMIT0 = 41 # XXX 21?
46_TERMS = 21 # XXX 16?
47_TOL0 = sqrt3(_TOL)
50def _ct2(s, c):
51 '''(INTERNAL) Avoid singularities at poles.
52 '''
53 c = max(EPS0, c)
54 return c, (s / c)
57def _Fsum1(a, b, c=_0_0): # floats=True
58 '''(INTERNAL) C{Fsum} 1-primed.
59 '''
60 S = Fsum()
61 S._facc_(_1_0, a, b, c, _N_1_0, up=False)
62 return S
65def _Ks(**name_k):
66 '''(INTERNAL) Scale C{B{k} >= EPS0}.
67 '''
68 return Scalar_(Error=AlbersError, low=EPS0, **name_k) # > 0
71def _Lat(*lat, **Error_name_lat):
72 '''(INTERNAL) Latitude C{-90 <= B{lat} <= 90}.
73 '''
74 kwds = _xkwds(Error_name_lat, Error=AlbersError)
75 return Lat_(*lat, **kwds)
78def _qZx(albs):
79 '''(INTERNAL) Set C{albs._qZ} and C{albs._qx}.
80 '''
81 E = albs._datum.ellipsoid # _AlbersBase
82 albs._qZ = qZ = _1_0 + E.e21 * _atanheE(_1_0, E)
83 albs._qx = qZ / (_2_0 * E.e21)
84 return qZ
87class AlbersError(_ValueError):
88 '''An L{AlbersEqualArea}, L{AlbersEqualArea2}, L{AlbersEqualArea4},
89 L{AlbersEqualAreaCylindrical}, L{AlbersEqualAreaNorth},
90 L{AlbersEqualAreaSouth} or L{Albers7Tuple} issue.
91 '''
92 pass
95class _AlbersBase(_NamedBase):
96 '''(INTERNAL) Base class for C{AlbersEqualArea...} projections.
98 @see: I{Karney}'s C++ class U{AlbersEqualArea<https://GeographicLib.SourceForge.io/
99 html/classGeographicLib_1_1AlbersEqualArea.html>}, method C{Init}.
100 '''
101 _datum = _WGS84
102 _k = NN # or _k0_ or _k1_
103 _k0 = _Ks(k0=_1_0)
104# _k0n0 = None # (INTERNAL) k0 * no
105 _k02 = _1_0 # (INTERNAL) k0**2
106# _k02n0 = None # (INTERNAL) k02 * n0
107# _lat0 = None # lat origin
108 _lat1 = None # let 1st parallel
109 _lat2 = None # lat 2nd parallel
110 _m0 = _0_0 # if polar else sqrt(m02)
111# _m02 = None # (INTERNAL) cached
112# _n0 = None # (INTERNAL) cached
113 _nrho0 = _0_0 # if polar else m0 * E.a
114 _polar = False
115 _qx = None # (INTERNAL) see _qZx
116 _qZ = None # (INTERNAL) see _qZx
117# _scxi0_ = None # (INTERNAL) sec(xi) / (qZ * E.a2)
118 _sign = +1
119# _sxi0 = None # (INTERNAL) sin(xi)
120# _txi0 = None # (INTERNAL) tan(xi)
122 def __init__(self, sa1, ca1, sa2, ca2, k, datum, name):
123 '''(INTERNAL) New C{AlbersEqualArea...} instance.
124 '''
125 qZ = self._qZ
126 if datum not in (None, self._datum):
127 self._datum = _ellipsoidal_datum(datum, name=name)
128 qZ = _qZx(self)
129 elif qZ is None:
130 qZ = _qZx(_AlbersBase)
131 if name:
132 self.name = name
134 E = self.ellipsoid
135 c = min(ca1, ca2)
136 if _signBit(c):
137 raise AlbersError(clat1=ca1, clat2=ca2, txt=_negative_)
138 polar = c < EPS0 # == 0
140 # determine hemisphere of tangent latitude
141 if sa1 < 0: # and sa2 < 0:
142 self._sign = -1
143 # internally, tangent latitude positive
144 sa1, sa2 = neg_(sa1, sa2)
145 if sa1 > sa2: # make phi1 < phi2
146 sa1, sa2 = sa2, sa1
147 ca1, ca2 = ca2, ca1
148 if sa1 < 0: # or sa2 < 0:
149 raise AlbersError(slat1=sa1, slat2=sa2, txt=_negative_)
150 ca1, ta1 = _ct2(sa1, ca1)
151 ca2, ta2 = _ct2(sa2, ca2)
153 par1 = fabs(ta1 - ta2) < EPS02 # ta1 == ta2
154 if par1 or polar:
155 ta0, C = ta2, _1_0
156 else:
157 ta0, C = self._ta0C2(ca1, sa1, ta1, ca2, sa2, ta2)
159 self._lat0 = _Lat(lat0=self._sign * atand(ta0))
160 self._m02 = m02 = _1_x21(E.f1 * ta0)
161 self._n0 = n0 = ta0 / hypot1(ta0)
162 if polar:
163 self._polar = True
164# self._nrho0 = self._m0 = _0_0
165 else: # m0 = nrho0 / E.a
166 self._m0 = sqrt(m02)
167 self._nrho0 = self._m0 * E.a
168 t = self._txi0 = self._txif(ta0)
169 h = hypot1(t)
170 s = self._sxi0 = t / h
171 if par1:
172 self._k0n0 = self._k02n0 = n0
173 else:
174 self._k0s(k * sqrt(C / (m02 + n0 * qZ * s)))
175 self._scxi0_ = h / (qZ * E.a2)
177 def _a_b_sxi3(self, *ca_sa_ta_scb_4s):
178 '''(INTERNAL) Sum of C{sm1} terms and C{sin(xi)}s for ._ta0C2.
179 '''
180 _1 = _1_0
181 a = b = s = _0_0
182 for ca, sa, ta, scb in ca_sa_ta_scb_4s:
183 cxi, sxi, _ = self._cstxif3(ta)
184 if sa > 0:
185 sa += _1
186 a += (cxi / ca)**2 * sa / (sxi + _1)
187 b += scb * ca**2 / sa
188 else:
189 sa = _1 - sa
190 a += (_1 - sxi) / sa
191 b += scb * sa
192 s += sxi
193 return a, b, s
195 def _azik(self, t, ta):
196 '''(INTERNAL) Compute the azimuthal scale C{_Ks(k=k)}.
197 '''
198 E = self.ellipsoid
199 return _Ks(k=hypot1(E.b_a * ta) * self._k0 * t / E.a)
201 def _cstxif3(self, ta):
202 '''(INTERNAL) Get 3-tuple C{(cos, sin, tan)} of M{xi(ta)}.
203 '''
204 t = self._txif(ta)
205 c = _1_0 / hypot1(t)
206 s = c * t
207 return c, s, t
209 @Property_RO
210 def datum(self):
211 '''Get the datum (L{Datum}).
212 '''
213 return self._datum
215 @Property_RO
216 def ellipsoid(self):
217 '''Get the datum's ellipsoid (L{Ellipsoid}).
218 '''
219 return self.datum.ellipsoid
221 @Property_RO
222 def equatoradius(self):
223 '''Get the geodesic's equatorial radius, semi-axis (C{meter}).
224 '''
225 return self.ellipsoid.a
227 @Property_RO
228 def flattening(self):
229 '''Get the geodesic's flattening (C{float}).
230 '''
231 return self.ellipsoid.f
233 def forward(self, lat, lon, lon0=0, name=NN):
234 '''Convert a geodetic location to east- and northing.
236 @arg lat: Latitude of the location (C{degrees}).
237 @arg lon: Longitude of the location (C{degrees}).
238 @kwarg lon0: Optional central meridian longitude (C{degrees}).
239 @kwarg name: Optional name for the location (C{str}).
241 @return: An L{Albers7Tuple}C{(x, y, lat, lon, gamma, scale, datum)},
242 with C{lon} offset by B{C{lon0}} and reduced C{[-180,180]}.
244 @note: The origin latitude is returned by C{property lat0}. No
245 false easting or northing is added. The value of B{C{lat}}
246 should be in the range C{[-90..90] degrees}. The returned
247 values C{x} and C{y} will be large but finite for points
248 projecting to infinity, i.e. one or both of the poles.
249 '''
250 a = self.ellipsoid.a
251 s = self._sign
253 k0 = self._k0
254 n0 = self._n0
255 nrho0 = self._nrho0
256 txi0 = self._txi0
258 _, ta = _ct2(*sincos2d(s * _Lat(lat=lat)))
260 _, sxi, txi = self._cstxif3(ta)
261 dq = _Dsn(txi, txi0, sxi, self._sxi0) * \
262 (txi - txi0) * self._qZ
263 drho = a * dq / (sqrt(self._m02 - n0 * dq) + self._m0)
265 lon, _ = _diff182(lon0, lon)
266 x = radians(lon)
267 th = self._k02n0 * x
268 sth, cth = sincos2(th) # XXX sin, cos
269 if n0:
270 x = sth / n0
271 y = (_1_0 - cth) if cth < 0 else (sth**2 / (cth + _1_0))
272 y *= nrho0 / n0
273 else:
274 x *= self._k02
275 y = _0_0
276 t = nrho0 - n0 * drho
277 x = t * x / k0
278 y = s * (y + drho * cth) / k0
280 g = degrees360(s * th)
281 if t:
282 k0 = self._azik(t, ta)
283 return Albers7Tuple(x, y, lat, lon, g, k0, self.datum,
284 name=name or self.name)
286 @Property_RO
287 def ispolar(self):
288 '''Is this projection polar (C{bool})?
289 '''
290 return self._polar
292 isPolar = ispolar # synonym
294 def _k0s(self, k0):
295 '''(INTERNAL) Set C{._k0}, C{._k02}, etc.
296 '''
297 self._k0 = k0 = _Ks(k0=k0)
298 self._k02 = k02 = k0**2
299 self._k0n0 = k0 * self._n0
300 self._k02n0 = k02 * self._n0
302 @Property_RO
303 def lat0(self):
304 '''Get the latitude of the projection origin (C{degrees}).
306 This is the latitude of minimum azimuthal scale and
307 equals the B{C{lat}} in the 1-parallel L{AlbersEqualArea}
308 and lies between B{C{lat1}} and B{C{lat2}} for the
309 2-parallel L{AlbersEqualArea2} and L{AlbersEqualArea4}
310 projections.
311 '''
312 return self._lat0
314 @Property_RO
315 def lat1(self):
316 '''Get the latitude of the first parallel (C{degrees}).
317 '''
318 return self._lat1
320 @Property_RO
321 def lat2(self):
322 '''Get the latitude of the second parallel (C{degrees}).
324 @note: The second and first parallel latitudes are the
325 same instance for 1-parallel C{AlbersEqualArea*}
326 projections.
327 '''
328 return self._lat2
330 @deprecated_Property_RO
331 def majoradius(self): # PYCHOK no cover
332 '''DEPRECATED, use property C{equatoradius}.'''
333 return self.equatoradius
335 def rescale0(self, lat, k=1): # PYCHOK no cover
336 '''Set the azimuthal scale for this projection.
338 @arg lat: Northern latitude (C{degrees}).
339 @arg k: Azimuthal scale at latitude B{C{lat}} (C{scalar}).
341 @raise AlbersError: Invalid B{C{lat}} or B{C{k}}.
343 @note: This allows a I{latitude of conformality} to be specified.
344 '''
345 k0 = _Ks(k=k) / self.forward(lat, _0_0).scale
346 if self._k0 != k0:
347 _update_all(self)
348 self._k0s(k0)
350 def reverse(self, x, y, lon0=0, name=NN, LatLon=None, **LatLon_kwds):
351 '''Convert an east- and northing location to geodetic lat- and longitude.
353 @arg x: Easting of the location (C{meter}).
354 @arg y: Northing of the location (C{meter}).
355 @kwarg lon0: Optional central meridian longitude (C{degrees}).
356 @kwarg name: Optional name for the location (C{str}).
357 @kwarg LatLon: Class to use (C{LatLon}) or C{None}.
358 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword
359 arguments, ignored if C{B{LatLon} is None}.
361 @return: The geodetic (C{LatLon}) or if B{C{LatLon}} is C{None} an
362 L{Albers7Tuple}C{(x, y, lat, lon, gamma, scale, datum)}.
364 @note: The origin latitude is returned by C{property lat0}. No
365 false easting or northing is added. The returned value of
366 C{lon} is in the range C{[-180..180] degrees} and C{lat}
367 is in the range C{[-90..90] degrees}. If the given
368 B{C{x}} or B{C{y}} point is outside the valid projected
369 space the nearest pole is returned.
370 '''
371 k0 = self._k0
372 n0 = self._n0
373 k0n0 = self._k0n0
374 s = self._sign
375 txi = self._txi0
377 x = Meter(x=x)
378 nx = k0n0 * x
379 y = Meter(y=y)
380 y_ = s * y
381 ny = k0n0 * y_
382 t = nrho0 = self._nrho0
383 y1 = nrho0 - ny
385 den = hypot(nx, y1) + nrho0 # 0 implies origin with polar aspect
386 if den:
387 drho = _Fsum1(x * nx, y_ * nrho0 * _N_2_0, y_ * ny).fover(den / k0)
388 # dsxia = scxi0 * dsxi
389 t += drho * n0
390 d_ = (nrho0 + t) * drho * self._scxi0_ # / (qZ * E.a2)
391 d_2 = (txi * _2_0 - d_) * d_ + _1_0
392 txi = (txi - d_) / (sqrt(d_2) if d_2 > EPS02 else EPS0)
394 ta = self._tanf(txi)
395 lat = atand(s * ta)
397 th = atan2(nx, y1)
398 lon = degrees((th / self._k02n0) if n0 else (x / (y1 * k0)))
399 if lon0:
400 lon += _norm180(lon0)
401 lon = _norm180(lon)
403 n = name or self.name
404 if LatLon is None:
405 g = degrees360(s * th)
406 if den:
407 k0 = self._azik(t, ta)
408 r = Albers7Tuple(x, y, lat, lon, g, k0, self.datum, name=n)
409 else: # PYCHOK no cover
410 kwds = _xkwds(LatLon_kwds, datum=self.datum, name=n)
411 r = LatLon(lat, lon, **kwds)
412 return r
414 @Property_RO
415 def scale0(self):
416 '''Get the central scale for the projection (C{float}).
418 This is the azimuthal scale on the latitude of origin
419 of the projection, see C{property lat0}.
420 '''
421 return self._k0
423 def _ta0(self, s1_qZ, ta0, E):
424 '''(INTERNAL) Refine C{ta0} for C{._ta0C2}.
425 '''
426 e2 = E.e2
427 e21 = E.e21
428 e22 = E.e22 # == e2 / e21
429 tol = _tol(_TOL0, ta0)
430 _Ta02 = Fsum(ta0).fsum2_
431 _fabs = fabs
432 _fsum1 = fsum1f_
433 _sqrt = sqrt
434 _1, _2 = _1_0, _2_0
435 _4, _6 = _4_0, _6_0
436 for self._iteration in range(1, _NUMIT0): # 4 trips
437 ta02 = ta0**2
438 sca02 = ta02 + _1
439 sca0 = _sqrt(sca02)
440 sa0 = ta0 / sca0
441 sa01 = sa0 + _1
442 sa02 = sa0**2
443 # sa0m = 1 - sa0 = 1 / (sec(a0) * (tan(a0) + sec(a0)))
444 sa0m = _1 / (sca0 * (ta0 + sca0)) # scb0^2 * sa0
445 sa0m1 = sa0m / (_1 - e2 * sa0)
446 sa021 = _1 - e2 * sa02
448 g = (_1 + ta02 * e21) * sa0
449 dg = (_1 + ta02 * _2) * sca02 * e21 + e2
450 D = (_1 - (_1 + sa0 * _2 * sa01) * e2) * sa0m / (e21 * sa01) # dD/dsa0
451 dD = (_2 - (_6 + sa0 * _4) * sa02 * e2) / (e21 * sa01**2)
452 BA = (_atanh1(e2 * sa0m1**2) * e21 - e2 * sa0m) * sa0m1 \
453 - (_2 + (_1 + e2) * sa0) * sa0m**2 * e22 / sa021 # B + A
454 d = (_4 - (_1 + sa02) * e2 * _2) * e22 / (sa021**2 * sca02) # dAB
455 u = _fsum1(s1_qZ * g, -D, g * BA)
456 du = _fsum1(s1_qZ * dg, dD, dg * BA, g * d)
457 ta0, d = _Ta02(-u / du * (sca0 * sca02))
458 if _fabs(d) < tol:
459 return ta0
460 raise AlbersError(Fmt.no_convergence(d, tol), txt=repr(self))
462 def _ta0C2(self, ca1, sa1, ta1, ca2, sa2, ta2):
463 '''(INTERNAL) Compute C{ta0} and C{C} for C{.__init__}.
464 '''
465 E = self.ellipsoid
466 f1, e2 = E.f1, E.e2
467 _1 = _1_0
469 tb1 = f1 * ta1
470 tb2 = f1 * ta2
471 dtb12 = f1 * (tb1 + tb2)
472 scb12 = _1 + tb1**2
473 scb22 = _1 + tb2**2
475 dsn_2 = _Dsn(ta2, ta1, sa2, sa1) * _0_5
476 sa12 = sa1 * sa2
478 esa1_2 = (_1 - e2 * sa1**2) \
479 * (_1 - e2 * sa2**2)
480 esa12 = _1 + e2 * sa12
482 axi, bxi, sxi = self._a_b_sxi3((ca1, sa1, ta1, scb12),
483 (ca2, sa2, ta2, scb22))
485 dsxi = ((esa12 / esa1_2) + _DatanheE(sa2, sa1, E)) * dsn_2 / self._qx
486 C = _Fsum1(sxi * dtb12 / dsxi, scb22, scb12).fover(scb22 * scb12 * _2_0)
488 sa12 = fsum1f_(sa1, sa2, sa12)
489 axi *= (sa12 * e2 + _1) / (sa12 + _1)
490 bxi *= _Fsum1(sa1, sa2, esa12).fover(esa1_2) * e2 + _D2atanheE(sa1, sa2, E) * E.e21
491 s1_qZ = (axi * self._qZ - bxi) * dsn_2 / dtb12
492 ta0 = self._ta0(s1_qZ, (ta1 + ta2) * _0_5, E)
493 return ta0, C
495 def _tanf(self, txi): # in .Ellipsoid.auxAuthalic
496 '''(INTERNAL) Function M{tan-phi from tan-xi}.
497 '''
498 tol = _tol(_TOL, txi)
499 e2 = self.ellipsoid.e2
500 qx = self._qx
502 ta = txi
503 _Ta2 = Fsum(ta).fsum2_
504 _fabs = fabs
505 _sqrt3 = sqrt3
506 _txif = self._txif
507 _1 = _1_0
508 for self._iteration in range(1, _NUMIT): # max 2, mean 1.99
509 # dtxi / dta = (scxi / sca)^3 * 2 * (1 - e^2)
510 # / (qZ * (1 - e^2 * sa^2)^2)
511 ta2 = ta**2
512 sca2 = _1 + ta2
513 txia = _txif(ta)
514 s3qx = _sqrt3(sca2 / (txia**2 + _1)) * qx # * _1_x21(txia)
515 eta2 = (_1 - e2 * ta2 / sca2)**2
516 ta, d = _Ta2((txi - txia) * s3qx * eta2)
517 if _fabs(d) < tol:
518 return ta
519 raise AlbersError(Fmt.no_convergence(d, tol), txt=repr(self))
521 def toRepr(self, prec=6, **unused): # PYCHOK expected
522 '''Return a string representation of this projection.
524 @kwarg prec: Number of (decimal) digits, unstripped (C{int}).
526 @return: This projection as C{"<classname>(lat1, lat2, ...)"}
527 (C{str}).
528 '''
529 t = self.toStr(prec=prec, sep=_COMMASPACE_)
530 return Fmt.PAREN(self.classname, t)
532 def toStr(self, prec=6, sep=_SPACE_, **unused): # PYCHOK expected
533 '''Return a string representation of this projection.
535 @kwarg prec: Number of (decimal) digits, unstripped (C{int}).
536 @kwarg sep: Separator to join (C{str}).
538 @return: This projection as C{"lat1 lat2"} (C{str}).
539 '''
540 k = self._k
541 t = (self.lat1, self.lat2, self._k0) if k is _k1_ else (
542 (self.lat1, self._k0) if k is _k0_ else
543 (self.lat1,))
544 t = strs(t, prec=prec)
545 if k:
546 t = t[:-1] + (Fmt.EQUAL(k, t[-1]),)
547 if self.datum != _WGS84:
548 t += (Fmt.EQUAL(_datum_, self.datum),)
549 if self.name:
550 t += (Fmt.EQUAL(_name_, repr(self.name)),)
551 return t if sep is None else sep.join(t)
553 def _txif(self, ta): # in .Ellipsoid.auxAuthalic
554 '''(INTERNAL) Function M{tan-xi from tan-phi}.
555 '''
556 E = self.ellipsoid
557 _1 = _1_0
559 ca2 = _1_x21(ta)
560 sa = sqrt(ca2) * fabs(ta) # enforce odd parity
561 sa1 = _1 + sa
563 es1 = sa * E.e2
564 es1m1 = sa1 * (_1 - es1)
565 es1p1 = sa1 / (_1 + es1)
566 es2m1 = _1 - sa * es1
567 es2m1a = es2m1 * E.e21 # e2m
568 s = sqrt((ca2 / (es1p1 * es2m1a) + _atanheE(ca2 / es1m1, E))
569 * (es1m1 / es2m1a + _atanheE(es1p1, E)))
570 t = _Fsum1(sa / es2m1, _atanheE(sa, E)).fover(s)
571 return neg(t) if ta < 0 else t
574class AlbersEqualArea(_AlbersBase):
575 '''An Albers equal-area (authalic) projection with a single standard parallel.
577 @see: L{AlbersEqualArea2} and L{AlbersEqualArea4}.
578 '''
579 _k = _k0_
581 def __init__(self, lat, k0=1, datum=_WGS84, name=NN):
582 '''New L{AlbersEqualArea} projection.
584 @arg lat: Standard parallel (C{degrees}).
585 @kwarg k0: Azimuthal scale on the standard parallel (C{scalar}).
586 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},
587 L{Ellipsoid2} or L{a_f2Tuple}).
588 @kwarg name: Optional name for the projection (C{str}).
590 @raise AlbersError: Invalid B{C{lat}}, B{C{k0}} or no convergence.
591 '''
592 self._lat1 = self._lat2 = lat = _Lat(lat1=lat)
593 args = tuple(sincos2d(lat)) * 2 + (_Ks(k0=k0), datum, name)
594 _AlbersBase.__init__(self, *args)
597class AlbersEqualArea2(_AlbersBase):
598 '''An Albers equal-area (authalic) projection with two standard parallels.
600 @see: L{AlbersEqualArea} and L{AlbersEqualArea4}.
601 '''
602 _k = _k1_
604 def __init__(self, lat1, lat2, k1=1, datum=_WGS84, name=NN):
605 '''New L{AlbersEqualArea2} projection.
607 @arg lat1: First standard parallel (C{degrees}).
608 @arg lat2: Second standard parallel (C{degrees}).
609 @kwarg k1: Azimuthal scale on the standard parallels (C{scalar}).
610 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},
611 L{Ellipsoid2} or L{a_f2Tuple}).
612 @kwarg name: Optional name for the projection (C{str}).
614 @raise AlbersError: Invalid B{C{lat1}}m B{C{lat2}}, B{C{k1}}
615 or no convergence.
616 '''
617 self._lat1, self._lat2 = lats = _Lat(lat1=lat1), _Lat(lat2=lat2)
618 args = tuple(sincos2d_(*lats)) + (_Ks(k1=k1), datum, name)
619 _AlbersBase.__init__(self, *args)
622class AlbersEqualArea4(_AlbersBase):
623 '''An Albers equal-area (authalic) projection specified by the C{sin}
624 and C{cos} of both standard parallels.
626 @see: L{AlbersEqualArea} and L{AlbersEqualArea2}.
627 '''
628 _k = _k1_
630 def __init__(self, slat1, clat1, slat2, clat2, k1=1, datum=_WGS84, name=NN):
631 '''New L{AlbersEqualArea4} projection.
633 @arg slat1: Sine of first standard parallel (C{scalar}).
634 @arg clat1: Cosine of first standard parallel (non-negative C{scalar}).
635 @arg slat2: Sine of second standard parallel (C{scalar}).
636 @arg clat2: Cosine of second standard parallel (non-negative C{scalar}).
637 @kwarg k1: Azimuthal scale on the standard parallels (C{scalar}).
638 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},
639 L{Ellipsoid2} or L{a_f2Tuple}).
640 @kwarg name: Optional name for the projection (C{str}).
642 @raise AlbersError: Negative B{C{clat1}} or B{C{clat2}}, B{C{slat1}}
643 and B{C{slat2}} have opposite signs (hemispheres),
644 invalid B{C{k1}} or no convergence.
645 '''
646 def _Lat_s_c3(name, s, c):
647 r = Float_(hypot(s, c), name=name, Error=AlbersError)
648 L = _Lat(atan2d(s, c), name=name)
649 return L, (s / r), (c / r)
651 self._lat1, sa1, ca1 = _Lat_s_c3(_lat1_, slat1, clat1)
652 self._lat2, sa2, ca2 = _Lat_s_c3(_lat2_, slat2, clat2)
653 _AlbersBase.__init__(self, sa1, ca1, sa2, ca2, _Ks(k1=k1), datum, name)
656class AlbersEqualAreaCylindrical(_AlbersBase):
657 '''An L{AlbersEqualArea} projection at C{lat=0} and C{k0=1} degenerating
658 to the cylindrical-equal-area projection.
659 '''
660 _lat1 = _lat2 = _Lat(lat1=_0_0)
662 def __init__(self, lat=_0_0, datum=_WGS84, name=NN):
663 '''New L{AlbersEqualAreaCylindrical} projection.
665 @kwarg lat: Standard parallel (C{0 degrees} I{fixed}).
666 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},
667 L{Ellipsoid2} or L{a_f2Tuple}).
668 @kwarg name: Optional name for the projection (C{str}).
669 '''
670 _xlat(lat, _0_0, AlbersEqualAreaCylindrical)
671 _AlbersBase.__init__(self, _0_0, _1_0, _0_0, _1_0, 1, datum, name)
674class AlbersEqualAreaNorth(_AlbersBase):
675 '''An azimuthal L{AlbersEqualArea} projection at C{lat=90} and C{k0=1}
676 degenerating to the L{azimuthal} L{LambertEqualArea} projection.
677 '''
678 _lat1 = _lat2 = _Lat(lat1=_90_0)
680 def __init__(self, lat=_90_0, datum=_WGS84, name=NN):
681 '''New L{AlbersEqualAreaNorth} projection.
683 @kwarg lat: Standard parallel (C{90 degrees} I{fixed}).
684 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},
685 L{Ellipsoid2} or L{a_f2Tuple}).
686 @kwarg name: Optional name for the projection (C{str}).
687 '''
688 _xlat(lat, _90_0, AlbersEqualAreaNorth)
689 _AlbersBase.__init__(self, _1_0, _0_0, _1_0, _0_0, 1, datum, name)
692class AlbersEqualAreaSouth(_AlbersBase):
693 '''An azimuthal L{AlbersEqualArea} projection at C{lat=-90} and C{k0=1}
694 degenerating to the L{azimuthal} L{LambertEqualArea} projection.
695 '''
696 _lat1 = _lat2 = _Lat(lat1=_N_90_0)
698 def __init__(self, lat=_N_90_0, datum=_WGS84, name=NN):
699 '''New L{AlbersEqualAreaSouth} projection.
701 @kwarg lat: Standard parallel (C{-90 degrees} I{fixed}).
702 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},
703 L{Ellipsoid2} or L{a_f2Tuple}).
704 @kwarg name: Optional name for the projection (C{str}).
705 '''
706 _xlat(lat, _N_90_0, AlbersEqualAreaSouth)
707 _AlbersBase.__init__(self, _N_1_0, _0_0, _N_1_0, _0_0, 1, datum, name)
710class Albers7Tuple(_NamedTuple):
711 '''7-Tuple C{(x, y, lat, lon, gamma, scale, datum)}, in C{meter},
712 C{meter}, C{degrees90}, C{degrees180}, C{degrees360}, C{scalar} and
713 C{Datum} where C{(x, y)} is the projected, C{(lat, lon)} the geodetic
714 location, C{gamma} the meridian convergence at point, the bearing of
715 the y-axis measured clockwise from true North and C{scale} is the
716 azimuthal scale of the projection at point. The radial scale is
717 the reciprocal C{1 / scale}.
718 '''
719 _Names_ = (_x_, _y_, _lat_, _lon_, _gamma_, _scale_, _datum_)
720 _Units_ = ( Meter, Meter, Lat, Lon, Bearing, _Pass, _Pass)
723def _atanh1(x):
724 '''(INTERNAL) Function M{atanh(sqrt(x)) / sqrt(x) - 1}.
725 '''
726 s = fabs(x)
727 if 0 < s < _0_5: # for typical ...
728 # x < E.e^2 == 2 * E.f use ...
729 # x / 3 + x^2 / 5 + x^3 / 7 + ...
730 y, k = x, 3
731 _S2 = Fsum(y / k).fsum2_
732 for _ in range(_TERMS): # 9 terms
733 y *= x # x**n
734 k += 2 # 2*n + 1
735 s, d = _S2(y / k)
736 if not d:
737 break
738 elif s:
739 s = sqrt(s)
740 s = (atanh(s) if x > 0 else atan(s)) / s - _1_0
741 return s
744def _atanheE(x, E): # see Ellipsoid._es_atanh, .AuxLat._atanhee
745 '''(INTERNAL) Function M{atanhee(x)}, defined as ...
746 atanh( E.e * x) / E.e if f > 0 # oblate
747 atan (sqrt(-E.e2) * x) / sqrt(-E.e2) if f < 0 # prolate
748 x if f = 0.
749 '''
750 e = E.e
751 if E.f > 0: # .isOblate
752 x = atanh( x * e) / e
753 elif E.f < 0: # .isProlate
754 x = (atan2(-x * e, _N_1_0) if x < 0 else
755 atan2( x * e, _1_0)) / e
756 return x
759def _DatanheE(x, y, E): # see .rhumbx._DeatanhE
760 '''(INTERNAL) Function M{Datanhee(x, y)}, defined as
761 M{atanhee((x - y) / (1 - E.e^2 * x * y)) / (x - y)}.
762 '''
763 e = _1_0 - E.e2 * x * y
764 if e:
765 d = x - y
766 e = (_atanheE(d / e, E) / d) if d else (_1_0 / e)
767 return e
770def _D2atanheE(x, y, E):
771 '''(INTERNAL) Function M{D2atanhee(x, y)}, defined as
772 M{(Datanhee(1, y) - Datanhee(1, x)) / (y - x)}.
773 '''
774 s, e2 = _0_0, E.e2
775 if e2:
776 if ((fabs(x) + fabs(y)) * e2) < _0_5:
777 e = z = _1_0
778 k = 1
779 T = Fsum() # Taylor expansion
780 _T = T.fsum_
781 _C = Fsum().fsum_
782 _S2 = Fsum().fsum2_
783 for _ in range(_TERMS): # 15 terms
784 T *= y; p = _T(z); z *= x # PYCHOK ;
785 T *= y; t = _T(z); z *= x # PYCHOK ;
786 e *= e2
787 k += 2
788 s, d = _S2(_C(p, t) * e / k)
789 if not d:
790 break
791 else: # PYCHOK no cover
792 s = _1_0 - x
793 if s:
794 s = (_DatanheE(_1_0, y, E) - _DatanheE(x, y, E)) / s
795 return s
798def _Dsn(x, y, sx, sy):
799 '''(INTERNAL) Divided differences, defined as M{Df(x, y) = (f(x) - f(y)) / (x - y)}
800 with M{sn(x) = x / sqrt(1 + x^2)}: M{Dsn(x, y) = (x + y) / ((sn(x) + sn(y)) *
801 (1 + x^2) * (1 + y^2))}.
803 @see: U{W. M. Kahan and R. J. Fateman, "Sympbolic Computation of Divided
804 Differences"<https://People.EECS.Berkeley.EDU/~fateman/papers/divdiff.pdf>},
805 U{ACM SIGSAM Bulletin 33(2), 7-28 (1999)<https://DOI.org/10.1145/334714.334716>}
806 and U{AlbersEqualArea.hpp
807 <https://GeographicLib.SourceForge.io/C++/doc/AlbersEqualArea_8hpp_source.html>}.
808 '''
809 # sx = x / hypot1(x)
810 d, t = _1_0, (x * y)
811 if t > 0:
812 s = sx + sy
813 if s:
814 t = sx * sy / t
815 d = t**2 * (x + y) / s
816 elif x != y:
817 d = (sx - sy) / (x - y)
818 return d
821def _tol(tol, x):
822 '''(INTERNAL) Converge tolerance.
823 '''
824 return tol * max(_1_0, fabs(x))
827def _1_x21(x):
828 '''(INTERNAL) Return M{1 / (x**2 + 1)}.
829 '''
830 return _1_0 / (x**2 + _1_0)
833def _xlat(lat, f, where):
834 '''(INTERNAL) check fixed C{lat}.
835 '''
836 if lat is not f and _Lat(lat=lat) != f:
837 t = unstr(where.__name__, lat=lat)
838 raise AlbersError(t, txt=_not_(f))
841__all__ += _ALL_DOCS(_AlbersBase)
843# **) MIT License
844#
845# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.
846#
847# Permission is hereby granted, free of charge, to any person obtaining a
848# copy of this software and associated documentation files (the "Software"),
849# to deal in the Software without restriction, including without limitation
850# the rights to use, copy, modify, merge, publish, distribute, sublicense,
851# and/or sell copies of the Software, and to permit persons to whom the
852# Software is furnished to do so, subject to the following conditions:
853#
854# The above copyright notice and this permission notice shall be included
855# in all copies or substantial portions of the Software.
856#
857# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
858# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
859# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
860# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
861# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
862# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
863# OTHER DEALINGS IN THE SOFTWARE.