1

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

3

4 u'''(INTERNAL) Base class L{LatLonBase} for all elliposiodal, spherical and N-vectorial C{LatLon} classes.

5

6@see: I{(C) Chris Veness 2005-2024}' U{latlong<https://www.Movable-Type.co.UK/scripts/latlong.html>},

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

8 U{-vectors<https://www.Movable-Type.co.UK/scripts/latlong-vectors.html>} and I{Charles Karney}'s

9 U{Rhumb<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Rhumb.html>} and

10 U{RhumbLine<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1RhumbLine.html>} classes.

11'''

12

13 from pygeodesy.basics import _isin, isstr, map1, _xinstanceof

14 from pygeodesy.constants import EPS, EPS0, EPS1, EPS4, INT0, R_M, \

15 _EPSqrt as _TOL, _0_0, _0_5, _1_0, \

16 _360_0, _umod_360

17 from pygeodesy.datums import _spherical_datum

18 from pygeodesy.dms import F_D, F_DMS, latDMS, lonDMS, parse3llh

19# from pygeodesy.ecef import EcefKarney # _MODS

20 from pygeodesy.ecefLocals import _EcefLocal

21 from pygeodesy.errors import _AttributeError, IntersectionError, \

22 _incompatible, _IsnotError, _TypeError, \

23 _ValueError, _xattr, _xdatum, _xError, \

24 _xkwds, _xkwds_get, _xkwds_item2, _xkwds_not

25# from pygeodesy.fmath import favg # _MODS

26# from pygeodesy import formy as _formy # _MODS.into

27 from pygeodesy.internals import _passarg, typename

28 from pygeodesy.interns import NN, _COMMASPACE_, _concentric_, _height_, \

29 _intersection_, _LatLon_, _m_, _negative_, \

30 _no_, _overlap_, _too_, _point_ # PYCHOK used!

31# from pygeodesy.iters import PointsIter, points2 # _MODS

32# from pygeodesy.karney import Caps # _MODS

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

34 from pygeodesy.named import _name2__, _NamedBase, Fmt

35 from pygeodesy.namedTuples import Bounds2Tuple, LatLon2Tuple, PhiLam2Tuple, \

36 Trilaterate5Tuple, Vector3Tuple

37# from pygeodesy.nvectorBase import _N_vector_ # _MODS

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

39 property_RO, _update_all

40# from pygeodesy.streprs import Fmt, hstr # from .named, _MODS

41 from pygeodesy.units import _isDegrees, _isRadius, Distance_, Lat, Lon, \

42 Height, Radius, Radius_, Scalar, Scalar_

43 from pygeodesy.utily import sincos2d_, _unrollon, _unrollon3, _Wrap

44# from pygeodesy.vector2d import _circin6, Circin6Tuple, _circum3, circum4_, \

45# Circum3Tuple, _radii11ABC4 # _MODS

46# from pygeodesy.vector3d import nearestOn6, Vector3d # _MODS

47

48 from contextlib import contextmanager

49 from math import asin, cos, degrees, fabs, radians

50

51__all__ = _ALL_LAZY.latlonBase

52__version__ = '25.04.28'

53

54_formy = _MODS.into(formy=__name__)

55

56

57 class LatLonBase(_NamedBase, _EcefLocal):

58 '''(INTERNAL) Base class for ellipsoidal and spherical C{satLon}s.

59 '''

60 _clipid = INT0 # polygonal clip, see .booleans

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

62 _height = INT0 # height (C{meter}), default

63 _lat = 0 # latitude (C{degrees})

64 _lon = 0 # longitude (C{degrees})

65

66 def __init__(self, lat_llh, lon=None, height=0, datum=None, **wrap_name):

67 '''New C{LatLon}.

68

69 @arg lat_llh: Latitude (C{degrees} or DMS C{str} with N or S suffix) or

70 a previous C{LatLon} instance provided C{B{lon}=None}.

71 @kwarg lon: Longitude (C{degrees} or DMS C{str} with E or W suffix),

72 required if B{C{lat_llh}} is C{degrees} or C{str}.

73 @kwarg height: Optional height above (or below) the earth surface

74 (C{meter}, conventionally).

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

76 L{a_f2Tuple} or I{scalar} radius) or C{None}.

77 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and optional keyword

78 argument C{B{wrap}=False}, if C{True}, wrap or I{normalize}

79 B{C{lat}} and B{C{lon}} (C{bool}).

80

81 @return: New instance (C{LatLon}).

82

83 @raise RangeError: A B{C{lon}} or C{lat} value outside the valid

84 range and L{rangerrors} set to C{True}.

85

86 @raise TypeError: If B{C{lat_llh}} is not a C{LatLon}.

87

88 @raise UnitError: Invalid C{lat}, B{C{lon}} or B{C{height}}.

89 '''

90 w, n = self._wrap_name2(**wrap_name)

91 if n:

92 self.name = n

93

94 if lon is None:

95 lat, lon, height = _latlonheight3(lat_llh, height, w)

96 elif w:

97 lat, lon = _Wrap.latlonDMS2(lat_llh, lon)

98 else:

99 lat = lat_llh

100

101 self._lat = Lat(lat) # parseDMS2(lat, lon)

102 self._lon = Lon(lon) # PYCHOK LatLon2Tuple

103 if height: # elevation

104 self._height = Height(height)

105 if datum is not None:

106 self._datum = _spherical_datum(datum, name=self.name)

107

108 def __eq__(self, other):

109 return self.isequalTo(other)

110

111 def __ne__(self, other):

112 return not self.isequalTo(other)

113

114 def __str__(self):

115 return self.toStr(form=F_D, prec=6)

116

117 def antipode(self, height=None):

118 '''Return the antipode, the point diametrically opposite to

119 this point.

120

121 @kwarg height: Optional height of the antipode (C{meter}),

122 this point's height otherwise.

123

124 @return: The antipodal point (C{LatLon}).

125 '''

126 a = _formy.antipode(*self.latlon)

127 h = self._heigHt(height)

128 return self.classof(*a, height=h)

129

130 @deprecated_method

131 def bounds(self, wide, tall, radius=R_M): # PYCHOK no cover

132 '''DEPRECATED, use method C{boundsOf}.'''

133 return self.boundsOf(wide, tall, radius=radius)

134

135 def boundsOf(self, wide, tall, radius=R_M, height=None, **name):

136 '''Return the SW and NE lat-/longitude of a great circle

137 bounding box centered at this location.

138

139 @arg wide: Longitudinal box width (C{meter}, same units as

140 B{C{radius}} or C{degrees} if C{B{radius} is None}).

141 @arg tall: Latitudinal box size (C{meter}, same units as

142 B{C{radius}} or C{degrees} if C{B{radius} is None}).

143 @kwarg radius: Mean earth radius (C{meter}) or C{None} if I{both}

144 B{C{wide}} and B{C{tall}} are in C{degrees}.

145 @kwarg height: Height for C{latlonSW} and C{latlonNE} (C{meter}),

146 overriding the point's height.

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

148

149 @return: A L{Bounds2Tuple}C{(latlonSW, latlonNE)}, the lower-left

150 and upper-right corner (C{LatLon}).

151

152 @see: U{https://www.Movable-Type.co.UK/scripts/latlong-db.html}

153 '''

154 w = Scalar_(wide=wide) * _0_5

155 t = Scalar_(tall=tall) * _0_5

156 if radius is not None:

157 r = Radius_(radius)

158 c = cos(self.phi)

159 w = degrees(asin(w / r) / c) if fabs(c) > EPS0 else _0_0 # XXX

160 t = degrees(t / r)

161 y, t = self.lat, fabs(t)

162 x, w = self.lon, fabs(w)

163

164 h = self._heigHt(height)

165 sw = self.classof(y - t, x - w, height=h)

166 ne = self.classof(y + t, x + w, height=h)

167 return Bounds2Tuple(sw, ne, name=self._name__(name))

168

169 def chordTo(self, other, height=None, wrap=False):

170 '''Compute the length of the chord through the earth between

171 this and an other point.

172

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

174 @kwarg height: Overriding height for both points (C{meter}),

175 or if C{None}, use each point's height.

176 @kwarg wrap: If C{True}, wrap or I{normalize} the B{C{other}}

177 point (C{bool}).

178

179 @return: The chord length (conventionally C{meter}).

180

181 @raise TypeError: The B{C{other}} point is not C{LatLon}.

182 '''

183 def _v3d(ll, V3d=_MODS.vector3d.Vector3d):

184 t = ll.toEcef(height=height) # .toVector(Vector=V3d)

185 return V3d(t.x, t.y, t.z)

186

187 p = self.others(other)

188 if wrap:

189 p = _Wrap.point(p)

190 return _v3d(self).minus(_v3d(p)).length

191

192 def circin6(self, point2, point3, eps=EPS4, **wrap_name):

193 '''Return the radius and center of the I{inscribed} aka I{In-}circle

194 of the (planar) triangle formed by this and two other points.

195

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

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

198 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2}.

199 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and optional keyword

200 argument C{B{wrap}=False}, if C{True}, wrap or I{normalize}

201 the B{C{points}} (C{bool}).

202

203 @return: A L{Circin6Tuple}C{(radius, center, deltas, cA, cB, cC)}. The

204 C{center} and contact points C{cA}, C{cB} and C{cC}, each an

205 instance of this (sub-)class, are co-planar with this and the

206 two given points, see the B{Note} below.

207

208 @raise ImportError: Package C{numpy} not found, not installed or older

209 than version 1.10.

210

211 @raise IntersectionError: Near-coincident or -colinear points or

212 a trilateration or C{numpy} issue.

213

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

215

216 @note: The C{center} is trilaterated in cartesian (ECEF) space and converted

217 back to geodetic lat-, longitude and height. The latter, conventionally

218 in C{meter} indicates whether the C{center} is above, below or on the

219 surface of the earth model. If C{deltas} is C{None}, the C{center} is

220 I{un}ambigous. Otherwise C{deltas} is a L{LatLon3Tuple}C{(lat, lon,

221 height)} representing the differences between both results from

222 L{pygeodesy.trilaterate3d2} and C{center} is the mean thereof.

223

224 @see: Function L{pygeodesy.circin6}, method L{circum3}, U{Incircle

225 <https://MathWorld.Wolfram.com/Incircle.html>} and U{Contact Triangle

226 <https://MathWorld.Wolfram.com/ContactTriangle.html>}.

227 '''

228 w, n = self._wrap_name2(**wrap_name)

229

230 with _toCartesian3(self, point2, point3, w) as cs:

231 m = _MODS.vector2d

232 r, c, d, A, B, C = m._circin6(*cs, eps=eps, useZ=True, dLL3=True,

233 datum=self.datum) # PYCHOK unpack

234 return m.Circin6Tuple(r, c.toLatLon(), d, A.toLatLon(),

235 B.toLatLon(),

236 C.toLatLon(), name=n)

237

238 def circum3(self, point2, point3, circum=True, eps=EPS4, **wrap_name):

239 '''Return the radius and center of the smallest circle I{through} or I{containing}

240 this and two other points.

241

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

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

244 @kwarg circum: If C{True}, return the C{circumradius} and C{circumcenter},

245 always, ignoring the I{Meeus}' Type I case (C{bool}).

246 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2}.

247 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and optional keyword

248 argument C{B{wrap}=False}, if C{True}, wrap or I{normalize}

249 the B{C{points}} (C{bool}).

250

251 @return: A L{Circum3Tuple}C{(radius, center, deltas)}. The C{center}, an

252 instance of this (sub-)class, is co-planar with this and the two

253 given points. If C{deltas} is C{None}, the C{center} is

254 I{un}ambigous. Otherwise C{deltas} is a L{LatLon3Tuple}C{(lat,

255 lon, height)} representing the difference between both results

256 from L{pygeodesy.trilaterate3d2} and C{center} is the mean thereof.

257

258 @raise ImportError: Package C{numpy} not found, not installed or older than

259 version 1.10.

260

261 @raise IntersectionError: Near-concentric, -coincident or -colinear points,

262 incompatible C{Ecef} classes or a trilateration

263 or C{numpy} issue.

264

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

266

267 @note: The C{center} is trilaterated in cartesian (ECEF) space and converted

268 back to geodetic lat-, longitude and height. The latter, conventionally

269 in C{meter} indicates whether the C{center} is above, below or on the

270 surface of the earth model. If C{deltas} is C{None}, the C{center} is

271 I{un}ambigous. Otherwise C{deltas} is a L{LatLon3Tuple}C{(lat, lon,

272 height)} representing the difference between both results from

273 L{pygeodesy.trilaterate3d2} and C{center} is the mean thereof.

274

275 @see: Function L{pygeodesy.circum3} and methods L{circin6} and L{circum4_}.

276 '''

277 w, n = self._wrap_name2(**wrap_name)

278

279 with _toCartesian3(self, point2, point3, w, circum=circum) as cs:

280 m = _MODS.vector2d

281 r, c, d = m._circum3(*cs, circum=circum, eps=eps, useZ=True, dLL3=True, # XXX -3d2

282 clas=cs[0].classof, datum=self.datum) # PYCHOK unpack

283 return m.Circum3Tuple(r, c.toLatLon(), d, name=n)

284

285 def circum4_(self, *points, **wrap_name):

286 '''Best-fit a sphere through this and two or more other points.

287

288 @arg points: The other points (each a C{LatLon}).

289 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and optional keyword argument

290 C{B{wrap}=False}, if C{True}, wrap or I{normalize} the B{C{points}}

291 (C{bool}).

292

293 @return: A L{Circum4Tuple}C{(radius, center, rank, residuals)} with C{center} an

294 instance of this (sub-)class.

295

296 @raise ImportError: Package C{numpy} not found, not installed or older than

297 version 1.10.

298

299 @raise NumPyError: Some C{numpy} issue.

300

301 @raise TypeError: One of the B{C{points}} invalid.

302

303 @raise ValueError: Too few B{C{points}}.

304

305 @see: Function L{pygeodesy.circum4_} and L{circum3}.

306 '''

307 w, n = self._wrap_name2(**wrap_name)

308

309 def _cs(ps, C, w):

310 _wp = _Wrap.point if w else _passarg

311 for i, p in enumerate(ps):

312 yield C(i=i, points=_wp(p))

313

314 C = self._toCartesianEcef

315 c = C(point=self)

316 t = _MODS.vector2d.circum4_(c, Vector=c.classof, *_cs(points, C, w))

317 c = t.center.toLatLon(LatLon=self.classof)

318 return t.dup(center=c, name=n)

319

320 @property

321 def clipid(self):

322 '''Get the (polygonal) clip (C{int}).

323 '''

324 return self._clipid

325

326 @clipid.setter # PYCHOK setter!

327 def clipid(self, clipid):

328 '''Get the (polygonal) clip (C{int}).

329 '''

330 self._clipid = int(clipid)

331

332 @deprecated_method

333 def compassAngle(self, other, **adjust_wrap): # PYCHOK no cover

334 '''DEPRECATED, use method L{compassAngleTo}.'''

335 return self.compassAngleTo(other, **adjust_wrap)

336

337 def compassAngleTo(self, other, **adjust_wrap):

338 '''Return the angle from North for the direction vector between

339 this and an other point.

340

341 Suitable only for short, non-near-polar vectors up to a few

342 hundred Km or Miles. Use method C{initialBearingTo} for

343 larger distances.

344

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

346 @kwarg adjust_wrap: Optional keyword arguments for function

347 L{pygeodesy.compassAngle}.

348

349 @return: Compass angle from North (C{degrees360}).

350

351 @raise TypeError: The B{C{other}} point is not C{LatLon}.

352

353 @note: Courtesy of Martin Schultz.

354

355 @see: U{Local, flat earth approximation

356 <https://www.EdWilliams.org/avform.htm#flat>}.

357 '''

358 p = self.others(other)

359 return _formy.compassAngle(self.lat, self.lon, p.lat, p.lon, **adjust_wrap)

360

361 @deprecated_method

362 def cosineAndoyerLambertTo(self, other, **wrap):

363 '''DEPRECATED on 2024.12.31, use method L{cosineLawTo} with C{B{corr}=1}.'''

364 return self.cosineLawTo(other, corr=1, **wrap)

365

366 @deprecated_method

367 def cosineForsytheAndoyerLambertTo(self, other, **wrap):

368 '''DEPRECATED on 2024.12.31, use method L{cosineLawTo} with C{B{corr}=2}.'''

369 return self.cosineLawTo(other, corr=2, **wrap)

370

371 def cosineLawTo(self, other, **radius__corr_wrap):

372 '''Compute the distance between this and an other point using the U{Law of

373 Cosines<https://www.Movable-Type.co.UK/scripts/latlong.html#cosine-law>}

374 formula, optionally corrected.

375

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

377 @kwarg radius__corr_wrap: Optional earth C{B{radius}=None} (C{meter}),

378 overriding the equatorial or mean radius of this point's

379 datum's ellipsoid and keyword arguments for function

380 L{pygeodesy.cosineLaw}.

381

382 @return: Distance (C{meter}, same units as B{C{radius}}).

383

384 @raise TypeError: The B{C{other}} point is not C{LatLon}.

385

386 @see: Function L{pygeodesy.cosineLaw} and methods C{distanceTo*},

387 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo} /

388 L{hubenyTo}, L{flatPolarTo}, L{haversineTo}, L{thomasTo} and

389 L{vincentysTo}.

390 '''

391 c = _xkwds_get(radius__corr_wrap, corr=0)

392 return self._distanceTo_(_formy.cosineLaw_, other, **radius__corr_wrap) if c else \

393 self._distanceTo( _formy.cosineLaw, other, **radius__corr_wrap)

394

395 @property_RO

396 def datum(self): # PYCHOK no cover

397 '''I{Must be overloaded}.'''

398 self._notOverloaded()

399

400 def destinationXyz(self, delta, LatLon=None, **LatLon_kwds):

401 '''Calculate the destination using a I{local} delta from this point.

402

403 @arg delta: Local delta to the destination (L{XyzLocal}, L{Aer}, L{Enu}, L{Ned}

404 or L{Local9Tuple}).

405 @kwarg LatLon: Optional (geodetic) class to return the destination or C{None}.

406 @kwarg LatLon_kwds: Optionally, additional B{C{LatLon}} keyword arguments,

407 ignored if C{B{LatLon} is None}.

408

409 @return: An B{C{LatLon}} instance or if C{B{LatLon} is None}, a

410 L{LatLon4Tuple}C{(lat, lon, height, datum)} or L{LatLon3Tuple}C{(lat,

411 lon, height)} if a C{datum} keyword is specified or not.

412

413 @raise TypeError: Invalid B{C{delta}}, B{C{LatLon}} or B{C{LatLon_kwds}} item.

414 '''

415 t = self._ltp._local2ecef(delta, nine=True) # _EcefLocal._ltp

416 return t.toLatLon(LatLon=LatLon, **_xkwds(LatLon_kwds, name=self.name))

417

418 def _distanceTo(self, func, other, radius=None, **kwds):

419 '''(INTERNAL) Helper for distance methods C{<func>To}.

420 '''

421 p = self.others(other, up=2)

422 R = radius or (self._datum.ellipsoid.R1 if self._datum else R_M)

423 return func(self.lat, self.lon, p.lat, p.lon, radius=R, **kwds)

424

425 def _distanceTo_(self, func_, other, wrap=False, radius=None, **kwds):

426 '''(INTERNAL) Helper for (ellipsoidal) distance methods C{<func>To}.

427 '''

428 p = self.others(other, up=2)

429 D = self.datum or _spherical_datum(radius or R_M, func_)

430 lam21, phi2, _ = _Wrap.philam3(self.lam, p.phi, p.lam, wrap)

431 r = func_(phi2, self.phi, lam21, datum=D, **kwds)

432 return r * (radius or D.ellipsoid.a)

433

434 @Property_RO

435 def _Ecef_forward(self):

436 '''(INTERNAL) Helper for L{_ecef9} and L{toEcef} (C{callable}).

437 '''

438 return self.Ecef(self.datum, name=self.name).forward

439

440 @Property_RO

441 def _ecef9(self):

442 '''(INTERNAL) Helper for L{toCartesian}, L{toEcef} and L{toCartesian} (L{Ecef9Tuple}).

443 '''

444 return self._Ecef_forward(self, M=True)

445

446 @property_RO

447 def ellipsoidalLatLon(self):

448 '''Get the C{LatLon type} iff ellipsoidal, overloaded in L{LatLonEllipsoidalBase}.

449 '''

450 return False

451

452 @deprecated_method

453 def equals(self, other, eps=None): # PYCHOK no cover

454 '''DEPRECATED, use method L{isequalTo}.'''

455 return self.isequalTo(other, eps=eps)

456

457 @deprecated_method

458 def equals3(self, other, eps=None): # PYCHOK no cover

459 '''DEPRECATED, use method L{isequalTo3}.'''

460 return self.isequalTo3(other, eps=eps)

461

462 def equirectangularTo(self, other, **radius_adjust_limit_wrap):

463 '''Compute the distance between this and an other point

464 using the U{Equirectangular Approximation / Projection

465 <https://www.Movable-Type.co.UK/scripts/latlong.html#equirectangular>}.

466

467 Suitable only for short, non-near-polar distances up to a

468 few hundred Km or Miles. Use method L{haversineTo} or

469 C{distanceTo*} for more accurate and/or larger distances.

470

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

472 @kwarg radius_adjust_limit_wrap: Optional keyword arguments

473 for function L{pygeodesy.equirectangular},

474 overriding the default mean C{radius} of this

475 point's datum ellipsoid.

476

477 @return: Distance (C{meter}, same units as B{C{radius}}).

478

479 @raise TypeError: The B{C{other}} point is not C{LatLon}.

480

481 @see: Function L{pygeodesy.equirectangular} and methods L{cosineLawTo},

482 C{distanceTo*}, C{euclideanTo}, L{flatLocalTo} / L{hubenyTo},

483 L{flatPolarTo}, L{haversineTo}, L{thomasTo} and L{vincentysTo}.

484 '''

485 return self._distanceTo(_formy.equirectangular, other, **radius_adjust_limit_wrap)

486

487 def euclideanTo(self, other, **radius_adjust_wrap):

488 '''Approximate the C{Euclidian} distance between this and

489 an other point.

490

491 See function L{pygeodesy.euclidean} for the available B{C{options}}.

492

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

494 @kwarg radius_adjust_wrap: Optional keyword arguments for function

495 L{pygeodesy.euclidean}, overriding the default mean

496 C{radius} of this point's datum ellipsoid.

497

498 @return: Distance (C{meter}, same units as B{C{radius}}).

499

500 @raise TypeError: The B{C{other}} point is not C{LatLon}.

501

502 @see: Function L{pygeodesy.euclidean} and methods L{cosineLawTo}, C{distanceTo*},

503 L{equirectangularTo}, L{flatLocalTo} / L{hubenyTo}, L{flatPolarTo},

504 L{haversineTo}, L{thomasTo} and L{vincentysTo}.

505 '''

506 return self._distanceTo(_formy.euclidean, other, **radius_adjust_wrap)

507

508 def flatLocalTo(self, other, radius=None, **wrap):

509 '''Compute the distance between this and an other point using the

510 U{ellipsoidal Earth to plane projection

511 <https://WikiPedia.org/wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}

512 aka U{Hubeny<https://www.OVG.AT/de/vgi/files/pdf/3781/>} formula.

513

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

515 @kwarg radius: Mean earth radius (C{meter}) or C{None} for the I{equatorial

516 radius} of this point's datum ellipsoid.

517 @kwarg wrap: Optional keyword argument C{B{wrap}=False}, if C{True}, wrap

518 or I{normalize} and unroll the B{C{other}} point (C{bool}).

519

520 @return: Distance (C{meter}, same units as B{C{radius}}).

521

522 @raise TypeError: The B{C{other}} point is not C{LatLon}.

523

524 @raise ValueError: Invalid B{C{radius}}.

525

526 @see: Function L{pygeodesy.flatLocal}/L{pygeodesy.hubeny}, methods L{cosineLawTo},

527 C{distanceTo*}, L{equirectangularTo}, L{euclideanTo}, L{flatPolarTo},

528 L{haversineTo}, L{thomasTo} and L{vincentysTo} and U{local, flat Earth

529 approximation<https://www.edwilliams.org/avform.htm#flat>}.

530 '''

531 r = radius if _isin(radius, None, R_M, _1_0, 1) else Radius(radius)

532 return self._distanceTo_(_formy.flatLocal_, other, radius=r, **wrap) # PYCHOK kwargs

533

534 hubenyTo = flatLocalTo # for Karl Hubeny

535

536 def flatPolarTo(self, other, **radius_wrap):

537 '''Compute the distance between this and an other point using

538 the U{polar coordinate flat-Earth<https://WikiPedia.org/wiki/

539 Geographical_distance#Polar_coordinate_flat-Earth_formula>} formula.

540

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

542 @kwarg radius_wrap: Optional C{B{radius}=R_M} and C{B{wrap}=False} for

543 function L{pygeodesy.flatPolar}, overriding the default

544 C{mean radius} of this point's datum ellipsoid.

545

546 @return: Distance (C{meter}, same units as B{C{radius}}).

547

548 @raise TypeError: The B{C{other}} point is not C{LatLon}.

549

550 @see: Function L{pygeodesy.flatPolar} and methods L{cosineLawTo}, C{distanceTo*},

551 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo} / L{hubenyTo},

552 L{haversineTo}, L{thomasTo} and L{vincentysTo}.

553 '''

554 return self._distanceTo(_formy.flatPolar, other, **radius_wrap)

555

556 def hartzell(self, los=False, earth=None):

557 '''Compute the intersection of a Line-Of-Sight from this (geodetic) Point-Of-View

558 (pov) with this point's ellipsoid surface.

559

560 @kwarg los: Line-Of-Sight, I{direction} to the ellipsoid (L{Los}, L{Vector3d}),

561 C{True} for the I{normal, plumb} onto the surface or I{False} or

562 C{None} to point to the center of the ellipsoid.

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

564 or C{scalar} radius in C{meter}), overriding this point's C{datum}

565 ellipsoid.

566

567 @return: The intersection (C{LatLon}) with attribute C{.height} set to the distance

568 to this C{pov}.

569

570 @raise IntersectionError: Null or bad C{pov} or B{C{los}}, this C{pov} is inside

571 the ellipsoid or B{C{los}} points outside or away from

572 the ellipsoid.

573

574 @raise TypeError: Invalid B{C{los}} or invalid or undefined B{C{earth}} or C{datum}.

575

576 @see: Function L{hartzell<pygeodesy.formy.hartzell>} for further details.

577 '''

578 return _formy._hartzell(self, los, earth, LatLon=self.classof)

579

580 def haversineTo(self, other, **radius_wrap):

581 '''Compute the distance between this and an other point using the U{Haversine

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

583

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

585 @kwarg radius_wrap: Optional C{B{radius}=R_M} and C{B{wrap}=False} for function

586 L{pygeodesy.haversine}, overriding the default C{mean radius} of

587 this point's datum ellipsoid.

588

589 @return: Distance (C{meter}, same units as B{C{radius}}).

590

591 @raise TypeError: The B{C{other}} point is not C{LatLon}.

592

593 @see: Function L{pygeodesy.haversine} and methods L{cosineLawTo}, C{distanceTo*},

594 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo} / L{hubenyTo}, \

595 L{flatPolarTo}, L{thomasTo} and L{vincentysTo}.

596 '''

597 return self._distanceTo(_formy.haversine, other, **radius_wrap)

598

599 def _havg(self, other, f=_0_5, h=None):

600 '''(INTERNAL) Weighted, average height.

601

602 @arg other: An other point (C{LatLon}).

603 @kwarg f: Optional fraction (C{float}).

604 @kwarg h: Overriding height (C{meter}).

605

606 @return: Average, fractional height (C{float}) or the

607 overriding height B{C{h}} (C{Height}).

608 '''

609 return Height(h) if h is not None else \

610 _MODS.fmath.favg(self.height, other.height, f=f)

611

612 @Property

613 def height(self):

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

615 '''

616 return self._height

617

618 @height.setter # PYCHOK setter!

619 def height(self, height):

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

621

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

623

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

625 '''

626 h = Height(height)

627 if self._height != h:

628 _update_all(self)

629 self._height = h

630

631 def _heigHt(self, height):

632 '''(INTERNAL) Overriding this C{height}.

633 '''

634 return self.height if height is None else Height(height)

635

636 def height4(self, earth=None, normal=True, LatLon=None, **LatLon_kwds):

637 '''Compute the projection of this point on and the height above or below

638 this datum's ellipsoid surface.

639

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

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

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

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

644 @kwarg normal: If C{True}, the projection is the normal to this ellipsoid's

645 surface, otherwise the intersection of the I{radial} line to

646 this ellipsoid's center (C{bool}).

647 @kwarg LatLon: Optional class to return the projection, height and datum

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

649 @kwarg LatLon_kwds: Optionally, additional B{C{LatLon}} keyword arguments,

650 ignored if C{B{LatLon} is None}.

651

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

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

654

655 @return: A B{C{LatLon}} instance or if C{B{LatLon} is None}, a L{Vector4Tuple}C{(x,

656 y, z, h)} with the I{projection} C{x}, C{y} and C{z} coordinates and

657 height C{h} in C{meter}, conventionally.

658

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

660

661 @raise TypeError: Invalid B{C{LatLon}}, B{C{LatLon_kwds}} item, B{C{earth}}

662 or triaxial B{C{earth}} couldn't be converted to biaxial

663 B{C{LatLon}} datum.

664

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

666 '''

667 c = self.toCartesian()

668 if LatLon is None:

669 r = c.height4(earth=earth, normal=normal)

670 else:

671 c = c.height4(earth=earth, normal=normal, Cartesian=c.classof, height=0)

672 r = c.toLatLon(LatLon=LatLon, **_xkwds(LatLon_kwds, datum=c.datum, height=c.height))

673 if r.datum != c.datum:

674 raise _TypeError(earth=earth, datum=r.datum)

675 return r

676

677 def heightStr(self, prec=-2, m=_m_):

678 '''Return this point's B{C{height}} as C{str}ing.

679

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

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

682

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

684 '''

685 return _MODS.streprs.hstr(self.height, prec=prec, m=m)

686

687 def intersecant2(self, *args, **kwds): # PYCHOK no cover

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

689 self._notImplemented(*args, **kwds)

690

691 def _intersecend2(self, p, q, wrap, height, g_or_r, P, Q, unused): # in .LatLonEllipsoidalBaseDI.intersecant2

692 '''(INTERNAL) Interpolate 2 heights along a geodesic or rhumb

693 line and return the 2 intersecant points accordingly.

694 '''

695 if height is None:

696 hp = hq = _xattr(p, height=INT0)

697 h = _xattr(q, height=hp) # if isLatLon(q) else hp

698 if h != hp:

699 s = g_or_r._Inverse(p, q, wrap).s12

700 if s: # fmath.fidw?

701 s = (h - hp) / s # slope

702 hq += s * Q.s12

703 hp += s * P.s12

704 else:

705 hp = hq = _MODS.fmath.favg(hp, h)

706 else:

707 hp = hq = Height(height)

708

709# n = self.name or typename(unused)

710 p = q = self.classof(P.lat2, P.lon2, datum=g_or_r.datum, height=hp) # name=n

711 p._iteration = P.iteration

712 if P is not Q:

713 q = self.classof(Q.lat2, Q.lon2, datum=g_or_r.datum, height=hq) # name=n

714 q._iteration = Q.iteration

715 return p, q

716

717 @deprecated_method

718 def isantipode(self, other, eps=EPS): # PYCHOK no cover

719 '''DEPRECATED, use method L{isantipodeTo}.'''

720 return self.isantipodeTo(other, eps=eps)

721

722 def isantipodeTo(self, other, eps=EPS):

723 '''Check whether this and an other point are antipodal, on diametrically

724 opposite sides of the earth.

725

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

727 @kwarg eps: Tolerance for near-equality (C{degrees}).

728

729 @return: C{True} if points are antipodal within the given tolerance,

730 C{False} otherwise.

731 '''

732 p = self.others(other)

733 return _formy.isantipode(*(self.latlon + p.latlon), eps=eps)

734

735 @Property_RO

736 def isEllipsoidal(self):

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

738 '''

739 return _xattr(self.datum, isEllipsoidal=None)

740

741 def isequalTo(self, other, eps=None):

742 '''Compare this point with an other point, I{ignoring} height.

743

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

745 @kwarg eps: Tolerance for equality (C{degrees}).

746

747 @return: C{True} if both points are identical, I{ignoring} height,

748 C{False} otherwise.

749

750 @raise TypeError: The B{C{other}} point is not C{LatLon} or mismatch

751 of the B{C{other}} and this C{class} or C{type}.

752

753 @raise UnitError: Invalid B{C{eps}}.

754

755 @see: Method L{isequalTo3}.

756 '''

757 return _formy._isequalTo(self, self.others(other), eps=eps)

758

759 def isequalTo3(self, other, eps=None):

760 '''Compare this point with an other point, I{including} height.

761

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

763 @kwarg eps: Tolerance for equality (C{degrees}).

764

765 @return: C{True} if both points are identical I{including} height,

766 C{False} otherwise.

767

768 @raise TypeError: The B{C{other}} point is not C{LatLon} or mismatch

769 of the B{C{other}} and this C{class} or C{type}.

770

771 @see: Method L{isequalTo}.

772 '''

773 return self.height == self.others(other).height and \

774 _formy._isequalTo(self, other, eps=eps)

775

776 @Property_RO

777 def isnormal(self):

778 '''Return C{True} if this point is normal (C{bool}),

779 meaning C{abs(lat) <= 90} and C{abs(lon) <= 180}.

780

781 @see: Methods L{normal}, L{toNormal} and functions L{isnormal

782 <pygeodesy.isnormal>} and L{normal<pygeodesy.normal>}.

783 '''

784 return _formy.isnormal(self.lat, self.lon, eps=0)

785

786 @Property_RO

787 def isSpherical(self):

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

789 '''

790 return _xattr(self.datum, isSpherical=None)

791

792 @Property_RO

793 def lam(self):

794 '''Get the longitude (B{C{radians}}).

795 '''

796 return radians(self.lon)

797

798 @Property

799 def lat(self):

800 '''Get the latitude (C{degrees90}).

801 '''

802 return self._lat

803

804 @lat.setter # PYCHOK setter!

805 def lat(self, lat):

806 '''Set the latitude (C{str[N|S]} or C{degrees}).

807

808 @raise ValueError: Invalid B{C{lat}}.

809 '''

810 lat = Lat(lat) # parseDMS(lat, suffix=_NS_, clip=90)

811 if self._lat != lat:

812 _update_all(self)

813 self._lat = lat

814

815 @Property

816 def latlon(self):

817 '''Get the lat- and longitude (L{LatLon2Tuple}C{(lat, lon)}).

818 '''

819 return LatLon2Tuple(self._lat, self._lon, name=self.name)

820

821 @latlon.setter # PYCHOK setter!

822 def latlon(self, latlonh):

823 '''Set the lat- and longitude and optionally the height (2- or 3-tuple

824 or comma- or space-separated C{str} of C{degrees90}, C{degrees180}

825 and C{meter}).

826

827 @raise TypeError: Height of B{C{latlonh}} not C{scalar} or B{C{latlonh}}

828 not C{list} or C{tuple}.

829

830 @raise ValueError: Invalid B{C{latlonh}} or M{len(latlonh)}.

831

832 @see: Function L{pygeodesy.parse3llh} to parse a B{C{latlonh}} string

833 into a 3-tuple C{(lat, lon, h)}.

834 '''

835 if isstr(latlonh):

836 latlonh = parse3llh(latlonh, height=self.height)

837 else:

838 _xinstanceof(list, tuple, latlonh=latlonh)

839 if len(latlonh) == 3:

840 h = Height(latlonh[2], name=Fmt.SQUARE(latlonh=2))

841 elif len(latlonh) != 2:

842 raise _ValueError(latlonh=latlonh)

843 else:

844 h = self.height

845

846 llh = Lat(latlonh[0]), Lon(latlonh[1]), h # parseDMS2(latlonh[0], latlonh[1])

847 if (self._lat, self._lon, self._height) != llh:

848 _update_all(self)

849 self._lat, self._lon, self._height = llh

850

851 def latlon2(self, ndigits=0):

852 '''Return this point's lat- and longitude in C{degrees}, rounded.

853

854 @kwarg ndigits: Number of (decimal) digits (C{int}).

855

856 @return: A L{LatLon2Tuple}C{(lat, lon)}, both C{float} and rounded

857 away from zero.

858

859 @note: The C{round}ed values are always C{float}, also if B{C{ndigits}}

860 is omitted.

861 '''

862 return LatLon2Tuple(round(self.lat, ndigits),

863 round(self.lon, ndigits), name=self.name)

864

865 @deprecated_method

866 def latlon_(self, ndigits=0): # PYCHOK no cover

867 '''DEPRECATED, use method L{latlon2}.'''

868 return self.latlon2(ndigits=ndigits)

869

870 latlon2round = latlon_ # PYCHOK no cover

871

872 @Property

873 def latlonheight(self):

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

875 '''

876 return self.latlon.to3Tuple(self.height)

877

878 @latlonheight.setter # PYCHOK setter!

879 def latlonheight(self, latlonh):

880 '''Set the lat- and longitude and optionally the height

881 (2- or 3-tuple or comma- or space-separated C{str} of

882 C{degrees90}, C{degrees180} and C{meter}).

883

884 @see: Property L{latlon} for more details.

885 '''

886 self.latlon = latlonh

887

888 @Property

889 def lon(self):

890 '''Get the longitude (C{degrees180}).

891 '''

892 return self._lon

893

894 @lon.setter # PYCHOK setter!

895 def lon(self, lon):

896 '''Set the longitude (C{str[E|W]} or C{degrees}).

897

898 @raise ValueError: Invalid B{C{lon}}.

899 '''

900 lon = Lon(lon) # parseDMS(lon, suffix=_EW_, clip=180)

901 if self._lon != lon:

902 _update_all(self)

903 self._lon = lon

904

905# _ltp = _EcefLocal._ltp(self)

906

907 def nearestOn6(self, points, closed=False, height=None, wrap=False):

908 '''Locate the point on a path or polygon closest to this point.

909

910 Points are converted to and distances are computed in I{geocentric},

911 cartesian space.

912

913 @arg points: The path or polygon points (C{LatLon}[]).

914 @kwarg closed: Optionally, close the polygon (C{bool}).

915 @kwarg height: Optional height, overriding the height of this and all

916 other points (C{meter}). If C{None}, take the height

917 of points into account for distances.

918 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{points}}

919 (C{bool}).

920

921 @return: A L{NearestOn6Tuple}C{(closest, distance, fi, j, start, end)}

922 with the C{closest}, the C{start} and the C{end} point each an

923 instance of this C{LatLon} and C{distance} in C{meter}, same

924 units as the cartesian axes.

925

926 @raise PointsError: Insufficient number of B{C{points}}.

927

928 @raise TypeError: Some B{C{points}} or some B{C{points}}' C{Ecef} invalid.

929

930 @raise ValueError: Some B{C{points}}' C{Ecef} is incompatible.

931

932 @see: Function L{nearestOn6<pygeodesy.nearestOn6>}.

933 '''

934 def _cs(Ps, h, w, C):

935 p = None # not used

936 for i, q in Ps.enumerate():

937 if w and i:

938 q = _unrollon(p, q)

939 yield C(height=h, i=i, up=3, points=q)

940 p = q

941

942 C = self._toCartesianEcef # to verify datum and Ecef

943 Ps = self.PointsIter(points, wrap=wrap)

944

945 c = C(height=height, this=self) # this Cartesian

946 t = _MODS.vector3d.nearestOn6(c, _cs(Ps, height, wrap, C), closed=closed)

947 c, s, e = t.closest, t.start, t.end

948

949 kwds = _xkwds_not(None, LatLon=self.classof, # this LatLon

950 height=height)

951 _r = self.Ecef(self.datum).reverse

952 p = _r(c).toLatLon(**kwds)

953 s = _r(s).toLatLon(**kwds) if s is not c else p

954 e = _r(e).toLatLon(**kwds) if e is not c else p

955 return t.dup(closest=p, start=s, end=e)

956

957 def nearestTo(self, *args, **kwds): # PYCHOK no cover

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

959 self._notImplemented(*args, **kwds)

960

961 def normal(self):

962 '''Normalize this point I{in-place} to C{abs(lat) <= 90} and C{abs(lon) <= 180}.

963

964 @return: C{True} if this point was I{normal}, C{False} if it wasn't (but is now).

965

966 @see: Property L{isnormal} and method L{toNormal}.

967 '''

968 n = self.isnormal

969 if not n:

970 self.latlon = _formy.normal(*self.latlon)

971 return n

972

973 @property_RO

974 def _N_vector(self):

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

976 '''

977 _N = _MODS.nvectorBase._N_vector_

978 return _N(*self._n_xyz3, h=self.height, name=self.name)

979

980 @Property_RO

981 def _n_xyz3(self):

982 '''(INTERNAL) Get the n-vector components as L{Vector3Tuple}.

983 '''

984 return philam2n_xyz(self.phi, self.lam, name=self.name)

985

986 @Property_RO

987 def phi(self):

988 '''Get the latitude (B{C{radians}}).

989 '''

990 return radians(self.lat)

991

992 @Property_RO

993 def philam(self):

994 '''Get the lat- and longitude (L{PhiLam2Tuple}C{(phi, lam)}).

995 '''

996 return PhiLam2Tuple(self.phi, self.lam, name=self.name)

997

998 def philam2(self, ndigits=0):

999 '''Return this point's lat- and longitude in C{radians}, rounded.

1000

1001 @kwarg ndigits: Number of (decimal) digits (C{int}).

1002

1003 @return: A L{PhiLam2Tuple}C{(phi, lam)}, both C{float} and rounded

1004 away from zero.

1005

1006 @note: The C{round}ed values are C{float}, always.

1007 '''

1008 return PhiLam2Tuple(round(self.phi, ndigits),

1009 round(self.lam, ndigits), name=self.name)

1010

1011 @Property_RO

1012 def philamheight(self):

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

1014 '''

1015 return self.philam.to3Tuple(self.height)

1016

1017 @deprecated_method

1018 def points(self, points, **closed): # PYCHOK no cover

1019 '''DEPRECATED, use method L{points2}.'''

1020 return self.points2(points, **closed)

1021

1022 def points2(self, points, closed=True):

1023 '''Check a path or polygon represented by points.

1024

1025 @arg points: The path or polygon points (C{LatLon}[])

1026 @kwarg closed: Optionally, consider the polygon closed, ignoring any

1027 duplicate or closing final B{C{points}} (C{bool}).

1028

1029 @return: A L{Points2Tuple}C{(number, points)}, an C{int} and a C{list}

1030 or C{tuple}.

1031

1032 @raise PointsError: Insufficient number of B{C{points}}.

1033

1034 @raise TypeError: Some B{C{points}} are not C{LatLon}.

1035 '''

1036 return _MODS.iters.points2(points, closed=closed, base=self)

1037

1038 def PointsIter(self, points, loop=0, dedup=False, wrap=False):

1039 '''Return a C{PointsIter} iterator.

1040

1041 @arg points: The path or polygon points (C{LatLon}[])

1042 @kwarg loop: Number of loop-back points (non-negative C{int}).

1043 @kwarg dedup: If C{True}, skip duplicate points (C{bool}).

1044 @kwarg wrap: If C{True}, wrap or I{normalize} the enum-/iterated

1045 B{C{points}} (C{bool}).

1046

1047 @return: A new C{PointsIter} iterator.

1048

1049 @raise PointsError: Insufficient number of B{C{points}}.

1050 '''

1051 return _MODS.iters.PointsIter(points, base=self, loop=loop,

1052 dedup=dedup, wrap=wrap)

1053

1054 def radii11(self, point2, point3, wrap=False):

1055 '''Return the radii of the C{Circum-}, C{In-}, I{Soddy} and C{Tangent}

1056 circles of a (planar) triangle formed by this and two other points.

1057

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

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

1060 @kwarg wrap: If C{True}, wrap or I{normalize} B{C{point2}} and

1061 B{C{point3}} (C{bool}).

1062

1063 @return: L{Radii11Tuple}C{(rA, rB, rC, cR, rIn, riS, roS, a, b, c, s)}.

1064

1065 @raise IntersectionError: Near-coincident or -colinear points.

1066

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

1068

1069 @see: Function L{pygeodesy.radii11}, U{Incircle

1070 <https://MathWorld.Wolfram.com/Incircle.html>}, U{Soddy Circles

1071 <https://MathWorld.Wolfram.com/SoddyCircles.html>} and U{Tangent

1072 Circles<https://MathWorld.Wolfram.com/TangentCircles.html>}.

1073 '''

1074 with _toCartesian3(self, point2, point3, wrap) as cs:

1075 return _MODS.vector2d._radii11ABC4(*cs, useZ=True)[0]

1076

1077 def _rhumb3(self, exact, radius): # != .sphericalBase._rhumbs3

1078 '''(INTERNAL) Get the C{rhumb} for this point's datum or for

1079 the B{C{radius}}' earth model iff non-C{None}.

1080 '''

1081 try:

1082 d = self._rhumb3dict

1083 t = d[(exact, radius)]

1084 except KeyError:

1085 D = self.datum if radius is None else \

1086 _spherical_datum(radius) # ellipsoidal OK

1087 try:

1088 r = D.ellipsoid.rhumb_(exact=exact) # or D.isSpherical

1089 except AttributeError as x:

1090 raise _AttributeError(datum=D, radius=radius, cause=x)

1091 t = r, D, _MODS.karney.Caps

1092 if len(d) > 2:

1093 d.clear() # d[:] = {}

1094 d[(exact, radius)] = t # cache 3-tuple

1095 return t

1096

1097 @Property_RO

1098 def _rhumb3dict(self): # in ._update

1099 return {} # 3-item cache

1100

1101 def rhumbAzimuthTo(self, other, exact=False, radius=None, wrap=False, b360=False):

1102 '''Return the azimuth (bearing) of a rhumb line (loxodrome) between this and

1103 an other (ellipsoidal) point.

1104

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

1106 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method

1107 L{Ellipsoid.rhumb_}.

1108 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},

1109 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's datum.

1110 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{other}} point (C{bool}).

1111 @kwarg b360: If C{True}, return the azimuth as bearing in compass degrees (C{bool}).

1112

1113 @return: Rhumb azimuth (C{degrees180} or compass C{degrees360}).

1114

1115 @raise TypeError: The B{C{other}} point is incompatible or B{C{radius}} is invalid.

1116 '''

1117 r, _, Cs = self._rhumb3(exact, radius)

1118 z = r._Inverse(self, other, wrap, outmask=Cs.AZIMUTH).azi12

1119 return _umod_360(z + _360_0) if b360 else z

1120

1121 def rhumbDestination(self, distance, azimuth, radius=None, height=None, exact=False, **name):

1122 '''Return the destination point having travelled the given distance from this point along

1123 a rhumb line (loxodrome) of the given azimuth.

1124

1125 @arg distance: Distance travelled (C{meter}, same units as this point's datum (ellipsoid)

1126 axes or B{C{radius}}, may be negative.

1127 @arg azimuth: Azimuth (bearing) of the rhumb line (compass C{degrees}).

1128 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},

1129 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's datum.

1130 @kwarg height: Optional height, overriding the default height (C{meter}).

1131 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method L{Ellipsoid.rhumb_}.

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

1133

1134 @return: The destination point (ellipsoidal C{LatLon}).

1135

1136 @raise TypeError: Invalid B{C{radius}}.

1137

1138 @raise ValueError: Invalid B{C{distance}}, B{C{azimuth}}, B{C{radius}} or B{C{height}}.

1139 '''

1140 r, D, _ = self._rhumb3(exact, radius)

1141 d = r._Direct(self, azimuth, distance)

1142 h = self._heigHt(height)

1143 return self.classof(d.lat2, d.lon2, datum=D, height=h, **name)

1144

1145 def rhumbDistanceTo(self, other, exact=False, radius=None, wrap=False):

1146 '''Return the distance from this to an other point along a rhumb line (loxodrome).

1147

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

1149 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method L{Ellipsoid.rhumb_}.

1150 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},

1151 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's datum.

1152 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{other}} point (C{bool}).

1153

1154 @return: Distance (C{meter}, the same units as this point's datum (ellipsoid) axes or B{C{radius}}.

1155

1156 @raise TypeError: The B{C{other}} point is incompatible or B{C{radius}} is invalid.

1157

1158 @raise ValueError: Invalid B{C{radius}}.

1159 '''

1160 r, _, Cs = self._rhumb3(exact, radius)

1161 return r._Inverse(self, other, wrap, outmask=Cs.DISTANCE).s12

1162

1163 def rhumbIntersecant2(self, circle, point, other, height=None,

1164 **exact_radius_wrap_eps_tol):

1165 '''Compute the intersections of a circle and a rhumb line given as two points or as a

1166 point and azimuth.

1167

1168 @arg circle: Radius of the circle centered at this location (C{meter}), or a point

1169 on the circle (same C{LatLon} class).

1170 @arg point: The start point of the rhumb line (same C{LatLon} class).

1171 @arg other: An other point I{on} (same C{LatLon} class) or the azimuth I{of} (compass

1172 C{degrees}) the rhumb line.

1173 @kwarg height: Optional height for the intersection points (C{meter}, conventionally)

1174 or C{None} for interpolated heights.

1175 @kwarg exact_radius_wrap_eps_tol: Optional keyword arguments, see methods L{rhumbLine}

1176 and L{RhumbLineAux.Intersecant2} or L{RhumbLine.Intersecant2}.

1177

1178 @return: 2-Tuple of the intersection points (representing a chord), each an instance of

1179 this class. Both points are the same instance if the rhumb line is tangent to

1180 the circle.

1181

1182 @raise IntersectionError: The circle and rhumb line do not intersect.

1183

1184 @raise TypeError: Invalid B{C{point}}, B{C{circle}} or B{C{other}}.

1185

1186 @raise ValueError: Invalid B{C{circle}}, B{C{other}}, B{C{height}} or B{C{exact_radius_wrap}}.

1187

1188 @see: Methods L{RhumbLineAux.Intersecant2} and L{RhumbLine.Intersecant2}.

1189 '''

1190 def _kwds3(eps=EPS, tol=_TOL, wrap=False, **kwds):

1191 return kwds, wrap, dict(eps=eps, tol=tol)

1192

1193 exact_radius, w, eps_tol = _kwds3(**exact_radius_wrap_eps_tol)

1194

1195 p = _unrollon(self, self.others(point=point), wrap=w)

1196 try:

1197 r = Radius_(circle=circle) if _isRadius(circle) else \

1198 self.rhumbDistanceTo(self.others(circle=circle), wrap=w, **exact_radius)

1199 rl = p.rhumbLine(other, wrap=w, **exact_radius)

1200 P, Q = rl.Intersecant2(self.lat, self.lon, r, **eps_tol)

1201

1202 return self._intersecend2(p, other, w, height, rl.rhumb, P, Q,

1203 self.rhumbIntersecant2)

1204 except (TypeError, ValueError) as x:

1205 raise _xError(x, center=self, circle=circle, point=point, other=other,

1206 **exact_radius_wrap_eps_tol)

1207

1208 def rhumbLine(self, other, exact=False, radius=None, wrap=False, **name_caps):

1209 '''Get a rhumb line through this point at a given azimuth or through this and an other point.

1210

1211 @arg other: The azimuth I{of} (compass C{degrees}) or an other point I{on} (same

1212 C{LatLon} class) the rhumb line.

1213 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method L{Ellipsoid.rhumb_}.

1214 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},

1215 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's C{datum}.

1216 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{other}} point (C{bool}).

1217 @kwarg name_caps: Optional C{B{name}=str} and C{caps}, see L{RhumbLine} or L{RhumbLineAux} C{B{caps}}.

1218

1219 @return: A C{RhumbLine} instance (C{RhumbLine} or C{RhumbLineAux}).

1220

1221 @raise TypeError: Invalid B{C{radius}} or B{C{other}} not C{scalar} nor same C{LatLon} class.

1222

1223 @see: Modules L{rhumb.aux_} and L{rhumb.ekx}.

1224 '''

1225 r, _, Cs = self._rhumb3(exact, radius)

1226 kwds = _xkwds(name_caps, name=self.name, caps=Cs.LINE_OFF)

1227 rl = r._DirectLine( self, other, **kwds) if _isDegrees(other) else \

1228 r._InverseLine(self, self.others(other), wrap, **kwds)

1229 return rl

1230

1231 def rhumbMidpointTo(self, other, exact=False, radius=None, height=None, fraction=_0_5, **wrap_name):

1232 '''Return the (loxodromic) midpoint on the rhumb line between this and an other point.

1233

1234 @arg other: The other point (same C{LatLon} class).

1235 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method L{Ellipsoid.rhumb_}.

1236 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},

1237 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's datum.

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

1239 @kwarg fraction: Midpoint location from this point (C{scalar}), 0 for this, 1 for the B{C{other}},

1240 0.5 for halfway between this and the B{C{other}} point, may be negative or

1241 greater than 1.

1242 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and C{B{wrap}=False}, if C{True}, wrap or

1243 I{normalize} and unroll the B{C{other}} point (C{bool}).

1244

1245 @return: The midpoint at the given B{C{fraction}} along the rhumb line (same C{LatLon} class).

1246

1247 @raise TypeError: The B{C{other}} point is incompatible or B{C{radius}} is invalid.

1248

1249 @raise ValueError: Invalid B{C{height}} or B{C{fraction}}.

1250 '''

1251 w, n = self._wrap_name2(**wrap_name)

1252 r, D, _ = self._rhumb3(exact, radius)

1253 f = Scalar(fraction=fraction)

1254 d = r._Inverse(self, self.others(other), w) # C.AZIMUTH_DISTANCE

1255 d = r._Direct( self, d.azi12, d.s12 * f)

1256 h = self._havg(other, f=f, h=height)

1257 return self.classof(d.lat2, d.lon2, datum=D, height=h, name=n)

1258

1259 @property_RO

1260 def sphericalLatLon(self):

1261 '''Get the C{LatLon type} iff spherical, overloaded in L{LatLonSphericalBase}.

1262 '''

1263 return False

1264

1265 def thomasTo(self, other, **wrap):

1266 '''Compute the distance between this and an other point using U{Thomas'

1267 <https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} formula.

1268

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

1270 @kwarg wrap: Optional keyword argument C{B{wrap}=False}, if C{True}, wrap

1271 or I{normalize} and unroll the B{C{other}} point (C{bool}).

1272

1273 @return: Distance (C{meter}, same units as the axes of this point's datum ellipsoid).

1274

1275 @raise TypeError: The B{C{other}} point is not C{LatLon}.

1276

1277 @see: Function L{pygeodesy.thomas} and methods L{cosineLawTo}, C{distanceTo*},

1278 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo} / L{hubenyTo},

1279 L{flatPolarTo}, L{haversineTo} and L{vincentysTo}.

1280 '''

1281 return self._distanceTo_(_formy.thomas_, other, **wrap)

1282

1283 @deprecated_method

1284 def to2ab(self): # PYCHOK no cover

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

1286 return self.philam

1287

1288 def toCartesian(self, height=None, Cartesian=None, **Cartesian_kwds):

1289 '''Convert this point to cartesian, I{geocentric} coordinates, also known as

1290 I{Earth-Centered, Earth-Fixed} (ECEF).

1291

1292 @kwarg height: Optional height, overriding this point's height (C{meter},

1293 conventionally).

1294 @kwarg Cartesian: Optional class to return the geocentric coordinates

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

1296 @kwarg Cartesian_kwds: Optionally, additional B{C{Cartesian}} keyword

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

1298

1299 @return: A B{C{Cartesian}} instance or if B{C{Cartesian} is None}, an

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

1301 C{C=0} and C{M} if available.

1302

1303 @raise TypeError: Invalid B{C{Cartesian}} or B{C{Cartesian_kwds}} item.

1304

1305 @see: Methods C{toNvector}, C{toVector} and C{toVector3d}.

1306 '''

1307 r = self._ecef9 if height is None else self.toEcef(height=height)

1308 if Cartesian is not None: # class or .classof

1309 r = Cartesian(r, **self._name1__(Cartesian_kwds))

1310 _xdatum(r.datum, self.datum)

1311 return r

1312

1313 def _toCartesianEcef(self, height=None, i=None, up=2, **name_point):

1314 '''(INTERNAL) Convert to cartesian and check Ecef's before and after.

1315 '''

1316 p = self.others(up=up, **name_point)

1317 c = p.toCartesian(height=height)

1318 E = self.Ecef

1319 if E:

1320 for p in (p, c):

1321 e = _xattr(p, Ecef=None)

1322 if not _isin(e, None, E): # PYCHOK no cover

1323 n, _ = _xkwds_item2(name_point)

1324 n = Fmt.INDEX(n, i)

1325 t = _incompatible(typename(E))

1326 raise _ValueError(n, e, txt=t) # txt__

1327 return c

1328

1329 def toDatum(self, datum2, height=None, **name):

1330 '''I{Must be overloaded}.'''

1331 self._notOverloaded(datum2, height=height, **name)

1332

1333 def toEcef(self, height=None, M=False):

1334 '''Convert this point to I{geocentric} coordinates, also known as

1335 I{Earth-Centered, Earth-Fixed} (U{ECEF<https://WikiPedia.org/wiki/ECEF>}).

1336

1337 @kwarg height: Optional height, overriding this point's height (C{meter},

1338 conventionally).

1339 @kwarg M: Optionally, include the rotation L{EcefMatrix} (C{bool}).

1340

1341 @return: An L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with

1342 C{C=0} and C{M} if available.

1343

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

1345 '''

1346 return self._ecef9 if _isin(height, None, self.height) else \

1347 self._Ecef_forward(self.lat, self.lon, height=height, M=M)

1348

1349 @deprecated_method

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

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

1352 return self.latlonheight if _isin(height, None, self.height) else \

1353 self.latlon.to3Tuple(height)

1354

1355 def toNormal(self, deep=False, **name):

1356 '''Get this point I{normalized} to C{abs(lat) <= 90} and C{abs(lon) <= 180}.

1357

1358 @kwarg deep: If C{True}, make a deep, otherwise a shallow copy (C{bool}).

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

1360

1361 @return: A copy of this point, I{normalized} (C{LatLon}), optionally renamed.

1362

1363 @see: Property L{isnormal}, method L{normal} and function L{pygeodesy.normal}.

1364 '''

1365 ll = self.copy(deep=deep)

1366 _ = ll.normal()

1367 if name:

1368 ll.rename(name)

1369 return ll

1370

1371 def toNvector(self, h=None, Nvector=None, **name_Nvector_kwds):

1372 '''Convert this point to C{n-vector} (normal to the earth's surface) components,

1373 I{including height}.

1374

1375 @kwarg h: Optional height, overriding this point's height (C{meter}).

1376 @kwarg Nvector: Optional class to return the C{n-vector} components (C{Nvector})

1377 or C{None}.

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

1379 additional B{C{Nvector}} keyword arguments, ignored if C{B{Nvector}

1380 is None}.

1381

1382 @return: An B{C{Nvector}} instance or a L{Vector4Tuple}C{(x, y, z, h)} if

1383 C{B{Nvector} is None}.

1384

1385 @raise TypeError: Invalid B{C{h}}, B{C{Nvector}} or B{C{name_Nvector_kwds}}.

1386

1387 @see: Methods C{toCartesian}, C{toVector} and C{toVector3d}.

1388 '''

1389 h = self._heigHt(h)

1390 if Nvector is None:

1391 r = self._n_xyz3.to4Tuple(h)

1392 n, _ = _name2__(name_Nvector_kwds, _or_nameof=self)

1393 if n:

1394 r.rename(n)

1395 else:

1396 x, y, z = self._n_xyz3

1397 r = Nvector(x, y, z, h=h, ll=self, **self._name1__(name_Nvector_kwds))

1398 return r

1399

1400 def toStr(self, form=F_DMS, joined=_COMMASPACE_, m=_m_, **prec_sep_s_D_M_S): # PYCHOK expected

1401 '''Convert this point to a "lat, lon[, +/-height]" string, formatted in the

1402 given C{B{form}at}.

1403

1404 @kwarg form: The lat-/longitude C{B{form}at} to use (C{str}), see functions

1405 L{pygeodesy.latDMS} or L{pygeodesy.lonDMS}.

1406 @kwarg joined: Separator to join the lat-, longitude and height strings (C{str}

1407 or C{None} or C{NN} for non-joined).

1408 @kwarg m: Optional unit of the height (C{str}), use C{None} to exclude height

1409 from the returned string.

1410 @kwarg prec_sep_s_D_M_S: Optional C{B{prec}ision}, C{B{sep}arator}, B{C{s_D}},

1411 B{C{s_M}}, B{C{s_S}} and B{C{s_DMS}} keyword arguments, see function

1412 L{pygeodesy.toDMS} for details.

1413

1414 @return: This point in the specified C{B{form}at}, etc. (C{str} or a 2- or 3-tuple

1415 C{(lat_str, lon_str[, height_str])} if B{C{joined}} is C{NN} or C{None}).

1416

1417 @see: Function L{pygeodesy.latDMS} or L{pygeodesy.lonDMS} for more details about

1418 keyword arguments C{B{form}at}, C{B{prec}ision}, C{B{sep}arator}, B{C{s_D}},

1419 B{C{s_M}}, B{C{s_S}} and B{C{s_DMS}}.

1420 '''

1421 t = (latDMS(self.lat, form=form, **prec_sep_s_D_M_S),

1422 lonDMS(self.lon, form=form, **prec_sep_s_D_M_S))

1423 if self.height and m is not None:

1424 t += (self.heightStr(m=m),)

1425 return joined.join(t) if joined else t

1426

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

1428 '''Convert this point to a C{Vector} with the I{geocentric} C{(x, y, z)} (ECEF)

1429 coordinates, I{ignoring height}.

1430

1431 @kwarg Vector: Optional class to return the I{geocentric} components (L{Vector3d})

1432 or C{None}.

1433 @kwarg Vector_kwds: Optionally, additional B{C{Vector}} keyword arguments,

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

1435

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

1437 is None}.

1438

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

1440

1441 @see: Methods C{toCartesian}, C{toNvector} and C{toVector3d}.

1442 '''

1443 return self._ecef9.toVector(Vector=Vector, **self._name1__(Vector_kwds))

1444

1445 def toVector3d(self, norm=True, **Vector3d_kwds):

1446 '''Convert this point to a L{Vector3d} with the I{geocentric} C{(x, y, z)}

1447 (ECEF) coordinates, I{ignoring height}.

1448

1449 @kwarg norm: If C{False}, don't normalize the coordinates (C{bool}).

1450 @kwarg Vector3d_kwds: Optional L{Vector3d} keyword arguments.

1451

1452 @return: Named, unit vector or vector (L{Vector3d}).

1453

1454 @raise TypeError: Invalid B{C{Vector3d_kwds}}.

1455

1456 @see: Methods C{toCartesian}, C{toNvector} and C{toVector}.

1457 '''

1458 r = self.toVector(Vector=_MODS.vector3d.Vector3d, **Vector3d_kwds)

1459 if norm:

1460 r = r.unit(ll=self)

1461 return r

1462

1463 def toWm(self, **toWm_kwds):

1464 '''Convert this point to a WM coordinate.

1465

1466 @kwarg toWm_kwds: Optional L{pygeodesy.toWm} keyword arguments.

1467

1468 @return: The WM coordinate (L{Wm}).

1469

1470 @see: Function L{pygeodesy.toWm}.

1471 '''

1472 return _MODS.webmercator.toWm(self, **self._name1__(toWm_kwds))

1473

1474 @deprecated_method

1475 def to3xyz(self): # PYCHOK no cover

1476 '''DEPRECATED, use property L{xyz} or method L{toNvector}, L{toVector},

1477 L{toVector3d} or perhaps (geocentric) L{toEcef}.'''

1478 return self.xyz # self.toVector()

1479

1480# def _update(self, updated, *attrs, **setters):

1481# '''(INTERNAL) See C{_NamedBase._update}.

1482# '''

1483# if updated:

1484# self._rhumb3dict.clear()

1485# return _NamedBase._update(self, updated, *attrs, **setters)

1486

1487 def vincentysTo(self, other, **radius_wrap):

1488 '''Compute the distance between this and an other point using U{Vincenty's

1489 <https://WikiPedia.org/wiki/Great-circle_distance>} spherical formula.

1490

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

1492 @kwarg radius_wrap: Optional C{B{radius}=R_M} and C{B{wrap}=False} for

1493 function L{pygeodesy.vincentys}, overriding the default

1494 C{mean radius} of this point's datum ellipsoid.

1495

1496 @return: Distance (C{meter}, same units as B{C{radius}}).

1497

1498 @raise TypeError: The B{C{other}} point is not C{LatLon}.

1499

1500 @see: Function L{pygeodesy.vincentys} and methods L{cosineLawTo}, C{distanceTo*},

1501 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo} / L{hubenyTo},

1502 L{flatPolarTo}, L{haversineTo} and L{thomasTo}.

1503 '''

1504 return self._distanceTo(_formy.vincentys, other, **_xkwds(radius_wrap, radius=None))

1505

1506 def _wrap_name2(self, wrap=False, **name):

1507 '''(INTERNAL) Return the C{wrap} and C{name} value.

1508 '''

1509 return wrap, (self._name__(name) if name else NN)

1510

1511 @property_RO

1512 def xyz(self):

1513 '''Get the I{geocentric} C{(x, y, z)} coordinates (L{Vector3Tuple}C{(x, y, z)})

1514 '''

1515 return self._ecef9.xyz

1516

1517 @property_RO

1518 def xyz3(self):

1519 '''Get the I{geocentric} C{(x, y, z)} coordinates as C{3-tuple}.

1520 '''

1521 return tuple(self.xyz)

1522

1523 @Property_RO

1524 def xyzh(self):

1525 '''Get the I{geocentric} C{(x, y, z)} coordinates and height (L{Vector4Tuple}C{(x, y, z, h)})

1526 '''

1527 return self.xyz.to4Tuple(self.height)

1528

1529

1530 class _toCartesian3(object): # see also .formy._idllmn6, .geodesicw._wargs, .vector2d._numpy

1531 '''(INTERNAL) Wrapper to convert 2 other points.

1532 '''

1533 @contextmanager # <https://www.Python.org/dev/peps/pep-0343/> Examples

1534 def __call__(self, p, p2, p3, wrap, **kwds):

1535 try:

1536 if wrap:

1537 p2, p3 = map1(_Wrap.point, p2, p3)

1538 kwds = _xkwds(kwds, wrap=wrap)

1539 yield (p. toCartesian().copy(name=_point_), # copy to rename

1540 p._toCartesianEcef(up=4, point2=p2),

1541 p._toCartesianEcef(up=4, point3=p3))

1542 except (AssertionError, TypeError, ValueError) as x: # Exception?

1543 raise _xError(x, point=p, point2=p2, point3=p3, **kwds)

1544

1545_toCartesian3 = _toCartesian3() # PYCHOK singleton

1546

1547

1548 def _latlonheight3(latlonh, height, wrap): # in .points.LatLon_.__init__

1549 '''(INTERNAL) Get 3-tuple C{(lat, lon, height)}.

1550 '''

1551 try:

1552 lat, lon = latlonh.lat, latlonh.lon

1553 height = _xattr(latlonh, height=height)

1554 except AttributeError:

1555 raise _IsnotError(_LatLon_, latlonh=latlonh)

1556 if wrap:

1557 lat, lon = _Wrap.latlon(lat, lon)

1558 return lat, lon, height

1559

1560

1561 def latlon2n_xyz(lat_ll, lon=None, **name):

1562 '''Convert lat-, longitude to C{n-vector} (I{normal} to the earth's surface) X, Y and Z components.

1563

1564 @arg lat_ll: Latitude (C{degrees}) or a C{LatLon} instance, L{LatLon2Tuple} or other C{LatLon*Tuple}.

1565 @kwarg lon: Longitude (C{degrees}), required if C{B{lon_ll} is degrees}, ignored otherwise.

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

1567

1568 @return: A L{Vector3Tuple}C{(x, y, z)}.

1569

1570 @see: Function L{philam2n_xyz}.

1571

1572 @note: These are C{n-vector} x, y and z components, I{NOT geocentric} x, y and z (ECEF) coordinates!

1573 '''

1574 lat = lat_ll

1575 if lon is None:

1576 try:

1577 lat, lon = lat_ll.latlon

1578 except AttributeError:

1579 lat = lat_ll.lat

1580 lon = lat_ll.lon

1581 # Kenneth Gade eqn 3, but using right-handed

1582 # vector x -> 0°E,0°N, y -> 90°E,0°N, z -> 90°N

1583 sa, ca, sb, cb = sincos2d_(lat, lon)

1584 return Vector3Tuple(ca * cb, ca * sb, sa, **name)

1585

1586

1587 def philam2n_xyz(phi_ll, lam=None, **name):

1588 '''Convert lat-, longitude to C{n-vector} (I{normal} to the earth's surface) X, Y and Z components.

1589

1590 @arg phi_ll: Latitude (C{radians}) or a C{LatLon} instance with C{phi}, C{lam} or C{philam} attributes.

1591 @kwarg lam: Longitude (C{radians}), required if C{B{phi_ll} is radians}, ignored otherwise.

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

1593

1594 @return: A L{Vector3Tuple}C{(x, y, z)}.

1595

1596 @see: Function L{latlon2n_xyz}.

1597

1598 @note: These are C{n-vector} x, y and z components, I{NOT geocentric} x, y and z (ECEF) coordinates!

1599 '''

1600 phi = phi_ll

1601 if lam is None:

1602 try:

1603 phi, lam = phi_ll.philam

1604 except AttributeError:

1605 phi = phi_ll.phi

1606 lam = phi_ll.lam

1607 return latlon2n_xyz(degrees(phi), degrees(lam), **name)

1608

1609

1610 def _trilaterate5(p1, d1, p2, d2, p3, d3, area=True, eps=EPS1, radius=R_M, wrap=False): # MCCABE 13

1611 '''(INTERNAL) Trilaterate three points by I{area overlap} or by I{perimeter intersection} of three circles.

1612

1613 @note: The B{C{radius}} is needed only for C{n-vectorial} and C{sphericalTrigonometry.LatLon.distanceTo}

1614 methods and silently ignored by the C{ellipsoidalExact}, C{-GeodSolve}, C{-Karney} and

1615 C{-Vincenty.LatLon.distanceTo} methods.

1616 '''

1617 p2, p3, w = _unrollon3(p1, p2, p3, wrap)

1618 rw = dict(radius=radius, wrap=w)

1619

1620 r1 = Distance_(distance1=d1)

1621 r2 = Distance_(distance2=d2)

1622 r3 = Distance_(distance3=d3)

1623 m = 0 if area else (r1 + r2 + r3)

1624 pc = 0

1625 t = []

1626 for _ in range(3):

1627 try: # intersection of circle (p1, r1) and (p2, r2)

1628 c1, c2 = p1.intersections2(r1, p2, r2, wrap=w)

1629

1630 if area: # check overlap

1631 if c1 is c2: # abutting

1632 c = c1

1633 else: # nearest point on radical

1634 c = p3.nearestOn(c1, c2, within=True, wrap=w)

1635 d = r3 - p3.distanceTo(c, **rw)

1636 if d > eps: # sufficient overlap

1637 t.append((d, c))

1638 m = max(m, d)

1639

1640 else: # check intersection

1641 for c in ((c1,) if c1 is c2 else (c1, c2)):

1642 d = fabs(r3 - p3.distanceTo(c, **rw))

1643 if d < eps: # below margin

1644 t.append((d, c))

1645 m = min(m, d)

1646

1647 except IntersectionError as x:

1648 if _concentric_ in str(x): # XXX ConcentricError?

1649 pc += 1

1650

1651 p1, r1, p2, r2, p3, r3 = p2, r2, p3, r3, p1, r1 # rotate

1652

1653 if t: # get min, max, points and count ...

1654 t = tuple(sorted(t))

1655 n = len(t), # as 1-tuple

1656 # ... or for a single trilaterated result,

1657 # min *is* max, min- *is* maxPoint and n=1, 2 or 3

1658 return Trilaterate5Tuple(t[0] + t[-1] + n) # *(t[0] + ...)

1659

1660 elif area and pc == 3: # all pairwise concentric ...

1661 r, p = min((r1, p1), (r2, p2), (r3, p3))

1662 m = max(r1, r2, r3)

1663 # ... return "smallest" point twice, the smallest

1664 # and largest distance and n=0 for concentric

1665 return Trilaterate5Tuple(float(r), p, float(m), p, 0)

1666

1667 n, f = (_overlap_, max) if area else (_intersection_, min)

1668 t = _COMMASPACE_(_no_(n), '%s %.3g' % (typename(f), m))

1669 raise IntersectionError(area=area, eps=eps, wrap=wrap, txt=t)

1670

1671

1672__all__ += _ALL_DOCS(LatLonBase)

1673

1674# **) MIT License

1675#

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

1677#

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

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

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

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

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

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

1684#

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

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

1687#

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

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

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

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

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

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

1694# OTHER DEALINGS IN THE SOFTWARE.