Coverage for pygeodesy/ltp.py: 95%

421 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-06-01 11:43 -0400

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, _xsubclassof # .datums 

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, _xkwds_pop2 

24from pygeodesy.fmath import fabs, fdot, Fhorner 

25from pygeodesy.fsums import _floor, _Fsumf_, fsumf_, fsum1f_ 

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

27 _invalid_, _lat0_, _lon0_, _ltp_, _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 _name__, _name2__, _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, _isHeight, Meter 

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

39 wrap180, wrap360 

40from pygeodesy.vector3d import _ALL_LAZY, Vector3d 

41 

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

43 

44__all__ = _ALL_LAZY.ltp 

45__version__ = '24.05.31' 

46 

47_height0_ = _height_ + _0_ 

48_narrow_ = 'narrow' 

49_wide_ = 'wide' 

50 

51 

52class Attitude(_NamedBase): 

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

54 ''' 

55 _alt = Meter( alt =_0_0) 

56 _roll = Degrees(roll=_0_0) 

57 _tilt = Degrees(tilt=_0_0) 

58 _yaw = Bearing(yaw =_0_0) 

59 

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

61 '''New L{Attitude}. 

62 

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

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

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

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

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

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

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

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

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

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

73 

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

75 B{C{roll}}. 

76 

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

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

79 ''' 

80 if _isHeight(alt_attitude): 

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

82 else: 

83 try: 

84 t = alt_attitude.atyr 

85 except AttributeError: 

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

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

88 if v: 

89 setattr(self, n, v) 

90 n = _name__(name, _or_nameof=t) 

91 if n: 

92 self.name = n 

93 

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

95 def alt(self): 

96 return self._alt 

97 

98 @alt.setter # PYCHOK setter! 

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

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

101 if self._alt != a: 

102 _update_all(self) 

103 self._alt = a 

104 

105 altitude = alt 

106 

107 @Property_RO 

108 def atyr(self): 

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

110 ''' 

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

112 

113 @Property_RO 

114 def matrix(self): 

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

116 

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

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

119 ''' 

120 _f = fsum1f_ 

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

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

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

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

125 return ((ca * cb, _f(ca * sb * sg, -sa * cg), _f(ca * sb * cg, sa * sg)), 

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

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

128 

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

130 def roll(self): 

131 return self._roll 

132 

133 @roll.setter # PYCHOK setter! 

134 def roll(self, roll): 

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

136 if self._roll != r: 

137 _update_all(self) 

138 self._roll = r 

139 

140 bank = roll 

141 

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

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

144 

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

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

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

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

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

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

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

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

153 C{B{Vector} is None}. 

154 

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

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

157 

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

159 

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

161 

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

163 ''' 

164 try: 

165 try: 

166 xyz = map2(float, x_xyz.xyz) 

167 except AttributeError: 

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

169 except (TypeError, ValueError) as x: 

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

171 

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

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

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

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

176 

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

178 def tilt(self): 

179 return self._tilt 

180 

181 @tilt.setter # PYCHOK setter! 

182 def tilt(self, tilt): 

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

184 if self._tilt != t: 

185 _update_all(self) 

186 self._tilt = t 

187 

188 elevation = pitch = tilt 

189 

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

191 '''Format this attitude as string. 

192 

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

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

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

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

197 

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

199 ''' 

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

201 

202 @Property_RO 

203 def tyr3d(self): 

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

205 

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

207 ''' 

208 def _r2d(r): 

209 return fsumf_(_N_1_0, *r) 

210 

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

212 

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

214 def yaw(self): 

215 return self._yaw 

216 

217 @yaw.setter # PYCHOK setter! 

218 def yaw(self, yaw): 

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

220 if self._yaw != y: 

221 _update_all(self) 

222 self._yaw = y 

223 

224 bearing = heading = yaw 

225 

226 

227class AttitudeError(_ValueError): 

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

229 ''' 

230 pass 

231 

232 

233class Frustum(_NamedBase): 

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

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

236 

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

238 ''' 

239 _h_2 = _0_0 # half hfov in degrees 

240 _ltp = None # local tangent plane 

241 _tan_h_2 = _0_0 # tan(_h_2) 

242 _v_2 = _0_0 # half vfov in degrees 

243 

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

245 '''New L{Frustum}. 

246 

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

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

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

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

251 

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

253 ''' 

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

255 self._v_2 = _fov_2(vfov=vfov) 

256 

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

258 

259 if ltp: 

260 self._ltp = _xLtp(ltp) 

261 if name: 

262 self.name # PYCHOK effect 

263 

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

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

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

267 

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

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

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

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

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

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

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

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

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

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

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

279 frustum's C{ltp}. 

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

281 

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

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

284 

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

286 

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

288 

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

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

291 over the horizon. 

292 

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

294 ''' 

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

296 # left and right corners, or swapped 

297 if r < EPS: # no roll 

298 r = a * tan_h_2 

299 l = -r # PYCHOK l is ell 

300 else: # roll 

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

302 r *= -a # negate right positive 

303 l *= -a # PYCHOK l is ell 

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

305 return (l, y), (r, y) 

306 

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

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

309 s, c = sincos2d(b) 

310 _f = fsum1f_ 

311 for x, y in xy5: 

312 yield Xyz4Tuple(_f(x * c, y * s), 

313 _f(y * c, -x * s), 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}) for C{scalar} 

421 B{C{latlonh0}}, 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 and for C{scalar} B{C{latlonh0}}, ignored otherwise. 

425 @kwarg ecef: An ECEF converter (L{EcefKarney} I{only}) for C{scalar} 

426 B{C{latlonh0}}, ignored otherwise. 

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

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

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

430 for further details. 

431 

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

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

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

435 

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

437 

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

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

440 ''' 

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

442 

443 def __eq__(self, other): 

444 '''Compare this and an other instance. 

445 

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

447 

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

449 ''' 

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

451 other.ecef == self.ecef and 

452 other._t0 == self._t0) 

453 

454 @Property_RO 

455 def datum(self): 

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

457 ''' 

458 return self.ecef.datum 

459 

460 @Property_RO 

461 def ecef(self): 

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

463 ''' 

464 return self._ecef 

465 

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

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

468 

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

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

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

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

473 C{B{Xyz} is None}. 

474 

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

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

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

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

479 always. 

480 

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

482 ''' 

483 ltp = self 

484 if ecef.datum != ltp.datum: 

485 ecef = ecef.toDatum(ltp.datum) 

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

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

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

489 ltp, ecef, None, name=n) 

490 if Xyz: 

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

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

493 return r 

494 

495 @Property_RO 

496 def ellipsoid(self): 

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

498 ''' 

499 return self.ecef.datum.ellipsoid 

500 

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

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

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

504 

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

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

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

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

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

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

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

512 iff available (C{bool}). 

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

514 

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

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

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

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

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

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

521 

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

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

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

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

526 ''' 

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

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

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

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

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

532 

533 @Property_RO 

534 def height0(self): 

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

536 ''' 

537 return self._t0.height 

538 

539 @Property_RO 

540 def lat0(self): 

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

542 ''' 

543 return self._t0.lat 

544 

545 @Property_RO 

546 def latlonheight0(self): 

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

548 ''' 

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

550 

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

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

553 

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

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

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

557 

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

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

560 optionally including rotation matrix C{M} or C{None}. 

561 ''' 

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_RO 

723 def _enh_n_h(self): 

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

725 ''' 

726 _ChLV._enh_n_h = t = _args_kwds_names(_ChLV.reverse)[1:4] # overwrite property_RO 

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 

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

930 B{n})}. 

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

932 

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

934 respectively B{C{n}}. 

935 ''' 

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

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

938 

939 

940class ChLVa(_ChLV, LocalCartesian): 

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

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

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

944 

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

946 ''' 

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

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

949 

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

951 ''' 

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

953 

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

955 # overloaded for the _ChLV.forward.__doc__ 

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

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

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

959 

960 Y = fsumf_( 72.37, 211455.93 * b, 

961 -10938.51 * b * a, 

962 -0.36 * b * a2, 

963 -44.54 * b * b2) # + 600_000 

964 X = fsumf_(147.07, 308807.95 * a, 

965 3745.25 * b2, 

966 76.63 * a2, 

967 -194.56 * b2 * a, 

968 119.79 * a2 * a) # + 200_000 

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

970 

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

972 # overloaded for the _ChLV.reverse.__doc__ 

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

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

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

976 

977 lat = _Fsumf_(16.9023892, 3.238272 * b, 

978 -0.270978 * a2, 

979 -0.002528 * b2, 

980 -0.0447 * a2 * b, 

981 -0.014 * b2 * b).fover(ab_d) 

982 lon = _Fsumf_( 2.6779094, 4.728982 * a, 

983 0.791484 * a * b, 

984 0.1306 * a * b2, 

985 -0.0436 * a * a2).fover(ab_d) 

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

987 

988 

989class ChLVe(_ChLV, LocalCartesian): 

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

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

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

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

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

995 

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

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

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

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

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

1001 use function L{pygeodesy.degrees2grades}. 

1002 

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

1004 ''' 

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

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

1007 

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

1009 ''' 

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

1011 

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

1013 # overloaded for the _ChLV.forward.__doc__ 

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

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

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

1017 

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

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

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

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

1022 

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

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

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

1026 B6 = 0.000488 

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

1028 

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

1030 if gamma: 

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

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

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

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

1035 t = t, g 

1036 return t 

1037 

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

1039 # overloaded for the _ChLV.reverse.__doc__ 

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

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

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

1043 

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

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

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

1047 A6 = -0.4249 

1048 lat = _H(a, A0, z, A2, z, A4, z, A6).fover(s_d) 

1049 

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

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

1052 A5 = _H(b, 9.63495, 9.0243) 

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

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

1055 

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

1057 if gamma: 

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

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

1060 U5 = _H(b, 29.631, 27.873) 

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

1062 t = t, g 

1063 return t 

1064 

1065 

1066def _fov_2(**fov): 

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

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

1069 if EPS < f < _90_0: 

1070 return f 

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

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

1073 

1074 

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

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

1077 

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

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

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

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

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

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

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

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

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

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

1088 C{B{Vector} is None}. 

1089 

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

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

1092 

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

1094 

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

1096 

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

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

1099 ''' 

1100 d = Attitude4Tuple(_0_0, tilt, yaw, roll).tyr3d 

1101 if Vector is not type(d): 

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

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

1104 Vector(d.x, d.y, d.z, name=n, **kwds) 

1105 return d 

1106 

1107 

1108def _xLtp(ltp, *dflt): 

1109 '''(INTERNAL) Validate B{C{ltp}}. 

1110 ''' 

1111 if dflt and ltp is None: 

1112 ltp = dflt[0] 

1113 if isinstance(ltp, (LocalCartesian, Ltp)): 

1114 return ltp 

1115 raise _TypesError(_ltp_, ltp, Ltp, LocalCartesian) 

1116 

1117# **) MIT License 

1118# 

1119# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved. 

1120# 

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

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

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

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

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

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

1127# 

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

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

1130# 

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

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

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

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

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

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

1137# OTHER DEALINGS IN THE SOFTWARE.