Coverage for pygeodesy/ltp.py: 96%
419 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-20 14:00 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-20 14:00 -0400
2# -*- coding: utf-8 -*-
4u'''I{Local Tangent Plane} (LTP) and I{local} cartesian coordinates.
6I{Local cartesian} and I{local tangent plane} classes L{LocalCartesian}, approximations L{ChLVa}
7and L{ChLVe} and L{Ltp}, L{ChLV}, L{LocalError}, L{Attitude} and L{Frustum}.
9@see: U{Local tangent plane coordinates<https://WikiPedia.org/wiki/Local_tangent_plane_coordinates>}
10 and class L{LocalCartesian}, transcoded from I{Charles Karney}'s C++ classU{LocalCartesian
11 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1LocalCartesian.html>}.
12'''
13# make sure int/int division yields float quotient, see .basics
14from __future__ import division as _; del _ # PYCHOK semicolon
16from pygeodesy.basics import isscalar, issubclassof, map1, _xargs_names
17from pygeodesy.constants import EPS, INT0, _umod_360, _0_0, _0_01, _0_5, _1_0, \
18 _2_0, _60_0, _90_0, _100_0, _180_0, _3600_0, \
19 _N_1_0 # PYCHOK used!
20from pygeodesy.datums import _WGS84, _xinstanceof
21from pygeodesy.ecef import _EcefBase, EcefKarney, _llhn4, _xyzn4
22from pygeodesy.errors import _NotImplementedError, _TypesError, _ValueError, \
23 _xattr, _xkwds, _xkwds_get
24from pygeodesy.fmath import fabs, fdot, Fhorner
25from pygeodesy.fsums import _floor, Fsum, fsumf_, fsum1f_
26from pygeodesy.interns import NN, _0_, _COMMASPACE_, _DOT_, _ecef_, _height_, \
27 _invalid_, _lat0_, _lon0_, _ltp_, _M_, _name_, _too_
28# from pygeodesy.lazily import _ALL_LAZY # from vector3d
29from pygeodesy.ltpTuples import Attitude4Tuple, ChLVEN2Tuple, ChLV9Tuple, \
30 ChLVYX2Tuple, Footprint5Tuple, Local9Tuple, \
31 ChLVyx2Tuple, _XyzLocals4, _XyzLocals5, Xyz4Tuple
32from pygeodesy.named import _NamedBase, notOverloaded
33from pygeodesy.namedTuples import LatLon3Tuple, LatLon4Tuple, Vector3Tuple
34from pygeodesy.props import Property, Property_RO, property_doc_, property_RO, \
35 _update_all
36from pygeodesy.streprs import Fmt, strs, unstr
37from pygeodesy.units import Bearing, Degrees, Meter
38from pygeodesy.utily import cotd, sincos2d, sincos2d_, tand, tand_, wrap180, wrap360
39from pygeodesy.vector3d import _ALL_LAZY, Vector3d
41# from math import fabs, floor as _floor # from .fmath, .fsums
43__all__ = _ALL_LAZY.ltp
44__version__ = '23.08.06'
46_height0_ = _height_ + _0_
47_narrow_ = 'narrow'
48_wide_ = 'wide'
49_Xyz_ = 'Xyz'
52def _fov_2(**fov):
53 # Half a field-of-view angle in C{degrees}.
54 f = Degrees(Error=LocalError, **fov) * _0_5
55 if EPS < f < _90_0:
56 return f
57 t = _invalid_ if f < 0 else _too_(_wide_ if f > EPS else _narrow_)
58 raise LocalError(txt=t, **fov)
61class Attitude(_NamedBase):
62 '''The orientation of a plane or camera in space.
63 '''
64 _alt = Meter( alt =_0_0)
65 _roll = Degrees(roll=_0_0)
66 _tilt = Degrees(tilt=_0_0)
67 _yaw = Bearing(yaw =_0_0)
69 def __init__(self, alt_attitude=INT0, tilt=INT0, yaw=INT0, roll=INT0, name=NN):
70 '''New L{Attitude}.
72 @kwarg alt_attitude: An altitude (C{meter}) above earth or an attitude
73 (L{Attitude} or L{Attitude4Tuple}) with the
74 C{B{alt}itude}, B{C{tilt}}, B{C{yaw}} and B{C{roll}}.
75 @kwarg tilt: Pitch, elevation from horizontal (C{degrees180}), negative down
76 (clockwise rotation along and around the x- or East axis).
77 @kwarg yaw: Bearing, heading (compass C{degrees360}), clockwise from North
78 (counter-clockwise rotation along and around the z- or Up axis).
79 @kwarg roll: Roll, bank (C{degrees180}), positive to the right and down
80 (clockwise rotation along and around the y- or North axis).
81 @kwarg name: Optional name C{str}).
83 @raise AttitudeError: Invalid B{C{alt_attitude}}, B{C{tilt}}, B{C{yaw}} or
84 B{C{roll}}.
86 @see: U{Principal axes<https://WikiPedia.org/wiki/Aircraft_principal_axes>} and
87 U{Yaw, pitch, and roll rotations<http://MSL.CS.UIUC.edu/planning/node102.html>}.
88 '''
89 if isscalar(alt_attitude):
90 t = Attitude4Tuple(alt_attitude, tilt, yaw, roll)
91 else:
92 try:
93 t = alt_attitude.atyr
94 except AttributeError:
95 raise AttitudeError(alt=alt_attitude, tilt=tilt, yaw=yaw, rol=roll)
96 for n, v in t.items():
97 if v:
98 setattr(self, n, v)
99 n = name or t.name
100 if n:
101 self.name = n
103 @property_doc_(' altitude above earth in C{meter}.')
104 def alt(self):
105 return self._alt
107 @alt.setter # PYCHOK setter!
108 def alt(self, alt): # PYCHOK no cover
109 a = Meter(alt=alt, Error=AttitudeError)
110 if self._alt != a:
111 _update_all(self)
112 self._alt = a
114 altitude = alt
116 @Property_RO
117 def atyr(self):
118 '''Return this attitude's alt[itude], tilt, yaw and roll as an L{Attitude4Tuple}.
119 '''
120 return Attitude4Tuple(self.alt, self.tilt, self.yaw, self.roll, name=self.name)
122 @Property_RO
123 def matrix(self):
124 '''Get the 3x3 rotation matrix C{R(yaw)·R(tilt)·R(roll)}, aka I{ZYX} (C{float}, row-order).
126 @see: The matrix M of case 10 in U{Appendix A
127 <https://ntrs.NASA.gov/api/citations/19770019231/downloads/19770019231.pdf>}.
128 '''
129 def _5to3(x, y, _y, z, _z):
130 return x, fsum1f_(y, _y), fsum1f_(z, _z)
132 r0, r1, r2 = self._rows3
133 return _5to3(*r0), _5to3(*r1), r2
135 @property_doc_(' roll/bank in C{degrees180}, positive to the right and down.')
136 def roll(self):
137 return self._roll
139 @roll.setter # PYCHOK setter!
140 def roll(self, roll):
141 r = Degrees(roll=roll, wrap=wrap180, Error=AttitudeError)
142 if self._roll != r:
143 _update_all(self)
144 self._roll = r
146 bank = roll
148 @Property_RO
149 def _rows3(self):
150 # to follow the definitions of rotation angles alpha, beta and gamma:
151 # negate yaw since yaw is counter-clockwise around the z-axis, swap
152 # tilt and roll since tilt is around the x- and roll around the y-axis
153 sa, ca, sb, cb, sg, cg = sincos2d_(-self.yaw, self.roll, self.tilt)
154 return ((ca * cb, ca * sb * sg, -sa * cg, ca * sb * cg, sa * sg),
155 (sa * cb, sa * sb * sg, ca * cg, sa * sb * cg, -ca * sg),
156 ( -sb, cb * sg, cb * cg))
158 def rotate(self, x_xyz, y=None, z=None, Vector=None, **Vector_kwds):
159 '''Transform a (local) cartesian by this attitude's matrix.
161 @arg x_xyz: X component of vector (C{scalar}) or (3-D) vector
162 (C{Cartesian}, L{Vector3d} or L{Vector3Tuple}).
163 @kwarg y: Y component of vector (C{scalar}), same units as B{C{x}}.
164 @kwarg z: Z component of vector (C{scalar}), same units as B{C{x}}.
165 @kwarg Vector: Class to return transformed point (C{Cartesian},
166 L{Vector3d} or C{Vector3Tuple}) or C{None}.
167 @kwarg Vector_kwds: Optional, additional B{C{Vector}} keyword arguments,
168 ignored if C{B{Vector} is None}.
170 @return: A B{C{Vector}} instance or a L{Vector3Tuple}C{(x, y, z)} if
171 C{B{Vector}=None}.
173 @raise AttitudeError: Invalid B{C{x_xyz}}, B{C{y}} or B{C{z}}.
175 @see: U{Yaw, pitch, and roll rotations<http://MSL.CS.UIUC.edu/planning/node102.html>}.
176 '''
177 try:
178 try:
179 x, y, z = map( float, x_xyz.xyz)
180 except AttributeError:
181 x, y, z = map1(float, x_xyz, y, z)
182 except (TypeError, ValueError) as x:
183 raise AttitudeError(x_xyz=x_xyz, y=y, z=z, cause=x)
185 X, Y, Z = self._rows3
186 X = fdot(X, x, y, y, z, z)
187 Y = fdot(Y, x, y, y, z, z)
188 Z = fdot(Z, x, y, z)
189 return Vector3Tuple(X, Y, Z, name=self.name) if Vector is None else \
190 Vector(X, Y, Z, **_xkwds(Vector_kwds, name=self.name))
192 @property_doc_(' tilt/pitch/elevation from horizontal in C{degrees180}, negative down.')
193 def tilt(self):
194 return self._tilt
196 @tilt.setter # PYCHOK setter!
197 def tilt(self, tilt):
198 t = Degrees(tilt=tilt, wrap=wrap180, Error=AttitudeError)
199 if self._tilt != t:
200 _update_all(self)
201 self._tilt = t
203 elevation = pitch = tilt
205 def toStr(self, prec=6, sep=_COMMASPACE_, **unused): # PYCHOK signature
206 '''Format this attitude as string.
208 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
209 Trailing zero decimals are stripped for B{C{prec}} values
210 of 1 and above, but kept for negative B{C{prec}} values.
211 @kwarg sep: Separator to join (C{str}).
213 @return: This attitude (C{str}).
214 '''
215 return self.atyr.toStr(prec=prec, sep=sep)
217 @Property_RO
218 def tyr3d(self):
219 '''Get this attitude's (3-D) directional vector (L{Vector3d}).
221 @see: U{Yaw, pitch, and roll rotations<http://MSL.CS.UIUC.edu/planning/node102.html>}.
222 '''
223 def _r2d(r):
224 return fsumf_(_N_1_0, *r)
226 return Vector3d(*map(_r2d, self._rows3), name=tyr3d.__name__)
228 @property_doc_(' yaw/bearing/heading in compass C{degrees360}, clockwise from North.')
229 def yaw(self):
230 return self._yaw
232 @yaw.setter # PYCHOK setter!
233 def yaw(self, yaw):
234 y = Bearing(yaw=yaw, Error=AttitudeError)
235 if self._yaw != y:
236 _update_all(self)
237 self._yaw = y
239 bearing = heading = yaw
242class AttitudeError(_ValueError):
243 '''An L{Attitude} or L{Attitude4Tuple} issue.
244 '''
245 pass
248class Frustum(_NamedBase):
249 '''A rectangular pyramid, typically representing a camera's I{field-of-view}
250 (fov) and the intersection with (or projection to) a I{local tangent plane}.
252 @see: U{Viewing frustum<https://WikiPedia.org/wiki/Viewing_frustum>}.
253 '''
254 _h_2 = _0_0 # half hfov in degrees
255 _ltp = None # local tangent plane
256 _tan_h_2 = _0_0 # tan(_h_2)
257 _v_2 = _0_0 # half vfov in degrees
259 def __init__(self, hfov, vfov, ltp=None):
260 '''New L{Frustum}.
262 @arg hfov: Horizontal field-of-view (C{degrees180}).
263 @arg vfov: Vertical field-of-view (C{degrees180}).
264 @kwarg ltp: Optional I{local tangent plane} (L{Ltp}).
266 @raise LocalError: Invalid B{C{hfov}} or B{C{vfov}}.
267 '''
268 self._h_2 = h = _fov_2(hfov=hfov)
269 self._v_2 = _fov_2(vfov=vfov)
271 self._tan_h_2 = tand(h, fov_2=h)
273 if ltp:
274 self._ltp = _xLtp(ltp)
276 def footprint5(self, alt_attitude, tilt=0, yaw=0, roll=0, z=_0_0, ltp=None): # MCCABE 15
277 '''Compute the center and corners of the intersection with (or projection
278 to) the I{local tangent plane} (LTP).
280 @arg alt_attitude: An altitude (C{meter}) above I{local tangent plane} or
281 an attitude (L{Attitude} or L{Attitude4Tuple}) with the
282 C{B{alt}itude}, B{C{tilt}}, B{C{yaw}} and B{C{roll}}.
283 @kwarg tilt: Pitch, elevation from horizontal (C{degrees}), negative down
284 (clockwise rotation along and around the x- or East axis).
285 @kwarg yaw: Bearing, heading (compass C{degrees}), clockwise from North
286 (counter-clockwise rotation along and around the z- or Up axis).
287 @kwarg roll: Roll, bank (C{degrees}), positive to the right and down
288 (clockwise rotation along and around the y- or North axis).
289 @kwarg z: Optional height of the footprint (C{meter}) above I{local tangent plane}.
290 @kwarg ltp: The I{local tangent plane} (L{Ltp}), overriding this
291 frustum's C{ltp}.
293 @return: A L{Footprint5Tuple}C{(center, upperleft, upperight, loweright,
294 lowerleft)} with the C{center} and 4 corners, each an L{Xyz4Tuple}.
296 @raise TypeError: Invalid B{C{ltp}}.
298 @raise UnitError: Invalid B{C{altitude}}, B{C{tilt}}, B{C{roll}} or B{C{z}}.
300 @raise ValueError: If B{C{altitude}} too low, B{C{z}} too high or B{C{tilt}}
301 or B{C{roll}} -including B{C{vfov}} respectively B{C{hfov}}-
302 over the horizon.
304 @see: U{Principal axes<https://WikiPedia.org/wiki/Aircraft_principal_axes>}.
305 '''
306 def _xy2(a, e, h_2, tan_h_2, r):
307 # left and right corners, or swapped
308 if r < EPS: # no roll
309 r = a * tan_h_2
310 l = -r # PYCHOK l is ell
311 else: # roll
312 r, l = tand_(r - h_2, r + h_2, roll_hfov=r) # PYCHOK l is ell
313 r *= -a # negate right positive
314 l *= -a # PYCHOK l is ell
315 y = a * cotd(e, tilt_vfov=e)
316 return (l, y), (r, y)
318 def _xyz5(b, xy5, z, ltp):
319 # rotate (x, y)'s by bearing, clockwise
320 s, c = sincos2d(b)
321 for x, y in xy5:
322 yield Xyz4Tuple(fsum1f_(x * c, y * s),
323 fsum1f_(y * c, -x * s), z, ltp)
325 try:
326 a, t, y, r = alt_attitude.atyr
327 except AttributeError:
328 a, t, y, r = alt_attitude, tilt, yaw, roll
330 a = Meter(altitude=a)
331 if a < EPS: # too low
332 raise _ValueError(altitude=a)
333 if z: # PYCHOK no cover
334 z = Meter(z=z)
335 a -= z
336 if a < EPS: # z above a
337 raise _ValueError(altitude_z=a)
338 else:
339 z = _0_0
341 b = Degrees(yaw=y, wrap=wrap360) # bearing
342 e = -Degrees(tilt=t, wrap=wrap180) # elevation, pitch
343 if not EPS < e < _180_0:
344 raise _ValueError(tilt=t)
345 if e > _90_0:
346 e = _180_0 - e
347 b = _umod_360(b + _180_0)
349 r = Degrees(roll=r, wrap=wrap180) # roll center
350 x = (-a * tand(r, roll=r)) if r else _0_0
351 y = a * cotd(e, tilt=t) # ground range
352 if fabs(y) < EPS:
353 y = _0_0
355 # center and corners, clockwise from upperleft, rolled
356 xy5 = ((x, y),) + _xy2(a, e - self._v_2, self._h_2, self._tan_h_2, r) \
357 + _xy2(a, e + self._v_2, -self._h_2, -self._tan_h_2, r) # swapped
358 # turn center and corners by yaw, clockwise
359 p = self.ltp if ltp is None else ltp # None OK
360 return Footprint5Tuple(_xyz5(b, xy5, z, p)) # *_xyz5
362 @Property_RO
363 def hfov(self):
364 '''Get the horizontal C{fov} (C{degrees}).
365 '''
366 return Degrees(hfov=self._h_2 * _2_0)
368 @Property_RO
369 def ltp(self):
370 '''Get the I{local tangent plane} (L{Ltp}) or C{None}.
371 '''
372 return self._ltp
374 def toStr(self, prec=3, fmt=Fmt.F, sep=_COMMASPACE_): # PYCHOK signature
375 '''Convert this frustum to a "hfov, vfov, ltp" string.
377 @kwarg prec: Number of (decimal) digits, unstripped (0..8 or C{None}).
378 @kwarg fmt: Optional, C{float} format (C{str}).
379 @kwarg sep: Separator to join (C{str}).
381 @return: Frustum in the specified form (C{str}).
382 '''
383 t = self.hfov, self.vfov
384 if self.ltp:
385 t += self.ltp,
386 t = strs(t, prec=prec, fmt=fmt)
387 return sep.join(t) if sep else t
389 @Property_RO
390 def vfov(self):
391 '''Get the vertical C{fov} (C{degrees}).
392 '''
393 return Degrees(vfov=self._v_2 * _2_0)
396class LocalError(_ValueError):
397 '''A L{LocalCartesian} or L{Ltp} related issue.
398 '''
399 pass
402class LocalCartesian(_NamedBase):
403 '''Conversion between geodetic C{(lat, lon, height)} and I{local
404 cartesian} C{(x, y, z)} coordinates with I{geodetic} origin
405 C{(lat0, lon0, height0)}, transcoded from I{Karney}'s C++ class
406 U{LocalCartesian<https://GeographicLib.SourceForge.io/C++/doc/
407 classGeographicLib_1_1LocalCartesian.html>}.
409 The C{z} axis is normal to the ellipsoid, the C{y} axis points due
410 North. The plane C{z = -height0} is tangent to the ellipsoid.
412 The conversions all take place via geocentric coordinates using a
413 geocentric L{EcefKarney}, by default the WGS84 datum/ellipsoid.
415 @see: Class L{Ltp}.
416 '''
417 _ecef = EcefKarney(_WGS84)
418 _EcefClass = EcefKarney
419 _lon00 = INT0 # self.lon0
420 _t0 = None # origin (..., lat0, lon0, height0, ...) L{Ecef9Tuple}
421 _9Tuple = Local9Tuple
423 def __init__(self, latlonh0=INT0, lon0=INT0, height0=INT0, ecef=None, name=NN, **lon00):
424 '''New L{LocalCartesian} converter.
426 @kwarg latlonh0: The (geodetic) origin (C{LatLon}, L{LatLon4Tuple}, L{Ltp}
427 L{LocalCartesian} or L{Ecef9Tuple}) or the C{scalar}
428 latitude of the (goedetic) origin (C{degrees}).
429 @kwarg lon0: Longitude of the (goedetic) origin (C{degrees}) for C{scalar}
430 B{C{latlonh0}}, ignored otherwise.
431 @kwarg height0: Optional height (C{meter}, conventionally) at the (goedetic)
432 origin perpendicular to and above (or below) the ellipsoid's
433 surface and for C{scalar} B{C{latlonh0}}, ignored otherwise.
434 @kwarg ecef: An ECEF converter (L{EcefKarney} I{only}) for C{scalar}
435 B{C{latlonh0}}, ignored otherwise.
436 @kwarg name: Optional name (C{str}).
437 @kwarg lon00: An arbitrary, I{polar} longitude (C{degrees}), overriding
438 the default C{B{lon00}=B{lon0}}, see method C{reverse}.
440 @raise LocalError: If B{C{latlonh0}} not C{LatLon}, L{LatLon4Tuple}, L{Ltp},
441 L{LocalCartesian} or L{Ecef9Tuple} or B{C{latlonh0}},
442 B{C{lon0}}, B{C{height0}} or B{C{lon00}} invalid.
444 @raise TypeError: Invalid B{C{ecef}} or not L{EcefKarney}.
446 @note: If BC{latlonh0} is an L{Ltp} or L{LocalCartesian}, only C{lat0}, C{lon0},
447 C{height0} and I{polar} C{lon00} are copied, I{not} the ECEF converter.
448 '''
449 self.reset(latlonh0, lon0=lon0, height0=height0, ecef=ecef, name=name, **lon00)
451 def __eq__(self, other):
452 '''Compare this and an other instance.
454 @arg other: The other ellipsoid (L{LocalCartesian} or L{Ltp}).
456 @return: C{True} if equal, C{False} otherwise.
457 '''
458 return other is self or (isinstance(other, self.__class__) and
459 other.ecef == self.ecef and
460 other._t0 == self._t0)
462 @Property_RO
463 def datum(self):
464 '''Get the ECEF converter's datum (L{Datum}).
465 '''
466 return self.ecef.datum
468 @Property_RO
469 def ecef(self):
470 '''Get the ECEF converter (L{EcefKarney}).
471 '''
472 return self._ecef
474 def _ecef2local(self, ecef, Xyz, Xyz_kwds):
475 '''(INTERNAL) Convert geocentric/geodetic to local, like I{forward}.
477 @arg ecef: Geocentric (and geodetic) (L{Ecef9Tuple}).
478 @arg Xyz: An L{XyzLocal}, L{Enu} or L{Ned} I{class} or C{None}.
479 @arg Xyz_kwds: B{C{Xyz}} keyword arguments, ignored if C{B{Xyz} is None}.
481 @return: An C{B{Xyz}(x, y, z, ltp, **B{Xyz_kwds}} instance or if
482 C{B{Xyz} is None}, a L{Local9Tuple}C{(x, y, z, lat, lon,
483 height, ltp, ecef, M)} with this C{ltp}, B{C{ecef}}
484 (L{Ecef9Tuple}) converted to this C{datum} and C{M=None},
485 always.
486 '''
487 ltp = self
488 if ecef.datum != ltp.datum:
489 ecef = ecef.toDatum(ltp.datum)
490 x, y, z = self.M.rotate(ecef.xyz, *ltp._t0_xyz)
491 r = Local9Tuple(x, y, z, ecef.lat, ecef.lon, ecef.height,
492 ltp, ecef, None, name=ecef.name)
493 if Xyz:
494 if not issubclassof(Xyz, *_XyzLocals4): # Vector3d
495 raise _TypesError(_Xyz_, Xyz, *_XyzLocals4)
496 r = r.toXyz(Xyz=Xyz, **Xyz_kwds)
497 return r
499 @Property_RO
500 def ellipsoid(self):
501 '''Get the ECEF converter's ellipsoid (L{Ellipsoid}).
502 '''
503 return self.ecef.datum.ellipsoid
505 def forward(self, latlonh, lon=None, height=0, M=False, name=NN):
506 '''Convert I{geodetic} C{(lat, lon, height)} to I{local} cartesian
507 C{(x, y, z)}.
509 @arg latlonh: Either a C{LatLon}, L{Ltp}, L{Ecef9Tuple} or C{scalar}
510 (geodetic) latitude (C{degrees}).
511 @kwarg lon: Optional C{scalar} (geodetic) longitude for C{scalar}
512 B{C{latlonh}} (C{degrees}).
513 @kwarg height: Optional height (C{meter}, conventionally) perpendicular
514 to and above (or below) the ellipsoid's surface.
515 @kwarg M: Optionally, return the I{concatenated} rotation L{EcefMatrix},
516 iff available (C{bool}).
517 @kwarg name: Optional name (C{str}).
519 @return: A L{Local9Tuple}C{(x, y, z, lat, lon, height, ltp, ecef, M)}
520 with I{local} C{x}, C{y}, C{z}, I{geodetic} C{(lat}, C{lon},
521 C{height}, this C{ltp}, C{ecef} (L{Ecef9Tuple}) with
522 I{geocentric} C{x}, C{y}, C{z} (and I{geodetic} C{lat},
523 C{lon}, C{height}) and the I{concatenated} rotation matrix
524 C{M} (L{EcefMatrix}) if requested.
526 @raise LocalError: If B{C{latlonh}} not C{scalar}, C{LatLon}, L{Ltp},
527 L{Ecef9Tuple} or invalid or if B{C{lon}} not
528 C{scalar} for C{scalar} B{C{latlonh}} or invalid
529 or if B{C{height}} invalid.
530 '''
531 lat, lon, h, n = _llhn4(latlonh, lon, height, Error=LocalError, name=name)
532 t = self.ecef._forward(lat, lon, h, n, M=M)
533 x, y, z = self.M.rotate(t.xyz, *self._t0_xyz)
534 m = self.M.multiply(t.M) if M else None
535 return self._9Tuple(x, y, z, lat, lon, h, self, t, m, name=n or self.name)
537 @Property_RO
538 def height0(self):
539 '''Get the origin's height (C{meter}).
540 '''
541 return self._t0.height
543 @Property_RO
544 def lat0(self):
545 '''Get the origin's latitude (C{degrees}).
546 '''
547 return self._t0.lat
549 @Property_RO
550 def latlonheight0(self):
551 '''Get the origin's lat-, longitude and height (L{LatLon3Tuple}C{(lat, lon, height)}).
552 '''
553 return LatLon3Tuple(self.lat0, self.lon0, self.height0, name=self.name)
555 def _local2ecef(self, local, nine=False, M=False):
556 '''(INTERNAL) Convert I{local} to geocentric/geodetic, like I{.reverse}.
558 @arg local: Local (L{XyzLocal}, L{Enu}, L{Ned}, L{Aer} or L{Local9Tuple}).
559 @kwarg nine: Return 3- or 9-tuple (C{bool}).
560 @kwarg M: Include the rotation matrix (C{bool}).
562 @return: A I{geocentric} 3-tuple C{(x, y, z)} or if C{B{nine}=True},
563 an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)},
564 optionally including rotation matrix C{M} or C{None}.
565 '''
566 t = self.M.unrotate(local.xyz, *self._t0_xyz)
567 if nine:
568 t = self.ecef.reverse(*t, M=M)
569 return t
571 @Property_RO
572 def lon0(self):
573 '''Get the origin's longitude (C{degrees}).
574 '''
575 return self._t0.lon
577 @Property
578 def lon00(self):
579 '''Get the arbitrary, I{polar} longitude (C{degrees}).
580 '''
581 return self._lon00
583 @lon00.setter # PYCHOK setter!
584 def lon00(self, lon00):
585 '''Set the arbitrary, I{polar} longitude (C{degrees}).
586 '''
587 # lon00 <https://GitHub.com/mrJean1/PyGeodesy/issues/77>
588 self._lon00 = Degrees(lon00=lon00)
590 @Property_RO
591 def M(self):
592 '''Get the rotation matrix (C{EcefMatrix}).
593 '''
594 return self._t0.M
596 def reset(self, latlonh0=INT0, lon0=INT0, height0=INT0, ecef=None, name=NN, **lon00):
597 '''Reset this converter, see L{LocalCartesian.__init__} and L{Ltp.__init__} for more details.
598 '''
599 if isinstance(latlonh0, LocalCartesian):
600 if self._t0:
601 _update_all(self)
602 self._ecef = latlonh0.ecef
603 self._lon00 = latlonh0.lon00
604 self._t0 = latlonh0._t0
605 n = name or latlonh0.name
606 else:
607 lat0, lon0, height0, n = _llhn4(latlonh0, lon0, height0, suffix=_0_,
608 Error=LocalError, name=name or self.name)
609 if ecef: # PYCHOK no cover
610 _xinstanceof(self._EcefClass, ecef=ecef)
611 _update_all(self)
612 self._ecef = ecef
613 elif self._t0:
614 _update_all(self)
615 self._t0 = self.ecef._forward(lat0, lon0, height0, n, M=True)
616 self.lon00 = _xattr(latlonh0, lon00=_xkwds_get(lon00, lon00=lon0))
617 if n:
618 self.rename(n)
620 def reverse(self, xyz, y=None, z=None, M=False, name=NN, **lon00):
621 '''Convert I{local} C{(x, y, z)} to I{geodetic} C{(lat, lon, height)}.
623 @arg xyz: A I{local} (L{XyzLocal}, L{Enu}, L{Ned}, L{Aer}, L{Local9Tuple}) or
624 local C{x} coordinate (C{scalar}).
625 @kwarg y: Local C{y} coordinate for C{scalar} B{C{xyz}} and B{C{z}} (C{meter}).
626 @kwarg z: Local C{z} coordinate for C{scalar} B{C{xyz}} and B{C{y}} (C{meter}).
627 @kwarg M: Optionally, return the I{concatenated} rotation L{EcefMatrix}, iff
628 available (C{bool}).
629 @kwarg name: Optional name (C{str}).
630 @kwarg lon00: An arbitrary, I{polar} longitude (C{degrees}), returned for local
631 C{B{x}=0} and C{B{y}=0} at I{polar} latitudes C{abs(B{lat0}) == 90},
632 overriding property C{lon00} and default C{B{lon00}=B{lon0}}.
634 @return: An L{Local9Tuple}C{(x, y, z, lat, lon, height, ltp, ecef, M)} with
635 I{local} C{x}, C{y}, C{z}, I{geodetic} C{lat}, C{lon}, C{height},
636 this C{ltp}, an C{ecef} (L{Ecef9Tuple}) with the I{geocentric} C{x},
637 C{y}, C{z} (and I{geodetic} C{lat}, C{lon}, C{height}) and the
638 I{concatenated} rotation matrix C{M} (L{EcefMatrix}) if requested.
640 @raise LocalError: Invalid B{C{xyz}} or C{scalar} C{x} or B{C{y}} and/or B{C{z}}
641 not C{scalar} for C{scalar} B{C{xyz}}.
642 '''
643 x, y, z, n = _xyzn4(xyz, y, z, _XyzLocals5, Error=LocalError, name=name)
644 c = self.M.unrotate((x, y, z), *self._t0_xyz)
645 t = self.ecef.reverse(*c, M=M, lon00=_xkwds_get(lon00, lon00=self.lon00))
646 m = self.M.multiply(t.M) if M else None
647 return self._9Tuple(x, y, z, t.lat, t.lon, t.height, self, t, m, name=n or self.name)
649 @Property_RO
650 def _t0_xyz(self):
651 '''(INTERNAL) Get C{(x0, y0, z0)} as L{Vector3Tuple}.
652 '''
653 return self._t0.xyz
655 def toStr(self, prec=9, **unused): # PYCHOK signature
656 '''Return this L{LocalCartesian} as a string.
658 @kwarg prec: Precision, number of (decimal) digits (0..9).
660 @return: This L{LocalCartesian} representation (C{str}).
661 '''
662 return self.attrs(_lat0_, _lon0_, _height0_, _M_, _ecef_, _name_, prec=prec)
665class Ltp(LocalCartesian):
666 '''A I{local tangent plan} (LTP), a sub-class of C{LocalCartesian} with
667 (re-)configurable ECEF converter.
668 '''
669 _EcefClass = _EcefBase
671 def __init__(self, latlonh0=INT0, lon0=INT0, height0=INT0, ecef=None, name=NN, **lon00):
672 '''New C{Ltp}, see L{LocalCartesian.__init__} for more details.
674 @kwarg ecef: Optional ECEF converter (L{EcefKarney}, L{EcefFarrell21},
675 L{EcefFarrell22}, L{EcefSudano}, L{EcefVeness} or
676 L{EcefYou} I{instance}), overriding the default
677 L{EcefKarney}C{(datum=Datums.WGS84)} for C{scalar}.
679 @raise TypeError: Invalid B{C{ecef}}.
680 '''
681 LocalCartesian.reset(self, latlonh0, lon0=lon0, height0=height0,
682 ecef=ecef, name=name, **lon00)
684 @Property
685 def ecef(self):
686 '''Get this LTP's ECEF converter (C{Ecef...} I{instance}).
687 '''
688 return self._ecef
690 @ecef.setter # PYCHOK setter!
691 def ecef(self, ecef):
692 '''Set this LTP's ECEF converter (C{Ecef...} I{instance}).
694 @raise TypeError: Invalid B{C{ecef}}.
695 '''
696 _xinstanceof(_EcefBase, ecef=ecef)
697 if ecef != self._ecef: # PYCHOK no cover
698 self.reset(self._t0)
699 self._ecef = ecef
702class _ChLV(object):
703 '''(INTERNAL) Base class for C{ChLV*} classes.
704 '''
705 _03_falsing = ChLVyx2Tuple(0.6e6, 0.2e6)
706# _92_falsing = ChLVYX2Tuple(2.0e6, 1.0e6) # _95_ - _03_
707 _95_falsing = ChLVEN2Tuple(2.6e6, 1.2e6)
709 def _ChLV9Tuple(self, fw, M, name, *Y_X_h_lat_lon_h):
710 '''(INTERNAL) Helper for C{ChLVa/e.forward} and C{.reverse}.
711 '''
712 if bool(M): # PYCHOK no cover
713 m = self.forward if fw else self.reverse # PYCHOK attr
714 n = _DOT_(self.__class__.__name__, m.__name__)
715 raise _NotImplementedError(unstr(n, M=M), txt=None)
716 t = Y_X_h_lat_lon_h + (self, self._t0, None) # PYCHOK _t0
717 return ChLV9Tuple(t, name=name)
719 @property_RO
720 def _enh_n_h(self):
721 '''(INTERNAL) Get C{ChLV*.reverse} args[1:4] names, I{once}.
722 '''
723 t = _xargs_names(_ChLV.reverse)[1:4]
724 _ChLV._enh_n_h = t # overwrite this property_RO
725 # assert _xargs_names( ChLV.reverse)[1:4] == t
726 # assert _xargs_names(ChLVa.reverse)[1:4] == t
727 # assert _xargs_names(ChLVe.reverse)[1:4] == t
728 return t
730 def forward(self, latlonh, lon=None, height=0, M=None, name=NN): # PYCHOK no cover
731 '''Convert WGS84 geodetic to I{Swiss} projection coordinates.
733 @arg latlonh: Either a C{LatLon}, L{Ltp} or C{scalar} (geodetic) latitude (C{degrees}).
734 @kwarg lon: Optional, C{scalar} (geodetic) longitude for C{scalar} B{C{latlonh}} (C{degrees}).
735 @kwarg height: Optional, height, vertically above (or below) the surface of the ellipsoid
736 (C{meter}) for C{scalar} B{C{latlonh}} and B{C{lon}}.
737 @kwarg M: If C{True}, return the I{concatenated} rotation L{EcefMatrix} iff available
738 for C{ChLV} only, C{None} otherwise (C{bool}).
739 @kwarg name: Optional name (C{str}).
741 @return: A L{ChLV9Tuple}C{(Y, X, h_, lat, lon, height, ltp, ecef, M)} with the unfalsed
742 I{Swiss Y, X} coordinates, I{Swiss h_} height, the given I{geodetic} C{lat},
743 C{lon} and C{height}, this C{ChLV*} instance and C{ecef} (L{Ecef9Tuple}) at
744 I{Bern, Ch} and rotation matrix C{M}. The returned C{ltp} is this C{ChLV},
745 C{ChLVa} or C{ChLVe} instance.
747 @raise LocalError: Invalid or non-C{scalar} B{C{latlonh}}, B{C{lon}} or B{C{height}}.
748 '''
749 notOverloaded(self, latlonh, lon=lon, height=height, M=M, name=name)
751 def reverse(self, enh_, n=None, h_=0, M=None, **name): # PYCHOK no cover
752 '''Convert I{Swiss} projection to WGS84 geodetic coordinates.
754 @arg enh_: A Swiss projection (L{ChLV9Tuple}) or the C{scalar}, falsed I{Swiss E_LV95}
755 or I{y_LV03} easting (C{meter}).
756 @kwarg n: Falsed I{Swiss N_LV85} or I{x_LV03} northing for C{scalar} B{C{enh_}} and
757 B{C{h_}} (C{meter}).
758 @kwarg h_: I{Swiss h'} height for C{scalar} B{C{enh_}} and B{C{n}} (C{meter}).
759 @kwarg M: If C{True}, return the I{concatenated} rotation L{EcefMatrix} iff available
760 for C{ChLV} only, C{None} otherwise (C{bool}).
761 @kwarg name: Optional name (C{str}).
763 @return: A L{ChLV9Tuple}C{(Y, X, h_, lat, lon, height, ltp, ecef, M)} with the unfalsed
764 I{Swiss Y, X} coordinates, I{Swiss h_} height, the given I{geodetic} C{lat},
765 C{lon} and C{height}, this C{ChLV*} instance and C{ecef} (L{Ecef9Tuple}) at
766 I{Bern, Ch} and rotation matrix C{M}. The returned C{ltp} is this C{ChLV},
767 C{ChLVa} or C{ChLVe} instance.
769 @raise LocalError: Invalid or non-C{scalar} B{C{enh_}}, B{C{n}} or B{C{h_}}.
770 '''
771 notOverloaded(self, enh_, n=n, h_=h_, M=M, **name)
773 @staticmethod
774 def _falsing2(LV95):
775 '''(INTERNAL) Get the C{LV95} or C{LV03} falsing.
776 '''
777 return _ChLV._95_falsing if LV95 in (True, 95) else (
778 _ChLV._03_falsing if LV95 in (False, 3) else ChLVYX2Tuple(0, 0))
780 @staticmethod
781 def _llh2abh_3(lat, lon, h):
782 '''(INTERNAL) Helper for C{ChLVa/e.forward}.
783 '''
784 def _deg2ab(deg, sLL):
785 # convert degrees to arc-seconds
786 def _dms(ds, p, q, swap):
787 d = _floor(ds)
788 t = (ds - d) * p
789 m = _floor(t)
790 s = (t - m) * p
791 if swap:
792 d, s = s, d
793 return d + (m + s * q) * q
795 s = _dms(deg, _60_0, _0_01, False) # deg2sexag
796 s = _dms( s, _100_0, _60_0, True) # sexag2asec
797 return (s - sLL) / ChLV._s_ab
799 a = _deg2ab(lat, ChLV._sLat) # phi', lat_aux
800 b = _deg2ab(lon, ChLV._sLon) # lam', lng_aux
801 h_ = fsumf_(h, -ChLV.Bern.height, 2.73 * b, 6.94 * a)
802 return a, b, h_
804 @staticmethod
805 def _YXh_2abh3(Y, X, h_):
806 '''(INTERNAL) Helper for C{ChLVa/e.reverse}.
807 '''
808 def _YX2ab(YX):
809 return YX * ChLV._ab_m
811 a, b = map1(_YX2ab, Y, X)
812 h = fsumf_(h_, ChLV.Bern.height, -12.6 * a, -22.64 * b)
813 return a, b, h
815 def _YXh_n4(self, enh_, n, h_, **name):
816 '''(INTERNAL) Helper for C{ChLV*.reverse}.
817 '''
818 Y, X, h_, name = _xyzn4(enh_, n, h_, ChLV9Tuple,
819 _xyz_y_z_names=self._enh_n_h, **name)
820 if isinstance(enh_, ChLV9Tuple):
821 Y, X = enh_.Y, enh_.X
822 else: # isscalar(enh_)
823 Y, X = ChLV.unfalse2(Y, X) # PYCHOK ChLVYX2Tuple
824 return Y, X, h_, name
827class ChLV(_ChLV, Ltp):
828 '''Conversion between I{WGS84 geodetic} and I{Swiss} projection coordinates using
829 L{pygeodesy.EcefKarney}'s Earth-Centered, Earth-Fixed (ECEF) methods.
831 @see: U{Swiss projection formulas<https://www.SwissTopo.admin.CH/en/maps-data-online/
832 calculation-services.html>}, page 7ff, U{NAVREF<https://www.SwissTopo.admin.CH/en/
833 maps-data-online/calculation-services/navref.html>}, U{REFRAME<https://www.SwissTopo.admin.CH/
834 en/maps-data-online/calculation-services/reframe.html>} and U{SwissTopo Scripts GPS WGS84
835 <-> LV03<https://GitHub.com/ValentinMinder/Swisstopo-WGS84-LV03>}.
836 '''
837 _9Tuple = ChLV9Tuple
839 _ab_d = 0.36 # a, b units per degree, ...
840 _ab_m = 1.0e-6 # ... per meter and ...
841 _ab_M = _1_0 # ... per 1,000 kilometer
842 _s_d = _3600_0 # arc-seconds per degree ...
843 _s_ab = _s_d / _ab_d # ... and per a, b unit
844 _sLat = 169028.66 # Bern, Ch in ...
845 _sLon = 26782.5 # ... arc-seconds ...
846 # lat, lon, height == 46°57'08.66", 7°26'22.50", 49.55m ("new" 46°57'07.89", 7°26'22.335")
847 Bern = LatLon4Tuple(_sLat / _s_d, _sLon / _s_d, 49.55, _WGS84, name='Bern')
849 def __init__(self, latlonh0=Bern, **other_Ltp_kwds):
850 '''New ECEF-based I{WGS84-Swiss} L{ChLV} converter, centered at I{Bern, Ch}.
852 @kwarg latlonh0: The I{geodetic} origin and height, overriding C{Bern, Ch}.
853 @kwarg other_Ltp_kwds: Optional, other L{Ltp.__init__} keyword arguments.
855 @see: L{Ltp.__init__} for more information.
856 '''
857 Ltp.__init__(self, latlonh0, **_xkwds(other_Ltp_kwds, ecef=None, name=ChLV.Bern.name))
859 def forward(self, latlonh, lon=None, height=0, M=None, name=NN): # PYCHOK unused M
860 # overloaded for the _ChLV.forward.__doc__
861 return Ltp.forward(self, latlonh, lon=lon, height=height, M=M, name=name)
863 def reverse(self, enh_, n=None, h_=0, M=None, **name): # PYCHOK signature
864 # overloaded for the _ChLV.reverse.__doc__
865 Y, X, h_, name = self._YXh_n4(enh_, n, h_, **name)
866 return Ltp.reverse(self, Y, X, h_, M=M, name=name)
868 @staticmethod
869 def false2(Y, X, LV95=True, name=NN):
870 '''Add the I{Swiss LV95} or I{LV03} falsing.
872 @arg Y: Unfalsed I{Swiss Y} easting (C{meter}).
873 @arg X: Unfalsed I{Swiss X} northing (C{meter}).
874 @kwarg LV95: If C{True} add C{LV95} falsing, if C{False} add
875 C{LV03} falsing, otherwise leave unfalsed.
876 @kwarg name: Optional name (C{str}).
878 @return: A L{ChLVEN2Tuple}C{(E_LV95, N_LV95)} or a
879 L{ChLVyx2Tuple}C{(y_LV03, x_LV03)} with falsed B{C{Y}}
880 and B{C{X}}, otherwise a L{ChLVYX2Tuple}C{(Y, X)}
881 with B{C{Y}} and B{C{X}} as-is.
882 '''
883 e, n = t = _ChLV._falsing2(LV95)
884 return t.classof(e + Y, n + X, name=name)
886 @staticmethod
887 def isLV03(e, n):
888 '''Is C{(B{e}, B{n})} a valid I{Swiss LV03} projection?
890 @arg e: Falsed (or unfalsed) I{Swiss} easting (C{meter}).
891 @arg n: Falsed (or unfalsed) I{Swiss} northing (C{meter}).
893 @return: C{True} if C{(B{e}, B{n})} is a valid, falsed I{Swiss
894 LV03}, projection C{False} otherwise.
895 '''
896 # @see: U{Map<https://www.SwissTopo.admin.CH/en/knowledge-facts/
897 # surveying-geodesy/reference-frames/local/lv95.html>}
898 return 400.0e3 < e < 900.0e3 and 40.0e3 < n < 400.0e3
900 @staticmethod
901 def isLV95(e, n, raiser=True):
902 '''Is C{(B{e}, B{n})} a valid I{Swiss LV95} or I{LV03} projection?
904 @arg e: Falsed (or unfalsed) I{Swiss} easting (C{meter}).
905 @arg n: Falsed (or unfalsed) I{Swiss} northing (C{meter}).
906 @kwarg raiser: If C{True}, throw a L{LocalError} if B{C{e}} and
907 B{C{n}} are invalid I{Swiss LV95} nor I{LV03}.
909 @return: C{True} or C{False} if C{(B{e}, B{n})} is a valid I{Swiss
910 LV95} respectively I{LV03} projection, C{None} otherwise.
911 '''
912 if ChLV.isLV03(e, n):
913 return False
914 elif ChLV.isLV03(e - 2.0e6, n - 1.0e6): # _92_falsing = _95_ - _03_
915 return True
916 elif raiser: # PYCHOK no cover
917 raise LocalError(unstr(ChLV.isLV95, e=e, n=n))
918 return None
920 @staticmethod
921 def unfalse2(e, n, LV95=None, name=NN):
922 '''Remove the I{Swiss LV95} or I{LV03} falsing.
924 @arg e: Falsed I{Swiss E_LV95} or I{y_LV03} easting (C{meter}).
925 @arg n: Falsed I{Swiss N_LV95} or I{x_LV03} northing (C{meter}).
926 @kwarg LV95: If C{True} remove I{LV95} falsing, if C{False} remove
927 I{LV03} falsing, otherwise use method C{isLV95(B{e},
928 B{n})}.
929 @kwarg name: Optional name (C{str}).
931 @return: A L{ChLVYX2Tuple}C{(Y, X)} with the unfalsed B{C{e}}
932 respectively B{C{n}}.
933 '''
934 Y, X = _ChLV._falsing2(ChLV.isLV95(e, n) if LV95 is None else LV95)
935 return ChLVYX2Tuple(e - Y, n - X, name=name)
938class ChLVa(_ChLV, LocalCartesian):
939 '''Conversion between I{WGS84 geodetic} and I{Swiss} projection coordinates
940 using the U{Approximate<https://www.SwissTopo.admin.CH/en/maps-data-online/
941 calculation-services.html>} formulas, page 13.
943 @see: Older U{references<https://GitHub.com/alphasldiallo/Swisstopo-WGS84-LV03>}.
944 '''
945 def __init__(self, name=ChLV.Bern.name):
946 '''New I{Approximate WGS84-Swiss} L{ChLVa} converter, centered at I{Bern, Ch}.
948 @kwarg name: Optional name (C{str}), overriding C{Bern.name}.
949 '''
950 LocalCartesian.__init__(self, latlonh0=ChLV.Bern, name=name)
952 def forward(self, latlonh, lon=None, height=0, M=None, name=NN):
953 # overloaded for the _ChLV.forward.__doc__
954 lat, lon, h, name = _llhn4(latlonh, lon, height, name=name)
955 a, b, h_ = _ChLV._llh2abh_3(lat, lon, h)
956 a2, b2 = a**2, b**2
958 Y = fsumf_( 72.37, 211455.93 * b,
959 -10938.51 * b * a,
960 -0.36 * b * a2,
961 -44.54 * b * b2) # + 600_000
962 X = fsumf_(147.07, 308807.95 * a,
963 3745.25 * b2,
964 76.63 * a2,
965 -194.56 * b2 * a,
966 119.79 * a2 * a) # + 200_000
967 return self._ChLV9Tuple(True, M, name, Y, X, h_, lat, lon, h)
969 def reverse(self, enh_, n=None, h_=0, M=None, **name): # PYCHOK signature
970 # overloaded for the _ChLV.reverse.__doc__
971 Y, X, h_, name = self._YXh_n4(enh_, n, h_, **name)
972 a, b, h = _ChLV._YXh_2abh3(Y, X, h_)
973 ab_d, a2, b2 = ChLV._ab_d, a**2, b**2
975 lat = Fsum(16.9023892, 3.238272 * b,
976 -0.270978 * a2,
977 -0.002528 * b2,
978 -0.0447 * a2 * b,
979 -0.014 * b2 * b).fover(ab_d)
980 lon = Fsum( 2.6779094, 4.728982 * a,
981 0.791484 * a * b,
982 0.1306 * a * b2,
983 -0.0436 * a * a2).fover(ab_d)
984 return self._ChLV9Tuple(False, M, name, Y, X, h_, lat, lon, h)
987class ChLVe(_ChLV, LocalCartesian):
988 '''Conversion between I{WGS84 geodetic} and I{Swiss} projection coordinates
989 using the U{Ellipsoidal approximate<https://www.SwissTopo.admin.CH/en/
990 maps-data-online/calculation-services.html>} formulas, pp 10-11 and U{Bolliger,
991 J.<https://eMuseum.GGGS.CH/literatur-lv/liste-Dateien/1967_Bolliger_a.pdf>}
992 pp 148-151 (also U{GGGS<https://eMuseum.GGGS.CH/literatur-lv/liste.htm>}).
994 @note: Methods L{ChLVe.forward} and L{ChLVe.reverse} have an additional keyword
995 argument C{B{gamma}=False} to approximate the I{meridian convergence}.
996 If C{B{gamma}=True} a 2-tuple C{(t, gamma)} is returned with C{t} the
997 usual result (C{ChLV9Tuple}) and C{gamma}, the I{meridian convergence}
998 (decimal C{degrees}). To convert C{gamma} to C{grades} or C{gons},
999 use function L{pygeodesy.degrees2grades}.
1001 @see: Older U{references<https://GitHub.com/alphasldiallo/Swisstopo-WGS84-LV03>}.
1002 '''
1003 def __init__(self, name=ChLV.Bern.name):
1004 '''New I{Approximate WGS84-Swiss} L{ChLVe} converter, centered at I{Bern, Ch}.
1006 @kwarg name: Optional name (C{str}), overriding C{Bern.name}.
1007 '''
1008 LocalCartesian.__init__(self, latlonh0=ChLV.Bern, name=name)
1010 def forward(self, latlonh, lon=None, height=0, M=None, name=NN, gamma=False): # PYCHOK gamma
1011 # overloaded for the _ChLV.forward.__doc__
1012 lat, lon, h, name = _llhn4(latlonh, lon, height, name=name)
1013 a, b, h_ = _ChLV._llh2abh_3(lat, lon, h)
1014 ab_M, z, _F = ChLV._ab_M, 0, Fhorner
1016 B1 = _F(a, 211428.533991, -10939.608605, -2.658213, -8.539078, -0.00345, -0.007992)
1017 B3 = _F(a, -44.232717, 4.291740, -0.309883, 0.013924)
1018 B5 = _F(a, 0.019784, -0.004277)
1019 Y = _F(b, z, B1, z, B3, z, B5).fover(ab_M) # 1,000 Km!
1021 B0 = _F(a, z, 308770.746371, 75.028131, 120.435227, 0.009488, 0.070332, -0.00001)
1022 B2 = _F(a, 3745.408911, -193.792705, 4.340858, -0.376174, 0.004053)
1023 B4 = _F(a, -0.734684, 0.144466, -0.011842)
1024 B6 = 0.000488
1025 X = _F(b, B0, z, B2, z, B4, z, B6).fover(ab_M) # 1,000 Km!
1027 t = self._ChLV9Tuple(True, M, name, Y, X, h_, lat, lon, h)
1028 if gamma:
1029 U1 = _F(a, 2255515.207166, 2642.456961, 1.284180, 2.577486, 0.001165)
1030 U3 = _F(a, -412.991934, 64.106344, -2.679566, 0.123833)
1031 U5 = _F(a, 0.204129, -0.037725)
1032 g = _F(b, z, U1, z, U3, z, U5).fover(ChLV._ab_m) # * ChLV._ab_d degrees?
1033 t = t, g
1034 return t
1036 def reverse(self, enh_, n=None, h_=0, M=None, name=NN, gamma=False): # PYCHOK gamma
1037 # overloaded for the _ChLV.reverse.__doc__
1038 Y, X, h_, name = self._YXh_n4(enh_, n, h_, name=name)
1039 a, b, h = _ChLV._YXh_2abh3(Y, X, h_)
1040 s_d, _F, z = ChLV._s_d, Fhorner, 0
1042 A0 = _F(b, ChLV._sLat, 32386.4877666, -25.486822, -132.457771, 0.48747, 0.81305, -0.0069)
1043 A2 = _F(b, -2713.537919, -450.442705, -75.53194, -14.63049, -2.7604)
1044 A4 = _F(b, 24.42786, 13.20703, 4.7476)
1045 A6 = -0.4249
1046 lat = _F(a, A0, z, A2, z, A4, z, A6).fover(s_d)
1048 A1 = _F(b, 47297.3056722, 7925.714783, 1328.129667, 255.02202, 48.17474, 9.0243)
1049 A3 = _F(b, -442.709889, -255.02202, -96.34947, -30.0808)
1050 A5 = _F(b, 9.63495, 9.0243)
1051 lon = _F(a, ChLV._sLon, A1, z, A3, z, A5).fover(s_d)
1052 # == (ChLV._sLon + a * (A1 + a**2 * (A3 + a**2 * A5))) / s_d
1054 t = self._ChLV9Tuple(False, M, name, Y, X, h_, lat, lon, h)
1055 if gamma:
1056 U1 = _F(b, 106679.792202, 17876.57022, 4306.5241, 794.87772, 148.1545, 27.8725)
1057 U3 = _F(b, -1435.508, -794.8777, -296.309, -92.908)
1058 U5 = _F(b, 29.631, 27.873)
1059 g = _F(a, z, U1, z, U3, z, U5).fover(ChLV._s_ab) # degrees
1060 t = t, g
1061 return t
1064def tyr3d(tilt=INT0, yaw=INT0, roll=INT0, Vector=Vector3d, **Vector_kwds):
1065 '''Convert an attitude oriention into a (3-D) direction vector.
1067 @kwarg tilt: Pitch, elevation from horizontal (C{degrees}), negative down
1068 (clockwise rotation along and around the x-axis).
1069 @kwarg yaw: Bearing, heading (compass C{degrees360}), clockwise from North
1070 (counter-clockwise rotation along and around the z-axis).
1071 @kwarg roll: Roll, bank (C{degrees}), positive to the right and down
1072 (clockwise rotation along and around the y-axis).
1074 @return: A named B{C{Vector}} instance or if B{C{Vector}} is C{None},
1075 a named L{Vector3Tuple}C{(x, y, z)}.
1077 @see: U{Yaw, pitch, and roll rotations<http://MSL.CS.UIUC.edu/planning/node102.html>}
1078 and function L{pygeodesy.hartzell} argument C{los}.
1079 '''
1080 d = Attitude4Tuple(_0_0, tilt, yaw, roll).tyr3d
1081 return d if Vector is type(d) else (
1082 Vector3Tuple(d.x, d.y, d.z, name=d.name) if Vector is None else
1083 Vector(d.x, d.y, d.z, **_xkwds(Vector_kwds, name=d.name))) # PYCHOK indent
1086def _xLtp(ltp, *dflt):
1087 '''(INTERNAL) Validate B{C{ltp}}.
1088 '''
1089 if dflt and ltp is None:
1090 ltp = dflt[0]
1091 if isinstance(ltp, (LocalCartesian, Ltp)):
1092 return ltp
1093 raise _TypesError(_ltp_, ltp, Ltp, LocalCartesian)
1095# **) MIT License
1096#
1097# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.
1098#
1099# Permission is hereby granted, free of charge, to any person obtaining a
1100# copy of this software and associated documentation files (the "Software"),
1101# to deal in the Software without restriction, including without limitation
1102# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1103# and/or sell copies of the Software, and to permit persons to whom the
1104# Software is furnished to do so, subject to the following conditions:
1105#
1106# The above copyright notice and this permission notice shall be included
1107# in all copies or substantial portions of the Software.
1108#
1109# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1110# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1111# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1112# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1113# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1114# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1115# OTHER DEALINGS IN THE SOFTWARE.