Coverage for pygeodesy/rhumb/bases.py: 94%

1

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

3

4 u'''(INTERNAL) base classes C{RhumbBase} and C{RhumbLineBase}, pure Python version of I{Karney}'s

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

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

7 from I{GeographicLib versions 2.0} and I{2.2} and I{Karney}'s C++ example U{Rhumb intersect

8<https://SourceForge.net/p/geographiclib/discussion/1026620/thread/2ddc295e/>}.

9

10 Class L{RhumbLineBase} has been enhanced with methods C{Intersecant2}, C{Intersection} and C{PlumbTo}

11 to iteratively find the intersection of a rhumb line and a circle or an other rhumb line, respectively

12 a perpendicular geodesic or other rhumb line.

13

14 For more details, see the C++ U{GeographicLib<https://GeographicLib.SourceForge.io/C++/doc/index.html>}

15 documentation, especially the U{Class List<https://GeographicLib.SourceForge.io/C++/doc/annotated.html>},

16 the background information on U{Rhumb lines<https://GeographicLib.SourceForge.io/C++/doc/rhumb.html>},

17 the utily U{RhumbSolve<https://GeographicLib.SourceForge.io/C++/doc/RhumbSolve.1.html>} and U{Online

18 rhumb line calculations<https://GeographicLib.SourceForge.io/cgi-bin/RhumbSolve>}.

19

20 Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2014-2024) and licensed under the MIT/X11

21 License. For more information, see the U{GeographicLib<https://GeographicLib.SourceForge.io>} documentation.

22'''

23# make sure int/int division yields float quotient

24 from __future__ import division as _; del _ # noqa: E702 ;

25

26 from pygeodesy.basics import _copysign, itemsorted, unsigned0, _xinstanceof

27 from pygeodesy.constants import EPS, EPS0, EPS1, INT0, NAN, _over, \

28 _EPSqrt as _TOL, _0_0, _0_01, _1_0, _90_0

29 from pygeodesy.datums import Datum, _earth_datum, _spherical_datum, _WGS84

30 from pygeodesy.errors import IntersectionError, RhumbError, _xdatum, \

31 _xkwds, _xkwds_pop2, _Xorder

32# from pygeodesy.etm import ExactTransverseMercator # _MODS

33 from pygeodesy.fmath import euclid, favg, sqrt_a, Fsum

34# from pygeodesy.formy import opposing # _MODS

35# from pygeodesy.fsums import Fsum # from .fmath

36 from pygeodesy.internals import typename, _under

37 from pygeodesy.interns import NN, _coincident_, _COMMASPACE_, _Dash, \

38 _DMAIN_, _parallel_, _too_

39 from pygeodesy.karney import _atan2d, Caps, _CapsBase, _diff182, _fix90, \

40 _norm180, GDict

41# from pygeodesy.ktm import KTransverseMercator, _AlpCoeffs # _MODS

42 from pygeodesy.lazily import _ALL_DOCS, _ALL_MODS as _MODS

43 from pygeodesy.namedTuples import Distance2Tuple, LatLon2Tuple

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

45 property_RO, _update_all

46 from pygeodesy.streprs import Fmt, pairs

47 from pygeodesy.units import Float_, Lat, Lon, Meter, Radius_

48 from pygeodesy.utily import acos1, _azireversed, _loneg, sincos2d, sincos2d_, \

49 _unrollon, _Wrap

50 from pygeodesy.vector3d import _intersect3d3, Vector3d # in .Intersection below

51

52 from math import cos, fabs

53

54 __all__ = ()

55 __version__ = '25.05.12'

56

57 _anti_ = _Dash('anti')

58 _rls = [] # instances of C{RbumbLine...} to be updated

59 _TRIPS = 129 # .Intersection, .PlumbTo, 19+

60

61

62 class _Lat(Lat):

63 '''(INTERNAL) Latitude B{C{lat}}.

64 '''

65 def __init__(self, *lat, **Error_name):

66 kwds = _xkwds(Error_name, clip=0, Error=RhumbError)

67 Lat.__new__(_Lat, *lat, **kwds)

68

69

70 class _Lon(Lon):

71 '''(INTERNAL) Longitude B{C{lon}}.

72 '''

73 def __init__(self, *lon, **Error_name):

74 kwds = _xkwds(Error_name, clip=0, Error=RhumbError)

75 Lon.__new__(_Lon, *lon, **kwds)

76

77

78 def _update_all_rls(r):

79 '''(INTERNAL) Zap cached/memoized C{Property[_RO]}s

80 of any C{RhumbLine} instances tied to the given

81 C{Rhumb} instance B{C{r}}.

82 '''

83 # _xinstanceof(_MODS.rhumb.aux_.RhumbAux, _MODS.rhumb.ekx.Rhumb, r=r)

84 _update_all(r)

85 for rl in _rls: # PYCHOK use weakref?

86 if rl._rhumb is r:

87 _update_all(rl)

88

89

90 class RhumbBase(_CapsBase):

91 '''(INTERNAL) Base class for C{rhumb.aux_.RhumbAux} and C{rhumb.ekx.Rhumb}.

92 '''

93 _datum = _WGS84

94 _exact = True

95 _f_max = _0_01

96 _mTM = 6 # see .TMorder

97

98 def __init__(self, a_earth, f, exact, TMorder_name):

99 '''New C{RhumbAux} or C{Rhumb}.

100 '''

101 if TMorder_name:

102 M = self._mTM

103 m, name = _xkwds_pop2(TMorder_name, TMorder=M)

104 if m != M:

105 self.TMorder = m

106 else:

107 name = {}

108 _earth_datum(self, a_earth, f=f, **name)

109 if not exact:

110 self.exact = False

111 if name:

112 self.name = name

113

114 @Property_RO

115 def a(self):

116 '''Get the C{ellipsoid}'s equatorial radius, semi-axis (C{meter}).

117 '''

118 return self.ellipsoid.a

119

120 equatoradius = a

121

122 def ArcDirect(self, lat1, lon1, azi12, a12, outmask=Caps.LATITUDE_LONGITUDE):

123 '''Solve the I{direct rhumb} problem, optionally with area.

124

125 @arg lat1: Latitude of the first point (C{degrees90}).

126 @arg lon1: Longitude of the first point (C{degrees180}).

127 @arg azi12: Azimuth of the rhumb line (compass C{degrees}).

128 @arg a12: Angle along the rhumb line from the given to the

129 destination point (C{degrees}), can be negative.

130

131 @return: L{GDict} with 2 up to 8 items C{lat2, lon2, a12, S12,

132 lat1, lon1, azi12, s12} with the destination point's

133 latitude C{lat2} and longitude C{lon2} in C{degrees},

134 the rhumb angle C{a12} in C{degrees} and area C{S12}

135 under the rhumb line in C{meter} I{squared}.

136

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

138 only required for area C{S12} when C{B{exact}

139 is True} and L{RhumbAux}.

140

141 @note: If B{C{a12}} is large enough that the rhumb line crosses

142 a pole, the longitude of the second point is indeterminate

143 and C{NAN} is returned for C{lon2} and area C{S12}.

144

145 @note: If the given point is a pole, the cosine of its latitude is

146 taken to be C{sqrt(L{EPS})}. This position is extremely

147 close to the actual pole and allows the calculation to be

148 carried out in finite terms.

149 '''

150 s12 = a12 * self._mpd

151 return self._DirectRhumb(lat1, lon1, azi12, a12, s12, outmask)

152

153 @Property_RO

154 def b(self):

155 '''Get the C{ellipsoid}'s polar radius, semi-axis (C{meter}).

156 '''

157 return self.ellipsoid.b

158

159 polaradius = b

160

161 @property

162 def datum(self):

163 '''Get this rhumb's datum (L{Datum}).

164 '''

165 return self._datum

166

167 @datum.setter # PYCHOK setter!

168 def datum(self, datum):

169 '''Set this rhumb's datum (L{Datum}).

170

171 @raise RhumbError: If C{abs(B{f}} exceeds non-zero C{f_max} and C{exact=False}.

172 '''

173 _xinstanceof(Datum, datum=datum)

174 if self._datum != datum:

175 self._exactest(self.exact, datum.ellipsoid, self.f_max)

176 _update_all_rls(self)

177 self._datum = datum

178

179 def _Direct(self, ll1, azi12, s12, **outmask):

180 '''(INTERNAL) Short-cut version, see .latlonBase.rhumb....

181 '''

182 return self.Direct(ll1.lat, ll1.lon, azi12, s12, **outmask)

183

184 def Direct(self, lat1, lon1, azi12, s12, outmask=Caps.LATITUDE_LONGITUDE):

185 '''Solve the I{direct rhumb} problem, optionally with area.

186

187 @arg lat1: Latitude of the first point (C{degrees90}).

188 @arg lon1: Longitude of the first point (C{degrees180}).

189 @arg azi12: Azimuth of the rhumb line (compass C{degrees}).

190 @arg s12: Distance along the rhumb line from the given to

191 the destination point (C{meter}), can be negative.

192

193 @return: L{GDict} with 2 up to 8 items C{lat2, lon2, a12, S12,

194 lat1, lon1, azi12, s12} with the destination point's

195 latitude C{lat2} and longitude C{lon2} in C{degrees},

196 the rhumb angle C{a12} in C{degrees} and area C{S12}

197 under the rhumb line in C{meter} I{squared}.

198

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

200 only required for area C{S12} when C{B{exact}

201 is True} and L{RhumbAux}.

202

203 @note: If B{C{s12}} is large enough that the rhumb line crosses

204 a pole, the longitude of the second point is indeterminate

205 and C{NAN} is returned for C{lon2} and area C{S12}.

206

207 @note: If the given point is a pole, the cosine of its latitude is

208 taken to be C{sqrt(L{EPS})}. This position is extremely

209 close to the actual pole and allows the calculation to be

210 carried out in finite terms.

211 '''

212 a12 = _over(s12, self._mpd)

213 return self._DirectRhumb(lat1, lon1, azi12, a12, s12, outmask)

214

215 def Direct8(self, lat1, lon1, azi12, s12, outmask=Caps.LATITUDE_LONGITUDE_AREA):

216 '''Like method L{Rhumb.Direct} but returning a L{Rhumb8Tuple} with area C{S12}.

217 '''

218 return self.Direct(lat1, lon1, azi12, s12, outmask=outmask).toRhumb8Tuple()

219

220 def _DirectLine(self, ll1, azi12, **caps_name):

221 '''(INTERNAL) Short-cut version, see .latlonBase.

222 '''

223 return self.DirectLine(ll1.lat, ll1.lon, azi12, **caps_name)

224

225 def DirectLine(self, lat1, lon1, azi12, **caps_name):

226 '''Define a C{RhumbLine} in terms of the I{direct} rhumb

227 problem to compute several points on a single rhumb line.

228

229 @arg lat1: Latitude of the first point (C{degrees90}).

230 @arg lon1: Longitude of the first point (C{degrees180}).

231 @arg azi12: Azimuth of the rhumb line (compass C{degrees}).

232 @kwarg caps_name: Optional keyword arguments C{B{name}=NN} and

233 C{B{caps}=Caps.STANDARD}, a bit-or'ed combination of

234 L{Caps<pygeodesy.karney.Caps>} values specifying the

235 required capabilities. Include C{Caps.LINE_OFF} if

236 updates to the B{C{rhumb}} should I{not be reflected}

237 in this rhumb line.

238

239 @return: A C{RhumbLine...} instance and invoke its method

240 C{.Position} to compute each point.

241

242 @note: Updates to this rhumb are reflected in the returned

243 rhumb line, unless C{B{caps} |= Caps.LINE_OFF}.

244 '''

245 return self._RhumbLine(self, lat1, lon1, azi12, **caps_name)

246

247 Line = DirectLine # synonyms

248

249 def _DirectRhumb(self, lat1, lon1, azi12, a12, s12, outmask):

250 '''(INTERNAL) See methods C{.ArcDirect} and C{.Direct}.

251 '''

252 rl = self._RhumbLine(self, lat1, lon1, azi12, caps=Caps.LINE_OFF,

253 name=self.name)

254 return rl._Position(a12, s12, outmask | self._debug) # lat2, lon2, S12

255

256 @Property

257 def ellipsoid(self):

258 '''Get this rhumb's ellipsoid (L{Ellipsoid}).

259 '''

260 return self.datum.ellipsoid

261

262 @ellipsoid.setter # PYCHOK setter!

263 def ellipsoid(self, a_earth_f):

264 '''Set this rhumb's ellipsoid (L{Ellipsoid}, L{Ellipsoid2}, L{Datum} or

265 L{a_f2Tuple}) or (equatorial) radius and flattening (2-tuple C{(a, f)}).

266

267 @raise RhumbError: If C{abs(B{f}} exceeds non-zero C{f_max} and C{exact=False}.

268 '''

269 self.datum = _spherical_datum(a_earth_f, Error=RhumbError)

270

271 @Property

272 def exact(self):

273 '''Get the I{exact} option (C{bool}).

274 '''

275 return self._exact

276

277 @exact.setter # PYCHOK setter!

278 def exact(self, exact):

279 '''Set the I{exact} option (C{bool}). If C{True}, use I{exact} rhumb

280 expressions, otherwise a series expansion (accurate for oblate or

281 prolate ellipsoids with C{abs(flattening)} below C{f_max}.

282

283 @raise RhumbError: If C{B{exact}=False} and C{abs(flattening})

284 exceeds non-zero C{f_max}.

285

286 @see: Option U{B{-s}<https://GeographicLib.SourceForge.io/C++/doc/RhumbSolve.1.html>}

287 and U{ACCURACY<https://GeographicLib.SourceForge.io/C++/doc/RhumbSolve.1.html#ACCURACY>}.

288 '''

289 x = bool(exact)

290 if self._exact != x:

291 self._exactest(x, self.ellipsoid, self.f_max)

292 _update_all_rls(self)

293 self._exact = x

294

295 def _exactest(self, exact, ellipsoid, f_max):

296 # Helper for property setters C{ellipsoid}, C{exact} and C{f_max}

297 if fabs(ellipsoid.f) > f_max > 0 and not exact:

298 raise RhumbError(exact=exact, f=ellipsoid.f, f_max=f_max)

299

300 @Property_RO

301 def f(self):

302 '''Get the C{ellipsoid}'s flattening (C{float}).

303 '''

304 return self.ellipsoid.f

305

306 flattening = f

307

308 @property

309 def f_max(self):

310 '''Get the I{max.} flattening (C{float}).

311 '''

312 return self._f_max

313

314 @f_max.setter # PYCHOK setter!

315 def f_max(self, f_max): # PYCHOK no cover

316 '''Set the I{max.} flattening, not to exceed (C{float}).

317

318 @raise RhumbError: If C{exact=False} and C{abs(flattening})

319 exceeds non-zero C{f_max}.

320 '''

321 f = Float_(f_max=f_max, low=_0_0, high=EPS1)

322 if self._f_max != f:

323 self._exactest(self.exact, self.ellipsoid, f)

324 self._f_max = f

325

326 def _Inverse(self, ll1, ll2, wrap, **outmask):

327 '''(INTERNAL) Short-cut version, see .latlonBase.rhumb....

328 '''

329 if wrap:

330 ll2 = _unrollon(ll1, _Wrap.point(ll2))

331 return self.Inverse(ll1.lat, ll1.lon, ll2.lat, ll2.lon, **outmask)

332

333 def Inverse(self, lat1, lon1, lat2, lon2, outmask=Caps.AZIMUTH_DISTANCE):

334 '''Solve the I{inverse rhumb} problem.

335

336 @arg lat1: Latitude of the first point (C{degrees90}).

337 @arg lon1: Longitude of the first point (C{degrees180}).

338 @arg lat2: Latitude of the second point (C{degrees90}).

339 @arg lon2: Longitude of the second point (C{degrees180}).

340

341 @return: L{GDict} with 4 to 9 items C{lat1, lon1, lat2, lon2,

342 azi12, azi21, s12, a12, S12}, the rhumb line's azimuth

343 C{azi12} and I{reverse} azimuth C{azi21}, both in

344 compass C{degrees} between C{-180} and C{+180}, the

345 rhumb distance C{s12} and rhumb angle C{a12} between

346 both points in C{meter} respectively C{degrees} and

347 the area C{S12} under the rhumb line in C{meter}

348 I{squared}.

349

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

351 only required for L{RhumbAux} area C{S12}

352 when C{B{exact} is True}.

353

354 @note: The shortest rhumb line is found. If the end points are

355 on opposite meridians, there are two shortest rhumb lines

356 and the East-going one is chosen.

357

358 @note: If either point is a pole, the cosine of its latitude is

359 taken to be C{sqrt(L{EPS})}. This position is extremely

360 close to the actual pole and allows the calculation to be

361 carried out in finite terms.

362 '''

363 r = GDict(lat1=lat1, lon1=lon1, lat2=lat2, lon2=lon2, name=self.name)

364 Cs = Caps

365 if (outmask & Cs.AZIMUTH_DISTANCE_AREA):

366 lon12, _ = _diff182(lon1, lon2, K_2_0=True)

367 y, x, s1, s2 = self._Inverse4(lon12, r, outmask)

368 if (outmask & Cs.AZIMUTH):

369 z = _atan2d(y, x)

370 r.set_(azi12=z, azi21=_azireversed(z))

371 if (outmask & Cs.AREA):

372 S12 = self._S12d(s1, s2, lon12)

373 r.set_(S12=unsigned0(S12)) # like .gx

374 return r

375

376 def _Inverse4(self, lon12, r, outmask): # PYCHOK no cover

377 '''(INTERNAL) I{Must be overloaded}.'''

378 self._notOverloaded(lon12, r, Caps.toStr(outmask)) # underOK=True

379

380 def Inverse8(self, lat1, lon1, azi12, s12, outmask=Caps.AZIMUTH_DISTANCE_AREA):

381 '''Like method L{Rhumb.Inverse} but returning a L{Rhumb8Tuple} with area C{S12}.

382 '''

383 return self.Inverse(lat1, lon1, azi12, s12, outmask=outmask).toRhumb8Tuple()

384

385 def _InverseLine(self, ll1, ll2, wrap, **caps_name):

386 '''(INTERNAL) Short-cut version, see .latlonBase.

387 '''

388 if wrap:

389 ll2 = _unrollon(ll1, _Wrap.point(ll2))

390 return self.InverseLine(ll1.lat, ll1.lon, ll2.lat, ll2.lon, **caps_name)

391

392 def InverseLine(self, lat1, lon1, lat2, lon2, **caps_name):

393 '''Define a C{RhumbLine} in terms of the I{inverse} rhumb problem.

394

395 @arg lat1: Latitude of the first point (C{degrees90}).

396 @arg lon1: Longitude of the first point (C{degrees180}).

397 @arg lat2: Latitude of the second point (C{degrees90}).

398 @arg lon2: Longitude of the second point (C{degrees180}).

399 @kwarg caps_name: Optional keyword arguments C{B{name}=NN} and

400 C{B{caps}=Caps.STANDARD}, a bit-or'ed combination of

401 L{Caps<pygeodesy.karney.Caps>} values specifying the

402 required capabilities. Include C{Caps.LINE_OFF} if

403 updates to the B{C{rhumb}} should I{not be reflected}

404 in this rhumb line.

405

406 @return: A C{RhumbLine...} instance and invoke its method

407 C{ArcPosition} or C{Position} to compute points.

408

409 @note: Updates to this rhumb are reflected in the returned

410 rhumb line, unless C{B{caps} |= Caps.LINE_OFF}.

411 '''

412 r = self.Inverse(lat1, lon1, lat2, lon2, outmask=Caps.AZIMUTH)

413 return self._RhumbLine(self, lat1, lon1, r.azi12, **caps_name)

414

415 @Property_RO

416 def _mpd(self): # PYCHOK no cover

417 '''(INTERNAL) I{Must be overloaded}.'''

418 _MODS.named.notOverloaded(self)

419

420 @property_RO

421 def RAorder(self):

422 '''Get the I{Rhumb Area} order, C{None} always.

423 '''

424 return None

425

426 @property_RO

427 def _RhumbLine(self): # PYCHOK no cover

428 '''(INTERNAL) I{Must be overloaded}.'''

429 self._notOverloaded(underOK=True)

430

431 def _S12d(self, s1, s2, lon): # PYCHOK no cover

432 '''(INTERNAL) I{Must be overloaded}.'''

433 self._notOverloaded(s1, s2, lon) # underOK=True

434

435 @Property

436 def TMorder(self):

437 '''Get the L{KTransverseMercator} order (C{int}, 4, 5, 6, 7 or 8).

438 '''

439 return self._mTM

440

441 @TMorder.setter # PYCHOK setter!

442 def TMorder(self, order):

443 '''Set the L{KTransverseMercator} order (C{int}, 4, 5, 6, 7 or 8).

444

445 @note: Setting C{TMorder} turns property C{exact} off, but only

446 for L{Rhumb} instances.

447 '''

448 m = _Xorder(_MODS.ktm._AlpCoeffs, RhumbError, TMorder=order)

449 if self._mTM != m:

450 _update_all_rls(self)

451 self._mTM = m

452 if self.exact and isinstance(self, _MODS.rhumb.ekx.Rhumb):

453 self.exact = False

454

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

456 '''Return this C{Rhumb} as string.

457

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

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

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

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

462

463 @return: Tuple items (C{str}).

464 '''

465 d = dict(ellipsoid=self.ellipsoid, RAorder=self.RAorder,

466 exact=self.exact, TMorder=self.TMorder)

467 return sep.join(pairs(itemsorted(d, asorted=False), prec=prec))

468

469

470 class RhumbLineBase(_CapsBase):

471 '''(INTERNAL) Base class for C{rhumb.aux_.RhumbLineAux} and C{rhumb.ekx.RhumbLine}.

472 '''

473 _azi12 = _0_0

474 _calp = _1_0

475# _caps = \

476# _debug = 0

477# _lat1 = \

478# _lon1 = \

479# _lon12 = _0_0

480 _Rhumb = RhumbBase # compatible C{Rhumb} class

481 _rhumb = None # C{Rhumb} instance

482 _salp = \

483 _talp = _0_0

484

485 def __init__(self, rhumb, lat1, lon1, azi12, caps=Caps.STANDARD, name=NN):

486 '''New C{RhumbLine} or C{RhumbLineAux}.

487 '''

488 _xinstanceof(self._Rhumb, rhumb=rhumb)

489

490 self._lat1 = _Lat(lat1=_fix90(lat1))

491 self._lon1 = _Lon(lon1= lon1)

492 self._lon12 = _norm180(self._lon1)

493 if azi12: # non-zero, non-None

494 self.azi12 = _norm180(azi12)

495

496 n = name or rhumb.name

497 if n:

498 self.name=n

499

500 self._caps = caps

501 self._debug |= (caps | rhumb._debug) & Caps._DEBUG_DIRECT_LINE

502 if (caps & Caps.LINE_OFF): # copy to avoid updates

503 self._rhumb = rhumb.copy(deep=False, name=_under(rhumb.name))

504 else:

505 self._rhumb = rhumb

506 _rls.append(self)

507

508 def __del__(self): # XXX use weakref?

509 if _rls: # may be empty or None

510 try: # PYCHOK no cover

511 _rls.remove(self)

512 except (TypeError, ValueError):

513 pass

514 self._rhumb = None

515 # _update_all(self) # throws TypeError during Python 2 cleanup

516

517 def ArcPosition(self, a12, outmask=Caps.LATITUDE_LONGITUDE):

518 '''Compute a point at a given angular distance on this rhumb line.

519

520 @arg a12: The angle along this rhumb line from its origin to the

521 point (C{degrees}), can be negative.

522 @kwarg outmask: Bit-or'ed combination of L{Caps<pygeodesy.karney.Caps>}

523 values specifying the quantities to be returned.

524

525 @return: L{GDict} with 4 to 8 items C{azi12, a12, s12, S12, lat2,

526 lon2, lat1, lon1} with latitude C{lat2} and longitude

527 C{lon2} of the point in C{degrees}, the rhumb distance

528 C{s12} in C{meter} from the start point of and the area

529 C{S12} under this rhumb line in C{meter} I{squared}.

530

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

532 only required for L{RhumbLineAux} area C{S12}

533 when C{B{exact} is True}.

534

535 @note: If B{C{a12}} is large enough that the rhumb line crosses a

536 pole, the longitude of the second point is indeterminate and

537 C{NAN} is returned for C{lon2} and area C{S12}.

538

539 If the first point is a pole, the cosine of its latitude is

540 taken to be C{sqrt(L{EPS})}. This position is extremely

541 close to the actual pole and allows the calculation to be

542 carried out in finite terms.

543 '''

544 return self._Position(a12, self.degrees2m(a12), outmask)

545

546 @Property

547 def azi12(self):

548 '''Get this rhumb line's I{azimuth} (compass C{degrees}).

549 '''

550 return self._azi12

551

552 @azi12.setter # PYCHOK setter!

553 def azi12(self, azi12):

554 '''Set this rhumb line's I{azimuth} (compass C{degrees}).

555 '''

556 z = _norm180(azi12)

557 if self._azi12 != z:

558 if self._rhumb:

559 _update_all(self)

560 self._azi12 = z

561 self._salp, self._calp = t = sincos2d(z) # no NEG0

562 self._talp = _over(*t)

563

564 @property_RO

565 def azi12_sincos2(self): # PYCHOK no cover

566 '''Get the sine and cosine of this rhumb line's I{azimuth} (2-tuple C{(sin, cos)}).

567 '''

568 return self._scalp, self._calp

569

570 @property_RO

571 def datum(self):

572 '''Get this rhumb line's datum (L{Datum}).

573 '''

574 return self.rhumb.datum

575

576 def degrees2m(self, angle):

577 '''Convert an angular distance along this rhumb line to C{meter}.

578

579 @arg angle: Angular distance (C{degrees}).

580

581 @return: Distance (C{meter}).

582 '''

583 return float(angle) * self.rhumb._mpd

584

585 @deprecated_method

586 def distance2(self, lat, lon): # PYCHOK no cover

587 '''DEPRECATED on 23.09.23, use method L{RhumbLineAux.Inverse} or L{RhumbLine.Inverse}.

588

589 @return: A L{Distance2Tuple}C{(distance, initial)} with the C{distance}

590 in C{meter} and C{initial} bearing (azimuth) in C{degrees}.

591 '''

592 r = self.Inverse(lat, lon)

593 return Distance2Tuple(r.s12, r.azi12)

594

595 @property_RO

596 def ellipsoid(self):

597 '''Get this rhumb line's ellipsoid (L{Ellipsoid}).

598 '''

599 return self.rhumb.ellipsoid

600

601 @property_RO

602 def exact(self):

603 '''Get this rhumb line's I{exact} option (C{bool}).

604 '''

605 return self.rhumb.exact

606

607 def Intersecant2(self, lat0, lon0, radius, napier=True, **tol_eps):

608 '''Compute the intersection(s) of this rhumb line and a circle.

609

610 @arg lat0: Latitude of the circle center (C{degrees}).

611 @arg lon0: Longitude of the circle center (C{degrees}).

612 @arg radius: Radius of the circle (C{meter}, conventionally).

613 @kwarg napier: If C{True}, apply I{Napier}'s spherical triangle

614 instead of planar trigonometry (C{bool}).

615 @kwarg tol_eps: Optional keyword arguments, see method

616 method L{Intersection} for further details.

617

618 @return: 2-Tuple C{(P, Q)} with both intersections (representing

619 a rhumb chord), each a L{GDict} from method L{Intersection}

620 extended to 18 items by C{lat3, lon3, azi03, a03, s03}

621 with azimuth C{azi03} of, distance C{a03} in C{degrees}

622 and C{s03} in C{meter} along the rhumb line from the circle

623 C{lat0, lon0} to the chord center C{lat3, lon3}. If this

624 rhumb line is tangential to the circle, both points

625 are the same L{GDict} instance with distances C{s02} and

626 C{s03} near-equal to the B{C{radius}}.

627

628 @raise IntersectionError: The circle and this rhumb line

629 do not intersect.

630

631 @raise UnitError: Invalid B{C{radius}}.

632 '''

633 r = Radius_(radius)

634 p = q = self.PlumbTo(lat0, lon0, exact=None, **tol_eps)

635 a = q.s02

636 t = dict(lat3=q.lat2, lon3=q.lon2, azi03=q.azi02, a03=q.a02, s03=a)

637 if a < r:

638 t.update(iteration=q.iteration, lat0=q.lat1, lon0=q.lon1, # or lat0, lon0

639 name=typename(self.Intersecant2, self.name))

640 if fabs(a) < EPS0: # coincident centers

641 d, h = _0_0, r

642 else:

643 d = q.s12

644 if napier: # Napier rule (R1) cos(b) = cos(c) / cos(a)

645 # <https://WikiPedia.org/wiki/Spherical_trigonometry>

646 m = self.rhumb._mpr

647 h = (acos1(cos(r / m) / cos(a / m)) * m) if m else _0_0

648 else:

649 h = _copysign(sqrt_a(r, a), a)

650 p = q = self.Position(d + h).set_(**t)

651 if h:

652 q = self.Position(d - h).set_(**t)

653 elif a > r:

654 t = _too_(Fmt.distant(a))

655 raise IntersectionError(self, lat0, lon0, radius,

656 txt=t, **tol_eps)

657 else: # tangential

658 q.set_(**t) # == p.set(_**t)

659 return p, q

660

661 @deprecated_method

662 def intersection2(self, other, **tol_eps): # PYCHOK no cover

663 '''DEPRECATED on 23.10.10, use method L{Intersection}.'''

664 p = self.Intersection(other, **tol_eps)

665 r = LatLon2Tuple(p.lat2, p.lon2, name=typename(self.intersection2))

666 r._iteration = p.iteration

667 return r

668

669 def Intersection(self, other, tol=_TOL, **eps):

670 '''I{Iteratively} find the intersection of this and an other rhumb line.

671

672 @arg other: The other rhumb line (C{RhumbLine}).

673 @kwarg tol: Tolerance for longitudinal convergence and parallel

674 error (C{degrees}).

675 @kwarg eps: Tolerance for L{pygeodesy.intersection3d3} (C{EPS}).

676

677 @return: The intersection point, a L{Position}-like L{GDict} with

678 13 items C{lat1, lon1, azi12, a12, s12, lat2, lon2, lat0,

679 lon0, azi02, a02, s02, at} with the rhumb angle C{a02}

680 and rhumb distance C{s02} between the start point C{lat0,

681 lon0} of the B{C{other}} rhumb line and the intersection

682 C{lat2, lon2}, the azimuth C{azi02} of the B{C{other}}

683 rhumb line and the angle C{at} between both rhumb lines.

684 See method L{Position} for further details.

685

686 @raise IntersectionError: No convergence for this B{C{tol}} or

687 no intersection for an other reason.

688

689 @see: Methods C{distance2} and C{PlumbTo} and function

690 L{pygeodesy.intersection3d3}.

691

692 @note: Each iteration involves a round trip to this rhumb line's

693 L{ExactTransverseMercator} or L{KTransverseMercator}

694 projection and function L{pygeodesy.intersection3d3} in

695 that domain.

696 '''

697 _xinstanceof(RhumbLineBase, other=other)

698 _xdatum(self.rhumb, other.rhumb, Error=RhumbError)

699 try:

700 if self.others(other) is self:

701 raise ValueError(_coincident_)

702 # make invariants and globals locals

703 _s_3d, s_az = self._xTM3d, self.azi12

704 _o_3d, o_az = other._xTM3d, other.azi12

705 p = _MODS.formy.opposing(s_az, o_az, margin=tol)

706 if p is not None: # == isbool(p)

707 raise ValueError(_anti_(_parallel_) if p else _parallel_)

708 _diff = euclid # approximate length

709 _i3d3 = _intersect3d3 # NOT .vector3d.intersection3d3

710 _LL2T = LatLon2Tuple

711 _xTMr = self.xTM.reverse # ellipsoidal or spherical

712 # use halfway point as initial estimate

713 p = _LL2T(favg(self.lat1, other.lat1),

714 favg(self.lon1, other.lon1))

715 for i in range(1, _TRIPS):

716 v = _i3d3(_s_3d(p), s_az, # point + bearing

717 _o_3d(p), o_az, useZ=False, **eps)[0]

718 t = _xTMr(v.x, v.y, lon0=p.lon) # PYCHOK Reverse4Tuple

719 d = _diff(t.lon - p.lon, t.lat) # PYCHOK t.lat + p.lat - p.lat

720 p = _LL2T(t.lat + p.lat, t.lon) # PYCHOK t.lon + p.lon = lon0

721 if d < tol: # 19+ trips

722 break

723 else:

724 raise ValueError(Fmt.no_convergence(d, tol))

725

726 P = GDict(lat1=self.lat1, lat2=p.lat, lat0=other.lat1,

727 lon1=self.lon1, lon2=p.lon, lon0=other.lon1,

728 name=typename(self.Intersection, self.name))

729 r = self.Inverse(p.lat, p.lon, outmask=Caps.DISTANCE)

730 t = other.Inverse(p.lat, p.lon, outmask=Caps.DISTANCE)

731 P.set_(azi12= self.azi12, a12=r.a12, s12=r.s12,

732 azi02=other.azi12, a02=t.a12, s02=t.s12,

733 at=other.azi12 - self.azi12, iteration=i)

734 except Exception as x:

735 raise IntersectionError(self, other, tol=tol, cause=x, **eps)

736 return P

737

738 def Inverse(self, lat2, lon2, wrap=False, **outmask):

739 '''Return the rhumb angle, distance, azimuth, I{reverse} azimuth, etc. of

740 a rhumb line between the given point and this rhumb line's start point.

741

742 @arg lat2: Latitude of the point (C{degrees}).

743 @arg lon2: Longitude of the points (C{degrees}).

744 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}}

745 and B{C{lon2}} (C{bool}).

746

747 @return: L{GDict} with 8 items C{a12, s12, azi12, azi21, lat1, lon1,

748 lat2, lon2}, the rhumb angle C{a12} and rhumb distance C{s12}

749 between both points in C{degrees} respectively C{meter}, the

750 rhumb line's azimuth C{azi12} and I{reverse} azimuth C{azi21}

751 both in compass C{degrees} between C{-180} and C{+180}.

752 '''

753 if wrap:

754 _, lat2, lon2 = _Wrap.latlon3(self.lon1, _fix90(lat2), lon2, wrap)

755 r = self.rhumb.Inverse(self.lat1, self.lon1, lat2, lon2, **outmask)

756 return r

757

758 @Property_RO

759 def isLoxodrome(self):

760 '''Is this rhumb line a meridional (C{None}), a parallel

761 (C{False}) or a C{True} loxodrome?

762

763 @see: I{Osborne's} U{2.5 Rumb lines and loxodromes

764 <https://Zenodo.org/record/35392>}, page 37.

765 '''

766 return bool(self._salp) if self._calp else None

767

768 @Property_RO

769 def lat1(self):

770 '''Get this rhumb line's latitude (C{degrees90}).

771 '''

772 return self._lat1

773

774 @Property_RO

775 def lon1(self):

776 '''Get this rhumb line's longitude (C{degrees180}).

777 '''

778 return self._lon1

779

780 @Property_RO

781 def latlon1(self):

782 '''Get this rhumb line's lat- and longitude (L{LatLon2Tuple}C{(lat, lon)}).

783 '''

784 return LatLon2Tuple(self.lat1, self.lon1)

785

786 def m2degrees(self, distance):

787 '''Convert a distance along this rhumb line to an angular distance.

788

789 @arg distance: Distance (C{meter}).

790

791 @return: Angular distance (C{degrees}).

792 '''

793 return _over(float(distance), self.rhumb._mpd)

794

795 @property_RO

796 def _mu1(self): # PYCHOK no cover

797 '''(INTERNAL) I{Must be overloaded}.'''

798 self._notOverloaded(underOK=True)

799

800 def _mu2lat(self, mu2): # PYCHOK no cover

801 '''(INTERNAL) I{Must be overloaded}.'''

802 self._notOverloaded(mu2) # underOK=True

803

804 @deprecated_method

805 def nearestOn4(self, lat0, lon0, **exact_eps_est_tol): # PYCHOK no cover

806 '''DEPRECATED on 23.10.10, use method L{PlumbTo}.'''

807 P = self.PlumbTo(lat0, lon0, **exact_eps_est_tol)

808 r = _MODS.deprecated.classes.NearestOn4Tuple(P.lat2, P.lon2, P.s12, P.azi02,

809 name=typename(self.nearestOn4))

810 r._iteration = P.iteration

811 return r

812

813 @deprecated_method

814 def NearestOn(self, lat0, lon0, **exact_eps_est_tol): # PYCHOK no cover

815 '''DEPRECATED on 23.10.30, use method L{PlumbTo}.'''

816 return self.PlumbTo(lat0, lon0, **exact_eps_est_tol)

817

818 def PlumbTo(self, lat0, lon0, exact=None, eps=EPS, est=None, tol=_TOL):

819 '''Compute the I{perpendicular} intersection of this rhumb line with a geodesic

820 from the given point (transcoded from I{Karney}'s C++ U{rhumb-intercept

821 <https://SourceForge.net/p/geographiclib/discussion/1026620/thread/2ddc295e/>}).

822

823 @arg lat0: Latitude of the point on the geodesic (C{degrees}).

824 @arg lon0: Longitude of the point on the geodesic (C{degrees}).

825 @kwarg exact: If C{None}, use a rhumb line perpendicular to this rhumb line,

826 otherwise use an I{exact} C{Geodesic...} from the given point

827 perpendicular to this rhumb line (C{bool} or C{Geodesic...}),

828 see method L{geodesic_<pygeodesy.Ellipsoid.geodesic_>}.

829 @kwarg eps: Optional tolerance (C{EPS}), used only if C{B{exact} is None},

830 see function L{intersection3d3<pygeodesy.intersection3d3>}.

831 @kwarg est: Optionally, an initial estimate for the distance C{s12} of the

832 intersection I{along} this rhumb line (C{meter}), used only if

833 C{B{exact} is not None}.

834 @kwarg tol: Longitudinal convergence tolerance (C{degrees}) or distance

835 tolerance (C(meter)) when C{B{exact} is None}, respectively

836 C{not None}.

837

838 @return: The intersection point on this rhumb line, a L{GDict} from method

839 L{Intersection} if B{C{exact}=None}. If C{B{exact} is not None},

840 a L{Position}-like L{GDict} of 13 items C{azi12, a12, s12, lat2,

841 lat1, lat0, lon2, lon1, lon0, azi0, a02, s02, at} with distance

842 C{a02} in C{degrees} and C{s02} in C{meter} between the given point

843 C{lat0, lon0} and the intersection C{lat2, lon2}, geodesic azimuth

844 C{azi0} at the given point and the (perpendicular) angle C{at}

845 between the geodesic and this rhumb line at the intersection. The

846 I{geodesic} azimuth at the intersection is C{(at + azi12)}. See

847 method L{Position} for further details.

848

849 @raise ImportError: I{Karney}'s U{geographiclib

850 <https://PyPI.org/project/geographiclib>}

851 package not found or not installed.

852

853 @raise IntersectionError: No convergence for this B{C{eps}} or B{C{tol}} or

854 no intersection for some other reason.

855

856 @see: Methods C{distance2}, C{Intersecant2} and C{Intersection} and function

857 L{intersection3d3<pygeodesy.intersection3d3>}.

858 '''

859 Cs, tol = Caps, Float_(tol=tol, low=EPS, high=None)

860

861# def _over(p, q): # see @note at method C{.Position}

862# if p:

863# p = (p / (q or _copysign(tol, q))) if isfinite(q) else NAN

864# return p

865

866 if exact is None:

867 z = _norm180(self.azi12 + _90_0) # perpendicular azimuth

868 rl = RhumbLineBase(self.rhumb, lat0, lon0, z, caps=Cs.LINE_OFF)

869 P = self.Intersection(rl, tol=tol, eps=eps)

870

871 else: # C{rhumb-intercept}

872 E = self.ellipsoid

873 _gI = E.geodesic_(exact=exact).Inverse

874 gm = Cs.STANDARD | Cs._REDUCEDLENGTH_GEODESICSCALE # ^ Cs.DISTANCE_IN

875 _d2 = _diff182

876 if est is None: # get an estimate from the "perpendicular" geodesic

877 r = _gI(self.lat1, self.lon1, lat0, lon0, outmask=Cs.AZIMUTH_DISTANCE)

878 d, _ = _d2(r.azi2, self.azi12, K_2_0=True)

879 _, s12 = sincos2d(d)

880 s12 *= r.s12 # signed

881 else:

882 s12 = Meter(est=est)

883 try:

884 _abs = fabs

885 _ErT = E.rocPrimeVertical # aka rocTransverse

886 _ovr = _over

887 _S12 = Fsum(s12).fsum2f_

888 _scd = sincos2d_

889 for i in range(1, _TRIPS): # 9+, suffix 1 == C++ 2, 2 == C++ 3

890 P = self.Position(s12) # outmask=Cs.LATITUDE_LONGITUDE

891 r = _gI(lat0, lon0, P.lat2, P.lon2, outmask=gm)

892 d, _ = _d2(self.azi12, r.azi2, K_2_0=True)

893 s, c, s2, c2 = _scd(d, r.lat2)

894 c2 *= _ErT(r.lat2)

895 s *= _ovr(s2 * self._salp, c2) - _ovr(s * r.M21, r.m12)

896 s12, t = _S12(c / s) if s else (s12, s) # XXX _ovr?

897 if _abs(t) < tol: # or _abs(c) < EPS

898 break

899 P.set_(azi0=r.azi1, a02=r.a12, s02=r.s12, # azi2=r.azi2,

900 lat0=lat0, lon0=lon0, iteration=i, at=r.azi2 - self.azi12,

901 name=typename(self.PlumbTo, self.name))

902 except Exception as x: # Fsum(NAN) Value-, ZeroDivisionError

903 raise IntersectionError(lat0=lat0, lon0=lon0, tol=tol, exact=exact,

904 eps=eps, est=est, iteration=i, cause=x)

905

906 return P

907

908 def Position(self, s12, outmask=Caps.LATITUDE_LONGITUDE):

909 '''Compute a point at a given distance on this rhumb line.

910

911 @arg s12: The distance along this rhumb line from its origin to the point

912 (C{meters}), can be negative.

913 @kwarg outmask: Bit-or'ed combination of L{Caps<pygeodesy.karney.Caps>}

914 values specifying the quantities to be returned.

915

916 @return: L{GDict} with 4 to 8 items C{azi12, a12, s12, S12, lat2, lat1,

917 lon2, lon1} with latitude C{lat2} and longitude C{lon2} of the

918 point in C{degrees}, the rhumb angle C{a12} in C{degrees} from

919 the start point of and the area C{S12} under this rhumb line

920 in C{meter} I{squared}.

921

922 @raise ImportError: Package C{numpy} not found or not installed, required

923 only for L{RhumbLineAux} area C{S12} when C{B{exact}

924 is True}.

925

926 @note: If B{C{s12}} is large enough that the rhumb line crosses a pole, the

927 longitude of the second point is indeterminate and C{NAN} is returned

928 for C{lon2} and area C{S12}.

929

930 If the first point is a pole, the cosine of its latitude is taken to

931 be C{sqrt(L{EPS})}. This position is extremely close to the actual

932 pole and allows the calculation to be carried out in finite terms.

933 '''

934 return self._Position(self.m2degrees(s12), s12, outmask)

935

936 def _Position(self, a12, s12, outmask):

937 '''(INTERNAL) C{Arc-/Position} helper.

938 '''

939 r = GDict(azi12=self.azi12, a12=a12, s12=s12, name=self.name)

940 Cs = Caps

941 if (outmask & Cs.LATITUDE_LONGITUDE_AREA):

942 if a12 or s12:

943 mu12 = self._calp * a12

944 mu2 = self._mu1 + mu12

945 if fabs(mu2) > 90: # past pole

946 mu2 = _norm180(mu2) # reduce to [-180, 180)

947 if fabs(mu2) > 90: # point on anti-meridian

948 mu2 = _norm180(_loneg(mu2))

949 lat2 = self._mu2lat(mu2)

950 lon2 = S12 = NAN

951 else:

952 lat2, lon2, S1, S2 = self._Position4(a12, mu2, s12, mu12)

953 if (outmask & Cs.AREA):

954 S12 = self.rhumb._S12d(S1, S2, lon2)

955 S12 = unsigned0(S12) # like .gx

956# else:

957# S12 = None # unused

958 if (outmask & Cs.LONGITUDE):

959 if (outmask & Cs.LONG_UNROLL):

960 lon2 += self.lon1

961 else:

962 lon2 = _norm180(self._lon12 + lon2)

963 else: # coincident

964 lat2, lon2 = self.latlon1

965 S12 = _0_0

966

967 if (outmask & Cs.AREA):

968 r.set_(S12=S12)

969 if (outmask & Cs.LATITUDE):

970 r.set_(lat2=lat2, lat1=self.lat1)

971 if (outmask & Cs.LONGITUDE):

972 r.set_(lon2=lon2, lon1=self.lon1)

973 return r

974

975 def _Position4(self, a12, mu2, s12, mu12): # PYCHOK no cover

976 '''(INTERNAL) I{Must be overloaded}.'''

977 self._notOverloaded(a12, s12, mu2, mu12) # underOK=True

978

979 @Property_RO

980 def rhumb(self):

981 '''Get this rhumb line's rhumb (L{RhumbAux} or L{Rhumb}).

982 '''

983 return self._rhumb

984

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

986 '''Return this C{RhumbLine} as string.

987

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

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

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

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

992

993 @return: C{RhumbLine} (C{str}).

994 '''

995 d = dict(rhumb=self.rhumb, lat1=self.lat1, lon1=self.lon1,

996 azi12=self.azi12, exact=self.exact,

997 TMorder=self.TMorder, xTM=self.xTM)

998 return sep.join(pairs(itemsorted(d, asorted=False), prec=prec))

999

1000 @property_RO

1001 def TMorder(self):

1002 '''Get this rhumb line's I{Transverse Mercator} order (C{int}, 4, 5, 6, 7 or 8).

1003 '''

1004 return self.rhumb.TMorder

1005

1006 @Property_RO

1007 def xTM(self):

1008 '''Get this rhumb line's I{Transverse Mercator} projection (L{ExactTransverseMercator}

1009 if I{exact} and I{ellipsoidal}, otherwise L{KTransverseMercator} for C{TMorder}).

1010 '''

1011 E = self.ellipsoid

1012 # ExactTransverseMercator doesn't handle spherical earth models

1013 return _MODS.etm.ExactTransverseMercator(E) if self.exact and E.isEllipsoidal else \

1014 _MODS.ktm.KTransverseMercator(E, TMorder=self.TMorder)

1015

1016 def _xTM3d(self, latlon0, z=INT0, V3d=Vector3d):

1017 '''(INTERNAL) C{xTM.forward} this C{latlon1} to C{V3d} with B{C{latlon0}}

1018 as current intersection estimate and central meridian.

1019 '''

1020 t = self.xTM.forward(self.lat1 - latlon0.lat, self.lon1, lon0=latlon0.lon)

1021 return V3d(t.easting, t.northing, z)

1022

1023

1024 class _PseudoRhumbLine(RhumbLineBase):

1025 '''(INTERNAL) Pseudo-rhumb line for a geodesic (line), see C{geodesicw._PlumbTo}.

1026 '''

1027 def __init__(self, gl, name=NN):

1028 R = RhumbBase(gl.geodesic.ellipsoid, None, True, name)

1029 RhumbLineBase.__init__(self, R, gl.lat1, gl.lon1, 0, caps=Caps.LINE_OFF)

1030 self._azi1 = self.azi12 = gl.azi1

1031 self._gl = gl

1032 self._gD = gl.geodesic.Direct

1033

1034 def PlumbTo(self, lat0, lon0, **exact_eps_est_tol): # PYCHOK signature

1035 P = RhumbLineBase.PlumbTo(self, lat0, lon0, **exact_eps_est_tol)

1036 z, P = _xkwds_pop2(P, azi12=None)

1037 P.set_(azi1=self._gl.azi1, azi2=z)

1038 return P # geodesic L{Position}

1039

1040 def Position(self, s12, **unused): # PYCHOK signature

1041 r = self._gD(self.lat1, self.lon1, self._azi1, s12)

1042 self._azi1 = r.azi1

1043 self.azi12 = z = r.azi2

1044 self._salp, _ = sincos2d(z)

1045 return r.set_(azi12=z)

1046

1047

1048 __all__ += _ALL_DOCS(RhumbBase, RhumbLineBase)

1049

1050 if __name__ == _DMAIN_:

1051

1052 from pygeodesy import printf, Rhumb as Rh, RhumbAux as Ah

1053 from pygeodesy.basics import _zip

1054 from pygeodesy.ellipsoids import _EWGS84

1055

1056 Al = Ah(_EWGS84).Line(30, 0, 45)

1057 Rl = Rh(_EWGS84).Line(30, 0, 45)

1058

1059 for i in range(1, 10):

1060 s = .5e6 + 1e6 / i

1061 a = Al.Position(s).lon2

1062 r = Rl.Position(s).lon2

1063 e = (fabs(a - r) / a) if a else 0

1064 printf('# Position.lon2 %.14f vs %.14f, diff %g', r, a, e)

1065

1066 for exact in (None, False, True):

1067 for est in (None, 1e6):

1068 a = Al.PlumbTo(60, 0, exact=exact, est=est)

1069 r = Rl.PlumbTo(60, 0, exact=exact, est=est)

1070 printf('# %s, iteration=%s, exact=%s, est=%s\n# %s, iteration=%s',

1071 a.toRepr(), a.iteration, exact, est,

1072 r.toRepr(), r.iteration, nl=1)

1073

1074 NE_=(71.688899882813, 0.2555198244234, 44095641862956.11)

1075 LHR=(77.7683897102557, 5771083.38332803, 37395209100030.39)

1076 NRT=(-92.38888798169965, 12782581.067684170, -63760642939072.50)

1077

1078 def _ref(fmt, r3, x3):

1079 e3 = []

1080 for r, x in _zip(r3, x3): # strict=True

1081 e = fabs(r - x) / fabs(x)

1082 e3.append('%.g' % (e,))

1083 printf((fmt % r3) + ', rel errors: ' + ', '.join(e3))

1084

1085 for R in (Ah, Rh): # <https://GeographicLib.SourceForge.io/cgi-bin/RhumbSolve -p 9> version 2.2

1086 rh = R(exact=True) # WGS84 default

1087 printf('# %r', rh, nl=1)

1088 r = rh.Direct8(40.6, -73.8, 51, 5.5e6) # from JFK about NE

1089 _ref('# JFK NE lat2=%.12f, lon2=%.12f, S12=%.1f', (r.lat2, r.lon2, r.S12), NE_)

1090 r = rh.Inverse8(40.6, -73.8, 51.6, -0.5) # JFK to LHR

1091 _ref('# JFK-LHR azi12=%.12f, s12=%.3f S12=%.1f', (r.azi12, r.s12, r.S12), LHR)

1092 r = rh.Inverse8(40.6, -73.8, 35.8, 140.3) # JFK to Tokyo Narita

1093 _ref('# JFK-NRT azi12=%.12f, s12=%.3f S12=%.1f', (r.azi12, r.s12, r.S12), NRT)

1094

1095# % python3.10 -m pygeodesy3.rhumb.Bases

1096

1097# Position.lon2 11.61455846901637 vs 11.61455846901637, diff 3.05885e-16

1098# Position.lon2 7.58982302826842 vs 7.58982302826842, diff 2.34045e-16

1099# Position.lon2 6.28526067416369 vs 6.28526067416369, diff 2.82623e-16

1100# Position.lon2 5.63938995325146 vs 5.63938995325146, diff 1.57495e-16

1101# Position.lon2 5.25385527435707 vs 5.25385527435707, diff 0

1102# Position.lon2 4.99764604290380 vs 4.99764604290380, diff 8.88597e-16

1103# Position.lon2 4.81503363740473 vs 4.81503363740473, diff 1.84459e-16

1104# Position.lon2 4.67828821748836 vs 4.67828821748835, diff 5.69553e-16

1105# Position.lon2 4.57205667906283 vs 4.57205667906283, diff 5.82787e-16

1106

1107# Intersection(a02=17.798332, a12=19.521356, at=90.0, azi02=135.0, azi12=45.0, lat0=60.0, lat1=30.0, lat2=45.0, lon0=0.0, lon1=0.0, lon2=15.830286, name='Intersection', s02=1977981.142985, s12=2169465.957531), iteration=9, exact=None, est=None

1108# Intersection(a02=17.798332, a12=19.521356, at=90.0, azi02=135.0, azi12=45.0, lat0=60.0, lat1=30.0, lat2=45.0, lon0=0.0, lon1=0.0, lon2=15.830286, name='Intersection', s02=1977981.142985, s12=2169465.957531), iteration=9

1109

1110# Intersection(a02=17.798332, a12=19.521356, at=90.0, azi02=135.0, azi12=45.0, lat0=60.0, lat1=30.0, lat2=45.0, lon0=0.0, lon1=0.0, lon2=15.830286, name='Intersection', s02=1977981.142985, s12=2169465.957531), iteration=9, exact=None, est=1000000.0

1111# Intersection(a02=17.798332, a12=19.521356, at=90.0, azi02=135.0, azi12=45.0, lat0=60.0, lat1=30.0, lat2=45.0, lon0=0.0, lon1=0.0, lon2=15.830286, name='Intersection', s02=1977981.142985, s12=2169465.957531), iteration=9

1112

1113# PlumbTo(a02=17.967658, a12=27.74256, at=90.0, azi0=113.73626, azi12=45.0, lat0=60, lat1=30.0, lat2=49.634582, lon0=0, lon1=0.0, lon2=25.767876, name='PlumbTo', s02=1997960.116871, s12=3083112.636236), iteration=5, exact=False, est=None

1114# PlumbTo(a02=17.967658, a12=27.74256, at=90.0, azi0=113.73626, azi12=45.0, lat0=60, lat1=30.0, lat2=49.634582, lon0=0, lon1=0.0, lon2=25.767876, name='PlumbTo', s02=1997960.116871, s12=3083112.636236), iteration=5

1115

1116# PlumbTo(a02=17.967658, a12=27.74256, at=90.0, azi0=113.73626, azi12=45.0, lat0=60, lat1=30.0, lat2=49.634582, lon0=0, lon1=0.0, lon2=25.767876, name='PlumbTo', s02=1997960.116871, s12=3083112.636236), iteration=7, exact=False, est=1000000.0

1117# PlumbTo(a02=17.967658, a12=27.74256, at=90.0, azi0=113.73626, azi12=45.0, lat0=60, lat1=30.0, lat2=49.634582, lon0=0, lon1=0.0, lon2=25.767876, name='PlumbTo', s02=1997960.116871, s12=3083112.636236), iteration=7

1118

1119# PlumbTo(a02=17.967658, a12=27.74256, at=90.0, azi0=113.73626, azi12=45.0, lat0=60, lat1=30.0, lat2=49.634582, lon0=0, lon1=0.0, lon2=25.767876, name='PlumbTo', s02=1997960.116871, s12=3083112.636236), iteration=5, exact=True, est=None

1120# PlumbTo(a02=17.967658, a12=27.74256, at=90.0, azi0=113.73626, azi12=45.0, lat0=60, lat1=30.0, lat2=49.634582, lon0=0, lon1=0.0, lon2=25.767876, name='PlumbTo', s02=1997960.116871, s12=3083112.636236), iteration=5

1121

1122# PlumbTo(a02=17.967658, a12=27.74256, at=90.0, azi0=113.73626, azi12=45.0, lat0=60, lat1=30.0, lat2=49.634582, lon0=0, lon1=0.0, lon2=25.767876, name='PlumbTo', s02=1997960.116871, s12=3083112.636236), iteration=7, exact=True, est=1000000.0

1123# PlumbTo(a02=17.967658, a12=27.74256, at=90.0, azi0=113.73626, azi12=45.0, lat0=60, lat1=30.0, lat2=49.634582, lon0=0, lon1=0.0, lon2=25.767876, name='PlumbTo', s02=1997960.116871, s12=3083112.636236), iteration=7

1124

1125# RhumbAux(RAorder=None, TMorder=6, ellipsoid=Ellipsoid(name='WGS84', a=6378137, b=6356752.31424518, f_=298.25722356, f=0.00335281, f2=0.00336409, n=0.00167922, e=0.08181919, e2=0.00669438, e21=0.99330562, e22=0.0067395, e32=0.00335843, A=6367449.14582341, L=10001965.72931272, R1=6371008.77141506, R2=6371007.18091847, R3=6371000.79000916, Rbiaxial=6367453.63451633, Rtriaxial=6372797.5559594), exact=True)

1126# JFK NE lat2=71.688899882813, lon2=0.255519824423, S12=44095641862956.1, rel errors: 4e-16, 2e-13, 4e-16

1127# JFK-LHR azi12=77.768389710256, s12=5771083.383 S12=37395209100030.4, rel errors: 5e-16, 3e-16, 8e-16

1128# JFK-NRT azi12=-92.388887981700, s12=12782581.068 S12=-63760642939072.5, rel errors: 0, 1e-16, 7e-16

1129

1130# Rhumb(RAorder=6, TMorder=6, ellipsoid=Ellipsoid(name='WGS84', a=6378137, b=6356752.31424518, f_=298.25722356, f=0.00335281, f2=0.00336409, n=0.00167922, e=0.08181919, e2=0.00669438, e21=0.99330562, e22=0.0067395, e32=0.00335843, A=6367449.14582341, L=10001965.72931272, R1=6371008.77141506, R2=6371007.18091847, R3=6371000.79000916, Rbiaxial=6367453.63451633, Rtriaxial=6372797.5559594), exact=True)

1131# JFK NE lat2=71.688899882813, lon2=0.255519824423, S12=44095641862956.1, rel errors: 2e-16, 1e-13, 5e-16

1132# JFK-LHR azi12=77.768389710256, s12=5771083.383 S12=37395209100030.4, rel errors: 4e-16, 3e-16, 6e-16

1133# JFK-NRT azi12=-92.388887981700, s12=12782581.068 S12=-63760642939072.5, rel errors: 0, 1e-16, 1e-16

1134

1135# **) MIT License

1136#

1137# Copyright (C) 2022-2026 -- mrJean1 at Gmail -- All Rights Reserved.

1138#

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

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

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

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

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

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

1145#

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

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

1148#

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

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

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

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

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

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

1155# OTHER DEALINGS IN THE SOFTWARE.