Coverage for pygeodesy/ltp.py: 95%

423 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-01-10 16:55 -0500

1 

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

3 

4u'''I{Local Tangent Plane} (LTP) and I{local} cartesian coordinates. 

5 

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}. 

8 

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 

15 

16from pygeodesy.basics import _args_kwds_names, map1, map2, _xinstanceof, \ 

17 _xsubclassof # .datums 

18from pygeodesy.constants import EPS, INT0, _umod_360, _0_0, _0_01, _0_5, _1_0, \ 

19 _2_0, _60_0, _90_0, _100_0, _180_0, _3600_0, \ 

20 _N_1_0 # PYCHOK used! 

21# from pygeodesy.datums import _WGS84 # from .ecef 

22from pygeodesy.ecef import _EcefBase, EcefKarney, Ecef9Tuple, _llhn4, \ 

23 _xyzn4, _WGS84 

24from pygeodesy.errors import _NotImplementedError, _ValueError, _xattr, \ 

25 _xkwds, _xkwds_get, _xkwds_pop2 

26from pygeodesy.fmath import fabs, fdot, fdot_, Fhorner 

27from pygeodesy.fsums import _floor, fsumf_ 

28from pygeodesy.interns import _0_, _COMMASPACE_, _DOT_, _ecef_, _height_, _M_, \ 

29 _invalid_, _lat0_, _lon0_, _name_, _too_ 

30# from pygeodesy.lazily import _ALL_LAZY # from vector3d 

31from pygeodesy.ltpTuples import Attitude4Tuple, ChLVEN2Tuple, ChLV9Tuple, \ 

32 ChLVYX2Tuple, Footprint5Tuple, Local9Tuple, \ 

33 ChLVyx2Tuple, _XyzLocals4, _XyzLocals5, Xyz4Tuple 

34from pygeodesy.named import _name__, _name2__, _NamedBase, notOverloaded 

35from pygeodesy.namedTuples import LatLon3Tuple, LatLon4Tuple, Vector3Tuple 

36from pygeodesy.props import Property, Property_RO, property_doc_, \ 

37 property_ROver, _update_all 

38from pygeodesy.streprs import Fmt, strs, unstr 

39from pygeodesy.units import Bearing, Degrees, _isHeight, Meter 

40from pygeodesy.utily import cotd, _loneg, sincos2d, sincos2d_, tand, tand_, \ 

41 wrap180, wrap360 

42from pygeodesy.vector3d import _ALL_LAZY, Vector3d 

43 

44# from math import fabs, floor as _floor # from .fmath, .fsums 

45 

46__all__ = _ALL_LAZY.ltp 

47__version__ = '24.12.12' 

48 

49_height0_ = _height_ + _0_ 

50_narrow_ = 'narrow' 

51_wide_ = 'wide' 

52 

53 

54class Attitude(_NamedBase): 

55 '''The pose of a plane or camera in space. 

56 ''' 

57 _alt = Meter( alt =_0_0) 

58 _roll = Degrees(roll=_0_0) 

59 _tilt = Degrees(tilt=_0_0) 

60 _yaw = Bearing(yaw =_0_0) 

61 

62 def __init__(self, alt_attitude=INT0, tilt=INT0, yaw=INT0, roll=INT0, **name): 

63 '''New L{Attitude}. 

64 

65 @kwarg alt_attitude: Altitude (C{meter}) above earth or previous attitude 

66 (L{Attitude} or L{Attitude4Tuple}) with the C{B{alt}itude}, 

67 B{C{tilt}}, B{C{yaw}} and B{C{roll}}. 

68 @kwarg tilt: Pitch, elevation from horizontal (C{degrees180}), negative down 

69 (clockwise rotation along and around the x- or East axis). 

70 @kwarg yaw: Bearing, heading (compass C{degrees360}), clockwise from North 

71 (counter-clockwise rotation along and around the z- or Up axis). 

72 @kwarg roll: Roll, bank (C{degrees180}), positive to the right and down 

73 (clockwise rotation along and around the y- or North axis). 

74 @kwarg name: Optional C{B{name}=NN} C{str}). 

75 

76 @raise AttitudeError: Invalid B{C{alt_attitude}}, B{C{tilt}}, B{C{yaw}} or 

77 B{C{roll}}. 

78 

79 @see: U{Principal axes<https://WikiPedia.org/wiki/Aircraft_principal_axes>} and 

80 U{Yaw, pitch, and roll rotations<http://MSL.CS.UIUC.edu/planning/node102.html>}. 

81 ''' 

82 if _isHeight(alt_attitude): 

83 t = Attitude4Tuple(alt_attitude, tilt, yaw, roll) 

84 else: 

85 try: 

86 t = alt_attitude.atyr 

87 except AttributeError: 

88 raise AttitudeError(alt=alt_attitude, tilt=tilt, yaw=yaw, rol=roll) 

89 for n, v in t.items(): 

90 if v: 

91 setattr(self, n, v) 

92 n = _name__(name, _or_nameof=t) 

93 if n: 

94 self.name = n 

95 

96 @property_doc_(' altitude above earth in C{meter}.') 

97 def alt(self): 

98 return self._alt 

99 

100 @alt.setter # PYCHOK setter! 

101 def alt(self, alt): # PYCHOK no cover 

102 a = Meter(alt=alt, Error=AttitudeError) 

103 if self._alt != a: 

104 _update_all(self) 

105 self._alt = a 

106 

107 altitude = alt 

108 

109 @Property_RO 

110 def atyr(self): 

111 '''Return this attitude's alt[itude], tilt, yaw and roll as an L{Attitude4Tuple}. 

112 ''' 

113 return Attitude4Tuple(self.alt, self.tilt, self.yaw, self.roll, name=self.name) 

114 

115 @Property_RO 

116 def matrix(self): 

117 '''Get the 3x3 rotation matrix C{R(yaw)·R(tilt)·R(roll)}, aka I{ZYX} (C{float}, row-order). 

118 

119 @see: Matrix M of case 10 in U{Appendix A 

120 <https://ntrs.NASA.gov/api/citations/19770019231/downloads/19770019231.pdf>}. 

121 ''' 

122 # to follow the definitions of rotation angles alpha, beta and gamma: 

123 # negate yaw since yaw is counter-clockwise around the z-axis, swap 

124 # tilt and roll since tilt is around the x- and roll around the y-axis 

125 sa, ca, sb, cb, sg, cg = sincos2d_(-self.yaw, self.roll, self.tilt) 

126 return ((ca * cb, fdot_(ca, sb * sg, -sa, cg), fdot_(ca, sb * cg, sa, sg)), 

127 (sa * cb, fdot_(sa, sb * sg, ca, cg), fdot_(sa, sb * cg, -ca, sg)), 

128 ( -sb, cb * sg, cb * cg)) 

129 

130 @property_doc_(' roll/bank in C{degrees180}, positive to the right and down.') 

131 def roll(self): 

132 return self._roll 

133 

134 @roll.setter # PYCHOK setter! 

135 def roll(self, roll): 

136 r = Degrees(roll=roll, wrap=wrap180, Error=AttitudeError) 

137 if self._roll != r: 

138 _update_all(self) 

139 self._roll = r 

140 

141 bank = roll 

142 

143 def rotate(self, x_xyz, y=None, z=None, Vector=None, **name_Vector_kwds): 

144 '''Transform a (local) cartesian by this attitude's matrix. 

145 

146 @arg x_xyz: X component of vector (C{scalar}) or (3-D) vector (C{Cartesian}, 

147 L{Vector3d} or L{Vector3Tuple}). 

148 @kwarg y: Y component of vector (C{scalar}), same units as B{C{x}}. 

149 @kwarg z: Z component of vector (C{scalar}), same units as B{C{x}}. 

150 @kwarg Vector: Class to return transformed point (C{Cartesian}, L{Vector3d} 

151 or C{Vector3Tuple}) or C{None}. 

152 @kwarg name_Vector_kwds: Optional C{B{name}=NN} (C{str}) and optionally, 

153 additional B{C{Vector}} keyword arguments, ignored if C{B{Vector} 

154 is None}. 

155 

156 @return: A named B{C{Vector}} instance or if C{B{Vector} is None}, 

157 a named L{Vector3Tuple}C{(x, y, z)}. 

158 

159 @raise AttitudeError: Invalid B{C{x_xyz}}, B{C{y}} or B{C{z}}. 

160 

161 @raise TypeError: Invalid B{C{Vector}} or B{C{name_Vector_kwds}} item. 

162 

163 @see: U{Yaw, pitch, and roll rotations<http://MSL.CS.UIUC.edu/planning/node102.html>}. 

164 ''' 

165 try: 

166 try: 

167 xyz = map2(float, x_xyz.xyz3) 

168 except AttributeError: 

169 xyz = map1(float, x_xyz, y, z) 

170 except (TypeError, ValueError) as x: 

171 raise AttitudeError(x_xyz=x_xyz, y=y, z=z, cause=x) 

172 

173 x, y, z = (fdot(r, *xyz) for r in self.matrix) 

174 n, kwds = _name2__(name_Vector_kwds, _or_nameof=self) 

175 return Vector3Tuple(x, y, z, name=n) if Vector is None else \ 

176 Vector(x, y, z, name=n, **kwds) 

177 

178 @property_doc_(' tilt/pitch/elevation from horizontal in C{degrees180}, negative down.') 

179 def tilt(self): 

180 return self._tilt 

181 

182 @tilt.setter # PYCHOK setter! 

183 def tilt(self, tilt): 

184 t = Degrees(tilt=tilt, wrap=wrap180, Error=AttitudeError) 

185 if self._tilt != t: 

186 _update_all(self) 

187 self._tilt = t 

188 

189 elevation = pitch = tilt 

190 

191 def toStr(self, prec=6, sep=_COMMASPACE_, **unused): # PYCHOK signature 

192 '''Format this attitude as string. 

193 

194 @kwarg prec: The C{float} precision, number of decimal digits (0..9). 

195 Trailing zero decimals are stripped for B{C{prec}} values 

196 of 1 and above, but kept for negative B{C{prec}} values. 

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

198 

199 @return: This attitude (C{str}). 

200 ''' 

201 return self.atyr.toStr(prec=prec, sep=sep) 

202 

203 @Property_RO 

204 def tyr3d(self): 

205 '''Get this attitude's (3-D) directional vector (L{Vector3d}). 

206 

207 @see: U{Yaw, pitch, and roll rotations<http://MSL.CS.UIUC.edu/planning/node102.html>}. 

208 ''' 

209 def _r2d(r): 

210 return fsumf_(_N_1_0, *r) 

211 

212 return Vector3d(*map(_r2d, self.matrix), name__=tyr3d) 

213 

214 @property_doc_(' yaw/bearing/heading in compass C{degrees360}, clockwise from North.') 

215 def yaw(self): 

216 return self._yaw 

217 

218 @yaw.setter # PYCHOK setter! 

219 def yaw(self, yaw): 

220 y = Bearing(yaw=yaw, Error=AttitudeError) 

221 if self._yaw != y: 

222 _update_all(self) 

223 self._yaw = y 

224 

225 bearing = heading = yaw 

226 

227 

228class AttitudeError(_ValueError): 

229 '''An L{Attitude} or L{Attitude4Tuple} issue. 

230 ''' 

231 pass 

232 

233 

234class Frustum(_NamedBase): 

235 '''A rectangular pyramid, typically representing a camera's I{field-of-view} 

236 (fov) and the intersection with (or projection to) a I{local tangent plane}. 

237 

238 @see: U{Viewing frustum<https://WikiPedia.org/wiki/Viewing_frustum>}. 

239 ''' 

240 _h_2 = _0_0 # half hfov in degrees 

241 _ltp = None # local tangent plane 

242 _tan_h_2 = _0_0 # tan(_h_2) 

243 _v_2 = _0_0 # half vfov in degrees 

244 

245 def __init__(self, hfov, vfov, ltp=None, **name): 

246 '''New L{Frustum}. 

247 

248 @arg hfov: Horizontal field-of-view (C{degrees180}). 

249 @arg vfov: Vertical field-of-view (C{degrees180}). 

250 @kwarg ltp: Optional I{local tangent plane} (L{Ltp}). 

251 @kwarg name: Optional C{B{name}=NN} (C{str}). 

252 

253 @raise LocalError: Invalid B{C{hfov}} or B{C{vfov}}. 

254 ''' 

255 self._h_2 = h = _fov_2(hfov=hfov) 

256 self._v_2 = _fov_2(vfov=vfov) 

257 

258 self._tan_h_2 = tand(h, hfov_2=h) 

259 

260 if ltp: 

261 self._ltp = _xLtp(ltp) 

262 if name: 

263 self.name # PYCHOK effect 

264 

265 def footprint5(self, alt_attitude, tilt=0, yaw=0, roll=0, z=_0_0, ltp=None, **name): # MCCABE 15 

266 '''Compute the center and corners of the intersection with (or projection 

267 to) the I{local tangent plane} (LTP). 

268 

269 @arg alt_attitude: An altitude (C{meter}) above I{local tangent plane} or 

270 an attitude (L{Attitude} or L{Attitude4Tuple}) with the 

271 C{B{alt}itude}, B{C{tilt}}, B{C{yaw}} and B{C{roll}}. 

272 @kwarg tilt: Pitch, elevation from horizontal (C{degrees}), negative down 

273 (clockwise rotation along and around the x- or East axis). 

274 @kwarg yaw: Bearing, heading (compass C{degrees}), clockwise from North 

275 (counter-clockwise rotation along and around the z- or Up axis). 

276 @kwarg roll: Roll, bank (C{degrees}), positive to the right and down 

277 (clockwise rotation along and around the y- or North axis). 

278 @kwarg z: Optional height of the footprint (C{meter}) above I{local tangent plane}. 

279 @kwarg ltp: The I{local tangent plane} (L{Ltp}), overriding this 

280 frustum's C{ltp}. 

281 @kwarg name: Optional C{B{name}=NN} (C{str}). 

282 

283 @return: A L{Footprint5Tuple}C{(center, upperleft, upperight, loweright, 

284 lowerleft)} with the C{center} and 4 corners, each an L{Xyz4Tuple}. 

285 

286 @raise TypeError: Invalid B{C{ltp}}. 

287 

288 @raise UnitError: Invalid B{C{altitude}}, B{C{tilt}}, B{C{roll}} or B{C{z}}. 

289 

290 @raise ValueError: If B{C{altitude}} too low, B{C{z}} too high or B{C{tilt}} 

291 or B{C{roll}} -including B{C{vfov}} respectively B{C{hfov}}- 

292 over the horizon. 

293 

294 @see: U{Principal axes<https://WikiPedia.org/wiki/Aircraft_principal_axes>}. 

295 ''' 

296 def _xy2(a, e, h_2, tan_h_2, r): 

297 # left and right corners, or swapped 

298 if r < EPS: # no roll 

299 r = a * tan_h_2 

300 l = -r # PYCHOK l is ell 

301 else: # roll 

302 r, l = tand_(r - h_2, r + h_2, roll_hfov=r) # PYCHOK l is ell 

303 r *= -a # negate right positive 

304 l *= -a # PYCHOK l is ell 

305 y = a * cotd(e, tilt_vfov=e) 

306 return (l, y), (r, y) 

307 

308 def _xyz5(b, xy5, z, ltp): 

309 # rotate (x, y)'s by bearing, clockwise 

310 sc = sincos2d(b) 

311 for x, y in xy5: 

312 yield Xyz4Tuple(fdot(sc, x, y), 

313 fdot(sc, -x, y), z, ltp) 

314 

315 try: 

316 a, t, y, r = alt_attitude.atyr 

317 except AttributeError: 

318 a, t, y, r = alt_attitude, tilt, yaw, roll 

319 

320 a = Meter(altitude=a) 

321 if a < EPS: # too low 

322 raise _ValueError(altitude=a) 

323 if z: # PYCHOK no cover 

324 z = Meter(z=z) 

325 a -= z 

326 if a < EPS: # z above a 

327 raise _ValueError(altitude_z=a) 

328 else: 

329 z = _0_0 

330 

331 b = Degrees(yaw=y, wrap=wrap360) # bearing 

332 e = -Degrees(tilt=t, wrap=wrap180) # elevation, pitch 

333 if not EPS < e < _180_0: 

334 raise _ValueError(tilt=t) 

335 if e > _90_0: 

336 e = _loneg(e) 

337 b = _umod_360(b + _180_0) 

338 

339 r = Degrees(roll=r, wrap=wrap180) # roll center 

340 x = (-a * tand(r, roll=r)) if r else _0_0 

341 y = a * cotd(e, tilt=t) # ground range 

342 if fabs(y) < EPS: 

343 y = _0_0 

344 

345 v, h, t = self._v_2, self._h_2, self._tan_h_2 

346 # center and corners, clockwise from upperleft, rolled 

347 xy5 = ((x, y),) + _xy2(a, e - v, h, t, r) \ 

348 + _xy2(a, e + v, -h, -t, r) # swapped 

349 # turn center and corners by yaw, clockwise 

350 p = self.ltp if ltp is None else ltp # None OK 

351 return Footprint5Tuple(_xyz5(b, xy5, z, p), **name) # *_xyz5 

352 

353 @Property_RO 

354 def hfov(self): 

355 '''Get the horizontal C{fov} (C{degrees}). 

356 ''' 

357 return Degrees(hfov=self._h_2 * _2_0) 

358 

359 @Property_RO 

360 def ltp(self): 

361 '''Get the I{local tangent plane} (L{Ltp}) or C{None}. 

362 ''' 

363 return self._ltp 

364 

365 def toStr(self, prec=3, fmt=Fmt.F, sep=_COMMASPACE_): # PYCHOK signature 

366 '''Convert this frustum to a "hfov, vfov, ltp" string. 

367 

368 @kwarg prec: Number of (decimal) digits, unstripped (0..8 or C{None}). 

369 @kwarg fmt: Optional, C{float} format (C{letter}). 

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

371 

372 @return: Frustum in the specified form (C{str}). 

373 ''' 

374 t = self.hfov, self.vfov 

375 if self.ltp: 

376 t += self.ltp, 

377 t = strs(t, prec=prec, fmt=fmt) 

378 return sep.join(t) if sep else t 

379 

380 @Property_RO 

381 def vfov(self): 

382 '''Get the vertical C{fov} (C{degrees}). 

383 ''' 

384 return Degrees(vfov=self._v_2 * _2_0) 

385 

386 

387class LocalError(_ValueError): 

388 '''A L{LocalCartesian} or L{Ltp} related issue. 

389 ''' 

390 pass 

391 

392 

393class LocalCartesian(_NamedBase): 

394 '''Conversion between geodetic C{(lat, lon, height)} and I{local 

395 cartesian} C{(x, y, z)} coordinates with I{geodetic} origin 

396 C{(lat0, lon0, height0)}, transcoded from I{Karney}'s C++ class 

397 U{LocalCartesian<https://GeographicLib.SourceForge.io/C++/doc/ 

398 classGeographicLib_1_1LocalCartesian.html>}. 

399 

400 The C{z} axis is normal to the ellipsoid, the C{y} axis points due 

401 North. The plane C{z = -height0} is tangent to the ellipsoid. 

402 

403 The conversions all take place via geocentric coordinates using a 

404 geocentric L{EcefKarney}, by default the WGS84 datum/ellipsoid. 

405 

406 @see: Class L{Ltp}. 

407 ''' 

408 _ecef = EcefKarney(_WGS84) 

409 _Ecef = EcefKarney 

410 _lon00 = INT0 # self.lon0 

411 _t0 = None # origin (..., lat0, lon0, height0, ...) L{Ecef9Tuple} 

412 _9Tuple = Local9Tuple 

413 

414 def __init__(self, latlonh0=INT0, lon0=INT0, height0=INT0, ecef=None, **lon00_name): 

415 '''New L{LocalCartesian} converter. 

416 

417 @kwarg latlonh0: The (geodetic) origin (C{LatLon}, L{LatLon4Tuple}, L{Ltp} 

418 L{LocalCartesian} or L{Ecef9Tuple}) or the C{scalar} 

419 latitude of the (goedetic) origin (C{degrees}). 

420 @kwarg lon0: Longitude of the (goedetic) origin (C{degrees}), required if 

421 B{C{latlonh0}} is C{scalar}, ignored otherwise. 

422 @kwarg height0: Optional height (C{meter}, conventionally) at the (goedetic) 

423 origin perpendicular to and above (or below) the ellipsoid's 

424 surface, like B{C{lon0}}. 

425 @kwarg ecef: An ECEF converter (L{EcefKarney} I{only}), like B{C{lon0}}. 

426 @kwarg lon00_name: Optional C{B{name}=NN} (C{str}) and keyword argument 

427 C{B{lon00}=B{lon0}} for the arbitrary I{polar} longitude 

428 (C{degrees}), see method C{reverse} and property C{lon00} 

429 for further details. 

430 

431 @raise LocalError: If B{C{latlonh0}} not C{LatLon}, L{LatLon4Tuple}, L{Ltp}, 

432 L{LocalCartesian} or L{Ecef9Tuple} or B{C{latlonh0}}, 

433 B{C{lon0}}, B{C{height0}} or B{C{lon00}} invalid. 

434 

435 @raise TypeError: Invalid B{C{ecef}} or not L{EcefKarney}. 

436 

437 @note: If BC{latlonh0} is an L{Ltp} or L{LocalCartesian}, only C{lat0}, C{lon0}, 

438 C{height0} and I{polar} C{lon00} are copied, I{not} the ECEF converter. 

439 ''' 

440 self.reset(latlonh0, lon0=lon0, height0=height0, ecef=ecef, **lon00_name) 

441 

442 def __eq__(self, other): 

443 '''Compare this and an other instance. 

444 

445 @arg other: The other ellipsoid (L{LocalCartesian} or L{Ltp}). 

446 

447 @return: C{True} if equal, C{False} otherwise. 

448 ''' 

449 return other is self or (isinstance(other, self.__class__) and 

450 other.ecef == self.ecef and 

451 other._t0 == self._t0) 

452 

453 @Property_RO 

454 def datum(self): 

455 '''Get the ECEF converter's datum (L{Datum}). 

456 ''' 

457 return self.ecef.datum 

458 

459 @Property_RO 

460 def ecef(self): 

461 '''Get the ECEF converter (L{EcefKarney}). 

462 ''' 

463 return self._ecef 

464 

465 def _ecef2local(self, ecef, Xyz, name_Xyz_kwds): 

466 '''(INTERNAL) Convert geocentric/geodetic to local, like I{forward}. 

467 

468 @arg ecef: Geocentric (and geodetic) (L{Ecef9Tuple}). 

469 @arg Xyz: An L{XyzLocal}, L{Aer}, L{Enu} or L{Ned} I{class} or C{None}. 

470 @arg name_Xyz_kwds: Optional C{B{name}=NN} (C{str}) and optionally, 

471 additional B{C{Xyz}} keyword arguments, ignored if C{B{Xyz} 

472 is None}. 

473 

474 @return: An C{B{Xyz}(x, y, z, ltp, **B{name_Xyz_kwds}} instance or 

475 if C{B{Xyz} is None}, a L{Local9Tuple}C{(x, y, z, lat, lon, 

476 height, ltp, ecef, M)} with this C{ltp}, B{C{ecef}} 

477 (L{Ecef9Tuple}) converted to this C{datum} and C{M=None}, 

478 always. 

479 

480 @raise TypeError: Invalid B{C{Xyz}} or B{C{name_Xyz_kwds}} item. 

481 ''' 

482 _xinstanceof(Ecef9Tuple, ecef=ecef) 

483 if ecef.datum != self.datum: 

484 ecef = ecef.toDatum(self.datum) 

485 n, kwds = _name2__(name_Xyz_kwds, _or_nameof=ecef) 

486 x, y, z = self.M.rotate(ecef.xyz, *self._t0_xyz) 

487 r = Local9Tuple(x, y, z, ecef.lat, ecef.lon, ecef.height, 

488 self, ecef, None, name=n) 

489 if Xyz: 

490 _xsubclassof(*_XyzLocals4, Xyz=Xyz) # Vector3d 

491 r = r.toXyz(Xyz=Xyz, name=n, **kwds) 

492 return r 

493 

494 @Property_RO 

495 def ellipsoid(self): 

496 '''Get the ECEF converter's ellipsoid (L{Ellipsoid}). 

497 ''' 

498 return self.ecef.datum.ellipsoid 

499 

500 def forward(self, latlonh, lon=None, height=0, M=False, **name): 

501 '''Convert I{geodetic} C{(lat, lon, height)} to I{local} cartesian 

502 C{(x, y, z)}. 

503 

504 @arg latlonh: Either a C{LatLon}, L{Ltp}, L{Ecef9Tuple} or C{scalar} 

505 (geodetic) latitude (C{degrees}). 

506 @kwarg lon: Optional C{scalar} (geodetic) longitude for C{scalar} 

507 B{C{latlonh}} (C{degrees}). 

508 @kwarg height: Optional height (C{meter}, conventionally) perpendicular 

509 to and above (or below) the ellipsoid's surface. 

510 @kwarg M: Optionally, return the I{concatenated} rotation L{EcefMatrix}, 

511 iff available (C{bool}). 

512 @kwarg name: Optional C{B{name}=NN} (C{str}). 

513 

514 @return: A L{Local9Tuple}C{(x, y, z, lat, lon, height, ltp, ecef, M)} 

515 with I{local} C{x}, C{y}, C{z}, I{geodetic} C{(lat}, C{lon}, 

516 C{height}, this C{ltp}, C{ecef} (L{Ecef9Tuple}) with 

517 I{geocentric} C{x}, C{y}, C{z} (and I{geodetic} C{lat}, 

518 C{lon}, C{height}) and the I{concatenated} rotation matrix 

519 C{M} (L{EcefMatrix}) if requested. 

520 

521 @raise LocalError: If B{C{latlonh}} not C{scalar}, C{LatLon}, L{Ltp}, 

522 L{Ecef9Tuple} or invalid or if B{C{lon}} not 

523 C{scalar} for C{scalar} B{C{latlonh}} or invalid 

524 or if B{C{height}} invalid. 

525 ''' 

526 lat, lon, h, n = _llhn4(latlonh, lon, height, Error=LocalError, **name) 

527 t = self.ecef._forward(lat, lon, h, n, M=M) 

528 x, y, z = self.M.rotate(t.xyz, *self._t0_xyz) 

529 m = self.M.multiply(t.M) if M else None 

530 return self._9Tuple(x, y, z, lat, lon, h, self, t, m, name=n or self.name) 

531 

532 @Property_RO 

533 def height0(self): 

534 '''Get the origin's height (C{meter}). 

535 ''' 

536 return self._t0.height 

537 

538 @Property_RO 

539 def lat0(self): 

540 '''Get the origin's latitude (C{degrees}). 

541 ''' 

542 return self._t0.lat 

543 

544 @Property_RO 

545 def latlonheight0(self): 

546 '''Get the origin's lat-, longitude and height (L{LatLon3Tuple}C{(lat, lon, height)}). 

547 ''' 

548 return LatLon3Tuple(self.lat0, self.lon0, self.height0, name=self.name) 

549 

550 def _local2ecef(self, local, nine=False, M=False): 

551 '''(INTERNAL) Convert I{local} to geocentric/geodetic, like I{.reverse}. 

552 

553 @arg local: Local (L{XyzLocal}, L{Enu}, L{Ned}, L{Aer} or L{Local9Tuple}). 

554 @kwarg nine: If C{True}, return a 9-, otherwise a 3-tuple (C{bool}). 

555 @kwarg M: Include the rotation matrix (C{bool}). 

556 

557 @return: A I{geocentric} 3-tuple C{(x, y, z)} or if C{B{nine}=True}, an 

558 L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with 

559 rotation matrix C{M} (L{EcefMatrix}) if requested. 

560 ''' 

561 _xinstanceof(*_XyzLocals5, local=local) 

562 t = self.M.unrotate(local.xyz, *self._t0_xyz) 

563 if nine: 

564 t = self.ecef.reverse(*t, M=M) 

565 return t 

566 

567 @Property_RO 

568 def lon0(self): 

569 '''Get the origin's longitude (C{degrees}). 

570 ''' 

571 return self._t0.lon 

572 

573 @Property 

574 def lon00(self): 

575 '''Get the arbitrary, I{polar} longitude (C{degrees}). 

576 ''' 

577 return self._lon00 

578 

579 @lon00.setter # PYCHOK setter! 

580 def lon00(self, lon00): 

581 '''Set the arbitrary, I{polar} longitude (C{degrees}). 

582 ''' 

583 # lon00 <https://GitHub.com/mrJean1/PyGeodesy/issues/77> 

584 self._lon00 = Degrees(lon00=lon00) 

585 

586 @Property_RO 

587 def M(self): 

588 '''Get the rotation matrix (C{EcefMatrix}). 

589 ''' 

590 return self._t0.M 

591 

592 def reset(self, latlonh0=INT0, lon0=INT0, height0=INT0, ecef=None, **lon00_name): 

593 '''Reset this converter, see L{LocalCartesian.__init__} for more details. 

594 ''' 

595 _, name = _xkwds_pop2(lon00_name, lon00=None) # PYCHOK get **name 

596 if isinstance(latlonh0, LocalCartesian): 

597 if self._t0: 

598 _update_all(self) 

599 self._ecef = latlonh0.ecef 

600 self._lon00 = latlonh0.lon00 

601 self._t0 = latlonh0._t0 

602 n = _name__(name, _or_nameof=latlonh0) 

603 else: 

604 n = _name__(name, _or_nameof=self) 

605 lat0, lon0, height0, n = _llhn4(latlonh0, lon0, height0, suffix=_0_, 

606 Error=LocalError, name=n) 

607 if ecef: # PYCHOK no cover 

608 _xinstanceof(self._Ecef, ecef=ecef) 

609 _update_all(self) 

610 self._ecef = ecef 

611 elif self._t0: 

612 _update_all(self) 

613 self._t0 = self.ecef._forward(lat0, lon0, height0, n, M=True) 

614 self.lon00 = _xattr(latlonh0, lon00=_xkwds_get(lon00_name, lon00=lon0)) 

615 if n: 

616 self.rename(n) 

617 

618 def reverse(self, xyz, y=None, z=None, M=False, **lon00_name): 

619 '''Convert I{local} C{(x, y, z)} to I{geodetic} C{(lat, lon, height)}. 

620 

621 @arg xyz: A I{local} (L{XyzLocal}, L{Enu}, L{Ned}, L{Aer}, L{Local9Tuple}) or 

622 local C{x} coordinate (C{scalar}). 

623 @kwarg y: Local C{y} coordinate for C{scalar} B{C{xyz}} and B{C{z}} (C{meter}). 

624 @kwarg z: Local C{z} coordinate for C{scalar} B{C{xyz}} and B{C{y}} (C{meter}). 

625 @kwarg M: Optionally, return the I{concatenated} rotation L{EcefMatrix}, iff 

626 available (C{bool}). 

627 @kwarg lon00_name: Optional C{B{name}=NN} (C{str}) and keyword argument 

628 C{B{lon00}=B{lon0}} for the arbitrary I{polar} longitude 

629 (C{degrees}), overriding see the property C{B{lon00}=B{lon0}} 

630 value. The I{polar} longitude (C{degrees}) is returned with 

631 I{polar} latitudes C{abs(B{lat0}) == 90} for local C{B{x}=0} 

632 and C{B{y}=0} locations. 

633 

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. 

639 

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 lon00, name =_xkwds_pop2(lon00_name, lon00=self.lon00) 

644 x, y, z, n = _xyzn4(xyz, y, z, _XyzLocals5, Error=LocalError, name=name) 

645 c = self.M.unrotate((x, y, z), *self._t0_xyz) 

646 t = self.ecef.reverse(*c, M=M, lon00=lon00) 

647 m = self.M.multiply(t.M) if M else None 

648 return self._9Tuple(x, y, z, t.lat, t.lon, t.height, self, t, m, name=n or self.name) 

649 

650 @Property_RO 

651 def _t0_xyz(self): 

652 '''(INTERNAL) Get C{(x0, y0, z0)} as L{Vector3Tuple}. 

653 ''' 

654 return self._t0.xyz 

655 

656 def toStr(self, prec=9, **unused): # PYCHOK signature 

657 '''Return this L{LocalCartesian} as a string. 

658 

659 @kwarg prec: Precision, number of (decimal) digits (0..9). 

660 

661 @return: This L{LocalCartesian} representation (C{str}). 

662 ''' 

663 return self.attrs(_lat0_, _lon0_, _height0_, _M_, _ecef_, _name_, prec=prec) 

664 

665 

666class Ltp(LocalCartesian): 

667 '''A I{local tangent plan} (LTP), a sub-class of C{LocalCartesian} with 

668 (re-)configurable ECEF converter. 

669 ''' 

670 _Ecef = _EcefBase 

671 

672 def __init__(self, latlonh0=INT0, lon0=INT0, height0=INT0, ecef=None, **lon00_name): 

673 '''New C{Ltp}, see L{LocalCartesian.__init__} for more details. 

674 

675 @kwarg ecef: Optional ECEF converter (L{EcefKarney}, L{EcefFarrell21}, 

676 L{EcefFarrell22}, L{EcefSudano}, L{EcefVeness} or 

677 L{EcefYou} I{instance}), overriding the default 

678 L{EcefKarney}C{(datum=Datums.WGS84)} for C{scalar}. 

679 

680 @see: Class L{LocalCartesian<LocalCartesian.__init__>} for further details. 

681 

682 @raise TypeError: Invalid B{C{ecef}}. 

683 ''' 

684 LocalCartesian.reset(self, latlonh0, lon0=lon0, height0=height0, 

685 ecef=ecef, **lon00_name) 

686 

687 @Property 

688 def ecef(self): 

689 '''Get this LTP's ECEF converter (C{Ecef...} I{instance}). 

690 ''' 

691 return self._ecef 

692 

693 @ecef.setter # PYCHOK setter! 

694 def ecef(self, ecef): 

695 '''Set this LTP's ECEF converter (C{Ecef...} I{instance}). 

696 

697 @raise TypeError: Invalid B{C{ecef}}. 

698 ''' 

699 _xinstanceof(_EcefBase, ecef=ecef) 

700 if self._ecef != ecef: # PYCHOK no cover 

701 self.reset(self._t0) 

702 self._ecef = ecef 

703 

704 

705class _ChLV(object): 

706 '''(INTERNAL) Base class for C{ChLV*} classes. 

707 ''' 

708 _03_falsing = ChLVyx2Tuple(0.6e6, 0.2e6) 

709# _92_falsing = ChLVYX2Tuple(2.0e6, 1.0e6) # _95_ - _03_ 

710 _95_falsing = ChLVEN2Tuple(2.6e6, 1.2e6) 

711 

712 def _ChLV9Tuple(self, fw, M, name, *Y_X_h_lat_lon_h): 

713 '''(INTERNAL) Helper for C{ChLVa/e.forward} and C{.reverse}. 

714 ''' 

715 if bool(M): # PYCHOK no cover 

716 m = self.forward if fw else self.reverse # PYCHOK attr 

717 n = _DOT_(self.__class__.__name__, m.__name__) 

718 raise _NotImplementedError(unstr(n, M=M), txt=None) 

719 t = Y_X_h_lat_lon_h + (self, self._t0, None) # PYCHOK _t0 

720 return ChLV9Tuple(t, name=name) 

721 

722 @property_ROver 

723 def _enh_n_h(self): 

724 '''(INTERNAL) Get C{ChLV*.reverse} args[1:4] names, I{once}. 

725 ''' 

726 t = _args_kwds_names(_ChLV.reverse)[1:4] 

727 # assert _args_kwds_names( ChLV.reverse)[1:4] == t 

728 # assert _args_kwds_names(ChLVa.reverse)[1:4] == t 

729 # assert _args_kwds_names(ChLVe.reverse)[1:4] == t 

730 return t # overwrite property_ROver 

731 

732 def forward(self, latlonh, lon=None, height=0, M=None, **name): # PYCHOK no cover 

733 '''Convert WGS84 geodetic to I{Swiss} projection coordinates. I{Must be overloaded}. 

734 

735 @arg latlonh: Either a C{LatLon}, L{Ltp} or C{scalar} (geodetic) latitude (C{degrees}). 

736 @kwarg lon: Optional, C{scalar} (geodetic) longitude for C{scalar} B{C{latlonh}} (C{degrees}). 

737 @kwarg height: Optional, height, vertically above (or below) the surface of the ellipsoid 

738 (C{meter}) for C{scalar} B{C{latlonh}} and B{C{lon}}. 

739 @kwarg M: If C{True}, return the I{concatenated} rotation L{EcefMatrix} iff available 

740 for C{ChLV} only, C{None} otherwise (C{bool}). 

741 @kwarg name: Optional C{B{name}=NN} (C{str}). 

742 

743 @return: A L{ChLV9Tuple}C{(Y, X, h_, lat, lon, height, ltp, ecef, M)} with the unfalsed 

744 I{Swiss Y, X} coordinates, I{Swiss h_} height, the given I{geodetic} C{lat}, 

745 C{lon} and C{height}, this C{ChLV*} instance and C{ecef} (L{Ecef9Tuple}) at 

746 I{Bern, Ch} and rotation matrix C{M}. The returned C{ltp} is this C{ChLV}, 

747 C{ChLVa} or C{ChLVe} instance. 

748 

749 @raise LocalError: Invalid or non-C{scalar} B{C{latlonh}}, B{C{lon}} or B{C{height}}. 

750 ''' 

751 notOverloaded(self, latlonh, lon=lon, height=height, M=M, **name) 

752 

753 def reverse(self, enh_, n=None, h_=0, M=None, **name): # PYCHOK no cover 

754 '''Convert I{Swiss} projection to WGS84 geodetic coordinates. 

755 

756 @arg enh_: A Swiss projection (L{ChLV9Tuple}) or the C{scalar}, falsed I{Swiss E_LV95} 

757 or I{y_LV03} easting (C{meter}). 

758 @kwarg n: Falsed I{Swiss N_LV85} or I{x_LV03} northing for C{scalar} B{C{enh_}} and 

759 B{C{h_}} (C{meter}). 

760 @kwarg h_: I{Swiss h'} height for C{scalar} B{C{enh_}} and B{C{n}} (C{meter}). 

761 @kwarg M: If C{True}, return the I{concatenated} rotation L{EcefMatrix} iff available 

762 for C{ChLV} only, C{None} otherwise (C{bool}). 

763 @kwarg name: Optional C{B{name}=NN} (C{str}). 

764 

765 @return: A L{ChLV9Tuple}C{(Y, X, h_, lat, lon, height, ltp, ecef, M)} with the unfalsed 

766 I{Swiss Y, X} coordinates, I{Swiss h_} height, the given I{geodetic} C{lat}, 

767 C{lon} and C{height}, this C{ChLV*} instance and C{ecef} (L{Ecef9Tuple}) at 

768 I{Bern, Ch} and rotation matrix C{M}. The returned C{ltp} is this C{ChLV}, 

769 C{ChLVa} or C{ChLVe} instance. 

770 

771 @raise LocalError: Invalid or non-C{scalar} B{C{enh_}}, B{C{n}} or B{C{h_}}. 

772 ''' 

773 notOverloaded(self, enh_, n=n, h_=h_, M=M, **name) 

774 

775 @staticmethod 

776 def _falsing2(LV95): 

777 '''(INTERNAL) Get the C{LV95} or C{LV03} falsing. 

778 ''' 

779 return _ChLV._95_falsing if LV95 in (True, 95) else ( 

780 _ChLV._03_falsing if LV95 in (False, 3) else ChLVYX2Tuple(0, 0)) 

781 

782 @staticmethod 

783 def _llh2abh_3(lat, lon, h): 

784 '''(INTERNAL) Helper for C{ChLVa/e.forward}. 

785 ''' 

786 def _deg2ab(deg, sLL): 

787 # convert degrees to arc-seconds 

788 def _dms(ds, p, q, swap): 

789 d = _floor(ds) 

790 t = (ds - d) * p 

791 m = _floor(t) 

792 s = (t - m) * p 

793 if swap: 

794 d, s = s, d 

795 return d + (m + s * q) * q 

796 

797 s = _dms(deg, _60_0, _0_01, False) # deg2sexag 

798 s = _dms( s, _100_0, _60_0, True) # sexag2asec 

799 return (s - sLL) / ChLV._s_ab 

800 

801 a = _deg2ab(lat, ChLV._sLat) # phi', lat_aux 

802 b = _deg2ab(lon, ChLV._sLon) # lam', lng_aux 

803 h_ = fsumf_(h, -ChLV.Bern.height, 2.73 * b, 6.94 * a) 

804 return a, b, h_ 

805 

806 @staticmethod 

807 def _YXh_2abh3(Y, X, h_): 

808 '''(INTERNAL) Helper for C{ChLVa/e.reverse}. 

809 ''' 

810 def _YX2ab(YX): 

811 return YX * ChLV._ab_m 

812 

813 a, b = map1(_YX2ab, Y, X) 

814 h = fsumf_(h_, ChLV.Bern.height, -12.6 * a, -22.64 * b) 

815 return a, b, h 

816 

817 def _YXh_n4(self, enh_, n, h_, **name): 

818 '''(INTERNAL) Helper for C{ChLV*.reverse}. 

819 ''' 

820 Y, X, h_, name = _xyzn4(enh_, n, h_, ChLV9Tuple, 

821 _xyz_y_z_names=self._enh_n_h, **name) 

822 if isinstance(enh_, ChLV9Tuple): 

823 Y, X = enh_.Y, enh_.X 

824 else: # isscalar(enh_) 

825 Y, X = ChLV.unfalse2(Y, X) # PYCHOK ChLVYX2Tuple 

826 return Y, X, h_, name 

827 

828 

829class ChLV(_ChLV, Ltp): 

830 '''Conversion between I{WGS84 geodetic} and I{Swiss} projection coordinates using 

831 L{pygeodesy.EcefKarney}'s Earth-Centered, Earth-Fixed (ECEF) methods. 

832 

833 @see: U{Swiss projection formulas<https://www.SwissTopo.admin.CH/en/maps-data-online/ 

834 calculation-services.html>}, page 7ff, U{NAVREF<https://www.SwissTopo.admin.CH/en/ 

835 maps-data-online/calculation-services/navref.html>}, U{REFRAME<https://www.SwissTopo.admin.CH/ 

836 en/maps-data-online/calculation-services/reframe.html>} and U{SwissTopo Scripts GPS WGS84 

837 <-> LV03<https://GitHub.com/ValentinMinder/Swisstopo-WGS84-LV03>}. 

838 ''' 

839 _9Tuple = ChLV9Tuple 

840 

841 _ab_d = 0.36 # a, b units per degree, ... 

842 _ab_m = 1.0e-6 # ... per meter and ... 

843 _ab_M = _1_0 # ... per 1,000 Km or 1 Mm 

844 _s_d = _3600_0 # arc-seconds per degree ... 

845 _s_ab = _s_d / _ab_d # ... and per a, b unit 

846 _sLat = 169028.66 # Bern, Ch in ... 

847 _sLon = 26782.5 # ... arc-seconds ... 

848 # lat, lon, height == 46°57'08.66", 7°26'22.50", 49.55m ("new" 46°57'07.89", 7°26'22.335") 

849 Bern = LatLon4Tuple(_sLat / _s_d, _sLon / _s_d, 49.55, _WGS84, name='Bern') 

850 

851 def __init__(self, latlonh0=Bern, **other_Ltp_kwds): 

852 '''New ECEF-based I{WGS84-Swiss} L{ChLV} converter, centered at I{Bern, Ch}. 

853 

854 @kwarg latlonh0: The I{geodetic} origin and height, overriding C{Bern, Ch}. 

855 @kwarg other_Ltp_kwds: Optional, other L{Ltp.__init__} keyword arguments. 

856 

857 @see: L{Ltp.__init__} for more information. 

858 ''' 

859 Ltp.__init__(self, latlonh0, **_xkwds(other_Ltp_kwds, ecef=None, name=ChLV.Bern.name)) 

860 

861 def forward(self, latlonh, lon=None, height=0, M=None, **name): # PYCHOK unused M 

862 # overloaded for the _ChLV.forward.__doc__ 

863 return Ltp.forward(self, latlonh, lon=lon, height=height, M=M, **name) 

864 

865 def reverse(self, enh_, n=None, h_=0, M=None, **name): # PYCHOK signature 

866 # overloaded for the _ChLV.reverse.__doc__ 

867 Y, X, h_, n = self._YXh_n4(enh_, n, h_, **name) 

868 return Ltp.reverse(self, Y, X, h_, M=M, name=n) 

869 

870 @staticmethod 

871 def false2(Y, X, LV95=True, **name): 

872 '''Add the I{Swiss LV95} or I{LV03} falsing. 

873 

874 @arg Y: Unfalsed I{Swiss Y} easting (C{meter}). 

875 @arg X: Unfalsed I{Swiss X} northing (C{meter}). 

876 @kwarg LV95: If C{True}, add C{LV95} falsing, if C{False} add 

877 C{LV03} falsing, otherwise leave unfalsed. 

878 @kwarg name: Optional C{B{name}=NN} (C{str}). 

879 

880 @return: A L{ChLVEN2Tuple}C{(E_LV95, N_LV95)} or a 

881 L{ChLVyx2Tuple}C{(y_LV03, x_LV03)} with falsed B{C{Y}} 

882 and B{C{X}}, otherwise a L{ChLVYX2Tuple}C{(Y, X)} 

883 with B{C{Y}} and B{C{X}} as-is. 

884 ''' 

885 e, n = t = _ChLV._falsing2(LV95) 

886 return t.classof(e + Y, n + X, **name) 

887 

888 @staticmethod 

889 def isLV03(e, n): 

890 '''Is C{(B{e}, B{n})} a valid I{Swiss LV03} projection? 

891 

892 @arg e: Falsed (or unfalsed) I{Swiss} easting (C{meter}). 

893 @arg n: Falsed (or unfalsed) I{Swiss} northing (C{meter}). 

894 

895 @return: C{True} if C{(B{e}, B{n})} is a valid, falsed I{Swiss 

896 LV03}, projection C{False} otherwise. 

897 ''' 

898 # @see: U{Map<https://www.SwissTopo.admin.CH/en/knowledge-facts/ 

899 # surveying-geodesy/reference-frames/local/lv95.html>} 

900 return 400.0e3 < e < 900.0e3 and 40.0e3 < n < 400.0e3 

901 

902 @staticmethod 

903 def isLV95(e, n, raiser=True): 

904 '''Is C{(B{e}, B{n})} a valid I{Swiss LV95} or I{LV03} projection? 

905 

906 @arg e: Falsed (or unfalsed) I{Swiss} easting (C{meter}). 

907 @arg n: Falsed (or unfalsed) I{Swiss} northing (C{meter}). 

908 @kwarg raiser: If C{True}, throw a L{LocalError} if B{C{e}} and 

909 B{C{n}} are invalid I{Swiss LV95} nor I{LV03}. 

910 

911 @return: C{True} or C{False} if C{(B{e}, B{n})} is a valid I{Swiss 

912 LV95} respectively I{LV03} projection, C{None} otherwise. 

913 ''' 

914 if ChLV.isLV03(e, n): 

915 return False 

916 elif ChLV.isLV03(e - 2.0e6, n - 1.0e6): # _92_falsing = _95_ - _03_ 

917 return True 

918 elif raiser: # PYCHOK no cover 

919 raise LocalError(unstr(ChLV.isLV95, e=e, n=n)) 

920 return None 

921 

922 @staticmethod 

923 def unfalse2(e, n, LV95=None, **name): 

924 '''Remove the I{Swiss LV95} or I{LV03} falsing. 

925 

926 @arg e: Falsed I{Swiss E_LV95} or I{y_LV03} easting (C{meter}). 

927 @arg n: Falsed I{Swiss N_LV95} or I{x_LV03} northing (C{meter}). 

928 @kwarg LV95: If C{True}, remove I{LV95} falsing, if C{False} remove 

929 I{LV03} falsing, otherwise use method C{isLV95(B{e}, B{n})}. 

930 @kwarg name: Optional C{B{name}=NN} (C{str}). 

931 

932 @return: A L{ChLVYX2Tuple}C{(Y, X)} with the unfalsed B{C{e}} 

933 respectively B{C{n}}. 

934 ''' 

935 Y, X = _ChLV._falsing2(ChLV.isLV95(e, n) if LV95 is None else LV95) 

936 return ChLVYX2Tuple(e - Y, n - X, **name) 

937 

938 

939class ChLVa(_ChLV, LocalCartesian): 

940 '''Conversion between I{WGS84 geodetic} and I{Swiss} projection coordinates 

941 using the U{Approximate<https://www.SwissTopo.admin.CH/en/maps-data-online/ 

942 calculation-services.html>} formulas, page 13. 

943 

944 @see: Older U{references<https://GitHub.com/alphasldiallo/Swisstopo-WGS84-LV03>}. 

945 ''' 

946 def __init__(self, name=ChLV.Bern.name): 

947 '''New I{Approximate WGS84-Swiss} L{ChLVa} converter, centered at I{Bern, Ch}. 

948 

949 @kwarg name: Optional C{B{name}=Bern.name} (C{str}). 

950 ''' 

951 LocalCartesian.__init__(self, latlonh0=ChLV.Bern, name=name) 

952 

953 def forward(self, latlonh, lon=None, height=0, M=None, **name): 

954 # overloaded for the _ChLV.forward.__doc__ 

955 lat, lon, h, n = _llhn4(latlonh, lon, height, **name) 

956 a, b, h_ = _ChLV._llh2abh_3(lat, lon, h) 

957 a2, b2 = a**2, b**2 

958 

959 Y = fdot_(211455.93, b, 

960 -10938.51, b * a, 

961 -0.36, b * a2, 

962 -44.54, b * b2, start=72.37) # + 600_000 

963 X = fdot_(308807.95, a, 

964 3745.25, b2, 

965 76.63, a2, 

966 -194.56, b2 * a, 

967 119.79, a2 * a, start=147.07) # + 200_000 

968 return self._ChLV9Tuple(True, M, n, Y, X, h_, lat, lon, h) 

969 

970 def reverse(self, enh_, n=None, h_=0, M=None, **name): # PYCHOK signature 

971 # overloaded for the _ChLV.reverse.__doc__ 

972 Y, X, h_, n = self._YXh_n4(enh_, n, h_, **name) 

973 a, b, h = _ChLV._YXh_2abh3(Y, X, h_) 

974 ab_d, a2, b2 = ChLV._ab_d, a**2, b**2 

975 

976 lat = fdot_(3.238272, b, 

977 -0.270978, a2, 

978 -0.002528, b2, 

979 -0.0447, a2 * b, 

980 -0.014, b2 * b, start=16.9023892) / ab_d 

981 lon = fdot_(4.728982, a, 

982 0.791484, a * b, 

983 0.1306, a * b2, 

984 -0.0436, a * a2, start=2.6779094) / ab_d 

985 return self._ChLV9Tuple(False, M, n, Y, X, h_, lat, lon, h) 

986 

987 

988class ChLVe(_ChLV, LocalCartesian): 

989 '''Conversion between I{WGS84 geodetic} and I{Swiss} projection coordinates 

990 using the U{Ellipsoidal approximate<https://www.SwissTopo.admin.CH/en/ 

991 maps-data-online/calculation-services.html>} formulas, pp 10-11 and U{Bolliger, 

992 J.<https://eMuseum.GGGS.CH/literatur-lv/liste-Dateien/1967_Bolliger_a.pdf>} 

993 pp 148-151 (also U{GGGS<https://eMuseum.GGGS.CH/literatur-lv/liste.htm>}). 

994 

995 @note: Methods L{ChLVe.forward} and L{ChLVe.reverse} have an additional keyword 

996 argument C{B{gamma}=False} to approximate the I{meridian convergence}. 

997 If C{B{gamma}=True} a 2-tuple C{(t, gamma)} is returned with C{t} the 

998 usual result (C{ChLV9Tuple}) and C{gamma}, the I{meridian convergence} 

999 (decimal C{degrees}). To convert C{gamma} to C{grades} or C{gons}, 

1000 use function L{pygeodesy.degrees2grades}. 

1001 

1002 @see: Older U{references<https://GitHub.com/alphasldiallo/Swisstopo-WGS84-LV03>}. 

1003 ''' 

1004 def __init__(self, name=ChLV.Bern.name): 

1005 '''New I{Approximate WGS84-Swiss} L{ChLVe} converter, centered at I{Bern, Ch}. 

1006 

1007 @kwarg name: Optional C{B{name}=Bern.name} (C{str}). 

1008 ''' 

1009 LocalCartesian.__init__(self, latlonh0=ChLV.Bern, name=name) 

1010 

1011 def forward(self, latlonh, lon=None, height=0, M=None, gamma=False, **name): # PYCHOK gamma 

1012 # overloaded for the _ChLV.forward.__doc__ 

1013 lat, lon, h, n = _llhn4(latlonh, lon, height, **name) 

1014 a, b, h_ = _ChLV._llh2abh_3(lat, lon, h) 

1015 ab_M, z, _H = ChLV._ab_M, 0, Fhorner 

1016 

1017 B1 = _H(a, 211428.533991, -10939.608605, -2.658213, -8.539078, -0.00345, -0.007992) 

1018 B3 = _H(a, -44.232717, 4.291740, -0.309883, 0.013924) 

1019 B5 = _H(a, 0.019784, -0.004277) 

1020 Y = _H(b, z, B1, z, B3, z, B5).fover(ab_M) # 1,000 Km! 

1021 

1022 B0 = _H(a, z, 308770.746371, 75.028131, 120.435227, 0.009488, 0.070332, -0.00001) 

1023 B2 = _H(a, 3745.408911, -193.792705, 4.340858, -0.376174, 0.004053) 

1024 B4 = _H(a, -0.734684, 0.144466, -0.011842) 

1025 X = _H(b, B0, z, B2, z, B4, z, 0.000488).fover(ab_M) # 1,000 Km! 

1026 

1027 t = self._ChLV9Tuple(True, M, n, Y, X, h_, lat, lon, h) 

1028 if gamma: 

1029 U1 = _H(a, 2255515.207166, 2642.456961, 1.284180, 2.577486, 0.001165) 

1030 U3 = _H(a, -412.991934, 64.106344, -2.679566, 0.123833) 

1031 U5 = _H(a, 0.204129, -0.037725) 

1032 g = _H(b, z, U1, z, U3, z, U5).fover(ChLV._ab_m) # * ChLV._ab_d degrees? 

1033 t = t, g 

1034 return t 

1035 

1036 def reverse(self, enh_, n=None, h_=0, M=None, gamma=False, **name): # PYCHOK gamma 

1037 # overloaded for the _ChLV.reverse.__doc__ 

1038 Y, X, h_, n = self._YXh_n4(enh_, n, h_, **name) 

1039 a, b, h = _ChLV._YXh_2abh3(Y, X, h_) 

1040 s_d, _H, z = ChLV._s_d, Fhorner, 0 

1041 

1042 A0 = _H(b, ChLV._sLat, 32386.4877666, -25.486822, -132.457771, 0.48747, 0.81305, -0.0069) 

1043 A2 = _H(b, -2713.537919, -450.442705, -75.53194, -14.63049, -2.7604) 

1044 A4 = _H(b, 24.42786, 13.20703, 4.7476) 

1045 lat = _H(a, A0, z, A2, z, A4, z, -0.4249).fover(s_d) 

1046 

1047 A1 = _H(b, 47297.3056722, 7925.714783, 1328.129667, 255.02202, 48.17474, 9.0243) 

1048 A3 = _H(b, -442.709889, -255.02202, -96.34947, -30.0808) 

1049 A5 = _H(b, 9.63495, 9.0243) 

1050 lon = _H(a, ChLV._sLon, A1, z, A3, z, A5).fover(s_d) 

1051 # == (ChLV._sLon + a * (A1 + a**2 * (A3 + a**2 * A5))) / s_d 

1052 

1053 t = self._ChLV9Tuple(False, M, n, Y, X, h_, lat, lon, h) 

1054 if gamma: 

1055 U1 = _H(b, 106679.792202, 17876.57022, 4306.5241, 794.87772, 148.1545, 27.8725) 

1056 U3 = _H(b, -1435.508, -794.8777, -296.309, -92.908) 

1057 U5 = _H(b, 29.631, 27.873) 

1058 g = _H(a, z, U1, z, U3, z, U5).fover(ChLV._s_ab) # degrees 

1059 t = t, g 

1060 return t 

1061 

1062 

1063def _fov_2(**fov): 

1064 # Half a field-of-view angle in C{degrees}. 

1065 f = Degrees(Error=LocalError, **fov) * _0_5 

1066 if EPS < f < _90_0: 

1067 return f 

1068 t = _invalid_ if f < 0 else _too_(_wide_ if f > EPS else _narrow_) 

1069 raise LocalError(txt=t, **fov) 

1070 

1071 

1072def _toLocal(inst, ltp, Xyz, Xyz_kwds): 

1073 '''(INTERNAL) Helper for C{CartesianBase.toAer}, C{CartesianBase.toEnu}, 

1074 C{CartesianBase.toLocal}, C{CartesianBase.toNed} and C{latLonBase.toLocal}. 

1075 ''' 

1076 return _xLtp(ltp, inst._Ltp)._ecef2local(inst._ecef9, Xyz, Xyz_kwds) 

1077 

1078 

1079def _toLtp(inst, Ecef, ecef9, name): 

1080 '''(INTERNAL) Helper for C{CartesianBase.toLtp}, C{ecef.toLtp} and C{latLonBase.toLtp}. 

1081 ''' 

1082 return inst._Ltp if (not name) and Ecef in (None, inst.Ecef) else \ 

1083 Ltp(ecef9, ecef=Ecef(inst.datum), name=inst._name__(name)) 

1084 

1085 

1086def tyr3d(tilt=INT0, yaw=INT0, roll=INT0, Vector=Vector3d, **name_Vector_kwds): 

1087 '''Convert an attitude pose into a (3-D) direction vector. 

1088 

1089 @kwarg tilt: Pitch, elevation from horizontal (C{degrees}), negative down 

1090 (clockwise rotation along and around the x-axis). 

1091 @kwarg yaw: Bearing, heading (compass C{degrees360}), clockwise from North 

1092 (counter-clockwise rotation along and around the z-axis). 

1093 @kwarg roll: Roll, bank (C{degrees}), positive to the right and down 

1094 (clockwise rotation along and around the y-axis). 

1095 @kwarg Vector: Class to return the direction vector (C{Cartesian}, 

1096 L{Vector3d} or C{Vector3Tuple}) or C{None}. 

1097 @kwarg name_Vector_kwds: Optional C{B{name}=NN} (C{str}) and optionally, 

1098 additional B{C{Vector}} keyword arguments, ignored if C{B{Vector} 

1099 is None}. 

1100 

1101 @return: A named B{C{Vector}} instance or if C{B{Vector} is None}, 

1102 a named L{Vector3Tuple}C{(x, y, z)}. 

1103 

1104 @raise AttitudeError: Invalid B{C{tilt}}, B{C{yaw}} or B{C{roll}}. 

1105 

1106 @raise TypeError: Invalid B{C{Vector}} or B{C{name_Vector_kwds}}. 

1107 

1108 @see: U{Yaw, pitch, and roll rotations<http://MSL.CS.UIUC.edu/planning/node102.html>} 

1109 and function L{pygeodesy.hartzell} argument C{los}, Line-Of-Sight. 

1110 ''' 

1111 v = Attitude4Tuple(_0_0, tilt, yaw, roll).tyr3d 

1112 if Vector is not type(v): 

1113 n, kwds = _name2__(name_Vector_kwds, name__=tyr3d) 

1114 v = Vector3Tuple(v.x, v.y, v.z, name=n) if Vector is None else \ 

1115 Vector(v.x, v.y, v.z, name=n, **kwds) 

1116 elif name_Vector_kwds: 

1117 n, _ = _name2__(name_Vector_kwds) 

1118 if n: 

1119 v = v.copy(name=n) 

1120 return v 

1121 

1122 

1123def _xLtp(ltp, *dflt): 

1124 '''(INTERNAL) Validate B{C{ltp}} if not C{None} else B{C{dflt}}. 

1125 ''' 

1126 if dflt and ltp is None: 

1127 ltp = dflt[0] 

1128 _xinstanceof(Ltp, LocalCartesian, ltp=ltp) 

1129 return ltp 

1130 

1131# **) MIT License 

1132# 

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

1134# 

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

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

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

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

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

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

1141# 

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

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

1144# 

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

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

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

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

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

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

1151# OTHER DEALINGS IN THE SOFTWARE.