1

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

3

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

5

6 I{Local cartesian} and I{local tangent plane} classes L{LocalCartesian}, approximations L{ChLVa}

7 and 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

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

15

16 from pygeodesy.basics import isscalar, issubclassof, map1, map2, \

17 _xargs_kwds_names

18 from 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, _xinstanceof

22 from pygeodesy.ecef import _EcefBase, EcefKarney, _llhn4, _xyzn4

23 from pygeodesy.errors import _NotImplementedError, _TypesError, _ValueError, \

24 _xattr, _xkwds, _xkwds_get

25 from pygeodesy.fmath import fabs, fdot, Fhorner

26 from pygeodesy.fsums import _floor, Fsum, fsumf_, fsum1f_

27 from pygeodesy.interns import NN, _0_, _COMMASPACE_, _DOT_, _ecef_, _height_, \

28 _invalid_, _lat0_, _lon0_, _ltp_, _M_, _name_, _too_

29# from pygeodesy.lazily import _ALL_LAZY # from vector3d

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

31 ChLVYX2Tuple, Footprint5Tuple, Local9Tuple, \

32 ChLVyx2Tuple, _XyzLocals4, _XyzLocals5, Xyz4Tuple

33 from pygeodesy.named import _NamedBase, notOverloaded

34 from pygeodesy.namedTuples import LatLon3Tuple, LatLon4Tuple, Vector3Tuple

35 from pygeodesy.props import Property, Property_RO, property_doc_, property_RO, \

36 _update_all

37 from pygeodesy.streprs import Fmt, strs, unstr

38 from pygeodesy.units import Bearing, Degrees, Meter

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

40 wrap180, wrap360

41 from pygeodesy.vector3d import _ALL_LAZY, Vector3d

42

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

44

45__all__ = _ALL_LAZY.ltp

46__version__ = '23.11.16'

47

48_height0_ = _height_ + _0_

49_narrow_ = 'narrow'

50_wide_ = 'wide'

51_Xyz_ = 'Xyz'

52

53

54 def _fov_2(**fov):

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

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

57 if EPS < f < _90_0:

58 return f

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

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

61

62

63 class Attitude(_NamedBase):

64 '''The orientation of a plane or camera in space.

65 '''

66 _alt = Meter( alt =_0_0)

67 _roll = Degrees(roll=_0_0)

68 _tilt = Degrees(tilt=_0_0)

69 _yaw = Bearing(yaw =_0_0)

70

71 def __init__(self, alt_attitude=INT0, tilt=INT0, yaw=INT0, roll=INT0, name=NN):

72 '''New L{Attitude}.

73

74 @kwarg alt_attitude: An altitude (C{meter}) above earth or an attitude

75 (L{Attitude} or L{Attitude4Tuple}) with the

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

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

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

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

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

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

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

83 @kwarg name: Optional name C{str}).

84

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

86 B{C{roll}}.

87

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

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

90 '''

91 if isscalar(alt_attitude):

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

93 else:

94 try:

95 t = alt_attitude.atyr

96 except AttributeError:

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

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

99 if v:

100 setattr(self, n, v)

101 n = name or t.name

102 if n:

103 self.name = n

104

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

106 def alt(self):

107 return self._alt

108

109 @alt.setter # PYCHOK setter!

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

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

112 if self._alt != a:

113 _update_all(self)

114 self._alt = a

115

116 altitude = alt

117

118 @Property_RO

119 def atyr(self):

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

121 '''

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

123

124 @Property_RO

125 def matrix(self):

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

127

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

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

130 '''

131 _f = fsum1f_

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

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

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

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

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

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

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

139

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

141 def roll(self):

142 return self._roll

143

144 @roll.setter # PYCHOK setter!

145 def roll(self, roll):

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

147 if self._roll != r:

148 _update_all(self)

149 self._roll = r

150

151 bank = roll

152

153 def rotate(self, x_xyz, y=None, z=None, Vector=None, **Vector_kwds):

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

155

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

157 (C{Cartesian}, L{Vector3d} or L{Vector3Tuple}).

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

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

160 @kwarg Vector: Class to return transformed point (C{Cartesian},

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

162 @kwarg Vector_kwds: Optional, additional B{C{Vector}} keyword arguments,

163 ignored if C{B{Vector} is None}.

164

165 @return: A B{C{Vector}} instance or a L{Vector3Tuple}C{(x, y, z)} if

166 C{B{Vector}=None}.

167

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

169

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

171 '''

172 try:

173 try:

174 xyz = map2(float, x_xyz.xyz)

175 except AttributeError:

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

177 except (TypeError, ValueError) as x:

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

179

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

181 return Vector3Tuple(x, y, z, name=self.name) if Vector is None else \

182 Vector(x, y, z, **_xkwds(Vector_kwds, name=self.name))

183

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

185 def tilt(self):

186 return self._tilt

187

188 @tilt.setter # PYCHOK setter!

189 def tilt(self, tilt):

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

191 if self._tilt != t:

192 _update_all(self)

193 self._tilt = t

194

195 elevation = pitch = tilt

196

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

198 '''Format this attitude as string.

199

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

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

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

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

204

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

206 '''

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

208

209 @Property_RO

210 def tyr3d(self):

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

212

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

214 '''

215 def _r2d(r):

216 return fsumf_(_N_1_0, *r)

217

218 return Vector3d(*map(_r2d, self.matrix), name=tyr3d.__name__)

219

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

221 def yaw(self):

222 return self._yaw

223

224 @yaw.setter # PYCHOK setter!

225 def yaw(self, yaw):

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

227 if self._yaw != y:

228 _update_all(self)

229 self._yaw = y

230

231 bearing = heading = yaw

232

233

234 class AttitudeError(_ValueError):

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

236 '''

237 pass

238

239

240 class Frustum(_NamedBase):

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

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

243

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

245 '''

246 _h_2 = _0_0 # half hfov in degrees

247 _ltp = None # local tangent plane

248 _tan_h_2 = _0_0 # tan(_h_2)

249 _v_2 = _0_0 # half vfov in degrees

250

251 def __init__(self, hfov, vfov, ltp=None):

252 '''New L{Frustum}.

253

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

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

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

257

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

259 '''

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

261 self._v_2 = _fov_2(vfov=vfov)

262

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

264

265 if ltp:

266 self._ltp = _xLtp(ltp)

267

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

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

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

271

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

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

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

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

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

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

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

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

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

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

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

283 frustum's C{ltp}.

284

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

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

287

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

289

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

291

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

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

294 over the horizon.

295

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

297 '''

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

299 # left and right corners, or swapped

300 if r < EPS: # no roll

301 r = a * tan_h_2

302 l = -r # PYCHOK l is ell

303 else: # roll

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

305 r *= -a # negate right positive

306 l *= -a # PYCHOK l is ell

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

308 return (l, y), (r, y)

309

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

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

312 s, c = sincos2d(b)

313 _f = fsum1f_

314 for x, y in xy5:

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

316 _f(y * c, -x * s), z, ltp)

317

318 try:

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

320 except AttributeError:

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

322

323 a = Meter(altitude=a)

324 if a < EPS: # too low

325 raise _ValueError(altitude=a)

326 if z: # PYCHOK no cover

327 z = Meter(z=z)

328 a -= z

329 if a < EPS: # z above a

330 raise _ValueError(altitude_z=a)

331 else:

332 z = _0_0

333

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

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

336 if not EPS < e < _180_0:

337 raise _ValueError(tilt=t)

338 if e > _90_0:

339 e = _loneg(e)

340 b = _umod_360(b + _180_0)

341

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

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

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

345 if fabs(y) < EPS:

346 y = _0_0

347

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

349 # center and corners, clockwise from upperleft, rolled

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

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

352 # turn center and corners by yaw, clockwise

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

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

355

356 @Property_RO

357 def hfov(self):

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

359 '''

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

361

362 @Property_RO

363 def ltp(self):

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

365 '''

366 return self._ltp

367

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

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

370

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

372 @kwarg fmt: Optional, C{float} format (C{str}).

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

374

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

376 '''

377 t = self.hfov, self.vfov

378 if self.ltp:

379 t += self.ltp,

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

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

382

383 @Property_RO

384 def vfov(self):

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

386 '''

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

388

389

390 class LocalError(_ValueError):

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

392 '''

393 pass

394

395

396 class LocalCartesian(_NamedBase):

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

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

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

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

401 classGeographicLib_1_1LocalCartesian.html>}.

402

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

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

405

406 The conversions all take place via geocentric coordinates using a

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

408

409 @see: Class L{Ltp}.

410 '''

411 _ecef = EcefKarney(_WGS84)

412 _Ecef = EcefKarney

413 _lon00 = INT0 # self.lon0

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

415 _9Tuple = Local9Tuple

416

417 def __init__(self, latlonh0=INT0, lon0=INT0, height0=INT0, ecef=None, name=NN, **lon00):

418 '''New L{LocalCartesian} converter.

419

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

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

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

423 @kwarg lon0: Longitude of the (goedetic) origin (C{degrees}) for C{scalar}

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

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

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

427 surface and for C{scalar} B{C{latlonh0}}, ignored otherwise.

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

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

430 @kwarg name: Optional name (C{str}).

431 @kwarg lon00: An arbitrary, I{polar} longitude (C{degrees}), overriding

432 the default C{B{lon00}=B{lon0}}, see method C{reverse}.

433

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

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

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

437

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

439

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

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

442 '''

443 self.reset(latlonh0, lon0=lon0, height0=height0, ecef=ecef, name=name, **lon00)

444

445 def __eq__(self, other):

446 '''Compare this and an other instance.

447

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

449

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

451 '''

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

453 other.ecef == self.ecef and

454 other._t0 == self._t0)

455

456 @Property_RO

457 def datum(self):

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

459 '''

460 return self.ecef.datum

461

462 @Property_RO

463 def ecef(self):

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

465 '''

466 return self._ecef

467

468 def _ecef2local(self, ecef, Xyz, Xyz_kwds):

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

470

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

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

473 @arg Xyz_kwds: B{C{Xyz}} keyword arguments, ignored if C{B{Xyz} is None}.

474

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

476 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 ltp = self

482 if ecef.datum != ltp.datum:

483 ecef = ecef.toDatum(ltp.datum)

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

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

486 ltp, ecef, None, name=ecef.name)

487 if Xyz:

488 if not issubclassof(Xyz, *_XyzLocals4): # Vector3d

489 raise _TypesError(_Xyz_, Xyz, *_XyzLocals4)

490 r = r.toXyz(Xyz=Xyz, **Xyz_kwds)

491 return r

492

493 @Property_RO

494 def ellipsoid(self):

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

496 '''

497 return self.ecef.datum.ellipsoid

498

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

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

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

502

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

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

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

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

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

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

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

510 iff available (C{bool}).

511 @kwarg name: Optional name (C{str}).

512

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

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

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

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

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

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

519

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

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

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

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

524 '''

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

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

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

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

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

530

531 @Property_RO

532 def height0(self):

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

534 '''

535 return self._t0.height

536

537 @Property_RO

538 def lat0(self):

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

540 '''

541 return self._t0.lat

542

543 @Property_RO

544 def latlonheight0(self):

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

546 '''

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

548

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

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

551

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

553 @kwarg nine: Return 3- or 9-tuple (C{bool}).

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

555

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

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

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

559 '''

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

561 if nine:

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

563 return t

564

565 @Property_RO

566 def lon0(self):

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

568 '''

569 return self._t0.lon

570

571 @Property

572 def lon00(self):

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

574 '''

575 return self._lon00

576

577 @lon00.setter # PYCHOK setter!

578 def lon00(self, lon00):

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

580 '''

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

582 self._lon00 = Degrees(lon00=lon00)

583

584 @Property_RO

585 def M(self):

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

587 '''

588 return self._t0.M

589

590 def reset(self, latlonh0=INT0, lon0=INT0, height0=INT0, ecef=None, name=NN, **lon00):

591 '''Reset this converter, see L{LocalCartesian.__init__} and L{Ltp.__init__} for more details.

592 '''

593 if isinstance(latlonh0, LocalCartesian):

594 if self._t0:

595 _update_all(self)

596 self._ecef = latlonh0.ecef

597 self._lon00 = latlonh0.lon00

598 self._t0 = latlonh0._t0

599 n = name or latlonh0.name

600 else:

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

602 Error=LocalError, name=name or self.name)

603 if ecef: # PYCHOK no cover

604 _xinstanceof(self._Ecef, ecef=ecef)

605 _update_all(self)

606 self._ecef = ecef

607 elif self._t0:

608 _update_all(self)

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

610 self.lon00 = _xattr(latlonh0, lon00=_xkwds_get(lon00, lon00=lon0))

611 if n:

612 self.rename(n)

613

614 def reverse(self, xyz, y=None, z=None, M=False, name=NN, **lon00):

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

616

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

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

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

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

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

622 available (C{bool}).

623 @kwarg name: Optional name (C{str}).

624 @kwarg lon00: An arbitrary, I{polar} longitude (C{degrees}), returned for local

625 C{B{x}=0} and C{B{y}=0} at I{polar} latitudes C{abs(B{lat0}) == 90},

626 overriding property C{lon00} and default C{B{lon00}=B{lon0}}.

627

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

629 I{local} C{x}, C{y}, C{z}, I{geodetic} C{lat}, C{lon}, C{height},

630 this C{ltp}, an C{ecef} (L{Ecef9Tuple}) with the I{geocentric} C{x},

631 C{y}, C{z} (and I{geodetic} C{lat}, C{lon}, C{height}) and the

632 I{concatenated} rotation matrix C{M} (L{EcefMatrix}) if requested.

633

634 @raise LocalError: Invalid B{C{xyz}} or C{scalar} C{x} or B{C{y}} and/or B{C{z}}

635 not C{scalar} for C{scalar} B{C{xyz}}.

636 '''

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

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

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

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

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

642

643 @Property_RO

644 def _t0_xyz(self):

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

646 '''

647 return self._t0.xyz

648

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

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

651

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

653

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

655 '''

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

657

658

659 class Ltp(LocalCartesian):

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

661 (re-)configurable ECEF converter.

662 '''

663 _Ecef = _EcefBase

664

665 def __init__(self, latlonh0=INT0, lon0=INT0, height0=INT0, ecef=None, name=NN, **lon00):

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

667

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

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

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

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

672

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

674 '''

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

676 ecef=ecef, name=name, **lon00)

677

678 @Property

679 def ecef(self):

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

681 '''

682 return self._ecef

683

684 @ecef.setter # PYCHOK setter!

685 def ecef(self, ecef):

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

687

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

689 '''

690 _xinstanceof(_EcefBase, ecef=ecef)

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

692 self.reset(self._t0)

693 self._ecef = ecef

694

695

696 class _ChLV(object):

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

698 '''

699 _03_falsing = ChLVyx2Tuple(0.6e6, 0.2e6)

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

701 _95_falsing = ChLVEN2Tuple(2.6e6, 1.2e6)

702

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

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

705 '''

706 if bool(M): # PYCHOK no cover

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

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

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

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

711 return ChLV9Tuple(t, name=name)

712

713 @property_RO

714 def _enh_n_h(self):

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

716 '''

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

718 # assert _xargs_kwds_names( ChLV.reverse)[1:4] == t

719 # assert _xargs_kwds_names(ChLVa.reverse)[1:4] == t

720 # assert _xargs_kwds_names(ChLVe.reverse)[1:4] == t

721 return t

722

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

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

725

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

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

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

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

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

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

732 @kwarg name: Optional name (C{str}).

733

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

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

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

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

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

739

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

741 '''

742 notOverloaded(self, latlonh, lon=lon, height=height, M=M, name=name)

743

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

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

746

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

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

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

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

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

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

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

754 @kwarg name: Optional name (C{str}).

755

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

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

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

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

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

761

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

763 '''

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

765

766 @staticmethod

767 def _falsing2(LV95):

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

769 '''

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

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

772

773 @staticmethod

774 def _llh2abh_3(lat, lon, h):

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

776 '''

777 def _deg2ab(deg, sLL):

778 # convert degrees to arc-seconds

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

780 d = _floor(ds)

781 t = (ds - d) * p

782 m = _floor(t)

783 s = (t - m) * p

784 if swap:

785 d, s = s, d

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

787

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

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

790 return (s - sLL) / ChLV._s_ab

791

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

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

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

795 return a, b, h_

796

797 @staticmethod

798 def _YXh_2abh3(Y, X, h_):

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

800 '''

801 def _YX2ab(YX):

802 return YX * ChLV._ab_m

803

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

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

806 return a, b, h

807

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

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

810 '''

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

812 _xyz_y_z_names=self._enh_n_h, **name)

813 if isinstance(enh_, ChLV9Tuple):

814 Y, X = enh_.Y, enh_.X

815 else: # isscalar(enh_)

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

817 return Y, X, h_, name

818

819

820 class ChLV(_ChLV, Ltp):

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

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

823

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

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

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

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

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

829 '''

830 _9Tuple = ChLV9Tuple

831

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

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

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

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

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

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

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

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

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

841

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

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

844

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

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

847

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

849 '''

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

851

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

853 # overloaded for the _ChLV.forward.__doc__

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

855

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

857 # overloaded for the _ChLV.reverse.__doc__

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

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

860

861 @staticmethod

862 def false2(Y, X, LV95=True, name=NN):

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

864

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

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

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

868 C{LV03} falsing, otherwise leave unfalsed.

869 @kwarg name: Optional name (C{str}).

870

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

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

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

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

875 '''

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

877 return t.classof(e + Y, n + X, name=name)

878

879 @staticmethod

880 def isLV03(e, n):

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

882

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

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

885

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

887 LV03}, projection C{False} otherwise.

888 '''

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

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

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

892

893 @staticmethod

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

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

896

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

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

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

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

901

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

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

904 '''

905 if ChLV.isLV03(e, n):

906 return False

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

908 return True

909 elif raiser: # PYCHOK no cover

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

911 return None

912

913 @staticmethod

914 def unfalse2(e, n, LV95=None, name=NN):

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

916

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

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

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

920 I{LV03} falsing, otherwise use method C{isLV95(B{e},

921 B{n})}.

922 @kwarg name: Optional name (C{str}).

923

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

925 respectively B{C{n}}.

926 '''

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

928 return ChLVYX2Tuple(e - Y, n - X, name=name)

929

930

931 class ChLVa(_ChLV, LocalCartesian):

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

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

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

935

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

937 '''

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

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

940

941 @kwarg name: Optional name (C{str}), overriding C{Bern.name}.

942 '''

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

944

945 def forward(self, latlonh, lon=None, height=0, M=None, name=NN):

946 # overloaded for the _ChLV.forward.__doc__

947 lat, lon, h, name = _llhn4(latlonh, lon, height, name=name)

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

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

950

951 Y = fsumf_( 72.37, 211455.93 * b,

952 -10938.51 * b * a,

953 -0.36 * b * a2,

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

955 X = fsumf_(147.07, 308807.95 * a,

956 3745.25 * b2,

957 76.63 * a2,

958 -194.56 * b2 * a,

959 119.79 * a2 * a) # + 200_000

960 return self._ChLV9Tuple(True, M, name, Y, X, h_, lat, lon, h)

961

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

963 # overloaded for the _ChLV.reverse.__doc__

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

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

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

967

968 lat = Fsum(16.9023892, 3.238272 * b,

969 -0.270978 * a2,

970 -0.002528 * b2,

971 -0.0447 * a2 * b,

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

973 lon = Fsum( 2.6779094, 4.728982 * a,

974 0.791484 * a * b,

975 0.1306 * a * b2,

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

977 return self._ChLV9Tuple(False, M, name, Y, X, h_, lat, lon, h)

978

979

980 class ChLVe(_ChLV, LocalCartesian):

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

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

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

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

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

986

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

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

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

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

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

992 use function L{pygeodesy.degrees2grades}.

993

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

995 '''

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

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

998

999 @kwarg name: Optional name (C{str}), overriding C{Bern.name}.

1000 '''

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

1002

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

1004 # overloaded for the _ChLV.forward.__doc__

1005 lat, lon, h, name = _llhn4(latlonh, lon, height, name=name)

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

1007 ab_M, z, _F = ChLV._ab_M, 0, Fhorner

1008

1009 B1 = _F(a, 211428.533991, -10939.608605, -2.658213, -8.539078, -0.00345, -0.007992)

1010 B3 = _F(a, -44.232717, 4.291740, -0.309883, 0.013924)

1011 B5 = _F(a, 0.019784, -0.004277)

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

1013

1014 B0 = _F(a, z, 308770.746371, 75.028131, 120.435227, 0.009488, 0.070332, -0.00001)

1015 B2 = _F(a, 3745.408911, -193.792705, 4.340858, -0.376174, 0.004053)

1016 B4 = _F(a, -0.734684, 0.144466, -0.011842)

1017 B6 = 0.000488

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

1019

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

1021 if gamma:

1022 U1 = _F(a, 2255515.207166, 2642.456961, 1.284180, 2.577486, 0.001165)

1023 U3 = _F(a, -412.991934, 64.106344, -2.679566, 0.123833)

1024 U5 = _F(a, 0.204129, -0.037725)

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

1026 t = t, g

1027 return t

1028

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

1030 # overloaded for the _ChLV.reverse.__doc__

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

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

1033 s_d, _F, z = ChLV._s_d, Fhorner, 0

1034

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

1036 A2 = _F(b, -2713.537919, -450.442705, -75.53194, -14.63049, -2.7604)

1037 A4 = _F(b, 24.42786, 13.20703, 4.7476)

1038 A6 = -0.4249

1039 lat = _F(a, A0, z, A2, z, A4, z, A6).fover(s_d)

1040

1041 A1 = _F(b, 47297.3056722, 7925.714783, 1328.129667, 255.02202, 48.17474, 9.0243)

1042 A3 = _F(b, -442.709889, -255.02202, -96.34947, -30.0808)

1043 A5 = _F(b, 9.63495, 9.0243)

1044 lon = _F(a, ChLV._sLon, A1, z, A3, z, A5).fover(s_d)

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

1046

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

1048 if gamma:

1049 U1 = _F(b, 106679.792202, 17876.57022, 4306.5241, 794.87772, 148.1545, 27.8725)

1050 U3 = _F(b, -1435.508, -794.8777, -296.309, -92.908)

1051 U5 = _F(b, 29.631, 27.873)

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

1053 t = t, g

1054 return t

1055

1056

1057 def tyr3d(tilt=INT0, yaw=INT0, roll=INT0, Vector=Vector3d, **Vector_kwds):

1058 '''Convert an attitude oriention into a (3-D) direction vector.

1059

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

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

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

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

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

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

1066

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

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

1069

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

1071 and function L{pygeodesy.hartzell} argument C{los}.

1072 '''

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

1074 return d if Vector is type(d) else (

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

1076 Vector(d.x, d.y, d.z, **_xkwds(Vector_kwds, name=d.name))) # PYCHOK indent

1077

1078

1079 def _xLtp(ltp, *dflt):

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

1081 '''

1082 if dflt and ltp is None:

1083 ltp = dflt[0]

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

1085 return ltp

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

1087

1088# **) MIT License

1089#

1090# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.

1091#

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

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

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

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

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

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

1098#

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

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

1101#

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

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

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

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

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

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

1108# OTHER DEALINGS IN THE SOFTWARE.