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}' 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 isscalar, isstr, map1, _xinstanceof

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

15 _0_0, _0_5, _1_0, _180_0

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

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

18# from pygeodesy.ecef import EcefKarney # _MODS

19 from pygeodesy.errors import _AttributeError, _incompatible, \

20 _IsnotError, IntersectionError, \

21 _TypeError, _ValueError, _xattr, _xdatum, \

22 _xError, _xkwds, _xkwds_not

23 from pygeodesy.fmath import favg, sqrt_a

24 from pygeodesy.formy import antipode, compassAngle, cosineAndoyerLambert_, \

25 cosineForsytheAndoyerLambert_, cosineLaw, \

26 equirectangular, euclidean, flatLocal_, \

27 flatPolar, _hartzell, haversine, isantipode, \

28 _isequalTo, isnormal, normal, philam2n_xyz, \

29 thomas_, vincentys, _spherical_datum

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

31 _intersection_, _LatLon_, _m_, _negative_, \

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

33# from pygeodesy.iters import PointsIter, points2 # from .vector3d, _MODS

34# from pygeodesy.karney import Caps # _MODS

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

36# from pygeodesy.ltp import Ltp, _xLtp # _MODS

37 from pygeodesy.named import _NamedBase, notOverloaded, Fmt

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

39 Trilaterate5Tuple, Vector3Tuple

40# from pygeodesy.nvectorBase import _N_vector_ # _MODS

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

42 property_RO, _update_all

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

44 from pygeodesy.units import Distance_, Lat, Lon, Height, Radius, Radius_, \

45 Scalar, Scalar_

46 from pygeodesy.utily import sincos2d, _unrollon, _unrollon3, _Wrap

47 from pygeodesy.vector2d import _circin6, Circin6Tuple, _circum3, circum4_, \

48 Circum3Tuple, _radii11ABC

49 from pygeodesy.vector3d import nearestOn6, Vector3d, PointsIter

50

51 from contextlib import contextmanager

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

53

54__all__ = _ALL_LAZY.latlonBase

55__version__ = '23.10.08'

56

57

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

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

60 '''

61 try:

62 lat, lon = latlonh.lat, latlonh.lon

63 height = _xattr(latlonh, height=height)

64 except AttributeError:

65 raise _IsnotError(_LatLon_, latlonh=latlonh)

66 if wrap:

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

68 return lat, lon, height

69

70

71 class LatLonBase(_NamedBase):

72 '''(INTERNAL) Base class for C{LatLon} points on spherical or

73 ellipsoidal earth models.

74 '''

75 _clipid = INT0 # polygonal clip, see .booleans

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

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

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

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

80

81 def __init__(self, latlonh, lon=None, height=0, wrap=False, name=NN, datum=None):

82 '''New C{LatLon}.

83

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

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

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

87 C(None), indicating B{C{latlonh}} is a C{LatLon}.

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

89 (C{meter}, conventionally).

90 @kwarg wrap: If C{True}, wrap or I{normalize} B{C{lat}} and B{C{lon}}

91 (C{bool}).

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

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

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

95

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

97

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

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

100

101 @raise TypeError: If B{C{latlonh}} is not a C{LatLon}.

102

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

104

105 @example:

106

107 >>> p = LatLon(50.06632, -5.71475)

108 >>> q = LatLon('50°03′59″N', """005°42'53"W""")

109 >>> r = LatLon(p)

110 '''

111 if name:

112 self.name = name

113

114 if lon is None:

115 lat, lon, height = _latlonheight3(latlonh, height, wrap)

116 elif wrap:

117 lat, lon = _Wrap.latlonDMS2(latlonh, lon)

118 else:

119 lat = latlonh

120

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

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

123 if height: # elevation

124 self._height = Height(height)

125 if datum:

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

127

128 def __eq__(self, other):

129 return self.isequalTo(other)

130

131 def __ne__(self, other):

132 return not self.isequalTo(other)

133

134 def __str__(self):

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

136

137 def antipode(self, height=None):

138 '''Return the antipode, the point diametrically opposite

139 to this point.

140

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

142 this point's height otherwise.

143

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

145 '''

146 h = self._heigHt(height)

147 return self.classof(*antipode(*self.latlon), height=h)

148

149 @deprecated_method

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

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

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

153

154 def boundsOf(self, wide, tall, radius=R_M, height=None):

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

156 bounding box centered at this location.

157

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

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

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

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

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

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

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

165 overriding the point's height.

166

167 @return: A L{Bounds2Tuple}C{(latlonSW, latlonNE)}, the

168 lower-left and upper-right corner (C{LatLon}).

169

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

171 '''

172 w = Scalar_(wide=wide) * _0_5

173 t = Scalar_(tall=tall) * _0_5

174 if radius is not None:

175 r = Radius_(radius)

176 c = cos(self.phi)

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

178 t = degrees(t / r)

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

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

181

182 h = self._heigHt(height)

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

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

185 return Bounds2Tuple(sw, ne, name=self.name)

186

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

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

189 this and an other point.

190

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

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

193 or C{None} for each point's height.

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

195 point (C{bool}).

196

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

198

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

200 '''

201 def _v3d(ll):

202 t = ll.toEcef(height=height) # .toVector(Vector=Vector3d)

203 return Vector3d(t.x, t.y, t.z)

204

205 p = self.others(other)

206 if wrap:

207 p = _Wrap.point(p)

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

209

210 def circin6(self, point2, point3, eps=EPS4, wrap=False):

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

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

213

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

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

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

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

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

219

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

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

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

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

224

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

226 than version 1.10.

227

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

229 a trilateration or C{numpy} issue.

230

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

232

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

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

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

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

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

238 height)} representing the differences between both results from

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

240

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

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

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

244 '''

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

246 r, c, d, cA, cB, cC = _circin6(*cs, eps=eps, useZ=True, dLL3=True,

247 datum=self.datum) # PYCHOK unpack

248 return Circin6Tuple(r, c.toLatLon(), d, cA.toLatLon(), cB.toLatLon(), cC.toLatLon())

249

250 def circum3(self, point2, point3, circum=True, eps=EPS4, wrap=False):

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

252 this and two other points.

253

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

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

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

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

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

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

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

261

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

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

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

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

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

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

268

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

270 version 1.10.

271

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

273 incompatible C{Ecef} classes or a trilateration

274 or C{numpy} issue.

275

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

277

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

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

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

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

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

283 height)} representing the difference between both results from

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

285

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

287 '''

288 with _toCartesian3(self, point2, point3, wrap, circum=circum) as cs:

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

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

291 return Circum3Tuple(r, c.toLatLon(), d)

292

293 def circum4_(self, *points, **wrap):

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

295

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

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

298 (C{bool}), default C{False}.

299

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

301 an instance of this (sub-)class.

302

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

304 version 1.10.

305

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

307

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

309

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

311

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

313 '''

314 def _cs(ps, C, wrap=False):

315 _wp = _Wrap.point if wrap else (lambda p: p)

316 for i, p in enumerate(ps):

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

318

319 C = self._toCartesianEcef

320 c = C(point=self)

321 t = circum4_(c, Vector=c.classof, *_cs(points, C, **wrap))

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

323 return t.dup(center=c)

324

325 @property

326 def clipid(self):

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

328 '''

329 return self._clipid

330

331 @clipid.setter # PYCHOK setter!

332 def clipid(self, clipid):

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

334 '''

335 self._clipid = int(clipid)

336

337 @deprecated_method

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

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

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

341

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

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

344 this and an other point.

345

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

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

348 larger distances.

349

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

351 @kwarg adjust_wrap: Optional keyword arguments for function

352 L{pygeodesy.compassAngle}.

353

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

355

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

357

358 @note: Courtesy of Martin Schultz.

359

360 @see: U{Local, flat earth approximation

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

362 '''

363 p = self.others(other)

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

365

366 def cosineAndoyerLambertTo(self, other, wrap=False):

367 '''Compute the distance between this and an other point using the U{Andoyer-Lambert correction<https://

368 navlib.net/wp-content/uploads/2013/10/admiralty-manual-of-navigation-vol-1-1964-english501c.pdf>}

369 of the U{Law of Cosines<https://www.Movable-Type.co.UK/scripts/latlong.html#cosine-law>} formula.

370

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

372 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

373 the B{C{other}} point (C{bool}).

374

375 @return: Distance (C{meter}, same units as the axes of this

376 point's datum ellipsoid).

377

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

379

380 @see: Function L{pygeodesy.cosineAndoyerLambert} and methods

381 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo},

382 C{distanceTo*}, L{equirectangularTo}, L{euclideanTo},

383 L{flatLocalTo}/L{hubenyTo}, L{flatPolarTo}, L{haversineTo},

384 L{thomasTo} and L{vincentysTo}.

385 '''

386 return self._distanceTo_(cosineAndoyerLambert_, other, wrap=wrap)

387

388 def cosineForsytheAndoyerLambertTo(self, other, wrap=False):

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

390 the U{Forsythe-Andoyer-Lambert correction

391 <https://www2.UNB.Ca/gge/Pubs/TR77.pdf>} of the U{Law of Cosines

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

393 formula.

394

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

396 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

397 the B{C{other}} point (C{bool}).

398

399 @return: Distance (C{meter}, same units as the axes of

400 this point's datum ellipsoid).

401

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

403

404 @see: Function L{pygeodesy.cosineForsytheAndoyerLambert} and methods

405 L{cosineAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},

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

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

408 '''

409 return self._distanceTo_(cosineForsytheAndoyerLambert_, other, wrap=wrap)

410

411 def cosineLawTo(self, other, radius=None, wrap=False):

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

413 U{spherical Law of Cosines

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

415 formula.

416

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

418 @kwarg radius: Mean earth radius (C{meter}) or C{None}

419 for the mean radius of this point's datum

420 ellipsoid.

421 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

422 the B{C{other}} point (C{bool}).

423

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

425

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

427

428 @see: Function L{pygeodesy.cosineLaw} and methods L{cosineAndoyerLambertTo},

429 L{cosineForsytheAndoyerLambertTo}, C{distanceTo*}, L{equirectangularTo},

430 L{euclideanTo}, L{flatLocalTo}/L{hubenyTo}, L{flatPolarTo},

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

432 '''

433 return self._distanceTo(cosineLaw, other, radius, wrap=wrap)

434

435 @property_RO

436 def datum(self): # PYCHOK no cover

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

438 notOverloaded(self)

439

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

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

442

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

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

445 @kwarg LatLon: Optional (geodetic) class to return the destination

446 or C{None}.

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

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

449

450 @return: Destination as a C{B{LatLon}(lat, lon, **B{LatLon_kwds})}

451 instance or if C{B{LatLon} is None}, a L{LatLon3Tuple}C{(lat,

452 lon, height)} respectively L{LatLon4Tuple}C{(lat, lon,

453 height, datum)} depending on whether a C{datum} keyword

454 is un-/specified.

455

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

457 '''

458 t = self._ltp._local2ecef(delta, nine=True)

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

460

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

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

463 '''

464 p, r = self.others(other, up=2), radius

465 if r is None:

466 r = self._datum.ellipsoid.R1 if self._datum else R_M

467 return func(self.lat, self.lon, p.lat, p.lon, radius=r, **kwds)

468

469 def _distanceTo_(self, func_, other, wrap=False, radius=None):

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

471 '''

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

473 D = self.datum

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

475 r = func_(phi2, self.phi, lam21, datum=D)

476 return r * (D.ellipsoid.a if radius is None else radius)

477

478 @Property_RO

479 def Ecef(self):

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

481 '''

482 return _MODS.ecef.EcefKarney # default

483

484 @Property_RO

485 def _Ecef_forward(self):

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

487 '''

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

489

490 @Property_RO

491 def _ecef9(self):

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

493 '''

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

495

496 @deprecated_method

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

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

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

500

501 @deprecated_method

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

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

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

505

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

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

508 using the U{Equirectangular Approximation / Projection

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

510

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

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

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

514

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

516 @kwarg radius_adjust_limit_wrap: Optional keyword arguments

517 for function L{pygeodesy.equirectangular},

518 overriding the default mean C{radius} of this

519 point's datum ellipsoid.

520

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

522

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

524

525 @see: Function L{pygeodesy.equirectangular} and methods L{cosineAndoyerLambertTo},

526 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},

527 C{euclideanTo}, L{flatLocalTo}/L{hubenyTo}, L{flatPolarTo},

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

529 '''

530 return self._distanceTo(equirectangular, other, **radius_adjust_limit_wrap)

531

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

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

534 an other point.

535

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

537

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

539 @kwarg radius_adjust_wrap: Optional keyword arguments for function

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

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

542

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

544

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

546

547 @see: Function L{pygeodesy.euclidean} and methods L{cosineAndoyerLambertTo},

548 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},

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

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

551 '''

552 return self._distanceTo(euclidean, other, **radius_adjust_wrap)

553

554 def flatLocalTo(self, other, radius=None, wrap=False):

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

556 U{ellipsoidal Earth to plane projection

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

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

559

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

561 @kwarg radius: Mean earth radius (C{meter}) or C{None} for

562 the I{equatorial radius} of this point's

563 datum ellipsoid.

564 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

565 the B{C{other}} point (C{bool}).

566

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

568

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

570

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

572

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

574 L{cosineAndoyerLambertTo}, L{cosineForsytheAndoyerLambertTo},

575 L{cosineLawTo}, C{distanceTo*}, L{equirectangularTo}, L{euclideanTo},

576 L{flatPolarTo}, L{haversineTo}, L{thomasTo} and L{vincentysTo} and

577 U{local, flat Earth approximation<https://www.edwilliams.org/avform.htm#flat>}.

578 '''

579 return self._distanceTo_(flatLocal_, other, wrap=wrap, radius=

580 radius if radius in (None, R_M, _1_0, 1) else Radius(radius)) # PYCHOK kwargs

581

582 hubenyTo = flatLocalTo # for Karl Hubeny

583

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

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

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

587 Geographical_distance#Polar_coordinate_flat-Earth_formula>} formula.

588

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

590 @kwarg radius_wrap: Optional keyword arguments for function

591 L{pygeodesy.flatPolar}, overriding the

592 default mean C{radius} of this point's

593 datum ellipsoid.

594

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

596

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

598

599 @see: Function L{pygeodesy.flatPolar} and methods L{cosineAndoyerLambertTo},

600 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},

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

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

603 '''

604 return self._distanceTo(flatPolar, other, **radius_wrap)

605

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

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

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

609

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

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

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

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

614 this point's C{datum} ellipsoid.

615

616 @return: The ellipsoid intersection (C{LatLon}) with C{.height} set

617 to the distance to this C{pov}.

618

619 @raise IntersectionError: Null or bad C{pov} or B{C{los}}, this C{pov}

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

621 outside or away from the ellipsoid.

622

623 @raise TypeError: Invalid B{C{los}}.

624

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

626 '''

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

628

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

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

631 U{Haversine<https://www.Movable-Type.co.UK/scripts/latlong.html>}

632 formula.

633

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

635 @kwarg radius_wrap: Optional keyword arguments for function

636 L{pygeodesy.haversine}, overriding the

637 default mean C{radius} of this point's

638 datum ellipsoid.

639

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

641

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

643

644 @see: Function L{pygeodesy.haversine} and methods L{cosineAndoyerLambertTo},

645 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},

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

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

648 '''

649 return self._distanceTo(haversine, other, **radius_wrap)

650

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

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

653

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

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

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

657

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

659 the overriding B{C{height}} (C{Height}).

660 '''

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

662 favg(self.height, other.height, f=f)

663

664 @Property

665 def height(self):

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

667 '''

668 return self._height

669

670 @height.setter # PYCHOK setter!

671 def height(self, height):

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

673

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

675

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

677 '''

678 h = Height(height)

679 if self._height != h:

680 _update_all(self)

681 self._height = h

682

683 def _heigHt(self, height):

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

685 '''

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

687

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

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

690 on this datum's or on an other earth's ellipsoid surface.

691

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

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

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

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

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

697 ellipsoid's surface, otherwise the intersection of the

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

699 @kwarg LatLon: Optional class to return the height and projection

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

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

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

703

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

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

706

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

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

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

710

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

712

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

714

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

716 '''

717 c = self.toCartesian()

718 if LatLon is None:

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

720 else:

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

722 r = r.toLatLon(LatLon=LatLon, **_xkwds(LatLon_kwds, height=r.height))

723 return r

724

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

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

727

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

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

730

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

732 '''

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

734

735 @deprecated_method

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

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

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

739

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

741 '''Check whether this and an other point are antipodal,

742 on diametrically opposite sides of the earth.

743

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

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

746

747 @return: C{True} if points are antipodal within the given

748 tolerance, C{False} otherwise.

749 '''

750 p = self.others(other)

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

752

753 @Property_RO

754 def isEllipsoidal(self):

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

756 '''

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

758

759 @Property_RO

760 def isEllipsoidalLatLon(self):

761 '''Get C{LatLon} base.

762 '''

763 return False

764

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

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

767

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

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

770

771 @return: C{True} if both points are identical,

772 I{ignoring} height, C{False} otherwise.

773

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

775 or mismatch of the B{C{other}} and

776 this C{class} or C{type}.

777

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

779

780 @see: Method L{isequalTo3}.

781 '''

782 return _isequalTo(self, self.others(other), eps=eps)

783

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

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

786

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

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

789

790 @return: C{True} if both points are identical

791 I{including} height, C{False} otherwise.

792

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

794 or mismatch of the B{C{other}} and

795 this C{class} or C{type}.

796

797 @see: Method L{isequalTo}.

798 '''

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

800 _isequalTo(self, other, eps=eps)

801

802 @Property_RO

803 def isnormal(self):

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

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

806

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

808 L{pygeodesy.isnormal} and L{pygeodesy.normal}.

809 '''

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

811

812 @Property_RO

813 def isSpherical(self):

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

815 '''

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

817

818 @Property_RO

819 def lam(self):

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

821 '''

822 return radians(self.lon)

823

824 @Property

825 def lat(self):

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

827 '''

828 return self._lat

829

830 @lat.setter # PYCHOK setter!

831 def lat(self, lat):

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

833

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

835 '''

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

837 if self._lat != lat:

838 _update_all(self)

839 self._lat = lat

840

841 @Property

842 def latlon(self):

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

844 '''

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

846

847 @latlon.setter # PYCHOK setter!

848 def latlon(self, latlonh):

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

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

851 of C{degrees90}, C{degrees180} and C{meter}).

852

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

854 B{C{latlonh}} not C{list} or C{tuple}.

855

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

857

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

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

860 '''

861 if isstr(latlonh):

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

863 else:

864 _xinstanceof(list, tuple, latlonh=latlonh)

865 if len(latlonh) == 3:

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

867 elif len(latlonh) != 2:

868 raise _ValueError(latlonh=latlonh)

869 else:

870 h = self.height

871

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

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

874 _update_all(self)

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

876

877 def latlon2(self, ndigits=0):

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

879

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

881

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

883 and rounded away from zero.

884

885 @note: The C{round}ed values are always C{float}, also

886 if B{C{ndigits}} is omitted.

887 '''

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

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

890

891 @deprecated_method

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

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

894 return self.latlon2(ndigits=ndigits)

895

896 latlon2round = latlon_ # PYCHOK no cover

897

898 @Property

899 def latlonheight(self):

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

901 '''

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

903

904 @latlonheight.setter # PYCHOK setter!

905 def latlonheight(self, latlonh):

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

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

908 of C{degrees90}, C{degrees180} and C{meter}).

909

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

911 '''

912 self.latlon = latlonh

913

914 @Property

915 def lon(self):

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

917 '''

918 return self._lon

919

920 @lon.setter # PYCHOK setter!

921 def lon(self, lon):

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

923

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

925 '''

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

927 if self._lon != lon:

928 _update_all(self)

929 self._lon = lon

930

931 @Property_RO

932 def _ltp(self):

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

934 '''

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

936

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

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

939

940 Points are converted to and distances are computed in

941 I{geocentric}, cartesian space.

942

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

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

945 @kwarg height: Optional height, overriding the height of

946 this and all other points (C{meter}). If

947 C{None}, take the height of points into

948 account for distances.

949 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

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

951

952 @return: A L{NearestOn6Tuple}C{(closest, distance, fi, j,

953 start, end)} with the C{closest}, the C{start}

954 and the C{end} point each an instance of this

955 C{LatLon} and C{distance} in C{meter}, same

956 units as the cartesian axes.

957

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

959

960 @raise TypeError: Some B{C{points}} or some B{C{points}}'

961 C{Ecef} invalid.

962

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

964

965 @see: Function L{pygeodesy.nearestOn6}.

966 '''

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

968 p = None # not used

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

970 if w and i:

971 q = _unrollon(p, q)

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

973 p = q

974

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

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

977

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

979 t = nearestOn6(c, _cs(Ps, height, wrap, C), closed=closed)

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

981

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

983 height=height)

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

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

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

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

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

989

990 def normal(self):

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

992 C{abs(lon) <= 180}.

993

994 @return: C{True} if this point was I{normal}, C{False} if it

995 wasn't (but is now).

996

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

998 '''

999 n = self.isnormal

1000 if not n:

1001 self.latlon = normal(*self.latlon)

1002 return n

1003

1004 @Property_RO

1005 def _N_vector(self):

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

1007 '''

1008 return _MODS.nvectorBase._N_vector_(*self.xyzh)

1009

1010 @Property_RO

1011 def phi(self):

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

1013 '''

1014 return radians(self.lat)

1015

1016 @Property_RO

1017 def philam(self):

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

1019 '''

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

1021

1022 def philam2(self, ndigits=0):

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

1024

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

1026

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

1028 and rounded away from zero.

1029

1030 @note: The C{round}ed values are always C{float}, also

1031 if B{C{ndigits}} is omitted.

1032 '''

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

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

1035

1036 @Property_RO

1037 def philamheight(self):

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

1039 '''

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

1041

1042 @deprecated_method

1043 def points(self, points, closed=True): # PYCHOK no cover

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

1045 return self.points2(points, closed=closed)

1046

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

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

1049

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

1051 @kwarg closed: Optionally, consider the polygon closed,

1052 ignoring any duplicate or closing final

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

1054

1055 @return: A L{Points2Tuple}C{(number, points)}, an C{int}

1056 and C{list} or C{tuple}.

1057

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

1059

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

1061 '''

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

1063

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

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

1066

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

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

1069 @kwarg dedup: Skip duplicate points (C{bool}).

1070 @kwarg wrap: If C{True}, wrap or I{normalize} the

1071 enum-/iterated B{C{points}} (C{bool}).

1072

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

1074

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

1076 '''

1077 return PointsIter(points, base=self, loop=loop, dedup=dedup, wrap=wrap)

1078

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

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

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

1082

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

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

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

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

1087

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

1089

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

1091

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

1093

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

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

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

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

1098 '''

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

1100 return _radii11ABC(*cs, useZ=True)[0]

1101

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

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

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

1105 ''' # in .ellipsoidalBase.LatLonEllipsoidalBase.intersecant2

1106 try:

1107 d = self._rhumb3dict

1108 t = d[(exact, radius)]

1109 except KeyError:

1110 D = self.datum if radius is None else _spherical_datum(radius) # ellipsoidal OK

1111 try:

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

1113 except AttributeError as x:

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

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

1116 while d:

1117 d.popitem()

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

1119 return t

1120

1121 @Property_RO

1122 def _rhumb3dict(self):

1123 return {} # single-item cache

1124

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

1126 '''Return the azimuth (bearing) of a rhumb line (loxodrome)

1127 between this and an other (ellipsoidal) point.

1128

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

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

1131 see method L{Ellipsoid.rhumb_}.

1132 @kwarg radius: Optional earth radius (C{meter}) or earth model

1133 (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} or

1134 L{a_f2Tuple}), overriding this point's datum.

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

1136 B{C{other}} point (C{bool}).

1137

1138 @return: Rhumb azimuth (compass C{degrees360}).

1139

1140 @raise TypeError: The B{C{other}} point is incompatible or

1141 B{C{radius}} is invalid.

1142 '''

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

1144 return r._Inverse(self, other, wrap, outmask=Cs.AZIMUTH).azi12

1145

1146 def rhumbDestination(self, distance, azimuth, exact=False, radius=None, height=None):

1147 '''Return the destination point having travelled the given distance

1148 from this point along a rhumb line (loxodrome) at the given azimuth.

1149

1150 @arg distance: Distance travelled (C{meter}, same units as this

1151 point's datum (ellipsoid) axes or B{C{radius}},

1152 may be negative.

1153 @arg azimuth: Azimuth (bearing) at this point (compass C{degrees}).

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

1155 see method L{Ellipsoid.rhumb_}.

1156 @kwarg radius: Optional earth radius (C{meter}) or earth model

1157 (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} or

1158 L{a_f2Tuple}), overriding this point's datum.

1159 @kwarg height: Optional height, overriding the default height

1160 (C{meter}).

1161

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

1163

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

1165

1166 @raise ValueError: Invalid B{C{distance}}, B{C{azimuth}},

1167 B{C{radius}} or B{C{height}}.

1168 '''

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

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

1171 h = self._heigHt(height)

1172 return self.classof(d.lat2, d.lon2, datum=D, height=h)

1173

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

1175 '''Return the distance from this to an other point along

1176 a rhumb line (loxodrome).

1177

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

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

1180 see method L{Ellipsoid.rhumb_}.

1181 @kwarg radius: Optional earth radius (C{meter}) or earth model

1182 (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} or

1183 L{a_f2Tuple}), overriding this point's datum.

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

1185 B{C{other}} point (C{bool}).

1186

1187 @return: Distance (C{meter}, the same units as this point's

1188 datum (ellipsoid) axes or B{C{radius}}.

1189

1190 @raise TypeError: The B{C{other}} point is incompatible or

1191 B{C{radius}} is invalid.

1192

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

1194 '''

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

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

1197

1198 def rhumbLine(self, azimuth_other, exact=False, radius=None, wrap=False,

1199 **name_caps):

1200 '''Get a rhumb line through this point at a given azimuth or

1201 through this and an other point.

1202

1203 @arg azimuth_other: The azimuth of the rhumb line (compass

1204 C{degrees}) or the other point (C{LatLon}).

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

1206 see method L{Ellipsoid.rhumb_}.

1207 @kwarg radius: Optional earth radius (C{meter}) or earth model

1208 (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} or

1209 L{a_f2Tuple}), overriding this point's datum.

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

1211 C{azimuth_B{other}} point (C{bool}).

1212 @kwarg name_caps: Optional C{B{name}=str} and C{caps}, see

1213 L{RhumbLine} C{B{caps}}.

1214

1215 @return: A C{RhumbLine} instance.

1216

1217 @raise TypeError: Invalid B{C{radius}} or BC{C{azimuth_other}}

1218 not a C{scalar} nor a C{LatLon}.

1219

1220 @see: Modules L{rhumbaux} and L{rhumbx}.

1221 '''

1222 r, _, _ = self._rhumb3(exact, radius)

1223 a, kwds = azimuth_other, _xkwds(name_caps, name=self.name)

1224 if isscalar(a):

1225 r = r._DirectLine(self, a, **kwds)

1226 elif isinstance(a, LatLonBase): # isLatLon(a)

1227 r = r._InverseLine(self, a, wrap, **kwds)

1228 else:

1229 raise _TypeError(azimuth_other=a)

1230 return r

1231

1232 def rhumbMidpointTo(self, other, exact=False, radius=None,

1233 height=None, fraction=_0_5, wrap=False):

1234 '''Return the (loxodromic) midpoint on the rhumb line between

1235 this and an other point.

1236

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

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

1239 see method L{Ellipsoid.rhumb_}.

1240 @kwarg radius: Optional earth radius (C{meter}) or earth model

1241 (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} or

1242 L{a_f2Tuple}), overriding this point's datum.

1243 @kwarg height: Optional height, overriding the mean height

1244 (C{meter}).

1245 @kwarg fraction: Midpoint location from this point (C{scalar}), 0

1246 for this, 1 for the B{C{other}}, 0.5 for halfway

1247 between this and the B{C{other}} point, may be

1248 negative or greater than 1.

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

1250 B{C{other}} point (C{bool}).

1251

1252 @return: The midpoint at the given B{C{fraction}} along the

1253 rhumb line (C{LatLon}).

1254

1255 @raise TypeError: The B{C{other}} point is incompatible or

1256 B{C{radius}} is invalid.

1257

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

1259 '''

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

1261 f = Scalar(fraction=fraction)

1262 d = r._Inverse(self, other, wrap) # C.AZIMUTH_DISTANCE

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

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

1265 return self.classof(d.lat2, d.lon2, datum=D, height=h)

1266

1267 def thomasTo(self, other, wrap=False):

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

1269 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>}

1270 formula.

1271

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

1273 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

1274 the B{C{other}} point (C{bool}).

1275

1276 @return: Distance (C{meter}, same units as the axes of

1277 this point's datum ellipsoid).

1278

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

1280

1281 @see: Function L{pygeodesy.thomas} and methods L{cosineAndoyerLambertTo},

1282 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},

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

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

1285 '''

1286 return self._distanceTo_(thomas_, other, wrap=wrap)

1287

1288 @deprecated_method

1289 def to2ab(self): # PYCHOK no cover

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

1291 return self.philam

1292

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

1294 '''Convert this point to cartesian, I{geocentric} coordinates,

1295 also known as I{Earth-Centered, Earth-Fixed} (ECEF).

1296

1297 @kwarg height: Optional height, overriding this point's height

1298 (C{meter}, conventionally).

1299 @kwarg Cartesian: Optional class to return the geocentric

1300 coordinates (C{Cartesian}) or C{None}.

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

1302 keyword arguments, ignored if

1303 C{B{Cartesian} is None}.

1304

1305 @return: A B{C{Cartesian}} or if B{C{Cartesian}} is C{None},

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

1307 datum)} with C{C=0} and C{M} if available.

1308

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

1310 '''

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

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

1313 r = self._xnamed(Cartesian(r, **Cartesian_kwds))

1314 _xdatum(r.datum, self.datum)

1315 return r

1316

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

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

1319 '''

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

1321 c = p.toCartesian(height=height)

1322 E = self.Ecef

1323 if E:

1324 for p in (p, c):

1325 e = getattr(p, LatLonBase.Ecef.name, None)

1326 if e not in (None, E): # PYCHOK no cover

1327 n, _ = name_point.popitem()

1328 if i is not None:

1329 Fmt.SQUARE(n, i)

1330 raise _ValueError(n, e, txt=_incompatible(E.__name__))

1331 return c

1332

1333 def toDatum(self, datum2, height=None, name=NN):

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

1335 notOverloaded(self, datum2, height=height, name=name)

1336

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

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

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

1340

1341 @kwarg height: Optional height, overriding this point's height

1342 (C{meter}, conventionally).

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

1344

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

1346 with C{C=0} and C{M} if available.

1347

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

1349 '''

1350 return self._ecef9 if height in (None, self.height) else \

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

1352

1353 @deprecated_method

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

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

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

1357 self.latlon.to3Tuple(height)

1358

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

1360 '''Convert this I{geodetic} point to I{local} C{X}, C{Y} and C{Z}.

1361

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

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

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

1365 overriding this point's LTP (L{Ltp}).

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

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

1368

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

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

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

1372

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

1374 '''

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

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

1377

1378 def toLtp(self, Ecef=None):

1379 '''Return the I{local tangent plane} (LTP) for this point.

1380

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

1382 L{EcefYou}), overriding this point's C{Ecef}.

1383 '''

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

1385 self, ecef=Ecef(self.datum), name=self.name)

1386

1387 def toNormal(self, deep=False, name=NN):

1388 '''Get this point I{normalized} to C{abs(lat) <= 90}

1389 and C{abs(lon) <= 180}.

1390

1391 @kwarg deep: If C{True} make a deep, otherwise a

1392 shallow copy (C{bool}).

1393 @kwarg name: Optional name of the copy (C{str}).

1394

1395 @return: A copy of this point, I{normalized} and

1396 optionally renamed (C{LatLon}).

1397

1398 @see: Property L{isnormal}, method L{normal} and function

1399 L{pygeodesy.normal}.

1400 '''

1401 ll = self.copy(deep=deep)

1402 _ = ll.normal()

1403 if name:

1404 ll.rename(name)

1405 return ll

1406

1407 def toNvector(self, h=None, Nvector=None, **Nvector_kwds):

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

1409 components, I{including height}.

1410

1411 @kwarg h: Optional height, overriding this point's

1412 height (C{meter}).

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

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

1415 @kwarg Nvector_kwds_wrap: Optional, additional B{C{Nvector}}

1416 keyword arguments, ignored if C{B{Nvector}

1417 is None}.

1418

1419 @return: A B{C{Nvector}} or a L{Vector4Tuple}C{(x, y, z, h)}

1420 if B{C{Nvector}} is C{None}.

1421

1422 @raise TypeError: Invalid B{C{Nvector}} or B{C{Nvector_kwds}}.

1423 '''

1424 return self.toVector(Vector=Nvector, h=self.height if h is None else h,

1425 ll=self, **Nvector_kwds)

1426

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

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

1429 in the given C{B{form}at}.

1430

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

1432 functions L{pygeodesy.latDMS} or L{pygeodesy.lonDMS}.

1433 @kwarg joined: Separator to join the lat-, longitude and heigth

1434 strings (C{str} or C{None} or C{NN} for non-joined).

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

1436 exclude height from the returned string.

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

1438 B{C{s_D}}, B{C{s_M}}, B{C{s_S}} and B{C{s_DMS}} keyword

1439 arguments, see function L{pygeodesy.latDMS} or

1440 L{pygeodesy.lonDMS}.

1441

1442 @return: This point in the specified C{B{form}at}, etc. (C{str} or

1443 a 2- or 3-tuple C{(lat_str, lon_str[, height_str])} if

1444 C{B{joined}=NN} or C{B{joined}=None}).

1445

1446 @see: Function L{pygeodesy.latDMS} or L{pygeodesy.lonDMS} for more

1447 details about keyword arguments C{B{form}at}, C{B{prec}ision},

1448 C{B{sep}arator}, B{C{s_D}}, B{C{s_M}}, B{C{s_S}} and B{C{s_DMS}}.

1449

1450 @example:

1451

1452 >>> LatLon(51.4778, -0.0016).toStr() # 51°28′40″N, 000°00′06″W

1453 >>> LatLon(51.4778, -0.0016).toStr(F_D) # 51.4778°N, 000.0016°W

1454 >>> LatLon(51.4778, -0.0016, 42).toStr() # 51°28′40″N, 000°00′06″W, +42.00m

1455 '''

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

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

1458 if self.height and m is not None:

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

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

1461

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

1463 '''Convert this point to C{n-vector} (normal to the earth's

1464 surface) components, I{ignoring height}.

1465

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

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

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

1469 keyword arguments, ignored if

1470 C{B{Vector} is None}.

1471

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

1473 if B{C{Vector}} is C{None}.

1474

1475 @raise TypeError: Invalid B{C{Vector}} or B{C{kwds}}.

1476

1477 @note: These are C{n-vector} x, y and z components,

1478 I{NOT} geocentric (ECEF) x, y and z coordinates!

1479 '''

1480 r = self._vector3tuple

1481 if Vector is not None:

1482 r = Vector(*r, **_xkwds(Vector_kwds, name=self.name))

1483 return r

1484

1485 def toVector3d(self):

1486 '''Convert this point to C{n-vector} (normal to the earth's

1487 surface) components, I{ignoring height}.

1488

1489 @return: Unit vector (L{Vector3d}).

1490

1491 @note: These are C{n-vector} x, y and z components,

1492 I{NOT} geocentric (ECEF) x, y and z coordinates!

1493 '''

1494 return self._vector3d # XXX .unit()

1495

1496 def toWm(self, **toWm_kwds):

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

1498

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

1500

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

1502

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

1504 '''

1505 return self._wm if not toWm_kwds else _MODS.webmercator.toWm(

1506 self, **_xkwds(toWm_kwds, name=self.name))

1507

1508 @deprecated_method

1509 def to3xyz(self): # PYCHOK no cover

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

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

1512 return self.xyz # self.toVector()

1513

1514 @Property_RO

1515 def _vector3d(self):

1516 '''(INTERNAL) Cache for L{toVector3d}.

1517 '''

1518 return self.toVector(Vector=Vector3d) # XXX .unit()

1519

1520 @Property_RO

1521 def _vector3tuple(self):

1522 '''(INTERNAL) Cache for L{toVector}.

1523 '''

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

1525

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

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

1528 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}

1529 spherical formula.

1530

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

1532 @kwarg radius_wrap: Optional keyword arguments for function

1533 L{pygeodesy.vincentys}, overriding the

1534 default mean C{radius} of this point's

1535 datum ellipsoid.

1536

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

1538

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

1540

1541 @see: Function L{pygeodesy.vincentys} and methods L{cosineAndoyerLambertTo},

1542 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},

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

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

1545 '''

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

1547

1548 @Property_RO

1549 def _wm(self):

1550 '''(INTERNAL) Get this point as webmercator (L{Wm}).

1551 '''

1552 return _MODS.webmercator.toWm(self)

1553

1554 @Property_RO

1555 def xyz(self):

1556 '''Get the C{n-vector} X, Y and Z components (L{Vector3Tuple}C{(x, y, z)})

1557

1558 @note: These are C{n-vector} x, y and z components, I{NOT}

1559 geocentric (ECEF) x, y and z coordinates!

1560 '''

1561 return self.toVector(Vector=Vector3Tuple)

1562

1563 @Property_RO

1564 def xyzh(self):

1565 '''Get the C{n-vector} X, Y, Z and H components (L{Vector4Tuple}C{(x, y, z, h)})

1566

1567 @note: These are C{n-vector} x, y and z components, I{NOT}

1568 geocentric (ECEF) x, y and z coordinates!

1569 '''

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

1571

1572

1573 class _toCartesian3(object): # see also .geodesicw._wargs, .vector2d._numpy

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

1575 '''

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

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

1578 try:

1579 if wrap:

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

1581 kwds = _xkwds(kwds, wrap=wrap)

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

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

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

1585 except (AssertionError, TypeError, ValueError) as x:

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

1587

1588_toCartesian3 = _toCartesian3() # PYCHOK singleton

1589

1590

1591 def _trilaterate5(p1, d1, p2, d2, p3, d3, area=True, eps=EPS1, # MCCABE 13

1592 radius=R_M, wrap=False):

1593 '''(INTERNAL) Trilaterate three points by I{area overlap} or by

1594 I{perimeter intersection} of three circles.

1595

1596 @note: The B{C{radius}} is only needed for both the n-vectorial

1597 and C{sphericalTrigonometry.LatLon.distanceTo} methods and

1598 silently ignored by the C{ellipsoidalExact}, C{-GeodSolve},

1599 C{-Karney} and C{-Vincenty.LatLon.distanceTo} methods.

1600 '''

1601 p2, p3, w = _unrollon3(p1, p2, p3, wrap)

1602

1603 r1 = Distance_(distance1=d1)

1604 r2 = Distance_(distance2=d2)

1605 r3 = Distance_(distance3=d3)

1606 m = 0 if area else (r1 + r2 + r3)

1607 pc = 0

1608 t = []

1609 for _ in range(3):

1610 try: # intersection of circle (p1, r1) and (p2, r2)

1611 c1, c2 = p1.intersections2(r1, p2, r2, wrap=w)

1612

1613 if area: # check overlap

1614 if c1 is c2: # abutting

1615 c = c1

1616 else: # nearest point on radical

1617 c = p3.nearestOn(c1, c2, within=True, wrap=w)

1618 d = r3 - p3.distanceTo(c, radius=radius, wrap=w)

1619 if d > eps: # sufficient overlap

1620 t.append((d, c))

1621 m = max(m, d)

1622

1623 else: # check intersection

1624 for c in ((c1,) if c1 is c2 else (c1, c2)):

1625 d = fabs(r3 - p3.distanceTo(c, radius=radius, wrap=w))

1626 if d < eps: # below margin

1627 t.append((d, c))

1628 m = min(m, d)

1629

1630 except IntersectionError as x:

1631 if _concentric_ in str(x): # XXX ConcentricError?

1632 pc += 1

1633

1634 p1, r1, p2, r2, p3, r3 = p2, r2, p3, r3, p1, r1 # rotate

1635

1636 if t: # get min, max, points and count ...

1637 t = tuple(sorted(t))

1638 n = len(t), # as 1-tuple

1639 # ... or for a single trilaterated result,

1640 # min *is* max, min- *is* maxPoint and n=1, 2 or 3

1641 return Trilaterate5Tuple(t[0] + t[-1] + n) # *(t[0] + ...)

1642

1643 elif area and pc == 3: # all pairwise concentric ...

1644 r, p = min((r1, p1), (r2, p2), (r3, p3))

1645 m = max(r1, r2, r3)

1646 # ... return "smallest" point twice, the smallest

1647 # and largest distance and n=0 for concentric

1648 return Trilaterate5Tuple(float(r), p, float(m), p, 0)

1649

1650 n, f = (_overlap_, max) if area else (_intersection_, min)

1651 t = _COMMASPACE_(_no_(n), '%s %.3g' % (f.__name__, m))

1652 raise IntersectionError(area=area, eps=eps, wrap=wrap, txt=t)

1653

1654

1655 def _intersecend2(p, d, a, b, r, radius, height, exact): # in .ellipsoidalBas, .sphericalBase

1656 '''(INTERNAL) Compute the intersecant2 end-result.

1657 '''

1658 if d > EPS:

1659 s, c = sincos2d(b - a)

1660 s = sqrt_a(r, fabs(s * d))

1661 if s > r:

1662 raise IntersectionError(_too_(Fmt.distant(s)))

1663 elif (r - s) < EPS:

1664 return p, p # tangent

1665 c *= d

1666 else: # p and c coincide

1667 s, c = r, 0

1668 t = ()

1669 for d, b in ((s + c, b), (s - c, b + _180_0)): # bearing direction first

1670 t += (p.destination(d, b, radius=radius, height=height) if exact is None else

1671 p.rhumbDestination(d, b, radius=radius, height=height, exact=exact)),

1672 return t

1673

1674

1675__all__ += _ALL_DOCS(LatLonBase)

1676

1677# **) MIT License

1678#

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

1680#

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

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

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

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

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

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

1687#

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

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

1690#

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

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

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

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

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

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

1697# OTHER DEALINGS IN THE SOFTWARE.