1

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

3

4 u'''(INTERNAL) Private elliposiodal and spherical C{Nvector} base classes

5 L{LatLonNvectorBase} and L{NvectorBase} and function L{sumOf}.

6

7 Pure Python implementation of C{n-vector}-based geodesy tools for ellipsoidal

8 earth models, transcoded from JavaScript originals by I{(C) Chris Veness 2005-2016}

9 and published under the same MIT Licence**, see U{Vector-based geodesy

10<https://www.Movable-Type.co.UK/scripts/latlong-vectors.html>}.

11'''

12

13# from pygeodesy.basics import map1 # from .namedTuples

14 from pygeodesy.constants import EPS, EPS1, EPS_2, R_M, _2_0, _N_2_0

15# from pygeodesy.datums import _spherical_datum # from .formy

16 from pygeodesy.errors import IntersectionError, _ValueError, VectorError, \

17 _xkwds, _xkwds_pop2

18 from pygeodesy.fmath import fdot, fidw, hypot_ # PYCHOK fdot shared

19 from pygeodesy.fsums import Fsum, fsumf_

20 from pygeodesy.formy import _isequalTo, n_xyz2latlon, n_xyz2philam, \

21 _spherical_datum

22# from pygeodesy.internals import _under # from .named

23 from pygeodesy.interns import NN, _1_, _2_, _3_, _bearing_, _coincident_, \

24 _COMMASPACE_, _distance_, _h_, _insufficient_, \

25 _intersection_, _no_, _point_, _pole_, _SPACE_

26 from pygeodesy.latlonBase import LatLonBase, _ALL_DOCS, _ALL_LAZY, _MODS

27# from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS # from .latlonBase

28 from pygeodesy.named import _xother3, _under

29 from pygeodesy.namedTuples import Trilaterate5Tuple, Vector3Tuple, \

30 Vector4Tuple, map1

31 from pygeodesy.props import deprecated_method, Property_RO, property_doc_, \

32 property_RO, _update_all

33 from pygeodesy.streprs import Fmt, hstr, unstr, _xattrs

34 from pygeodesy.units import Bearing, Height, Radius_, Scalar

35 from pygeodesy.utily import sincos2d, _unrollon, _unrollon3

36 from pygeodesy.vector3d import Vector3d, _xyzhdlln4

37

38 from math import fabs, sqrt

39

40__all__ = _ALL_LAZY.nvectorBase

41__version__ = '24.06.06'

42

43

44 class NvectorBase(Vector3d): # XXX kept private

45 '''Base class for ellipsoidal and spherical C{Nvector}s.

46 '''

47 _datum = None # L{Datum}, overriden

48 _h = Height(h=0) # height (C{meter})

49 _H = NN # height prefix (C{str}), '↑' in JS version

50

51 def __init__(self, x_xyz, y=None, z=None, h=0, datum=None, **ll_name):

52 '''New n-vector normal to the earth's surface.

53

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

55 (C{Nvector}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}).

56 @kwarg y: Y component of vector (C{scalar}), ignored if B{C{x_xyz}} is not

57 C{scalar}, otherwise same units as B{C{x_xyz}}.

58 @kwarg z: Z component of vector (C{scalar}), ignored if B{C{x_xyz}} is not

59 C{scalar}, otherwise same units as B{C{x_xyz}}.

60 @kwarg h: Optional height above surface (C{meter}).

61 @kwarg datum: Optional, I{pass-thru} datum (L{Datum}).

62 @kwarg ll_name: Optional C{B{name}=NN} (C{str}) and optional, original

63 latlon C{B{ll}=None} (C{LatLon}).

64

65 @raise TypeError: Non-scalar B{C{x}}, B{C{y}} or B{C{z}} coordinate or

66 B{C{x_xyz}} not an C{Nvector}, L{Vector3Tuple} or

67 L{Vector4Tuple} or invalid B{C{datum}}.

68 '''

69 h, d, ll, n = _xyzhdlln4(x_xyz, h, datum, **ll_name)

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

71 if h:

72 self.h = h

73 if d is not None:

74 self._datum = _spherical_datum(d, name=n) # pass-thru

75

76 @Property_RO

77 def datum(self):

78 '''Get the I{pass-thru} datum (C{Datum}) or C{None}.

79 '''

80 return self._datum

81

82 @property_RO

83 def Ecef(self):

84 '''Get the ECEF I{class} (L{EcefKarney}), I{once}.

85 '''

86 NvectorBase.Ecef = E = _MODS.ecef.EcefKarney # overwrite property_RO

87 return E

88

89 @property_RO

90 def ellipsoidalNvector(self):

91 '''Get the C{Nvector type} iff ellipsoidal, overloaded in L{pygeodesy.ellipsoidalNvector.Nvector}.

92 '''

93 return False

94

95 @property_doc_(''' the height above surface (C{meter}).''')

96 def h(self):

97 '''Get the height above surface (C{meter}).

98 '''

99 return self._h

100

101 @h.setter # PYCHOK setter!

102 def h(self, h):

103 '''Set the height above surface (C{meter}).

104

105 @raise TypeError: If B{C{h}} invalid.

106

107 @raise VectorError: If B{C{h}} invalid.

108 '''

109 h = Height(h=h, Error=VectorError)

110 if self._h != h:

111 _update_all(self)

112 self._h = h

113

114 @property_doc_(''' the height prefix (C{str}).''')

115 def H(self):

116 '''Get the height prefix (C{str}).

117 '''

118 return self._H

119

120 @H.setter # PYCHOK setter!

121 def H(self, H):

122 '''Set the height prefix (C{str}).

123 '''

124 self._H = str(H) if H else NN

125

126 def hStr(self, prec=-2, m=NN):

127 '''Return a string for the height B{C{h}}.

128

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

130 @kwarg m: Optional unit of the height (C{str}).

131

132 @see: Function L{pygeodesy.hstr}.

133 '''

134 return NN(self.H, hstr(self.h, prec=prec, m=m))

135

136 @Property_RO

137 def isEllipsoidal(self):

138 '''Check whether this n-vector is ellipsoidal (C{bool} or C{None} if unknown).

139 '''

140 return self.datum.isEllipsoidal if self.datum else None

141

142 @Property_RO

143 def isSpherical(self):

144 '''Check whether this n-vector is spherical (C{bool} or C{None} if unknown).

145 '''

146 return self.datum.isSpherical if self.datum else None

147

148 @Property_RO

149 def lam(self):

150 '''Get the (geodetic) longitude in C{radians} (C{float}).

151 '''

152 return self.philam.lam

153

154 @Property_RO

155 def lat(self):

156 '''Get the (geodetic) latitude in C{degrees} (C{float}).

157 '''

158 return self.latlon.lat

159

160 @Property_RO

161 def latlon(self):

162 '''Get the (geodetic) lat-, longitude in C{degrees} (L{LatLon2Tuple}C{(lat, lon)}).

163 '''

164 return n_xyz2latlon(self.x, self.y, self.z, name=self.name)

165

166 @Property_RO

167 def latlonheight(self):

168 '''Get the (geodetic) lat-, longitude in C{degrees} and height (L{LatLon3Tuple}C{(lat, lon, height)}).

169 '''

170 return self.latlon.to3Tuple(self.h)

171

172 @Property_RO

173 def latlonheightdatum(self):

174 '''Get the lat-, longitude in C{degrees} with height and datum (L{LatLon4Tuple}C{(lat, lon, height, datum)}).

175 '''

176 return self.latlonheight.to4Tuple(self.datum)

177

178 @Property_RO

179 def lon(self):

180 '''Get the (geodetic) longitude in C{degrees} (C{float}).

181 '''

182 return self.latlon.lon

183

184 @Property_RO

185 def phi(self):

186 '''Get the (geodetic) latitude in C{radians} (C{float}).

187 '''

188 return self.philam.phi

189

190 @Property_RO

191 def philam(self):

192 '''Get the (geodetic) lat-, longitude in C{radians} (L{PhiLam2Tuple}C{(phi, lam)}).

193 '''

194 return n_xyz2philam(self.x, self.y, self.z, name=self.name)

195

196 @Property_RO

197 def philamheight(self):

198 '''Get the (geodetic) lat-, longitude in C{radians} and height (L{PhiLam3Tuple}C{(phi, lam, height)}).

199 '''

200 return self.philam.to3Tuple(self.h)

201

202 @Property_RO

203 def philamheightdatum(self):

204 '''Get the lat-, longitude in C{radians} with height and datum (L{PhiLam4Tuple}C{(phi, lam, height, datum)}).

205 '''

206 return self.philamheight.to4Tuple(self.datum)

207

208 @property_RO

209 def sphericalNvector(self):

210 '''Get the C{Nvector type} iff spherical, overloaded in L{pygeodesy.sphericalNvector.Nvector}.

211 '''

212 return False

213

214 @deprecated_method

215 def to2ab(self): # PYCHOK no cover

216 '''DEPRECATED, use property L{philam}.

217

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

219 '''

220 return self.philam

221

222 @deprecated_method

223 def to3abh(self, height=None): # PYCHOK no cover

224 '''DEPRECATED, use property L{philamheight} or C{philam.to3Tuple(B{height})}.

225

226 @kwarg height: Optional height, overriding this

227 n-vector's height (C{meter}).

228

229 @return: A L{PhiLam3Tuple}C{(phi, lam, height)}.

230

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

232 '''

233 return self.philamheight if height in (None, self.h) else \

234 self.philam.to3Tuple(height)

235

236 def toCartesian(self, h=None, Cartesian=None, datum=None, **Cartesian_kwds):

237 '''Convert this n-vector to C{Nvector}-based cartesian (ECEF) coordinates.

238

239 @kwarg h: Optional height, overriding this n-vector's height (C{meter}).

240 @kwarg Cartesian: Optional class to return the (ECEF) coordinates

241 (C{Cartesian}).

242 @kwarg datum: Optional datum (C{Datum}), overriding this datum.

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

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

245

246 @return: The cartesian (ECEF) coordinates (B{C{Cartesian}}) or

247 if C{B{Cartesian} is None}, an L{Ecef9Tuple}C{(x, y, z,

248 lat, lon, height, C, M, datum)} with C{C} and C{M} if

249 available.

250

251 @raise TypeError: Invalid B{C{Cartesian}} or B{C{Cartesian_kwds}}

252 argument.

253

254 @raise ValueError: Invalid B{C{h}}.

255 '''

256 D = _spherical_datum(datum or self.datum, name=self.name)

257 E = D.ellipsoid

258 h = self.h if h is None else Height(h)

259

260 x, y, z = self.x, self.y, self.z

261 # Kenneth Gade eqn 22

262 n = E.b / hypot_(x * E.a_b, y * E.a_b, z)

263 r = h + n * E.a2_b2

264

265 x *= r

266 y *= r

267 z *= h + n

268

269 if Cartesian is None:

270 r = self.Ecef(D).reverse(x, y, z, M=True)

271 else:

272 kwds = _xkwds(Cartesian_kwds, datum=D) # h=0

273 r = Cartesian(x, y, z, **kwds)

274 return self._xnamed(r)

275

276 @deprecated_method

277 def to2ll(self): # PYCHOK no cover

278 '''DEPRECATED, use property L{latlon}.

279

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

281 '''

282 return self.latlon

283

284 @deprecated_method

285 def to3llh(self, height=None): # PYCHOK no cover

286 '''DEPRECATED, use property C{latlonheight} or C{latlon.to3Tuple(B{height})}.

287

288 @kwarg height: Optional height, overriding this

289 n-vector's height (C{meter}).

290

291 @return: A L{LatLon3Tuple}C{(lat, lon, height)}.

292

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

294 '''

295 return self.latlonheight if height in (None, self.h) else \

296 self.latlon.to3Tuple(height)

297

298 def toLatLon(self, height=None, LatLon=None, datum=None, **LatLon_kwds):

299 '''Convert this n-vector to an C{Nvector}-based geodetic point.

300

301 @kwarg height: Optional height, overriding this n-vector's

302 height (C{meter}).

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

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

305 @kwarg datum: Optional, spherical datum (C{Datum}).

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

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

308

309 @return: The geodetic point (C{LatLon}) or if C{B{LatLon} is None},

310 an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M,

311 datum)} with C{C} and C{M} if available.

312

313 @raise TypeError: Invalid B{C{LatLon}} or B{C{LatLon_kwds}}

314 argument.

315

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

317 '''

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

319 h = self.h if height is None else Height(height)

320 # use self.Cartesian(Cartesian=None) for better accuracy of the height

321 # than self.Ecef(d).forward(self.lat, self.lon, height=h, M=True)

322 if LatLon is None:

323 r = self.toCartesian(h=h, Cartesian=None, datum=d)

324 else:

325 kwds = _xkwds(LatLon_kwds, height=h, datum=d)

326 r = LatLon(self.lat, self.lon, **self._name1__(kwds))

327 return r

328

329 def toStr(self, prec=5, fmt=Fmt.PAREN, sep=_COMMASPACE_): # PYCHOK expected

330 '''Return a string representation of this n-vector.

331

332 Height component is only included if non-zero.

333

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

335 @kwarg fmt: Enclosing backets format (C{str}).

336 @kwarg sep: Optional separator between components (C{str}).

337

338 @return: Comma-separated C{"(x, y, z [, h])"} enclosed in

339 B{C{fmt}} brackets (C{str}).

340 '''

341 t = Vector3d.toStr(self, prec=prec, fmt=NN, sep=sep)

342 if self.h:

343 t = sep.join((t, self.hStr()))

344 return (fmt % (t,)) if fmt else t

345

346 def toVector3d(self, norm=True):

347 '''Convert this n-vector to a 3-D vector, I{ignoring height}.

348

349 @kwarg norm: Normalize the 3-D vector (C{bool}).

350

351 @return: The (normalized) vector (L{Vector3d}).

352 '''

353 v = Vector3d.unit(self) if norm else self

354 return Vector3d(v.x, v.y, v.z, name=self.name)

355

356 @deprecated_method

357 def to4xyzh(self, h=None): # PYCHOK no cover

358 '''DEPRECATED, use property L{xyzh} or C{xyz.to4Tuple(B{h})}.'''

359 return self.xyzh if h in (None, self.h) else Vector4Tuple(

360 self.x, self.y, self.z, h, name=self.name)

361

362 def unit(self, ll=None):

363 '''Normalize this n-vector to unit length.

364

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

366

367 @return: Normalized vector (C{Nvector}).

368 '''

369 return _xattrs(Vector3d.unit(self, ll=ll), _under(_h_))

370

371 @Property_RO

372 def xyzh(self):

373 '''Get this n-vector's components (L{Vector4Tuple}C{(x, y, z, h)})

374 '''

375 return self.xyz.to4Tuple(self.h)

376

377

378 NorthPole = NvectorBase(0, 0, +1, name='NorthPole') # North pole (C{Nvector})

379 SouthPole = NvectorBase(0, 0, -1, name='SouthPole') # South pole (C{Nvector})

380

381

382 class _N_vector_(NvectorBase):

383 '''(INTERNAL) Minimal, low-overhead C{n-vector}.

384 '''

385 def __init__(self, x, y, z, h=0, **name):

386 self._x, self._y, self._z = x, y, z

387 if h:

388 self._h = h

389 if name:

390 self.name = name

391

392

393 class LatLonNvectorBase(LatLonBase):

394 '''(INTERNAL) Base class for n-vector-based ellipsoidal and

395 spherical C{LatLon} classes.

396 '''

397

398 def _update(self, updated, *attrs, **setters): # PYCHOK _Nv=None

399 '''(INTERNAL) Zap cached attributes if updated.

400

401 @see: C{ellipsoidalNvector.LatLon} and C{sphericalNvector.LatLon}

402 for the special case of B{C{_Nv}}.

403 '''

404 if updated:

405 _Nv, setters = _xkwds_pop2(setters, _Nv=None)

406 if _Nv is not None:

407 if _Nv._fromll is not None:

408 _Nv._fromll = None

409 self._Nv = None

410 LatLonBase._update(self, updated, *attrs, **setters)

411

412# def distanceTo(self, other, **kwds): # PYCHOK no cover

413# '''I{Must be overloaded}.'''

414# self._notOverloaded(other, **kwds)

415

416 def intersections2(self, radius1, other, radius2, **kwds): # PYCHOK expected

417 '''B{Not implemented}, throws a C{NotImplementedError} always.'''

418 self._notImplemented(radius1, other, radius2, **kwds)

419

420 def others(self, *other, **name_other_up):

421 '''Refined class comparison.

422

423 @arg other: The other instance (C{LatLonNvectorBase}).

424 @kwarg name_other_up: Overriding C{name=other} and C{up=1}

425 keyword arguments.

426

427 @return: The B{C{other}} if compatible.

428

429 @raise TypeError: Incompatible B{C{other}} C{type}.

430 '''

431 if other:

432 other0 = other[0]

433 if isinstance(other0, (self.__class__, LatLonNvectorBase)): # XXX NvectorBase?

434 return other0

435

436 other, name, up = _xother3(self, other, **name_other_up)

437 if not isinstance(other, (self.__class__, LatLonNvectorBase)): # XXX NvectorBase?

438 LatLonBase.others(self, other, name=name, up=up + 1)

439 return other

440

441 def toNvector(self, **Nvector_and_kwds): # PYCHOK signature

442 '''Convert this point to C{Nvector} components, I{including height}.

443

444 @kwarg Nvector_and_kwds: Optional C{Nvector} class and C{Nvector} keyword arguments,

445 Specify C{B{Nvector}=...} to override this C{Nvector} class

446 or use C{B{Nvector}=None}.

447

448 @return: An C{Nvector} or if C{Nvector is None}, a L{Vector4Tuple}C{(x, y, z, h)}.

449

450 @raise TypeError: Invalid C{Nvector} or other B{C{Nvector_and_kwds}} item.

451 '''

452 return LatLonBase.toNvector(self, **_xkwds(Nvector_and_kwds, Nvector=NvectorBase))

453

454 def triangulate(self, bearing1, other, bearing2, height=None, wrap=False): # PYCHOK signature

455 '''Locate a point given this, an other point and the (initial) bearing

456 from this and the other point.

457

458 @arg bearing1: Bearing at this point (compass C{degrees360}).

459 @arg other: The other point (C{LatLon}).

460 @arg bearing2: Bearing at the other point (compass C{degrees360}).

461 @kwarg height: Optional height at the triangulated point, overriding

462 the mean height (C{meter}).

463 @kwarg wrap: If C{True}, use this and the B{C{other}} point

464 I{normalized} (C{bool}).

465

466 @return: Triangulated point (C{LatLon}).

467

468 @raise TypeError: Invalid B{C{other}} point.

469

470 @raise Valuerror: Points coincide.

471 '''

472 return _triangulate(self, bearing1, self.others(other), bearing2,

473 height=height, wrap=wrap, LatLon=self.classof)

474

475 def trilaterate(self, distance1, point2, distance2, point3, distance3,

476 radius=R_M, height=None, useZ=False, wrap=False):

477 '''Locate a point at given distances from this and two other points.

478

479 @arg distance1: Distance to this point (C{meter}, same units

480 as B{C{radius}}).

481 @arg point2: Second reference point (C{LatLon}).

482 @arg distance2: Distance to point2 (C{meter}, same units as

483 B{C{radius}}).

484 @arg point3: Third reference point (C{LatLon}).

485 @arg distance3: Distance to point3 (C{meter}, same units as

486 B{C{radius}}).

487 @kwarg radius: Mean earth radius (C{meter}).

488 @kwarg height: Optional height at trilaterated point, overriding

489 the mean height (C{meter}, same units as B{C{radius}}).

490 @kwarg useZ: Include Z component iff non-NaN, non-zero (C{bool}).

491 @kwarg wrap: If C{True}, use this, B{C{point2}} and B{C{point3}}

492 I{normalized} (C{bool}).

493

494 @return: Trilaterated point (C{LatLon}).

495

496 @raise IntersectionError: No intersection, trilateration failed.

497

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

499

500 @raise ValueError: Some B{C{points}} coincide or invalid B{C{distance1}},

501 B{C{distance2}}, B{C{distance3}} or B{C{radius}}.

502

503 @see: U{Trilateration<https://WikiPedia.org/wiki/Trilateration>},

504 Veness' JavaScript U{Trilateration<https://www.Movable-Type.co.UK/

505 scripts/latlong-vectors.html>} and method C{LatLon.trilaterate5}

506 of other, non-C{Nvector LatLon} classes.

507 '''

508 return _trilaterate(self, distance1, self.others(point2=point2), distance2,

509 self.others(point3=point3), distance3,

510 radius=radius, height=height, useZ=useZ,

511 wrap=wrap, LatLon=self.classof)

512

513 def trilaterate5(self, distance1, point2, distance2, point3, distance3, # PYCHOK signature

514 area=False, eps=EPS1, radius=R_M, wrap=False):

515 '''B{Not implemented} for C{B{area}=True} and falls back to method

516 C{trilaterate} otherwise.

517

518 @return: A L{Trilaterate5Tuple}C{(min, minPoint, max, maxPoint, n)}

519 with a single trilaterated intersection C{minPoint I{is}

520 maxPoint}, C{min I{is} max} the nearest intersection

521 margin and count C{n = 1}.

522

523 @raise NotImplementedError: Keyword argument C{B{area}=True} not

524 (yet) supported.

525

526 @see: Method L{trilaterate} for other and more details.

527 '''

528 if area:

529 self._notImplemented(area=area)

530

531 t = _trilaterate(self, distance1, self.others(point2=point2), distance2,

532 self.others(point3=point3), distance3,

533 radius=radius, useZ=True, wrap=wrap,

534 LatLon=self.classof)

535 # ... and handle B{C{eps}} and C{IntersectionError}

536 # like function C{.latlonBase._trilaterate5}

537 d = self.distanceTo(t, radius=radius, wrap=wrap) # PYCHOK distanceTo

538 d = min(fabs(distance1 - d), fabs(distance2 - d), fabs(distance3 - d))

539 if d < eps: # min is max, minPoint is maxPoint

540 return Trilaterate5Tuple(d, t, d, t, 1) # n = 1

541 t = _SPACE_(_no_(_intersection_), Fmt.PAREN(min.__name__, Fmt.f(d, prec=3)))

542 raise IntersectionError(area=area, eps=eps, radius=radius, wrap=wrap, txt=t)

543

544

545 def _nsumOf(nvs, h_None, Vector, Vector_kwds): # .sphericalNvector, .vector3d

546 '''(INTERNAL) Separated to allow callers to embellish exceptions.

547 '''

548 X, Y, Z, n = Fsum(), Fsum(), Fsum(), 0

549 H = Fsum() if h_None is None else n

550 for n, v in enumerate(nvs or ()): # one pass

551 X += v.x

552 Y += v.y

553 Z += v.z

554 H += v.h

555 if n < 1:

556 raise ValueError(_SPACE_(Fmt.PARENSPACED(len=n), _insufficient_))

557

558 x, y, z = map1(float, X, Y, Z)

559 h = H.fover(n) if h_None is None else h_None

560 return Vector3Tuple(x, y, z).to4Tuple(h) if Vector is None else \

561 Vector(x, y, z, **_xkwds(Vector_kwds, h=h))

562

563

564 def sumOf(nvectors, Vector=None, h=None, **Vector_kwds):

565 '''Return the I{vectorial} sum of two or more n-vectors.

566

567 @arg nvectors: Vectors to be added (C{Nvector}[]).

568 @kwarg Vector: Optional class for the vectorial sum (C{Nvector})

569 or C{None}.

570 @kwarg h: Optional height, overriding the mean height (C{meter}).

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

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

573

574 @return: Vectorial sum (B{C{Vector}}) or a L{Vector4Tuple}C{(x, y,

575 z, h)} if B{C{Vector}} is C{None}.

576

577 @raise VectorError: No B{C{nvectors}}.

578 '''

579 try:

580 return _nsumOf(nvectors, h, Vector, Vector_kwds)

581 except (TypeError, ValueError) as x:

582 raise VectorError(nvectors=nvectors, Vector=Vector, cause=x)

583

584

585 def _triangulate(point1, bearing1, point2, bearing2, height=None,

586 wrap=False, **LatLon_and_kwds):

587 # (INTERNAL) Locate a point given two known points and initial

588 # bearings from those points, see C{LatLon.triangulate} above

589

590 def _gc(p, b, _i_):

591 n = p.toNvector()

592 de = NorthPole.cross(n, raiser=_pole_).unit() # east vector @ n

593 dn = n.cross(de) # north vector @ n

594 s, c = sincos2d(Bearing(b, name=_bearing_ + _i_))

595 dest = de.times(s)

596 dnct = dn.times(c)

597 d = dnct.plus(dest) # direction vector @ n

598 return n.cross(d) # great circle point + bearing

599

600 if wrap:

601 point2 = _unrollon(point1, point2, wrap=wrap)

602 if _isequalTo(point1, point2, eps=EPS):

603 raise _ValueError(points=point2, wrap=wrap, txt=_coincident_)

604

605 gc1 = _gc(point1, bearing1, _1_) # great circle p1 + b1

606 gc2 = _gc(point2, bearing2, _2_) # great circle p2 + b2

607

608 n = gc1.cross(gc2, raiser=_point_) # n-vector of intersection point

609 h = point1._havg(point2, h=height)

610 kwds = _xkwds(LatLon_and_kwds, height=h)

611 return n.toLatLon(**kwds) # Nvector(n.x, n.y, n.z).toLatLon(...)

612

613

614 def _trilaterate(point1, distance1, point2, distance2, point3, distance3,

615 radius=R_M, height=None, useZ=False,

616 wrap=False, **LatLon_and_kwds):

617 # (INTERNAL) Locate a point at given distances from

618 # three other points, see LatLon.triangulate above

619

620 def _nr2(p, d, r, _i_, *qs): # .toNvector and angular distance squared

621 for q in qs:

622 if _isequalTo(p, q, eps=EPS):

623 raise _ValueError(points=p, txt=_coincident_)

624 return p.toNvector(), (Scalar(d, name=_distance_ + _i_) / r)**2

625

626 p1, r = point1, Radius_(radius)

627 p2, p3, _ = _unrollon3(p1, point2, point3, wrap)

628

629 n1, r12 = _nr2(p1, distance1, r, _1_)

630 n2, r22 = _nr2(p2, distance2, r, _2_, p1)

631 n3, r32 = _nr2(p3, distance3, r, _3_, p1, p2)

632

633 # the following uses x,y coordinate system with origin at n1, x axis n1->n2

634 y = n3.minus(n1)

635 x = n2.minus(n1)

636 z = None

637

638 d = x.length # distance n1->n2

639 if d > EPS_2: # and y.length > EPS_2:

640 X = x.unit() # unit vector in x direction n1->n2

641 i = X.dot(y) # signed magnitude of x component of n1->n3

642 Y = y.minus(X.times(i)).unit() # unit vector in y direction

643 j = Y.dot(y) # signed magnitude of y component of n1->n3

644 if fabs(j) > EPS_2:

645 # courtesy of U{Carlos Freitas<https://GitHub.com/mrJean1/PyGeodesy/issues/33>}

646 x = fsumf_(r12, -r22, d**2) / (d * _2_0) # n1->intersection x- and ...

647 y = fsumf_(r12, -r32, i**2, j**2, x * i * _N_2_0) / (j * _2_0) # ... y-component

648 # courtesy of U{AleixDev<https://GitHub.com/mrJean1/PyGeodesy/issues/43>}

649 z = fsumf_(max(r12, r22, r32), -(x**2), -(y**2)) # XXX not just r12!

650 if z > EPS:

651 n = n1.plus(X.times(x)).plus(Y.times(y))

652 if useZ: # include Z component

653 Z = X.cross(Y) # unit vector perpendicular to plane

654 n = n.plus(Z.times(sqrt(z)))

655 if height is None:

656 h = fidw((point1.height, point2.height, point3.height),

657 map1(fabs, distance1, distance2, distance3))

658 else:

659 h = Height(height)

660 kwds = _xkwds(LatLon_and_kwds, height=h)

661 return n.toLatLon(**kwds) # Nvector(n.x, n.y, n.z).toLatLon(...)

662

663 # no intersection, d < EPS_2 or fabs(j) < EPS_2 or z < EPS

664 t = _SPACE_(_no_, _intersection_, NN)

665 raise IntersectionError(point1=point1, distance1=distance1,

666 point2=point2, distance2=distance2,

667 point3=point3, distance3=distance3,

668 txt=unstr(t, z=z, useZ=useZ, wrap=wrap))

669

670

671__all__ += _ALL_DOCS(LatLonNvectorBase, NvectorBase, sumOf) # classes

672

673# **) MIT License

674#

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

676#

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

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

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

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

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

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

683#

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

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

686#

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

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

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

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

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

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

693# OTHER DEALINGS IN THE SOFTWARE.