Coverage for pygeodesy/cartesianBase.py: 95%

202 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-05-24 10:28 -0400

1 

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

3 

4u'''(INTERNAL) Private base classes for elliposiodal, spherical and N-/vectorial 

5C{Cartesian}s. 

6 

7After I{(C) Chris Veness 2011-2015} published under the same MIT Licence**, 

8see U{https://www.Movable-Type.co.UK/scripts/latlong.html}, 

9U{https://www.Movable-Type.co.UK/scripts/latlong-vectors.html} and 

10U{https://www.Movable-Type.co.UK/scripts/geodesy/docs/latlon-ellipsoidal.js.html}.. 

11''' 

12 

13# from pygeodesy.basics import _xinstanceof # from .datums 

14from pygeodesy.constants import EPS0, isnear0, _1_0, _N_1_0, _2_0, _4_0, _6_0 

15from pygeodesy.datums import Datum, _spherical_datum, _WGS84, _xinstanceof 

16from pygeodesy.errors import _IsnotError, _ValueError, _xdatum, _xkwds 

17from pygeodesy.fmath import cbrt, hypot_, hypot2, sqrt # hypot 

18from pygeodesy.fsums import Fmt, fsumf_ 

19from pygeodesy.interns import NN, _COMMASPACE_, _height_, _not_ 

20from pygeodesy.interns import _ellipsoidal_, _spherical_ # PYCHOK used! 

21from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS 

22from pygeodesy.namedTuples import Bearing2Tuple, Height, LatLon4Tuple, Vector4Tuple, \ 

23 Vector3Tuple # PYCHOK .ellipsoidal-, .sphericalBase 

24from pygeodesy.props import deprecated_method, Property, Property_RO, \ 

25 property_doc_, _update_all 

26# from pygeodesy.resections impoty cassini, collins5, pierlot, tienstra7 

27# from pygeodesy.streprs import Fmt # from .fsums 

28# from pygeodesy.units import Height # from .namedTuples 

29from pygeodesy.vector3d import Vector3d, _xyzhdn3 

30 

31# from math import sqrt # from .fmath 

32 

33__all__ = _ALL_LAZY.cartesianBase 

34__version__ = '23.05.23' 

35 

36 

37class CartesianBase(Vector3d): 

38 '''(INTERNAL) Base class for ellipsoidal and spherical C{Cartesian}. 

39 ''' 

40 _datum = None # L{Datum}, to be overriden 

41 _height = None # height (L{Height}), set or approximated 

42 

43 def __init__(self, x_xyz, y=None, z=None, datum=None, ll=None, name=NN): 

44 '''New C{Cartesian...}. 

45 

46 @arg x_xyz: Cartesian X coordinate (C{scalar}) or a C{Cartesian}, 

47 L{Ecef9Tuple}, L{Vector3Tuple} or L{Vector4Tuple}. 

48 @kwarg y: Cartesian Y coordinate (C{scalar}), ignored if B{C{x_xyz}} 

49 is not C{scalar}, otherwise same units as B{C{x_xyz}}. 

50 @kwarg z: Cartesian Z coordinate (C{scalar}), ignored if B{C{x_xyz}} 

51 is not C{scalar}, otherwise same units as B{C{x_xyz}}. 

52 @kwarg datum: Optional datum (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} 

53 or L{a_f2Tuple}). 

54 @kwarg ll: Optional, original latlon (C{LatLon}). 

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

56 

57 @raise TypeError: Non-scalar B{C{x_xyz}}, B{C{y}} or B{C{z}} 

58 coordinate or B{C{x_xyz}} not an L{Ecef9Tuple}, 

59 L{Vector3Tuple} or L{Vector4Tuple}. 

60 ''' 

61 h, d, n = _xyzhdn3(x_xyz, None, datum, ll) 

62 Vector3d.__init__(self, x_xyz, y=y, z=z, ll=ll, name=name or n) 

63 if h is not None: 

64 self._height = Height(h) 

65 if d is not None: 

66 self.datum = d 

67 

68# def __matmul__(self, other): # PYCHOK Python 3.5+ 

69# '''Return C{NotImplemented} for C{c_ = c @ datum} and C{c_ = c @ transform}. 

70# ''' 

71# return NotImplemented if isinstance(other, (Datum, Transform)) else \ 

72# _NotImplemented(self, other) 

73 

74 def cassini(self, pointB, pointC, alpha, beta, useZ=False): 

75 '''3-Point resection between this and 2 other points using U{Cassini 

76 <https://NL.WikiPedia.org/wiki/Achterwaartse_insnijding>}'s method. 

77 

78 @arg pointB: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, 

79 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}). 

80 @arg pointC: Center point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, 

81 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}). 

82 @arg alpha: Angle subtended by triangle side C{b} from B{C{pointA}} to 

83 B{C{pointC}} (C{degrees}, non-negative). 

84 @arg beta: Angle subtended by triangle side C{a} from B{C{pointB}} to 

85 B{C{pointC}} (C{degrees}, non-negative). 

86 @kwarg useZ: If C{True}, use and interpolate the Z component, otherwise 

87 force C{z=INT0} (C{bool}). 

88 

89 @note: Typically, B{C{pointC}} is between this and B{C{pointB}}. 

90 

91 @return: The survey point, an instance of this (sub-)class. 

92 

93 @raise ResectionError: Near-coincident, -colinear or -concyclic points 

94 or negative or invalid B{C{alpha}} or B{C{beta}}. 

95 

96 @raise TypeError: Invalid B{C{pointA}}, B{C{pointB}} or B{C{pointM}}. 

97 

98 @see: U{Three Point Resection Problem<https://Dokumen.tips/documents/ 

99 three-point-resection-problem-introduction-kaestner-burkhardt-method.html>} 

100 and function L{pygeodesy.cassini}. 

101 ''' 

102 return _MODS.resections.cassini(self, pointB, pointC, alpha, beta, 

103 useZ=useZ, datum=self.datum) 

104 

105 @deprecated_method 

106 def collins(self, pointB, pointC, alpha, beta, useZ=False): 

107 '''DEPRECATED, use method L{collins5}.''' 

108 return self.collins5(pointB, pointC, alpha, beta, useZ=useZ) 

109 

110 def collins5(self, pointB, pointC, alpha, beta, useZ=False): 

111 '''3-Point resection between this and 2 other points using U{Collins<https://Dokumen.tips/ 

112 documents/three-point-resection-problem-introduction-kaestner-burkhardt-method.html>}' method. 

113 

114 @arg pointB: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, 

115 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}). 

116 @arg pointC: Center point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, 

117 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}). 

118 @arg alpha: Angle subtended by triangle side C{b} from B{C{pointA}} to 

119 B{C{pointC}} (C{degrees}, non-negative). 

120 @arg beta: Angle subtended by triangle side C{a} from B{C{pointB}} to 

121 B{C{pointC}} (C{degrees}, non-negative). 

122 @kwarg useZ: If C{True}, use and interpolate the Z component, otherwise 

123 force C{z=INT0} (C{bool}). 

124 

125 @note: Typically, B{C{pointC}} is between this and B{C{pointB}}. 

126 

127 @return: L{Collins5Tuple}C{(pointP, pointH, a, b, c)} with survey C{pointP}, 

128 auxiliary C{pointH}, each an instance of this (sub-)class and 

129 triangle sides C{a}, C{b} and C{c}. 

130 

131 @raise ResectionError: Near-coincident, -colinear or -concyclic points 

132 or negative or invalid B{C{alpha}} or B{C{beta}}. 

133 

134 @raise TypeError: Invalid B{C{pointB}} or B{C{pointM}}. 

135 

136 @see: U{Collins' methode<https://NL.WikiPedia.org/wiki/Achterwaartse_insnijding>} 

137 and function L{pygeodesy.collins5}. 

138 ''' 

139 return _MODS.resections.collins5(self, pointB, pointC, alpha, beta, 

140 useZ=useZ, datum=self.datum) 

141 

142 @property_doc_(''' this cartesian's datum (L{Datum}).''') 

143 def datum(self): 

144 '''Get this cartesian's datum (L{Datum}). 

145 ''' 

146 return self._datum 

147 

148 @datum.setter # PYCHOK setter! 

149 def datum(self, datum): 

150 '''Set this cartesian's C{datum} I{without conversion} 

151 (L{Datum}), ellipsoidal or spherical. 

152 

153 @raise TypeError: The B{C{datum}} is not a L{Datum}. 

154 ''' 

155 d = _spherical_datum(datum, name=self.name) 

156 if self._datum: # is not None 

157 if self._datum.isEllipsoidal and not d.isEllipsoidal: 

158 raise _IsnotError(_ellipsoidal_, datum=datum) 

159 elif self._datum.isSpherical and not d.isSpherical: 

160 raise _IsnotError(_spherical_, datum=datum) 

161 if self._datum != d: 

162 _update_all(self) 

163 self._datum = d 

164 

165 def destinationXyz(self, delta, Cartesian=None, **Cartesian_kwds): 

166 '''Calculate the destination using a I{local} delta from this cartesian. 

167 

168 @arg delta: Local delta to the destination (L{XyzLocal}, L{Enu}, 

169 L{Ned} or L{Local9Tuple}). 

170 @kwarg Cartesian: Optional (geocentric) class to return the 

171 destination or C{None}. 

172 @kwarg Cartesian_kwds: Optional, additional B{C{Cartesian}} keyword 

173 arguments, ignored if C{B{Cartesian} is None}. 

174 

175 @return: Destination as a C{B{Cartesian}(x, y, z, **B{Cartesian_kwds})} 

176 instance or if C{B{Cartesian} is None}, an L{Ecef9Tuple}C{(x, y, 

177 z, lat, lon, height, C, M, datum)} with C{M=None} always. 

178 

179 @raise TypeError: Invalid B{C{delta}}, B{C{Cartesian}} or 

180 B{C{Cartesian_kwds}}. 

181 ''' 

182 if Cartesian is None: 

183 r = self._ltp._local2ecef(delta, nine=True) 

184 else: 

185 r = self._ltp._local2ecef(delta, nine=False) 

186 r = Cartesian(*r, **_xkwds(Cartesian_kwds, datum=self.datum)) 

187 return r._xnamed(r) if self.name else r 

188 

189 @Property_RO 

190 def Ecef(self): 

191 '''Get the ECEF I{class} (L{EcefKarney}), I{lazily}. 

192 ''' 

193 return _MODS.ecef.EcefKarney # default 

194 

195 @Property_RO 

196 def _ecef9(self): 

197 '''(INTERNAL) Helper for L{toEcef}, L{toLocal} and L{toLtp} (L{Ecef9Tuple}). 

198 ''' 

199 return self.Ecef(self.datum, name=self.name).reverse(self, M=True) 

200 

201 def hartzell(self, los=None, earth=None): 

202 '''Compute the intersection of a Line-Of-Sight (los) from this certesian 

203 Point-Of-View (pov) with this cartesian's ellipsoid surface. 

204 

205 @kwarg los: Line-Of-Sight, I{direction} to earth (L{Vector3d}) or 

206 C{None} to point to the ellipsoid's center. 

207 @kwarg earth: The earth model (L{Datum}, L{Ellipsoid}, L{Ellipsoid2}, 

208 L{a_f2Tuple} or C{scalar} radius in C{meter}) overriding 

209 this cartesian's C{datum} ellipsoid. 

210 

211 @return: The ellipsoid intersection (C{Cartesian}). 

212 

213 @raise IntersectionError: Null C{pov} or B{C{los}} vector, this C{pov} 

214 is inside the ellipsoid or B{C{los}} points 

215 outside the ellipsoid or in an opposite direction. 

216 

217 @raise TypeError: Invalid B{C{los}} or no B{C{datum}}. 

218 

219 @see: Function C{hartzell} for further details. 

220 ''' 

221 return _MODS.formy.hartzell(self, los=los, earth=earth or self.datum) 

222 

223 @Property 

224 def height(self): 

225 '''Get the height (C{meter}). 

226 ''' 

227 return self._height4.h if self._height is None else self._height 

228 

229 @height.setter # PYCHOK setter! 

230 def height(self, height): 

231 '''Set the height (C{meter}). 

232 

233 @raise TypeError: Invalid B{C{height}} C{type}. 

234 

235 @raise ValueError: Invalid B{C{height}}. 

236 ''' 

237 h = Height(height) 

238 if self._height != h: 

239 _update_all(self) 

240 self._height = h 

241 

242 @Property_RO 

243 def _height4(self): 

244 '''(INTERNAL) Get this C{height4}-tuple. 

245 ''' 

246 try: 

247 r = self.datum.ellipsoid.height4(self, normal=True) 

248 except (AttributeError, ValueError): # no datum, null cartesian, 

249 r = Vector4Tuple(self.x, self.y, self.z, 0, name=self.height4.__name__) 

250 return r 

251 

252 def height4(self, earth=None, normal=True, Cartesian=None, **Cartesian_kwds): 

253 '''Compute the height of this cartesian above or below and the projection 

254 on this datum's ellipsoid surface. 

255 

256 @kwarg earth: A datum, ellipsoid, triaxial ellipsoid or earth radius 

257 I{overriding} this datum (L{Datum}, L{Ellipsoid}, 

258 L{Ellipsoid2}, L{a_f2Tuple}, L{Triaxial}, L{Triaxial_}, 

259 L{JacobiConformal} or C{meter}, conventionally). 

260 @kwarg normal: If C{True} the projection is the nearest point on the 

261 ellipsoid's surface, otherwise the intersection of the 

262 radial line to the center and the ellipsoid's surface. 

263 @kwarg Cartesian: Optional class to return the height and projection 

264 (C{Cartesian}) or C{None}. 

265 @kwarg Cartesian_kwds: Optional, additional B{C{Cartesian}} keyword 

266 arguments, ignored if C{B{Cartesian} is None}. 

267 

268 @note: Use keyword argument C{height=0} to override C{B{Cartesian}.height} 

269 to {0} or any other C{scalar}, conventionally in C{meter}. 

270 

271 @return: An instance of B{C{Cartesian}} or if C{B{Cartesian} is None}, a 

272 L{Vector4Tuple}C{(x, y, z, h)} with the I{projection} C{x}, C{y} 

273 and C{z} coordinates and height C{h} in C{meter}, conventionally. 

274 

275 @raise TriaxialError: No convergence in triaxial root finding. 

276 

277 @raise TypeError: Invalid B{C{earth}}. 

278 

279 @see: L{Ellipsoid.height4} and L{Triaxial_.height4} for more information. 

280 ''' 

281 d = self.datum if earth is None else earth 

282 if normal and d == self.datum: 

283 r = self._height4 

284 elif isinstance(d, _MODS.triaxials.Triaxial_): 

285 r = d.height4(self, normal=normal) 

286 else: 

287 r = _spherical_datum(d).ellipsoid.height4(self, normal=normal) 

288 if Cartesian is not None: 

289 kwds = Cartesian_kwds.copy() 

290 h = kwds.pop(_height_, None) 

291 r = Cartesian(r, **kwds) 

292 if h is not None: 

293 r.height = Height(height=h) 

294 return r 

295 

296 @Property_RO 

297 def isEllipsoidal(self): 

298 '''Check whether this cartesian is ellipsoidal (C{bool} or C{None} if unknown). 

299 ''' 

300 return self.datum.isEllipsoidal if self._datum else None 

301 

302 @Property_RO 

303 def isSpherical(self): 

304 '''Check whether this cartesian is spherical (C{bool} or C{None} if unknown). 

305 ''' 

306 return self.datum.isSpherical if self._datum else None 

307 

308 @Property_RO 

309 def latlon(self): 

310 '''Get this cartesian's (geodetic) lat- and longitude in C{degrees} (L{LatLon2Tuple}C{(lat, lon)}). 

311 ''' 

312 return self.toEcef().latlon 

313 

314 @Property_RO 

315 def latlonheight(self): 

316 '''Get this cartesian's (geodetic) lat-, longitude in C{degrees} with height (L{LatLon3Tuple}C{(lat, lon, height)}). 

317 ''' 

318 return self.toEcef().latlonheight 

319 

320 @Property_RO 

321 def latlonheightdatum(self): 

322 '''Get this cartesian's (geodetic) lat-, longitude in C{degrees} with height and datum (L{LatLon4Tuple}C{(lat, lon, height, datum)}). 

323 ''' 

324 return self.toEcef().latlonheightdatum 

325 

326 @Property_RO 

327 def _ltp(self): 

328 '''(INTERNAL) Cache for L{toLtp}. 

329 ''' 

330 return _MODS.ltp.Ltp(self._ecef9, ecef=self.Ecef(self.datum), name=self.name) 

331 

332 @Property_RO 

333 def _N_vector(self): 

334 '''(INTERNAL) Get the (C{nvectorBase._N_vector_}). 

335 ''' 

336 x, y, z, h = self._n_xyzh4(self.datum) 

337 return _MODS.nvectorBase._N_vector_(x, y, z, h=h, name=self.name) 

338 

339 def _n_xyzh4(self, datum): 

340 '''(INTERNAL) Get the n-vector components as L{Vector4Tuple}. 

341 ''' 

342 def _ErrorEPS0(x): 

343 return _ValueError(origin=self, txt=Fmt.PARENTSPACED(EPS0=x)) 

344 

345 _xinstanceof(Datum, datum=datum) 

346 # <https://www.Movable-Type.co.UK/scripts/geodesy/docs/ 

347 # latlon-nvector-ellipsoidal.js.html#line309>, 

348 # <https://GitHub.com/pbrod/nvector>/src/nvector/core.py> 

349 # _equation23 and <https://www.NavLab.net/nvector> 

350 E = datum.ellipsoid 

351 x, y, z = self.xyz 

352 

353 # Kenneth Gade eqn 23 

354 p = hypot2(x, y) * E.a2_ 

355 q = z**2 * E.e21 * E.a2_ 

356 r = fsumf_(p, q, -E.e4) / _6_0 

357 s = (p * q * E.e4) / (_4_0 * r**3) 

358 t = cbrt(fsumf_(_1_0, s, sqrt(s * (_2_0 + s)))) 

359 if isnear0(t): 

360 raise _ErrorEPS0(t) 

361 u = fsumf_(_1_0, t, _1_0 / t) * r 

362 v = sqrt(u**2 + E.e4 * q) 

363 t = v * _2_0 

364 if t < EPS0: # isnear0 

365 raise _ErrorEPS0(t) 

366 w = fsumf_(u, v, -q) * E.e2 / t 

367 k = sqrt(fsumf_(u, v, w**2)) - w 

368 if isnear0(k): 

369 raise _ErrorEPS0(k) 

370 t = k + E.e2 

371 if isnear0(t): 

372 raise _ErrorEPS0(t) 

373 e = k / t 

374# d = e * hypot(x, y) 

375# tmp = 1 / hypot(d, z) == 1 / hypot(e * hypot(x, y), z) 

376 t = hypot_(x * e, y * e, z) # == 1 / tmp 

377 if t < EPS0: # isnear0 

378 raise _ErrorEPS0(t) 

379 h = fsumf_(k, E.e2, _N_1_0) / k * t 

380 s = e / t # == e * tmp 

381 return Vector4Tuple(x * s, y * s, z / t, h, name=self.name) 

382 

383 @Property_RO 

384 def philam(self): 

385 '''Get this cartesian's (geodetic) lat- and longitude in C{radians} (L{PhiLam2Tuple}C{(phi, lam)}). 

386 ''' 

387 return self.toEcef().philam 

388 

389 @Property_RO 

390 def philamheight(self): 

391 '''Get this cartesian's (geodetic) lat-, longitude in C{radians} with height (L{PhiLam3Tuple}C{(phi, lam, height)}). 

392 ''' 

393 return self.toEcef().philamheight 

394 

395 @Property_RO 

396 def philamheightdatum(self): 

397 '''Get this cartesian's (geodetic) lat-, longitude in C{radians} with height and datum (L{PhiLam4Tuple}C{(phi, lam, height, datum)}). 

398 ''' 

399 return self.toEcef().philamheightdatum 

400 

401 def pierlot(self, point2, point3, alpha12, alpha23, useZ=False): 

402 '''3-Point resection between this and two other points using U{Pierlot 

403 <http://www.Telecom.ULg.ac.Be/triangulation>}'s method C{ToTal}. 

404 

405 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, 

406 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}). 

407 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, 

408 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}). 

409 @arg alpha12: Angle subtended from this point to B{C{point2}} (C{degrees}). 

410 @arg alpha23: Angle subtended from B{C{point2}} to B{C{point3}} (C{degrees}). 

411 @kwarg useZ: If C{True}, interpolate the Z component, otherwise use C{z=INT0} 

412 (C{bool}). 

413 

414 @note: This point, B{C{point2}} and B{C{point3}} are ordered counter-clockwise. 

415 

416 @return: The survey (or robot) point, an instance of this (sub-)class. 

417 

418 @raise ResectionError: Near-coincident, -colinear or -concyclic points 

419 or invalid B{C{alpha12}} or B{C{alpha23}}. 

420 

421 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}. 

422 

423 @see: U{V. Pierlot, M. Van Droogenbroeck, "A New Three Object Triangulation 

424 Algorithm for Mobile Robot Positioning"<https://ORBi.ULiege.Be/ 

425 bitstream/2268/157469/1/Pierlot2014ANewThree.pdf>}, U{18 Triangulation 

426 Algorithms for 2D Positioning (also known as the Resection Problem) 

427 <http://www.Telecom.ULg.ac.Be/triangulation>} and functions 

428 L{pygeodesy.pierlot}. 

429 ''' 

430 return _MODS.resections.pierlot(self, point2, point3, alpha12, alpha23, 

431 useZ=useZ, datum=self.datum) 

432 

433 @deprecated_method 

434 def tienstra(self, pointB, pointC, alpha, beta=None, gamma=None, useZ=False): 

435 '''DEPRECATED, use method L{tienstra7}.''' 

436 return self.tienstra7(pointB, pointC, alpha, beta=beta, gamma=gamma, useZ=useZ) 

437 

438 def tienstra7(self, pointB, pointC, alpha, beta=None, gamma=None, useZ=False): 

439 '''3-Point resection between this and two other points using U{Tienstra 

440 <https://WikiPedia.org/wiki/Tienstra_formula>}'s formula. 

441 

442 @arg pointB: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, C{Vector4Tuple} or 

443 C{Vector2Tuple} if C{B{useZ}=False}). 

444 @arg pointC: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple}, C{Vector4Tuple} or 

445 C{Vector2Tuple} if C{B{useZ}=False}). 

446 @arg alpha: Angle subtended by triangle side C{a} from B{C{pointB}} to B{C{pointC}} (C{degrees}, 

447 non-negative). 

448 @kwarg beta: Angle subtended by triangle side C{b} from this to B{C{pointC}} (C{degrees}, 

449 non-negative) or C{None} if C{B{gamma} is not None}. 

450 @kwarg gamma: Angle subtended by triangle side C{c} from this to B{C{pointB}} (C{degrees}, 

451 non-negative) or C{None} if C{B{beta} is not None}. 

452 @kwarg useZ: If C{True}, use and interpolate the Z component, otherwise force C{z=INT0} 

453 (C{bool}). 

454 

455 @note: This point, B{C{pointB}} and B{C{pointC}} are ordered clockwise. 

456 

457 @return: L{Tienstra7Tuple}C{(pointP, A, B, C, a, b, c)} with survey C{pointP}, 

458 an instance of this (sub-)class and triangle angle C{A} at this point, 

459 C{B} at B{C{pointB}} and C{C} at B{C{pointC}} in C{degrees} and 

460 triangle sides C{a}, C{b} and C{c}. 

461 

462 @raise ResectionError: Near-coincident, -colinear or -concyclic points or sum of 

463 B{C{alpha}}, B{C{beta}} and B{C{gamma}} not C{360} or 

464 negative B{C{alpha}}, B{C{beta}} or B{C{gamma}}. 

465 

466 @raise TypeError: Invalid B{C{pointB}} or B{C{pointC}}. 

467 

468 @see: U{3-Point Resection Solver<http://MesaMike.org/geocache/GC1B0Q9/tienstra/>}, 

469 U{V. Pierlot, M. Van Droogenbroeck, "A New Three Object Triangulation..." 

470 <http://www.Telecom.ULg.ac.Be/publi/publications/pierlot/Pierlot2014ANewThree/>}, 

471 U{18 Triangulation Algorithms...<http://www.Telecom.ULg.ac.Be/triangulation/>} 

472 and function L{pygeodesy.tienstra7}. 

473 ''' 

474 return _MODS.resections.tienstra7(self, pointB, pointC, alpha, beta, gamma, 

475 useZ=useZ, datum=self.datum) 

476 

477 @deprecated_method 

478 def to2ab(self): # PYCHOK no cover 

479 '''DEPRECATED, use property C{philam}. 

480 

481 @return: A L{PhiLam2Tuple}C{(phi, lam)}. 

482 ''' 

483 return self.philam 

484 

485 @deprecated_method 

486 def to2ll(self): # PYCHOK no cover 

487 '''DEPRECATED, use property C{latlon}. 

488 

489 @return: A L{LatLon2Tuple}C{(lat, lon)}. 

490 ''' 

491 return self.latlon 

492 

493 @deprecated_method 

494 def to3llh(self, datum=None): # PYCHOK no cover 

495 '''DEPRECATED, use property L{latlonheightdatum} or L{latlonheight}. 

496 

497 @return: A L{LatLon4Tuple}C{(lat, lon, height, datum)}. 

498 

499 @note: This method returns a B{C{-4Tuple}} I{and not a} C{-3Tuple} 

500 as its name may suggest. 

501 ''' 

502 t = self.toLatLon(datum=datum, LatLon=None) 

503 return LatLon4Tuple(t.lat, t.lon, t.height, t.datum, name=self.name) 

504 

505# def _to3LLh(self, datum, LL, **pairs): # OBSOLETE 

506# '''(INTERNAL) Helper for C{subclass.toLatLon} and C{.to3llh}. 

507# ''' 

508# r = self.to3llh(datum) # LatLon3Tuple 

509# if LL is not None: 

510# r = LL(r.lat, r.lon, height=r.height, datum=datum, name=self.name) 

511# for n, v in pairs.items(): 

512# setattr(r, n, v) 

513# return r 

514 

515 def toDatum(self, datum2, datum=None): 

516 '''Convert this cartesian from one datum to an other. 

517 

518 @arg datum2: Datum to convert I{to} (L{Datum}). 

519 @kwarg datum: Datum to convert I{from} (L{Datum}). 

520 

521 @return: The converted point (C{Cartesian}). 

522 

523 @raise TypeError: B{C{datum2}} or B{C{datum}} 

524 invalid. 

525 ''' 

526 _xinstanceof(Datum, datum2=datum2) 

527 

528 c = self if datum in (None, self.datum) else \ 

529 self.toDatum(datum) 

530 

531 i, d = False, c.datum 

532 if d == datum2: 

533 return c.copy() if c is self else c 

534 

535 elif d == _WGS84: 

536 d = datum2 # convert from WGS84 to datum2 

537 

538 elif datum2 == _WGS84: 

539 i = True # convert to WGS84 by inverse transformation 

540 

541 else: # neither datum2 nor c.datum is WGS84, invert to WGS84 first 

542 c = c.toTransform(d.transform, inverse=True, datum=_WGS84) 

543 d = datum2 

544 

545 return c.toTransform(d.transform, inverse=i, datum=datum2) 

546 

547 convertDatum = toDatum # for backward compatibility 

548 

549 def toEcef(self): 

550 '''Convert this cartesian to I{geodetic} (lat-/longitude) coordinates. 

551 

552 @return: An L{Ecef9Tuple}C{(x, y, z, lat, lon, height, 

553 C, M, datum)} with C{C} and C{M} if available. 

554 

555 @raise EcefError: A C{.datum} or an ECEF issue. 

556 ''' 

557 return self._ecef9 

558 

559 def toLatLon(self, datum=None, height=None, LatLon=None, **LatLon_kwds): # see .ecef.Ecef9Tuple.toDatum 

560 '''Convert this cartesian to a geodetic (lat-/longitude) point. 

561 

562 @kwarg datum: Optional datum (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} 

563 or L{a_f2Tuple}). 

564 @kwarg height: Optional height, overriding the converted height 

565 (C{meter}), iff B{C{LatLon}} is not C{None}. 

566 @kwarg LatLon: Optional class to return the geodetic point 

567 (C{LatLon}) or C{None}. 

568 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword 

569 arguments, ignored if C{B{LatLon} is None}. 

570 

571 @return: The geodetic point (B{C{LatLon}}) or if B{C{LatLon}} 

572 is C{None}, an L{Ecef9Tuple}C{(x, y, z, lat, lon, 

573 height, C, M, datum)} with C{C} and C{M} if available. 

574 

575 @raise TypeError: Invalid B{C{datum}} or B{C{LatLon_kwds}}. 

576 ''' 

577 d = _spherical_datum(datum or self.datum, name=self.name) 

578 if d == self.datum: 

579 r = self.toEcef() 

580 else: 

581 c = self.toDatum(d) 

582 r = c.Ecef(d, name=self.name).reverse(c, M=LatLon is None) 

583 

584 if LatLon: # class or .classof 

585 h = r.height if height is None else Height(height) 

586 r = LatLon(r.lat, r.lon, datum=r.datum, height=h, 

587 **_xkwds(LatLon_kwds, name=r.name)) 

588 _xdatum(r.datum, d) 

589 return r 

590 

591 def toLocal(self, Xyz=None, ltp=None, **Xyz_kwds): 

592 '''Convert this I{geocentric} cartesian to I{local} C{X}, C{Y} and C{Z}. 

593 

594 @kwarg Xyz: Optional class to return C{X}, C{Y} and C{Z} 

595 (L{XyzLocal}, L{Enu}, L{Ned}) or C{None}. 

596 @kwarg ltp: The I{local tangent plane} (LTP) to use, 

597 overriding this cartesian's LTP (L{Ltp}). 

598 @kwarg Xyz_kwds: Optional, additional B{C{Xyz}} keyword 

599 arguments, ignored if C{B{Xyz} is None}. 

600 

601 @return: An B{C{Xyz}} instance or if C{B{Xyz} is None}, 

602 a L{Local9Tuple}C{(x, y, z, lat, lon, height, 

603 ltp, ecef, M)} with C{M=None} always. 

604 

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

606 ''' 

607 p = _MODS.ltp._xLtp(ltp, self._ltp) 

608 return p._ecef2local(self._ecef9, Xyz, Xyz_kwds) 

609 

610 def toLtp(self, Ecef=None): 

611 '''Return the I{local tangent plane} (LTP) for this cartesian. 

612 

613 @kwarg Ecef: Optional ECEF I{class} (L{EcefKarney}, ... 

614 L{EcefYou}), overriding this cartesian's C{Ecef}. 

615 ''' 

616 return self._ltp if Ecef in (None, self.Ecef) else _MODS.ltp.Ltp( 

617 self._ecef9, ecef=Ecef(self.datum), name=self.name) 

618 

619 def toNvector(self, Nvector=None, datum=None, **Nvector_kwds): 

620 '''Convert this cartesian to C{n-vector} components. 

621 

622 @kwarg Nvector: Optional class to return the C{n-vector} 

623 components (C{Nvector}) or C{None}. 

624 @kwarg datum: Optional datum (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} 

625 or L{a_f2Tuple}) overriding this cartesian's datum. 

626 @kwarg Nvector_kwds: Optional, additional B{C{Nvector}} keyword 

627 arguments, ignored if C{B{Nvector} is None}. 

628 

629 @return: The C{unit, n-vector} components (B{C{Nvector}}) or a 

630 L{Vector4Tuple}C{(x, y, z, h)} if B{C{Nvector}} is C{None}. 

631 

632 @raise TypeError: Invalid B{C{datum}}. 

633 

634 @raise ValueError: The B{C{Cartesian}} at origin. 

635 

636 @example: 

637 

638 >>> c = Cartesian(3980581, 97, 4966825) 

639 >>> n = c.toNvector() # (x=0.622818, y=0.00002, z=0.782367, h=0.242887) 

640 ''' 

641 d = _spherical_datum(datum or self.datum, name=self.name) 

642 r = self._N_vector.xyzh if self.datum == d else self._n_xyzh4(d) 

643 

644 if Nvector is not None: 

645 kwds = _xkwds(Nvector_kwds, h=r.h, datum=d) 

646 r = self._xnamed(Nvector(r.x, r.y, r.z, **kwds)) 

647 return r 

648 

649 def toStr(self, prec=3, fmt=Fmt.SQUARE, sep=_COMMASPACE_): # PYCHOK expected 

650 '''Return the string representation of this cartesian. 

651 

652 @kwarg prec: Number of (decimal) digits, unstripped (C{int}). 

653 @kwarg fmt: Enclosing backets format (string). 

654 @kwarg sep: Separator to join (string). 

655 

656 @return: Cartesian represented as "[x, y, z]" (string). 

657 ''' 

658 return Vector3d.toStr(self, prec=prec, fmt=fmt, sep=sep) 

659 

660 def toTransform(self, transform, inverse=False, datum=None): 

661 '''Return a new cartesian by applying a Helmert transform 

662 to this cartesian. 

663 

664 @arg transform: Transform to apply (L{Transform}). 

665 @kwarg inverse: Apply the inverse of the Helmert 

666 transform (C{bool}). 

667 @kwarg datum: Datum for the transformed cartesian (L{Datum}), 

668 overriding this cartesian's datum. 

669 

670 @return: The transformed cartesian (C{Cartesian}). 

671 

672 @raise Valuerror: If C{B{inverse}=True} and B{C{datum}} 

673 is not L{Datums}C{.WGS84}. 

674 ''' 

675 d = datum or self.datum 

676 if inverse and d != _WGS84: 

677 raise _ValueError(inverse=inverse, datum=d, 

678 txt=_not_(_WGS84.name)) 

679 

680 xyz = transform.transform(*self.xyz, inverse=inverse) 

681 return self.classof(xyz, datum=d) 

682 

683 def toVector(self, Vector=None, **Vector_kwds): 

684 '''Return this cartesian's components as vector. 

685 

686 @kwarg Vector: Optional class to return the C{n-vector} 

687 components (L{Vector3d}) or C{None}. 

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

689 arguments, ignored if C{B{Vector} is None}. 

690 

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

692 B{C{Vector}} is C{None}. 

693 

694 @raise TypeError: Invalid B{C{Vector}} or B{C{Vector_kwds}}. 

695 ''' 

696 return self.xyz if Vector is None else self._xnamed( 

697 Vector(self.x, self.y, self.z, **Vector_kwds)) 

698 

699 

700__all__ += _ALL_DOCS(CartesianBase) 

701 

702# **) MIT License 

703# 

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

705# 

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

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

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

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

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

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

712# 

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

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

715# 

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

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

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

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

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

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

722# OTHER DEALINGS IN THE SOFTWARE.