Coverage for pygeodesy/auxilats/auxAngle.py: 96%
224 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-03-25 15:01 -0400
« prev ^ index » next coverage.py v7.10.7, created at 2026-03-25 15:01 -0400
2# -*- coding: utf-8 -*-
4u'''(INTERNAL) I{Auxiliary} latitudes' base classes, constants and functions.
6Class L{AuxAngle} transcoded to Python from I{Karney}'s C++ class U{AuxAngle
7<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AuxAngle.html>}
8in I{GeographicLib version 2.2+}.
10Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2022-2024) and licensed
11under the MIT/X11 License. For more information, see the U{GeographicLib
12<https://GeographicLib.SourceForge.io>} documentation.
13'''
14# make sure int/int division yields float quotient, see .basics
15from __future__ import division as _; del _ # noqa: E702 ;
17from pygeodesy.auxilats.auxily import Aux, _Aux2Greek, AuxError
18from pygeodesy.basics import map1, map2, _xinstanceof
19from pygeodesy.constants import EPS, MAX, NAN, _0_0, _0_5, _1_0, _copysign_1_0, \
20 isfinite, isnan, _over, _pos_self
21# from pygeodesy.errors import AuxError # from .auxilats.auxily
22from pygeodesy.fmath import hypot, unstr
23from pygeodesy.fsums import _add_op_, _iadd_op_, _isub_op_, _sub_op_
24from pygeodesy.named import _Named, _ALL_DOCS, _MODS
25# from pygeodesy.lazily import _ALL_DOCS, _ALL_MODS as _MODS # from .named
26from pygeodesy.props import Property, Property_RO, property_RO, property_ROver, \
27 _update_all
28# from pygeodesy.streprs import unstr # from .fmath
29from pygeodesy.units import Degrees, Radians
30from pygeodesy.utily import atan2, atan2d, sincos2, sincos2d
32from math import asinh, copysign, degrees, fabs, radians, sinh
34__all__ = ()
35__version__ = '25.12.02'
37_MAX_2 = MAX * _0_5 # PYCHOK used!
38# del MAX
41class AuxAngle(_Named):
42 '''U{An accurate representation of angles
43 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AuxAngle.html>}
44 '''
45 _AUX = None # overloaded/-ridden
46 _diff = NAN # default
47 _iter = None # like ._NamedBase
48 _y = _0_0
49 _x = _1_0
51 def __init__(self, y_angle=_0_0, x=_1_0, aux=None, **name):
52 '''New L{AuxAngle}.
54 @kwarg y_angle: The Y component (C{scalar}, including C{INF}, C{NAN}
55 and C{NINF}) or a previous L{AuxAngle} instance.
56 @kwarg x: The X component, required if C{B{y_angle}} is C{scalar},
57 ignored otherwise.
58 @kwarg aux: I{Auxiliary} kind (C{Aux.KIND}), like B{C{x}}.
59 @kwarg name: Optional C{B{name}=NN} see (C{str}).
61 @raise AuxError: Invalid B{C{y_angle}}, B{C{x}} or B{C{aux}}.
62 '''
63 try:
64 try:
65 yx = y_angle._yx
66 aux = y_angle._AUX
67 if self._diff != y_angle._diff:
68 self._diff = y_angle._diff
69 except AttributeError:
70 yx = y_angle, x
71 if aux in _AUXClass:
72 if self._AUX != aux:
73 self._AUX = aux
74 elif aux is not None:
75 raise ValueError() # _invalid_
76 except Exception as X:
77 raise AuxError(y=y_angle, x=x, aux=aux, cause=X)
78 self._y, self._x = _yx2(yx)
79 if name:
80 self.name = name
82 def __abs__(self):
83 '''Return this angle's absolute value (L{AuxAngle}).
84 '''
85 a = self._copy_2(self.__abs__)
86 a._yx = map2(fabs, self._yx)
87 return a
89 def __add__(self, other):
90 '''Return C{B{self} + B{other}} as an L{AuxAngle}.
92 @arg other: An L{AuxAngle}.
94 @return: The sum (L{AuxAngle}).
96 @raise TypeError: Invalid B{C{other}}.
97 '''
98 a = self._copy_2(self.__add__)
99 return a._iadd(other, _add_op_)
101 def __bool__(self): # PYCHOK not special in Python 2-
102 '''Return C{True} if this angle is non-zero.
103 '''
104 return bool(self.tan)
106 def __eq__(self, other):
107 '''Return C{B{self} == B{other}} as C{bool}.
108 '''
109 return not self.__ne__(other)
111 def __float__(self):
112 '''Return this angle's C{tan} (C{float}).
113 '''
114 return self.tan
116 def __iadd__(self, other):
117 '''Apply C{B{self} += B{other}} to this angle.
119 @arg other: An L{AuxAngle}.
121 @return: This angle, updated (L{AuxAngle}).
123 @raise TypeError: Invalid B{C{other}}.
124 '''
125 return self._iadd(other, _iadd_op_)
127 def __isub__(self, other):
128 '''Apply C{B{self} -= B{other}} to this angle.
130 @arg other: An L{AuxAngle}.
132 @return: This instance, updated (L{AuxAngle}).
134 @raise TypeError: Invalid B{C{other}} type.
135 '''
136 return self._iadd(-other, _isub_op_)
138 def __ne__(self, other):
139 '''Return C{B{self} != B{other}} as C{bool}.
140 '''
141 _xinstanceof(AuxAngle, other=other)
142 y, x, r = self._yxr_normalized()
143 s, c, t = other._yxr_normalized()
144 return fabs(y - s) > EPS or fabs(x - c) > EPS \
145 or fabs(r - t) > EPS
147 def __neg__(self):
148 '''Return I{a copy of} this angle, negated.
149 '''
150 a = self._copy_2(self.__neg__)
151 if a.y or not a.x:
152 a.y = -a.y
153 else:
154 a.x = -a.x
155 return a
157 def __pos__(self):
158 '''Return this angle I{as-is}, like C{float.__pos__()}.
159 '''
160 return self if _pos_self else self._copy_2(self.__pos__)
162 def __radd__(self, other):
163 '''Return C{B{other} + B{self}} as an L{AuxAngle}.
165 @see: Method L{AuxAngle.__add__}.
166 '''
167 a = self._copy_r2(other, self.__radd__)
168 return a._iadd(self, _add_op_)
170 def __rsub__(self, other):
171 '''Return C{B{other} - B{self}} as an L{AuxAngle}.
173 @see: Method L{AuxAngle.__sub__}.
174 '''
175 a = self._copy_r2(other, self.__rsub__)
176 return a._iadd(-self, _sub_op_)
178 def __str__(self):
179 n = _Aux2Greek.get(self._AUX, self.classname)
180 return unstr(n, y=self.y, x=self.x, tan=self.tan)
182 def __sub__(self, other):
183 '''Return C{B{self} - B{other}} as an L{AuxAngle}.
185 @arg other: An L{AuxAngle}.
187 @return: The difference (L{AuxAngle}).
189 @raise TypeError: Invalid B{C{other}} type.
190 '''
191 a = self._copy_2(self.__sub__)
192 return a._iadd(-other, _sub_op_)
194 def _iadd(self, other, *unused): # op
195 '''(INTERNAL) Apply C{B{self} += B{other}}.
196 '''
197 _xinstanceof(AuxAngle, other=other)
198 # ignore zero other to preserve signs of _y and _x
199 if other.tan:
200 s, c = other._yx
201 y, x = self._yx
202 self._yx = (y * c + x * s), \
203 (x * c - y * s)
204 return self
206 def _copy_2(self, which):
207 '''(INTERNAL) Copy for I{dyadic} operators.
208 '''
209 return _Named.copy(self, deep=False, name__=which)
211 def _copy_r2(self, other, which):
212 '''(INTERNAL) Copy for I{reverse-dyadic} operators.
213 '''
214 _xinstanceof(AuxAngle, other=other)
215 return other._copy_2(which)
217 def copyquadrant(self, other):
218 '''Copy an B{C{other}} angle's quadrant into this angle (L{auxAngle}).
219 '''
220 _xinstanceof(AuxAngle, other=other)
221 self._yx = copysign(self.y, other.y), \
222 copysign(self.x, other.x)
223 return self
225 @Property_RO
226 def diff(self):
227 '''Get derivative C{dtan(Eta) / dtan(Phi)} (C{float} or C{NAN}).
228 '''
229 return self._diff
231 @staticmethod
232 def fromDegrees(deg, **aux_name):
233 '''Get an L{AuxAngle} from degrees.
234 '''
235 return _AuxClass(**aux_name)(*sincos2d(deg), **aux_name)
237 @staticmethod
238 def fromLambertianDegrees(psi, **aux_name):
239 '''Get an L{AuxAngle} from I{Lambertian} degrees.
240 '''
241 return _AuxClass(**aux_name)(sinh(radians(psi)), **aux_name)
243 @staticmethod
244 def fromLambertianRadians(psi, **aux_name):
245 '''Get an L{AuxAngle} from I{Lambertian} radians.
246 '''
247 return _AuxClass(**aux_name)(sinh(psi), **aux_name)
249 @staticmethod
250 def fromRadians(rad, **aux_name):
251 '''Get an L{AuxAngle} from radians.
252 '''
253 return _AuxClass(**aux_name)(*sincos2(rad), **aux_name)
255 @Property_RO
256 def iteration(self):
257 '''Get the iteration (C{int} or C{None}).
258 '''
259 return self._iter
261 def normal(self):
262 '''Normalize this angle I{in-place}.
264 @return: This angle, normalized (L{AuxAngle}).
265 '''
266 self._yx = self._yx_normalized
267 return self
269 @Property_RO
270 def normalized(self):
271 '''Get a normalized copy of this angle (L{AuxAngle}).
272 '''
273 y, x = self._yx_normalized
274 return self.classof(y, x, name=self.name, aux=self._AUX)
276 @property_ROver
277 def _RhumbAux(self):
278 '''(INTERNAL) Import the L{RhumbAux} class, I{once}.
279 '''
280 return _MODS.rhumb.aux_.RhumbAux # overwrite property_ROver
282 @Property_RO
283 def tan(self):
284 '''Get this angle's C{tan} (C{float}).
285 '''
286 y, x = self._yx
287 return _over(y, x) if isfinite(y) and y else y
289 def toBeta(self, rhumb):
290 '''Short for C{rhumb.auxDLat.convert(Aux.BETA, self, exact=rhumb.exact)}
291 '''
292 return self._toRhumbAux(rhumb, Aux.BETA)
294 def toChi(self, rhumb):
295 '''Short for C{rhumb.auxDLat.convert(Aux.CHI, self, exact=rhumb.exact)}
296 '''
297 return self._toRhumbAux(rhumb, Aux.CHI)
299 @Property_RO
300 def toDegrees(self):
301 '''Get this angle as L{Degrees}.
302 '''
303 return Degrees(atan2d(*self._yx), name=self.name)
305 @Property_RO
306 def toLambertianDegrees(self): # PYCHOK no cover
307 '''Get this angle's I{Lambertian} in L{Degrees}.
308 '''
309 r = self.toLambertianRadians
310 return Degrees(degrees(r), name=r.name)
312 @Property_RO
313 def toLambertianRadians(self):
314 '''Get this angle's I{Lambertian} in L{Radians}.
315 '''
316 return Radians(asinh(self.tan), name=self.name)
318 def toMu(self, rhumb):
319 '''Short for C{rhumb.auxDLat.convert(Aux.MU, self, exact=rhumb.exact)}
320 '''
321 return self._toRhumbAux(rhumb, Aux.MU)
323 def toPhi(self, rhumb):
324 '''Short for C{rhumb.auxDLat.convert(Aux.PHI, self, exact=rhumb.exact)}
325 '''
326 return self._toRhumbAux(rhumb, Aux.PHI)
328 @Property_RO
329 def toRadians(self):
330 '''Get this angle as L{Radians}.
331 '''
332 return Radians(atan2(*self._yx), name=self.name)
334 def _toRhumbAux(self, rhumb, aux):
335 '''(INTERNAL) Create an C{aux}-KIND angle from this angle.
336 '''
337 _xinstanceof(self._RhumbAux, rhumb=rhumb)
338 return rhumb._auxD.convert(aux, self, exact=rhumb.exact)
340 @Property
341 def x(self):
342 '''Get this angle's C{x} (C{float}).
343 '''
344 return self._x
346 @x.setter # PYCHOK setter!
347 def x(self, x): # PYCHOK no cover
348 '''Set this angle's C{x} (C{float}).
349 '''
350 x = float(x)
351 if self._x != x:
352 _update_all(self)
353 self._x = x
355 @property_RO
356 def _x_normalized(self):
357 '''(INTERNAL) Get the I{normalized} C{x}.
358 '''
359 _, x = self._yx_normalized
360 return x
362 @Property
363 def y(self):
364 '''Get this angle's C{y} (C{float}).
365 '''
366 return self._y
368 @y.setter # PYCHOK setter!
369 def y(self, y): # PYCHOK no cover
370 '''Set this angle's C{y} (C{float}).
371 '''
372 y = float(y)
373 if self.y != y:
374 _update_all(self)
375 self._y = y
377 @Property
378 def _yx(self):
379 '''(INTERNAL) Get this angle as 2-tuple C{(y, x)}.
380 '''
381 return self._y, self._x
383 @_yx.setter # PYCHOK setter!
384 def _yx(self, yx):
385 '''(INTERNAL) Set this angle's C{y} and C{x}.
386 '''
387 yx = _yx2(yx)
388 if self._yx != yx:
389 _update_all(self)
390 self._y, self._x = yx
392 @Property_RO
393 def _yx_normalized(self):
394 '''(INTERNAL) Get this angle as 2-tuple C{(y, x)}, I{normalized}.
395 '''
396 y, x = self._yx
397 if isfinite(y) and fabs(y) < _MAX_2 \
398 and fabs(x) < _MAX_2 \
399 and isfinite(self.tan):
400 h = hypot(y, x)
401 if h > 0 and y:
402 y = y / h # /= chokes PyChecker
403 x = x / h
404 if isnan(y): # PYCHOK no cover
405 y = _copysign_1_0(self.y)
406 if isnan(x): # PYCHOK no cover
407 x = _copysign_1_0(self.x)
408 else: # scalar 0
409 y, x = _0_0, _copysign_1_0(y * x)
410 else: # scalar NAN
411 y, x = NAN, _copysign_1_0(y * x)
412 return y, x
414 def _yxr_normalized(self, abs_y=False):
415 '''(INTERNAL) Get 3-tuple C{(y, x, r)}, I{normalized}
416 with C{y} or C{abs(y)} and C{r} as C{.toRadians}.
417 '''
418 y, x = self._yx_normalized
419 if abs_y:
420 y = fabs(y) # only y, not x
421 return y, x, atan2(y, x) # .toRadians
424class AuxBeta(AuxAngle):
425 '''A I{Parametric, Auxiliary} latitude.
426 '''
427 _AUX = Aux.BETA
429 @staticmethod
430 def fromDegrees(deg, **name):
431 '''Get an L{AuxBeta} from degrees.
432 '''
433 return AuxBeta(*sincos2d(deg), **name)
435 @staticmethod
436 def fromRadians(rad, **name):
437 '''Get an L{AuxBeta} from radians.
438 '''
439 return AuxBeta(*sincos2(rad), **name)
442class AuxChi(AuxAngle):
443 '''A I{Conformal, Auxiliary} latitude.
444 '''
445 _AUX = Aux.CHI
447 @staticmethod
448 def fromDegrees(deg, **name):
449 '''Get an L{AuxChi} from degrees.
450 '''
451 return AuxChi(*sincos2d(deg), **name)
454class AuxMu(AuxAngle):
455 '''A I{Rectifying, Auxiliary} latitude.
456 '''
457 _AUX = Aux.MU
459 @staticmethod
460 def fromDegrees(deg, **name):
461 '''Get an L{AuxMu} from degrees.
462 '''
463 return AuxMu(*sincos2d(deg), **name)
466class AuxPhi(AuxAngle):
467 '''A I{Geodetic or Geographic, Auxiliary} latitude.
468 '''
469 _AUX = Aux.PHI
470 _diff = _1_0 # see .auxLat._Newton
472 @staticmethod
473 def fromDegrees(deg, **name):
474 '''Get an L{AuxPhi} from degrees.
475 '''
476 return AuxPhi(*sincos2d(deg), **name)
479class AuxTheta(AuxAngle):
480 '''A I{Geocentric, Auxiliary} latitude.
481 '''
482 _AUX = Aux.THETA
484 @staticmethod
485 def fromDegrees(deg, **name):
486 '''Get an L{AuxTheta} from degrees.
487 '''
488 return AuxTheta(*sincos2d(deg), **name)
491class AuxXi(AuxAngle):
492 '''An I{Authalic, Auxiliary} latitude.
493 '''
494 _AUX = Aux.XI
496 @staticmethod
497 def fromDegrees(deg, **name):
498 '''Get an L{AuxXi} from degrees.
499 '''
500 return AuxXi(*sincos2d(deg), **name)
503_AUXClass = {Aux.BETA: AuxBeta,
504 Aux.CHI: AuxChi,
505 Aux.MU: AuxMu,
506 Aux.PHI: AuxPhi,
507# Aux.PSI: AuxPSI, # Isometric
508 Aux.THETA: AuxTheta,
509 Aux.XI: AuxXi}
511def _AuxClass(aux=None, **unused): # PYCHOK C{classof(aux)}
512 return _AUXClass.get(aux, AuxAngle)
515def _yx2(yx):
516 '''(INTERNAL) Validate 2-tuple C{(y, x)}.
517 '''
518 try:
519 y, x = yx
520 y, x = map1(float, y, x)
521 if not (y and isfinite(y)):
522 x = _copysign_1_0(x)
523 except (TypeError, ValueError) as X:
524 raise AuxError(y=y, x=x, cause=X)
525 return y, x
528__all__ += _ALL_DOCS(AuxAngle, *_AUXClass.values())
530# **) MIT License
531#
532# Copyright (C) 2023-2026 -- mrJean1 at Gmail -- All Rights Reserved.
533#
534# Permission is hereby granted, free of charge, to any person obtaining a
535# copy of this software and associated documentation files (the "Software"),
536# to deal in the Software without restriction, including without limitation
537# the rights to use, copy, modify, merge, publish, distribute, sublicense,
538# and/or sell copies of the Software, and to permit persons to whom the
539# Software is furnished to do so, subject to the following conditions:
540#
541# The above copyright notice and this permission notice shall be included
542# in all copies or substantial portions of the Software.
543#
544# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
545# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
546# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
547# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
548# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
549# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
550# OTHER DEALINGS IN THE SOFTWARE.