1

2# -*- coding: utf-8 -*-

3

4 u'''Albers Equal-Area projections.

5

6 Classes L{AlbersEqualArea}, L{AlbersEqualArea2}, L{AlbersEqualArea4},

7 L{AlbersEqualAreaCylindrical}, L{AlbersEqualAreaNorth}, L{AlbersEqualAreaSouth}

8 and L{AlbersError}, transcoded from I{Charles Karney}'s C++ class U{AlbersEqualArea

9<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AlbersEqualArea.html>}.

10

11 See 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

13 and the Albers Conical Equal-Area examples on pp 291-294.

14'''

15# make sure int/int division yields float quotient, see .basics

16 from __future__ import division as _; del _ # PYCHOK semicolon

17

18 from pygeodesy.basics import neg, neg_

19 from 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

22 from pygeodesy.datums import _ellipsoidal_datum, _WGS84

23 from pygeodesy.errors import _ValueError, _xkwds

24 from pygeodesy.fmath import hypot, hypot1, sqrt3

25 from pygeodesy.fsums import Fsum, _Fsum1f_, fsum1f_

26 from pygeodesy.interns import NN, _COMMASPACE_, _datum_, _gamma_, _k0_, \

27 _lat_, _lat1_, _lat2_, _lon_, _negative_, \

28 _scale_, _SPACE_, _x_, _y_

29 from pygeodesy.karney import _diff182, _norm180, _signBit

30 from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY

31 from pygeodesy.named import _name2__, _NamedBase, _NamedTuple, _Pass

32 from pygeodesy.props import deprecated_Property_RO, Property_RO, _update_all

33 from pygeodesy.streprs import Fmt, strs, unstr

34 from pygeodesy.units import Bearing, Float_, Lat, Lat_, Lon, Meter, Scalar_

35 from pygeodesy.utily import atan1, atan1d, atan2, degrees360, sincos2, \

36 sincos2d, sincos2d_

37

38 from math import atanh, degrees, fabs, radians, sqrt

39

40__all__ = _ALL_LAZY.albers

41__version__ = '24.11.24'

42

43_k1_ = 'k1'

44_NUMIT = 8 # XXX 4?

45_NUMIT0 = 41 # XXX 21?

46_TERMS = 31 # XXX 16?

47_TOL0 = sqrt3(_TOL)

48

49

50 def _ct2(s, c):

51 '''(INTERNAL) Avoid singularities at poles.

52 '''

53 c = max(EPS0, c)

54 return c, (s / c)

55

56

57 def _Ks(**name_k):

58 '''(INTERNAL) Scale C{B{k} >= EPS0}.

59 '''

60 return Scalar_(Error=AlbersError, low=EPS0, **name_k) # > 0

61

62

63 def _Lat(*lat, **Error_name_lat):

64 '''(INTERNAL) Latitude C{-90 <= B{lat} <= 90}.

65 '''

66 kwds = _xkwds(Error_name_lat, Error=AlbersError)

67 return Lat_(*lat, **kwds)

68

69

70 def _qZx(albs):

71 '''(INTERNAL) Set C{albs._qZ} and C{albs._qx}.

72 '''

73 E = albs._datum.ellipsoid # _AlbersBase

74 albs._qZ = qZ = _1_0 + E.e21 * _atanheE(_1_0, E)

75 albs._qx = qZ / (_2_0 * E.e21)

76 return qZ

77

78

79 class AlbersError(_ValueError):

80 '''An L{AlbersEqualArea}, L{AlbersEqualArea2}, L{AlbersEqualArea4},

81 L{AlbersEqualAreaCylindrical}, L{AlbersEqualAreaNorth},

82 L{AlbersEqualAreaSouth} or L{Albers7Tuple} issue.

83 '''

84 pass

85

86

87 class _AlbersBase(_NamedBase):

88 '''(INTERNAL) Base class for C{AlbersEqualArea...} projections.

89

90 @see: I{Karney}'s C++ class U{AlbersEqualArea<https://GeographicLib.SourceForge.io/

91 C++/doc/classGeographicLib_1_1AlbersEqualArea.html>}, method C{Init}.

92 '''

93 _datum = _WGS84

94 _k = NN # or _k0_ or _k1_

95 _k0 = _Ks(k0=_1_0)

96# _k0n0 = None # (INTERNAL) k0 * no

97 _k02 = _1_0 # (INTERNAL) k0**2

98# _k02n0 = None # (INTERNAL) k02 * n0

99# _lat0 = None # lat origin

100 _lat1 = None # let 1st parallel

101 _lat2 = None # lat 2nd parallel

102 _m0 = _0_0 # if polar else sqrt(m02)

103# _m02 = None # (INTERNAL) cached

104# _n0 = None # (INTERNAL) cached

105 _nrho0 = _0_0 # if polar else m0 * E.a

106 _polar = False

107 _qx = None # (INTERNAL) see _qZx

108 _qZ = None # (INTERNAL) see _qZx

109# _scxi0_ = None # (INTERNAL) sec(xi) / (qZ * E.a2)

110 _sign = +1

111# _sxi0 = None # (INTERNAL) sin(xi)

112# _txi0 = None # (INTERNAL) tan(xi)

113

114 def __init__(self, sa1, ca1, sa2, ca2, k, datum, **name):

115 '''(INTERNAL) New C{AlbersEqualArea...} instance.

116 '''

117 qZ = self._qZ

118 if datum not in (None, self._datum):

119 self._datum = _ellipsoidal_datum(datum, **name)

120 qZ = _qZx(self)

121 elif qZ is None:

122 qZ = _qZx(_AlbersBase)

123 if name:

124 self.name = name

125

126 E = self.ellipsoid

127 c = min(ca1, ca2)

128 if _signBit(c):

129 raise AlbersError(clat1=ca1, clat2=ca2, txt=_negative_)

130 polar = c < EPS0 # == 0

131

132 # determine hemisphere of tangent latitude

133 if sa1 < 0: # and sa2 < 0:

134 self._sign = -1

135 # internally, tangent latitude positive

136 sa1, sa2 = neg_(sa1, sa2)

137 if sa1 > sa2: # make phi1 < phi2

138 sa1, sa2 = sa2, sa1

139 ca1, ca2 = ca2, ca1

140 if sa1 < 0: # or sa2 < 0:

141 raise AlbersError(slat1=sa1, slat2=sa2, txt=_negative_)

142 ca1, ta1 = _ct2(sa1, ca1)

143 ca2, ta2 = _ct2(sa2, ca2)

144

145 par1 = fabs(ta1 - ta2) < EPS02 # ta1 == ta2

146 if par1 or polar:

147 ta0, C = ta2, _1_0

148 else:

149 ta0, C = self._ta0C2(ca1, sa1, ta1, ca2, sa2, ta2)

150

151 self._lat0 = _Lat(lat0=self._sign * atan1d(ta0))

152 self._m02 = m02 = _1_x21(E.f1 * ta0)

153 self._n0 = n0 = ta0 / hypot1(ta0)

154 if polar:

155 self._polar = True

156# self._nrho0 = self._m0 = _0_0

157 else: # m0 = nrho0 / E.a

158 self._m0 = sqrt(m02)

159 self._nrho0 = self._m0 * E.a

160 t = self._txi0 = self._txif(ta0)

161 h = hypot1(t)

162 s = self._sxi0 = t / h

163 if par1:

164 self._k0n0 = self._k02n0 = n0

165 else:

166 self._k0s(k * sqrt(C / (m02 + n0 * qZ * s)))

167 self._scxi0_ = h / (qZ * E.a2)

168

169 def _a_b_sxi3(self, *ca_sa_ta_scb_4s):

170 '''(INTERNAL) Sum of C{sm1} terms and C{sin(xi)}s for ._ta0C2.

171 '''

172 _1 = _1_0

173 a = b = s = _0_0

174 for ca, sa, ta, scb in ca_sa_ta_scb_4s:

175 cxi, sxi, _ = self._cstxif3(ta)

176 if sa > 0:

177 sa += _1

178 a += (cxi / ca)**2 * sa / (sxi + _1)

179 b += scb * ca**2 / sa

180 else:

181 sa = _1 - sa

182 a += (_1 - sxi) / sa

183 b += scb * sa

184 s += sxi

185 return a, b, s

186

187 def _azik(self, t, ta):

188 '''(INTERNAL) Compute the azimuthal scale C{_Ks(k=k)}.

189 '''

190 E = self.ellipsoid

191 return _Ks(k=hypot1(E.b_a * ta) * self._k0 * t / E.a)

192

193 def _cstxif3(self, ta):

194 '''(INTERNAL) Get 3-tuple C{(cos, sin, tan)} of M{xi(ta)}.

195 '''

196 t = self._txif(ta)

197 c = _1_0 / hypot1(t)

198 s = c * t

199 return c, s, t

200

201 @Property_RO

202 def datum(self):

203 '''Get the datum (L{Datum}).

204 '''

205 return self._datum

206

207 @Property_RO

208 def ellipsoid(self):

209 '''Get the datum's ellipsoid (L{Ellipsoid}).

210 '''

211 return self.datum.ellipsoid

212

213 @Property_RO

214 def equatoradius(self):

215 '''Get the C{ellipsoid}'s equatorial radius, semi-axis (C{meter}).

216 '''

217 return self.ellipsoid.a

218

219 a = equatoradius

220

221 @Property_RO

222 def flattening(self):

223 '''Get the C{ellipsoid}'s flattening (C{scalar}).

224 '''

225 return self.ellipsoid.f

226

227 f = flattening

228

229 def forward(self, lat, lon, lon0=0, **name):

230 '''Convert a geodetic location to east- and northing.

231

232 @arg lat: Latitude of the location (C{degrees}).

233 @arg lon: Longitude of the location (C{degrees}).

234 @kwarg lon0: Optional central meridian longitude (C{degrees}).

235 @kwarg name: Optional C{B{name}=NN} for the location (C{str}).

236

237 @return: An L{Albers7Tuple}C{(x, y, lat, lon, gamma, scale, datum)},

238 with C{lon} offset by B{C{lon0}} and reduced C{[-180,180]}.

239

240 @note: The origin latitude is returned by C{property lat0}. No

241 false easting or northing is added. The value of B{C{lat}}

242 should be in the range C{[-90..90] degrees}. The returned

243 values C{x} and C{y} will be large but finite for points

244 projecting to infinity, i.e. one or both of the poles.

245 '''

246 a = self.ellipsoid.a

247 s = self._sign

248

249 k0 = self._k0

250 n0 = self._n0

251 nrho0 = self._nrho0

252 txi0 = self._txi0

253

254 _, ta = _ct2(*sincos2d(s * _Lat(lat=lat)))

255

256 _, sxi, txi = self._cstxif3(ta)

257 dq = _Dsn(txi, txi0, sxi, self._sxi0) * \

258 (txi - txi0) * self._qZ

259 drho = a * dq / (sqrt(self._m02 - n0 * dq) + self._m0)

260

261 lon, _ = _diff182(lon0, lon)

262 x = radians(lon)

263 th = self._k02n0 * x

264 sth, cth = sincos2(th) # XXX sin, cos

265 if n0:

266 x = sth / n0

267 y = (_1_0 - cth) if cth < 0 else (sth**2 / (cth + _1_0))

268 y *= nrho0 / n0

269 else:

270 x *= self._k02

271 y = _0_0

272 t = nrho0 - n0 * drho

273 x = t * x / k0

274 y = s * (y + drho * cth) / k0

275

276 g = degrees360(s * th)

277 if t:

278 k0 = self._azik(t, ta)

279 return Albers7Tuple(x, y, lat, lon, g, k0, self.datum,

280 name=self._name__(name))

281

282 @Property_RO

283 def ispolar(self):

284 '''Is this projection polar (C{bool})?

285 '''

286 return self._polar

287

288 isPolar = ispolar # synonym

289

290 def _k0s(self, k0):

291 '''(INTERNAL) Set C{._k0}, C{._k02}, etc.

292 '''

293 self._k0 = k0 = _Ks(k0=k0)

294 self._k02 = k02 = k0**2

295 self._k0n0 = k0 * self._n0

296 self._k02n0 = k02 * self._n0

297

298 @Property_RO

299 def lat0(self):

300 '''Get the latitude of the projection origin (C{degrees}).

301

302 This is the latitude of minimum azimuthal scale and

303 equals the B{C{lat}} in the 1-parallel L{AlbersEqualArea}

304 and lies between B{C{lat1}} and B{C{lat2}} for the

305 2-parallel L{AlbersEqualArea2} and L{AlbersEqualArea4}

306 projections.

307 '''

308 return self._lat0

309

310 @Property_RO

311 def lat1(self):

312 '''Get the latitude of the first parallel (C{degrees}).

313 '''

314 return self._lat1

315

316 @Property_RO

317 def lat2(self):

318 '''Get the latitude of the second parallel (C{degrees}).

319

320 @note: The second and first parallel latitudes are the

321 same instance for 1-parallel C{AlbersEqualArea*}

322 projections.

323 '''

324 return self._lat2

325

326 @deprecated_Property_RO

327 def majoradius(self): # PYCHOK no cover

328 '''DEPRECATED, use property C{equatoradius}.'''

329 return self.equatoradius

330

331 def rescale0(self, lat, k=1): # PYCHOK no cover

332 '''Set the azimuthal scale for this projection.

333

334 @arg lat: Northern latitude (C{degrees}).

335 @arg k: Azimuthal scale at latitude B{C{lat}} (C{scalar}).

336

337 @raise AlbersError: Invalid B{C{lat}} or B{C{k}}.

338

339 @note: This allows a I{latitude of conformality} to be specified.

340 '''

341 k0 = _Ks(k=k) / self.forward(lat, _0_0).scale

342 if self._k0 != k0:

343 _update_all(self)

344 self._k0s(k0)

345

346 def reverse(self, x, y, lon0=0, LatLon=None, **name_LatLon_kwds):

347 '''Convert an east- and northing location to geodetic lat- and longitude.

348

349 @arg x: Easting of the location (C{meter}).

350 @arg y: Northing of the location (C{meter}).

351 @kwarg lon0: Optional central meridian longitude (C{degrees}).

352 @kwarg LatLon: Class to use (C{LatLon}) or C{None}.

353 @kwarg name_LatLon_kwds: Optional C{B{name}=NN} for the location

354 and optional, additional B{C{LatLon}} keyword

355 arguments, ignored if C{B{LatLon} is None}.

356

357 @return: The geodetic (C{LatLon}) or if C{B{LatLon} is None} an

358 L{Albers7Tuple}C{(x, y, lat, lon, gamma, scale, datum)}.

359

360 @note: The origin latitude is returned by C{property lat0}. No

361 false easting or northing is added. The returned value of

362 C{lon} is in the range C{[-180..180] degrees} and C{lat}

363 is in the range C{[-90..90] degrees}. If the given

364 B{C{x}} or B{C{y}} point is outside the valid projected

365 space the nearest pole is returned.

366 '''

367 k0 = self._k0

368 n0 = self._n0

369 k0n0 = self._k0n0

370 s = self._sign

371 txi = self._txi0

372

373 x = Meter(x=x)

374 nx = k0n0 * x

375 y = Meter(y=y)

376 y_ = s * y

377 ny = k0n0 * y_

378 t = nrho0 = self._nrho0

379 y1 = nrho0 - ny

380

381 den = hypot(nx, y1) + nrho0 # 0 implies origin with polar aspect

382 if den:

383 drho = _Fsum1f_(x * nx, y_ * nrho0 * _N_2_0, y_ * ny).fover(den / k0)

384 # dsxia = scxi0 * dsxi

385 t += drho * n0 # k0 below

386 d_ = (nrho0 + t) * drho * self._scxi0_ # / (qZ * E.a2)

387 t_ = txi - d_

388 d_ = (txi + t_) * d_ + _1_0

389 txi = t_ / (sqrt(d_) if d_ > EPS02 else EPS0)

390

391 ta = self._tanf(txi)

392 lat = atan1d(s * ta)

393

394 th = atan2(nx, y1)

395 lon = degrees((th / self._k02n0) if n0 else (x / (y1 * k0)))

396 if lon0:

397 lon += _norm180(lon0)

398 lon = _norm180(lon)

399

400 n, LatLon_kwds = _name2__(name_LatLon_kwds, _or_nameof=self)

401 if LatLon is None:

402 g = degrees360(s * th)

403 if den:

404 k0 = self._azik(t, ta)

405 r = Albers7Tuple(x, y, lat, lon, g, k0, self.datum, name=n)

406 else: # PYCHOK no cover

407 kwds = _xkwds(LatLon_kwds, datum=self.datum, name=n)

408 r = LatLon(lat, lon, **kwds)

409 return r

410

411 @Property_RO

412 def scale0(self):

413 '''Get the central scale for the projection (C{float}).

414

415 This is the azimuthal scale on the latitude of origin

416 of the projection, see C{property lat0}.

417 '''

418 return self._k0

419

420 def _ta0(self, s1_qZ, ta0, E):

421 '''(INTERNAL) Refine C{ta0} for C{._ta0C2}.

422 '''

423 e2 = E.e2

424 e21 = E.e21

425 e22 = E.e22 # == e2 / e21

426 tol = _tol(_TOL0, ta0)

427 _Ta02 = Fsum(ta0).fsum2f_

428 _1, _2 = _1_0, _2_0

429 _4, _6 = _4_0, _6_0

430 for self._iteration in range(1, _NUMIT0): # 4 trips

431 ta02 = ta0**2

432 sca02 = ta02 + _1

433 sca0 = sqrt(sca02)

434 sa0 = ta0 / sca0

435 sa01 = sa0 + _1

436 sa02 = sa0**2

437 # sa0m = 1 - sa0 = 1 / (sec(a0) * (tan(a0) + sec(a0)))

438 sa0m = _1 / (sca0 * (ta0 + sca0)) # scb0^2 * sa0

439 sa0m1 = sa0m / (_1 - e2 * sa0)

440 sa021 = _1 - e2 * sa02

441

442 g = (_1 + ta02 * e21) * sa0

443 dg = (_1 + ta02 * _2) * sca02 * e21 + e2

444 D = (_1 - (_1 + sa0 * _2 * sa01) * e2) * sa0m / (e21 * sa01) # dD/dsa0

445 dD = (_2 - (_6 + sa0 * _4) * sa02 * e2) / (e21 * sa01**2)

446 BA = (_atanh1(e2 * sa0m1**2) * e21 - e2 * sa0m) * sa0m1 \

447 - (_2 + (_1 + e2) * sa0) * sa0m**2 * e22 / sa021 # B + A

448 d = (_4 - (_1 + sa02) * e2 * _2) * e22 / (sa021**2 * sca02) # dAB

449 u = fsum1f_(s1_qZ * g, -D, g * BA)

450 du = fsum1f_(s1_qZ * dg, dD, dg * BA, g * d)

451 ta0, d = _Ta02(-u / du * (sca0 * sca02))

452 if fabs(d) < tol:

453 return ta0

454 raise AlbersError(Fmt.no_convergence(d, tol), txt=repr(self))

455

456 def _ta0C2(self, ca1, sa1, ta1, ca2, sa2, ta2):

457 '''(INTERNAL) Compute C{ta0} and C{C} for C{.__init__}.

458 '''

459 E = self.ellipsoid

460 f1, e2 = E.f1, E.e2

461 _1 = _1_0

462

463 tb1 = f1 * ta1

464 tb2 = f1 * ta2

465 dtb12 = f1 * (tb1 + tb2)

466 scb12 = _1 + tb1**2

467 scb22 = _1 + tb2**2

468

469 dsn_2 = _Dsn(ta2, ta1, sa2, sa1) * _0_5

470 sa12 = sa1 * sa2

471

472 esa1_2 = (_1 - e2 * sa1**2) \

473 * (_1 - e2 * sa2**2)

474 esa12 = _1 + e2 * sa12

475

476 axi, bxi, sxi = self._a_b_sxi3((ca1, sa1, ta1, scb12),

477 (ca2, sa2, ta2, scb22))

478

479 dsxi = ((esa12 / esa1_2) + _DatanheE(sa2, sa1, E)) * dsn_2 / self._qx

480 C = _Fsum1f_(sxi * dtb12 / dsxi, scb22, scb12).fover(scb22 * scb12 * _2_0)

481

482 S = _Fsum1f_(sa1, sa2, sa12)

483 axi *= (S * e2 + _1).fover(S + _1, raiser=False)

484 bxi *= _Fsum1f_(sa1, sa2, esa12).fover(esa1_2) * e2 + _D2atanheE(sa1, sa2, E) * E.e21

485 s1_qZ = (axi * self._qZ - bxi) * dsn_2 / dtb12

486 ta0 = self._ta0(s1_qZ, (ta1 + ta2) * _0_5, E)

487 return ta0, C

488

489 def _tanf(self, txi): # in .Ellipsoid.auxAuthalic

490 '''(INTERNAL) Function M{tan-phi from tan-xi}.

491 '''

492 tol = _tol(_TOL, txi)

493 e2 = self.ellipsoid.e2

494 qx = self._qx

495

496 ta = txi

497 _Ta2 = Fsum(ta).fsum2f_

498 _txif = self._txif

499 _1 = _1_0

500 for self._iteration in range(1, _NUMIT): # max 2, mean 1.99

501 # dtxi / dta = (scxi / sca)^3 * 2 * (1 - e^2)

502 # / (qZ * (1 - e^2 * sa^2)^2)

503 ta2 = ta**2

504 sca2 = _1 + ta2

505 txia = _txif(ta)

506 s3qx = sqrt3(sca2 / (txia**2 + _1)) * qx # * _1_x21(txia)

507 eta2 = (_1 - e2 * ta2 / sca2)**2

508 ta, d = _Ta2((txi - txia) * s3qx * eta2)

509 if fabs(d) < tol:

510 return ta

511 raise AlbersError(Fmt.no_convergence(d, tol), txt=repr(self))

512

513 def toRepr(self, prec=6, **unused): # PYCHOK expected

514 '''Return a string representation of this projection.

515

516 @kwarg prec: Number of (decimal) digits, unstripped (C{int}).

517

518 @return: This projection as C{"<classname>(lat1, lat2, ...)"}

519 (C{str}).

520 '''

521 t = self.toStr(prec=prec, sep=_COMMASPACE_)

522 return Fmt.PAREN(self.classname, t)

523

524 def toStr(self, prec=6, sep=_SPACE_, **unused): # PYCHOK expected

525 '''Return a string representation of this projection.

526

527 @kwarg prec: Number of (decimal) digits, unstripped (C{int}).

528 @kwarg sep: Separator to join (C{str}).

529

530 @return: This projection as C{"lat1 lat2"} (C{str}).

531 '''

532 k = self._k

533 t = (self.lat1, self.lat2, self._k0) if k is _k1_ else (

534 (self.lat1, self._k0) if k is _k0_ else

535 (self.lat1,))

536 t = strs(t, prec=prec)

537 if k:

538 t = t[:-1] + (Fmt.EQUAL(k, t[-1]),)

539 if self.datum != _WGS84:

540 t += Fmt.EQUAL(datum=self.datum),

541 if self.name:

542 t += Fmt.EQUAL(name=repr(self.name)),

543 return t if sep is None else sep.join(t)

544

545 def _txif(self, ta): # in .Ellipsoid.auxAuthalic

546 '''(INTERNAL) Function M{tan-xi from tan-phi}.

547 '''

548 E = self.ellipsoid

549 _1 = _1_0

550

551 ca2 = _1_x21(ta)

552 sa = sqrt(ca2) * fabs(ta) # enforce odd parity

553 sa1 = _1 + sa

554

555 es1 = sa * E.e2

556 es1m1 = sa1 * (_1 - es1)

557 es1p1 = sa1 / (_1 + es1)

558 es2m1 = _1 - sa * es1

559 es2m1a = es2m1 * E.e21 # e2m

560 s = sqrt((ca2 / (es1p1 * es2m1a) + _atanheE(ca2 / es1m1, E))

561 * (es1m1 / es2m1a + _atanheE(es1p1, E)))

562 t = _Fsum1f_(sa / es2m1, _atanheE(sa, E)).fover(s)

563 return neg(t) if ta < 0 else t

564

565

566 class AlbersEqualArea(_AlbersBase):

567 '''An Albers equal-area (authalic) projection with a single standard parallel.

568

569 @see: L{AlbersEqualArea2} and L{AlbersEqualArea4}.

570 '''

571 _k = _k0_

572

573 def __init__(self, lat, k0=1, datum=_WGS84, **name):

574 '''New L{AlbersEqualArea} projection.

575

576 @arg lat: Standard parallel (C{degrees}).

577 @kwarg k0: Azimuthal scale on the standard parallel (C{scalar}).

578 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},

579 L{Ellipsoid2} or L{a_f2Tuple}).

580 @kwarg name: Optional C{B{name}=NN} for the projection (C{str}).

581

582 @raise AlbersError: Invalid B{C{lat}}, B{C{k0}} or no convergence.

583 '''

584 self._lat1 = self._lat2 = lat = _Lat(lat1=lat)

585 args = tuple(sincos2d(lat)) * 2 + (_Ks(k0=k0), datum)

586 _AlbersBase.__init__(self, *args, **name)

587

588

589 class AlbersEqualArea2(_AlbersBase):

590 '''An Albers equal-area (authalic) projection with two standard parallels.

591

592 @see: L{AlbersEqualArea} and L{AlbersEqualArea4}.

593 '''

594 _k = _k1_

595

596 def __init__(self, lat1, lat2, k1=1, datum=_WGS84, **name):

597 '''New L{AlbersEqualArea2} projection.

598

599 @arg lat1: First standard parallel (C{degrees}).

600 @arg lat2: Second standard parallel (C{degrees}).

601 @kwarg k1: Azimuthal scale on the standard parallels (C{scalar}).

602 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},

603 L{Ellipsoid2} or L{a_f2Tuple}).

604 @kwarg name: Optional C{B{name}=NN} for the projection (C{str}).

605

606 @raise AlbersError: Invalid B{C{lat1}}m B{C{lat2}}, B{C{k1}}

607 or no convergence.

608 '''

609 self._lat1, self._lat2 = lats = _Lat(lat1=lat1), _Lat(lat2=lat2)

610 args = tuple(sincos2d_(*lats)) + (_Ks(k1=k1), datum)

611 _AlbersBase.__init__(self, *args, **name)

612

613

614 class AlbersEqualArea4(_AlbersBase):

615 '''An Albers equal-area (authalic) projection specified by the C{sin}

616 and C{cos} of both standard parallels.

617

618 @see: L{AlbersEqualArea} and L{AlbersEqualArea2}.

619 '''

620 _k = _k1_

621

622 def __init__(self, slat1, clat1, slat2, clat2, k1=1, datum=_WGS84, **name):

623 '''New L{AlbersEqualArea4} projection.

624

625 @arg slat1: Sine of first standard parallel (C{scalar}).

626 @arg clat1: Cosine of first standard parallel (non-negative C{scalar}).

627 @arg slat2: Sine of second standard parallel (C{scalar}).

628 @arg clat2: Cosine of second standard parallel (non-negative C{scalar}).

629 @kwarg k1: Azimuthal scale on the standard parallels (C{scalar}).

630 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},

631 L{Ellipsoid2} or L{a_f2Tuple}).

632 @kwarg name: Optional C{B{name}=NN} for the projection (C{str}).

633

634 @raise AlbersError: Negative B{C{clat1}} or B{C{clat2}}, B{C{slat1}}

635 and B{C{slat2}} have opposite signs (hemispheres),

636 invalid B{C{k1}} or no convergence.

637 '''

638 def _Lat_s_c3(n, s, c):

639 r = Float_(hypot(s, c), name=n, Error=AlbersError)

640 L = _Lat( atan1d(s, c), name=n)

641 return L, (s / r), (c / r)

642

643 self._lat1, sa1, ca1 = _Lat_s_c3(_lat1_, slat1, clat1)

644 self._lat2, sa2, ca2 = _Lat_s_c3(_lat2_, slat2, clat2)

645 _AlbersBase.__init__(self, sa1, ca1, sa2, ca2, _Ks(k1=k1), datum, **name)

646

647

648 class AlbersEqualAreaCylindrical(_AlbersBase):

649 '''An L{AlbersEqualArea} projection at C{lat=0} and C{k0=1} degenerating

650 to the cylindrical-equal-area projection.

651 '''

652 _lat1 = _lat2 = _Lat(lat1=_0_0)

653

654 def __init__(self, lat=_0_0, datum=_WGS84, **name):

655 '''New L{AlbersEqualAreaCylindrical} projection.

656

657 @kwarg lat: Standard parallel (C{0 degrees} I{fixed}).

658 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},

659 L{Ellipsoid2} or L{a_f2Tuple}).

660 @kwarg name: Optional C{B{name}=NN} for the projection (C{str}).

661 '''

662 _xlat(lat, _0_0, AlbersEqualAreaCylindrical)

663 _AlbersBase.__init__(self, _0_0, _1_0, _0_0, _1_0, 1, datum, **name)

664

665

666 class AlbersEqualAreaNorth(_AlbersBase):

667 '''An azimuthal L{AlbersEqualArea} projection at C{lat=90} and C{k0=1}

668 degenerating to the L{azimuthal} L{LambertEqualArea} projection.

669 '''

670 _lat1 = _lat2 = _Lat(lat1=_90_0)

671

672 def __init__(self, lat=_90_0, datum=_WGS84, **name):

673 '''New L{AlbersEqualAreaNorth} projection.

674

675 @kwarg lat: Standard parallel (C{90 degrees} I{fixed}).

676 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},

677 L{Ellipsoid2} or L{a_f2Tuple}).

678 @kwarg name: Optional C{B{name}=NN} for the projection (C{str}).

679 '''

680 _xlat(lat, _90_0, AlbersEqualAreaNorth)

681 _AlbersBase.__init__(self, _1_0, _0_0, _1_0, _0_0, 1, datum, **name)

682

683

684 class AlbersEqualAreaSouth(_AlbersBase):

685 '''An azimuthal L{AlbersEqualArea} projection at C{lat=-90} and C{k0=1}

686 degenerating to the L{azimuthal} L{LambertEqualArea} projection.

687 '''

688 _lat1 = _lat2 = _Lat(lat1=_N_90_0)

689

690 def __init__(self, lat=_N_90_0, datum=_WGS84, **name):

691 '''New L{AlbersEqualAreaSouth} projection.

692

693 @kwarg lat: Standard parallel (C{-90 degrees} I{fixed}).

694 @kwarg datum: Optional datum or ellipsoid (L{Datum}, L{Ellipsoid},

695 L{Ellipsoid2} or L{a_f2Tuple}).

696 @kwarg name: Optional C{B{name}=NN} for the projection (C{str}).

697 '''

698 _xlat(lat, _N_90_0, AlbersEqualAreaSouth)

699 _AlbersBase.__init__(self, _N_1_0, _0_0, _N_1_0, _0_0, 1, datum, **name)

700

701

702 class Albers7Tuple(_NamedTuple):

703 '''7-Tuple C{(x, y, lat, lon, gamma, scale, datum)}, in C{meter},

704 C{meter}, C{degrees90}, C{degrees180}, C{degrees360}, C{scalar} and

705 C{Datum} where C{(x, y)} is the projected, C{(lat, lon)} the geodetic

706 location, C{gamma} the meridian convergence at point, the bearing of

707 the y-axis measured clockwise from true North and C{scale} is the

708 azimuthal scale of the projection at point. The radial scale is

709 the reciprocal C{1 / scale}.

710 '''

711 _Names_ = (_x_, _y_, _lat_, _lon_, _gamma_, _scale_, _datum_)

712 _Units_ = ( Meter, Meter, Lat, Lon, Bearing, _Pass, _Pass)

713

714

715 def _atanh1(x):

716 '''(INTERNAL) Function M{atanh(sqrt(x)) / sqrt(x) - 1}.

717 '''

718 s = fabs(x)

719 if 0 < s < _0_5: # for typical ...

720 # x < E.e^2 == 2 * E.f use ...

721 # x / 3 + x^2 / 5 + x^3 / 7 + ...

722 y, k = x, 3

723 _S2 = Fsum(y / k).fsum2f_

724 for _ in range(_TERMS): # 9 terms

725 y *= x # x**n

726 k += 2 # 2*n + 1

727 s, d = _S2(y / k)

728 if not d:

729 break

730 elif s:

731 s = sqrt(s)

732 s = (atanh(s) if x > 0 else atan1(s)) / s - _1_0

733 return s

734

735

736 def _atanheE(x, E): # see Ellipsoid._es_atanh, .AuxLat._atanhee

737 '''(INTERNAL) Function M{atanhee(x)}, defined as ...

738 atanh( E.e * x) / E.e if f > 0 # oblate

739 atan (sqrt(-E.e2) * x) / sqrt(-E.e2) if f < 0 # prolate

740 x if f = 0.

741 '''

742 e = E.e # == sqrt(E.e2abs)

743 if e and x:

744 if E.f > 0: # .isOblate

745 x = atanh(x * e) / e

746 elif E.f < 0: # .isProlate

747 x = atan1(x * e) / e

748 return x

749

750

751 def _DatanheE(x, y, E): # see .rhumb.ekx._DeatanhE

752 '''(INTERNAL) Function M{Datanhee(x, y)}, defined as

753 M{atanhee((x - y) / (1 - E.e^2 * x * y)) / (x - y)}.

754 '''

755 e = _1_0 - E.e2 * x * y

756 if e:

757 d = x - y

758 e = (_atanheE(d / e, E) / d) if d else (_1_0 / e)

759 return e

760

761

762 def _D2atanheE(x, y, E):

763 '''(INTERNAL) Function M{D2atanhee(x, y)}, defined as

764 M{(Datanhee(1, y) - Datanhee(1, x)) / (y - x)}.

765 '''

766 s, e2 = _0_0, E.e2

767 if e2:

768 if ((fabs(x) + fabs(y)) * e2) < _0_5:

769 e = z = _1_0

770 k = 1

771 T = Fsum() # Taylor expansion

772 _T = T.Fsumf_

773 _C = Fsum().Fsum_

774 _S2 = Fsum().fsum2_

775 for _ in range(_TERMS): # 15 terms

776 T *= y; P = _T(z); z *= x # PYCHOK ;

777 T *= y; Q = _T(z); z *= x # PYCHOK ;

778 e *= e2

779 k += 2

780 s, d = _S2(_C(P, Q) * e / k)

781 if not d:

782 break

783 else: # PYCHOK no cover

784 s = _1_0 - x

785 if s:

786 s = (_DatanheE(_1_0, y, E) - _DatanheE(x, y, E)) / s

787 return s

788

789

790 def _Dsn(x, y, sx, sy):

791 '''(INTERNAL) Divided differences, defined as M{Df(x, y) = (f(x) - f(y)) / (x - y)}

792 with M{sn(x) = x / sqrt(1 + x^2)}: M{Dsn(x, y) = (x + y) / ((sn(x) + sn(y)) *

793 (1 + x^2) * (1 + y^2))}.

794

795 @see: U{W. M. Kahan and R. J. Fateman, "Sympbolic Computation of Divided

796 Differences"<https://People.EECS.Berkeley.EDU/~fateman/papers/divdiff.pdf>},

797 U{ACM SIGSAM Bulletin 33(2), 7-28 (1999)<https://DOI.org/10.1145/334714.334716>}

798 and U{AlbersEqualArea.hpp

799 <https://GeographicLib.SourceForge.io/C++/doc/AlbersEqualArea_8hpp_source.html>}.

800 '''

801 # sx = x / hypot1(x)

802 d, t = _1_0, (x * y)

803 if t > 0:

804 s = sx + sy

805 if s:

806 t = sx * sy / t

807 d = t**2 * (x + y) / s

808 elif x != y:

809 d = (sx - sy) / (x - y)

810 return d

811

812

813 def _tol(tol, x):

814 '''(INTERNAL) Converge tolerance.

815 '''

816 return tol * max(_1_0, fabs(x))

817

818

819 def _1_x21(x):

820 '''(INTERNAL) Return M{1 / (x**2 + 1)}.

821 '''

822 return _1_0 / (x**2 + _1_0)

823

824

825 def _xlat(lat, f, where):

826 '''(INTERNAL) check fixed C{lat}.

827 '''

828 if lat is not f and _Lat(lat=lat) != f:

829 t = unstr(where, lat=lat)

830 raise AlbersError(t, txt_not_=f)

831

832

833__all__ += _ALL_DOCS(_AlbersBase)

834

835# **) MIT License

836#

837# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.

838#

839# Permission is hereby granted, free of charge, to any person obtaining a

840# copy of this software and associated documentation files (the "Software"),

841# to deal in the Software without restriction, including without limitation

842# the rights to use, copy, modify, merge, publish, distribute, sublicense,

843# and/or sell copies of the Software, and to permit persons to whom the

844# Software is furnished to do so, subject to the following conditions:

845#

846# The above copyright notice and this permission notice shall be included

847# in all copies or substantial portions of the Software.

848#

849# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

850# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

851# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

852# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

853# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

854# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

855# OTHER DEALINGS IN THE SOFTWARE.