1

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

3

4 u'''A pure Python version of I{Karney}'s C++ classes U{Rhumb

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

6<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1RhumbLine.html>} from

7 I{GeographicLib version 2.0}.

8

9 Class L{RhumbLine} has been enhanced with methods C{intersection2} and C{nearestOn4} to iteratively

10 find the intersection of two rhumb lines, respectively the nearest point on a rumb line along a

11 geodesic or perpendicular rhumb line.

12

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

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

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

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

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

18

19 Copyright (C) U{Charles Karney<mailto:Charles@Karney.com>} (2014-2022) and licensed under the MIT/X11

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

21'''

22# make sure int/int division yields float quotient

23 from __future__ import division as _; del _ # PYCHOK semicolon

24

25 from pygeodesy.basics import copysign0, neg, unsigned0, _zip

26 from pygeodesy.constants import NAN, PI_2, _0_0s, _0_0, _0_5, \

27 _1_0, _2_0, _4_0, _720_0, _over

28 from pygeodesy.errors import itemsorted, RhumbError, _Xorder

29 from pygeodesy.fmath import hypot, hypot1

30# from pygeodesy.fsums import fsum1f_ # _MODS

31 from pygeodesy.interns import NN, _COMMASPACE_

32 from pygeodesy.karney import _atan2d, Caps, _diff182, _EWGS84, GDict, \

33 _GTuple, _norm180

34 from pygeodesy.ktm import KTransverseMercator, _Xs, \

35 _AlpCoeffs, _BetCoeffs # PYCHOK used!

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

37 from pygeodesy.props import deprecated_method, Property, Property_RO, property_RO

38 from pygeodesy.rhumbBase import RhumbBase, RhumbLineBase, pairs, _update_all_rls

39# from pygeodesy.streprs import pairs # from .rhumbBase

40 from pygeodesy.units import Int

41 from pygeodesy.utily import sincos2_

42

43 from math import asinh, atan, cos, cosh, fabs, radians, sin, sinh, sqrt, tan

44

45__all__ = _ALL_LAZY.rhumbx

46__version__ = '23.08.05'

47

48

49 class Rhumb(RhumbBase):

50 '''Class to solve the I{direct} and I{inverse rhumb} problems, based on

51 I{elliptic functions} or I{Krüger} series expansion.

52

53 @see: The U{Detailed Description<https://GeographicLib.SourceForge.io/C++/doc/

54 classGeographicLib_1_1Rhumb.html>} of I{Karney}'s C++ C{Rhumb Class}.

55 '''

56 _mRA = 6 # see .RAorder

57

58 def __init__(self, a_earth=_EWGS84, f=None, exact=True, name=NN, **RA_TMorder):

59 '''New C{rhumbx.Rhumb}.

60

61 @kwarg a_earth: This rhumb's earth model (L{Ellipsoid}, L{Ellipsoid2},

62 L{a_f2Tuple}, L{Datum}, 2-tuple C{(a, f)}) or the

63 (equatorial) radius (C{scalar}).

64 @kwarg f: The ellipsoid's flattening (C{scalar}), iff B{C{a_earth}} is

65 a C{scalar}, ignored otherwise.

66 @kwarg exact: If C{True}, use an addition theorem for elliptic integrals

67 to compute I{Divided differences}, otherwise use the I{Krüger}

68 series expansion (C{bool} or C{None}), see also properties

69 C{exact} and C{TMorder}.

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

71 @kwarg RA_TMorder: Optional keyword arguments B{C{RAorder}} and B{C{TMorder}}

72 to set the respective C{order}, see properties C{RAorder}

73 and C{TMorder} and method C{orders}.

74

75 @raise RhumbError: Invalid B{C{a_earth}}, B{C{f}} or B{C{RA_TMorder}}.

76 '''

77 RhumbBase.__init__(self, a_earth, f, exact, name)

78 if RA_TMorder:

79 self.orders(**RA_TMorder)

80

81 @Property_RO

82 def _A2(self): # Conformal2RectifyingCoeffs

83 m = self.TMorder

84 return _Xs(_AlpCoeffs, m, self.ellipsoid), m

85

86 @Property_RO

87 def _B2(self): # Rectifying2ConformalCoeffs

88 m = self.TMorder

89 return _Xs(_BetCoeffs, m, self.ellipsoid), m

90

91 def _DConformal2Rectifying(self, x, y): # radians

92 return _1_0 + (_sincosSeries(True, x, y, *self._A2) if self.f else _0_0)

93

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

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

96

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

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

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

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

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

102

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

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

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

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

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

108

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

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

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

112

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

114 taken to be C{epsilon}**-2 (where C{epsilon} is 2.0**-52.

115 This position is extremely close to the actual pole and

116 allows the calculation to be carried out in finite terms.

117 '''

118 rl = RhumbLine(self, lat1, lon1, azi12, caps=Caps.LINE_OFF,

119 name=self.name)

120 return rl.Position(s12, outmask | self._debug) # lat2, lon2, S12

121

122 @deprecated_method

123 def Direct7(self, lat1, lon1, azi12, s12, outmask=Caps.LATITUDE_LONGITUDE_AREA):

124 '''DEPRECATED, use method L{Rhumb.Direct8}.

125

126 @return: A I{DEPRECATED} L{Rhumb7Tuple}.

127 '''

128 return self.Direct8(lat1, lon1, azi12, s12, outmask=outmask)._to7Tuple()

129

130 def _DIsometrict(self, phix, phiy, tphix, tphiy, _Dtan_phix_phiy):

131 E = self.ellipsoid

132 return _Dtan_phix_phiy * _Dasinh(tphix, tphiy) - \

133 _Dsin(phix, phiy) * _DeatanhE(sin(phix), sin(phiy), E)

134

135 def _DIsometric2Rectifyingd(self, psix, psiy): # degrees

136 if self.exact:

137 E = self.ellipsoid

138 phix, phiy, tphix, tphiy = _Eaux4(E.auxIsometric, psix, psiy)

139 t = _Dtant(phix - phiy, tphix, tphiy)

140 r = _over(self._DRectifyingt( tphix, tphiy, t),

141 self._DIsometrict(phix, phiy, tphix, tphiy, t))

142 else:

143 x, y = radians(psix), radians(psiy)

144 r = self._DConformal2Rectifying(_gd(x), _gd(y)) * _Dgd(x, y)

145 return r

146

147 def _DRectifyingt(self, tphix, tphiy, _Dtan_phix_phiy):

148 E = self.ellipsoid

149 tbetx = E.f1 * tphix

150 tbety = E.f1 * tphiy

151 return (E.f1 * _Dtan_phix_phiy * E.b * PI_2

152 * _DfEt( tbetx, tbety, self._eF)

153 * _Datan(tbetx, tbety)) / E.L

154

155 def _DRectifying2Conformal(self, x, y): # radians

156 return _1_0 - (_sincosSeries(True, x, y, *self._B2) if self.f else _0_0)

157

158 def _DRectifying2Isometricd(self, mux, muy): # degrees

159 E = self.ellipsoid

160 phix, phiy, tphix, tphiy = _Eaux4(E.auxRectifying, mux, muy)

161 if self.exact:

162 t = _Dtant(phix - phiy, tphix, tphiy)

163 r = _over(self._DIsometrict(phix, phiy, tphix, tphiy, t),

164 self._DRectifyingt( tphix, tphiy, t))

165 else:

166 r = self._DRectifying2Conformal(radians(mux), radians(muy)) * \

167 _Dgdinv(E.es_taupf(tphix), E.es_taupf(tphiy))

168 return r

169

170 @Property_RO

171 def _eF(self):

172 '''(INTERNAL) Get the ellipsoid's elliptic function.

173 '''

174 # .k2 = 0.006739496742276434

175 return self._E._elliptic_e12 # _MODS.elliptic.Elliptic(-self._E._e12)

176

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

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

179

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

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

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

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

184

185 @return: L{GDict} with 5 to 8 items C{azi12, s12, a12, S12,

186 lat1, lon1, lat2, lon2}, the rhumb line's azimuth C{azi12}

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

188 distance C{s12} and rhumb angle C{a12} between both points

189 in C{meter} respectively C{degrees} and the area C{S12}

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

191

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

193 on opposite meridians, there are two shortest rhumb lines

194 and the East-going one is chosen.

195

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

197 taken to be C{epsilon}**-2 (where C{epsilon} is 2.0**-52).

198 This position is extremely close to the actual pole and

199 allows the calculation to be carried out in finite terms.

200 '''

201 r, Cs = GDict(name=self.name), Caps

202 if (outmask & Cs.AZIMUTH_DISTANCE_AREA):

203 r.set_(lat1=lat1, lon1=lon1, lat2=lat2, lon2=lon2)

204 E = self.ellipsoid

205 psi1 = E.auxIsometric(lat1)

206 psi2 = E.auxIsometric(lat2)

207 psi12 = psi2 - psi1

208 lon12, _ = _diff182(lon1, lon2)

209 if (outmask & Cs.AZIMUTH):

210 r.set_(azi12=_atan2d(lon12, psi12))

211 if (outmask & Cs.DISTANCE):

212 a12 = hypot(lon12, psi12) * self._DIsometric2Rectifyingd(psi2, psi1)

213 s12 = a12 * E._L_90

214 r.set_(s12=s12, a12=copysign0(a12, s12))

215 if (outmask & Cs.AREA):

216 r.set_(S12=self._S12d(lon12, psi2, psi1))

217 if ((outmask | self._debug) & Cs._DEBUG_INVERSE): # PYCHOK no cover

218 r.set_(a=E.a, f=E.f, f1=E.f1, L=E.L,

219 b=E.b, e=E.e, e2=E.e2, k2=self._eF.k2,

220 lon12=lon12, psi1=psi1, exact=self.exact,

221 psi12=psi12, psi2=psi2)

222 return r

223

224# def Inverse3(self, lat1, lon1, lat2, lon2): # PYCHOK outmask

225# '''Return the distance in C{meter} and the forward and

226# reverse azimuths (initial and final bearing) in C{degrees}.

227#

228# @return: L{Distance3Tuple}C{(distance, initial, final)}.

229# '''

230# r = self.Inverse(lat1, lon1, lat2, lon2)

231# return Distance3Tuple(r.s12, r.azi12, r.azi12)

232

233 @deprecated_method

234 def Inverse7(self, lat1, lon1, azi12, s12, outmask=Caps.AZIMUTH_DISTANCE_AREA):

235 '''DEPRECATED, use method L{Rhumb.Inverse8}.

236

237 @return: A I{DEPRECATED} L{Rhumb7Tuple}.

238 '''

239 return self.Inverse8(lat1, lon1, azi12, s12, outmask=outmask)._to7Tuple()

240

241 def _meanSinXi(self, x, y): # radians

242 s = _Dlog(cosh(x), cosh(y)) * _Dcosh(x, y)

243 if self.f:

244 s += _sincosSeries(False, _gd(x), _gd(y), *self._RA2) * _Dgd(x, y)

245 return s

246

247 @deprecated_method

248 def orders(self, RAorder=None, TMorder=None): # PYCHOK expected

249 '''DEPRECATED, use properties C{RAorder} and/or C{TMorder}.

250

251 Get and set the I{RAorder} and/or I{TMorder}.

252

253 @kwarg RAorder: I{Rhumb Area} order (C{int}, 4, 5, 6, 7

254 or 8).

255 @kwarg TMorder: I{Transverse Mercator} order (C{int}, 4,

256 5, 6, 7 or 8).

257

258 @return: L{RhumbOrder2Tuple}C{(RAorder, TMorder)} with

259 the previous C{RAorder} and C{TMorder} setting.

260 '''

261 t = RhumbOrder2Tuple(self.RAorder, self.TMorder)

262 if RAorder not in (None, t.RAorder): # PYCHOK attr

263 self.RAorder = RAorder

264 if TMorder not in (None, t.TMorder): # PYCHOK attr

265 self.TMorder = TMorder

266 return t

267

268 @Property_RO

269 def _RA2(self):

270 # for WGS84: (0, -0.0005583633519275459, -3.743803759172812e-07, -4.633682270824446e-10,

271 # RAorder 6: -7.709197397676237e-13, -1.5323287106694307e-15, -3.462875359099873e-18)

272 m = self.RAorder

273 return _Xs(_RACoeffs, m, self.ellipsoid, RA=True), m

274

275 @Property

276 def RAorder(self):

277 '''Get the I{Rhumb Area} order (C{int}, 4, 5, 6, 7 or 8).

278 '''

279 return self._mRA

280

281 @RAorder.setter # PYCHOK setter!

282 def RAorder(self, order):

283 '''Set the I{Rhumb Area} order (C{int}, 4, 5, 6, 7 or 8).

284 '''

285 n = _Xorder(_RACoeffs, RhumbError, RAorder=order)

286 if self._mRA != n:

287 _update_all_rls(self)

288 self._mRA = n

289

290 @Property_RO

291 def _RhumbLine(self):

292 '''(INTERNAL) Get this module's C{RhumbLine} class.

293 '''

294 return RhumbLine

295

296 def _S12d(self, lon12, psi2, psi1): # degrees

297 '''(INTERNAL) Compute the area C{S12}.

298 '''

299 r = (self.ellipsoid.areax if self.exact else

300 self.ellipsoid.area) * lon12 / _720_0

301 r *= self._meanSinXi(radians(psi2), radians(psi1))

302 return r

303

304 @Property

305 def TMorder(self):

306 '''Get the I{Transverse Mercator} order (C{int}, 4, 5, 6, 7 or 8).

307 '''

308 return self._mTM

309

310 @TMorder.setter # PYCHOK setter!

311 def TMorder(self, order):

312 '''Set the I{Transverse Mercator} order (C{int}, 4, 5, 6, 7 or 8).

313

314 @note: Setting C{TMorder} turns property C{exact} off.

315 '''

316 self.exact = self._TMorder(order)

317

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

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

320

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

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

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

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

325

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

327 '''

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

329 exact=self.exact, TMorder=self.TMorder)

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

331

332

333 class RhumbLine(RhumbLineBase):

334 '''Compute one or several points on a single rhumb line.

335

336 Class C{RhumbLine} facilitates the determination of points on

337 a single rhumb line. The starting point (C{lat1}, C{lon1})

338 and the azimuth C{azi12} are specified once.

339

340 Method C{RhumbLine.Position} returns the location of an other

341 point at distance C{s12} along and the area C{S12} under the

342 rhumb line.

343

344 Method C{RhumbLine.intersection2} finds the intersection between

345 two rhumb lines.

346

347 Method C{RhumbLine.nearestOn4} computes the nearest point on and

348 the distance to a rhumb line in different ways.

349 '''

350 _Rhumb = Rhumb # rhumbx.Rhumb

351

352 def __init__(self, rhumb, lat1=0, lon1=0, azi12=None, **caps_name): # PYCHOK signature

353 '''New C{rhumbx.RhumbLine}.

354

355 @arg rhumb: The rhumb reference (C{rhumbx.Rhumb}).

356 @kwarg lat1: Latitude of the start point (C{degrees90}).

357 @kwarg lon1: Longitude of the start point (C{degrees180}).

358 @kwarg azi12: Azimuth of this rhumb line (compass C{degrees}).

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

360 C{B{caps}=0}, a bit-or'ed combination of L{Caps}

361 values specifying the required capabilities. Include

362 C{Caps.LINE_OFF} if updates to the B{C{rhumb}} should

363 I{not} be reflected in this rhumb line.

364 '''

365 RhumbLineBase.__init__(self, rhumb, lat1, lon1, azi12, **caps_name)

366

367 @Property_RO

368 def _mu1(self):

369 '''(INTERNAL) Get the I{rectifying auxiliary} latitude C{mu} (C{degrees}).

370 '''

371 return self.ellipsoid.auxRectifying(self.lat1)

372

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

374 '''Compute a point at a distance on this rhumb line.

375

376 @arg s12: The distance along this rhumb between its point and

377 the other point (C{meters}), can be negative.

378 @kwarg outmask: Bit-or'ed combination of L{Caps} values specifying

379 the quantities to be returned.

380

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

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

383 C{lon2} of the point in C{degrees}, the rhumb angle C{a12}

384 in C{degrees} from the start point of and the area C{S12}

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

386

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

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

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

390

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

392 taken to be C{epsilon}**-2 (where C{epsilon} is 2**-52).

393 This position is extremely close to the actual pole and

394 allows the calculation to be carried out in finite terms.

395 '''

396 r, Cs = GDict(name=self.name), Caps

397 if (outmask & Cs.LATITUDE_LONGITUDE_AREA):

398 E, R = self.ellipsoid, self.rhumb

399 a12 = s12 / E._L_90

400 mu12 = self._calp * a12

401 mu2, x90 = self._mu22(mu12, self._mu1)

402 if x90: # PYCHOK no cover

403 lat2 = E.auxRectifying(mu2, inverse=True)

404 lon2 = NAN

405 if (outmask & Cs.AREA):

406 r.set_(S12=NAN)

407 else:

408 psi2 = self._psi1

409 if self._calp:

410 lat2 = E.auxRectifying(mu2, inverse=True)

411 psi12 = R._DRectifying2Isometricd(mu2,

412 self._mu1) * mu12

413 lon2 = psi12 * self._salp / self._calp

414 psi2 += psi12

415 else: # PYCHOK no cover

416 lat2 = self.lat1

417 lon2 = self._salp * s12 / self._r1rad

418 if (outmask & Cs.AREA):

419 S12 = R._S12d(lon2, self._psi1, psi2)

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

421 if (outmask & Cs.LONGITUDE):

422 if (outmask & Cs.LONG_UNROLL):

423 lon2 += self.lon1

424 else:

425 lon2 = _norm180(self._lon12 + lon2)

426 r.set_(azi12=self.azi12, s12=s12, a12=a12)

427 if (outmask & Cs.LATITUDE):

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

429 if (outmask & Cs.LONGITUDE):

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

431 if ((outmask | self._debug) & Cs._DEBUG_DIRECT_LINE): # PYCHOK no cover

432 r.set_(a=E.a, f=E.f, f1=E.f1, L=E.L, exact=R.exact,

433 b=E.b, e=E.e, e2=E.e2, k2=R._eF.k2,

434 calp=self._calp, mu1 =self._mu1, mu12=mu12,

435 salp=self._salp, psi1=self._psi1, mu2=mu2)

436 return r

437

438 @Property_RO

439 def _psi1(self):

440 '''(INTERNAL) Get the I{isometric auxiliary} latitude C{psi} (C{degrees}).

441 '''

442 return self.ellipsoid.auxIsometric(self.lat1)

443

444 @property_RO

445 def RAorder(self):

446 '''Get this rhumb line's I{Rhumb Area} order (C{int}, 4, 5, 6, 7 or 8).

447 '''

448 return self.rhumb.RAorder

449

450 @Property_RO

451 def _r1rad(self): # PYCHOK no cover

452 '''(INTERNAL) Get this rhumb line's parallel I{circle radius} (C{meter}).

453 '''

454 return radians(self.ellipsoid.circle4(self.lat1).radius)

455

456

457 class RhumbOrder2Tuple(_GTuple):

458 '''2-Tuple C{(RAorder, TMorder)} with a I{Rhumb Area} and

459 I{Transverse Mercator} order, both C{int}, DEPRECATED.

460 '''

461 _Names_ = (Rhumb.RAorder.name, Rhumb.TMorder.name)

462 _Units_ = ( Int, Int)

463

464

465# Use I{Divided Differences} to determine (mu2 - mu1) / (psi2 - psi1) accurately.

466# Definition: _Df(x,y,d) = (f(x) - f(y)) / (x - y), @see W. M. Kahan & R. J.

467# Fateman, "Symbolic computation of Divided Differences", SIGSAM Bull. 33(3),

468# 7-28 (1999). U{ACM<https://DL.ACM.org/doi/pdf/10.1145/334714.334716> and @see

469# U{UCB<https://www.CS.Berkeley.edu/~fateman/papers/divdiff.pdf>}, Dec 8, 1999.

470

471 def _Dasinh(x, y):

472 hx = hypot1(x)

473 d = x - y

474 if d:

475 hx *= y

476 hy = x * hypot1(y)

477 t = (d * (x + y) / (hy + hx)) if (x * y) > 0 else (hy - hx)

478 r = asinh(t) / d

479 else:

480 r = _1_0 / hx

481 return r

482

483

484 def _Datan(x, y):

485 xy = x * y

486 r = xy + _1_0

487 d = x - y

488 if d: # 2 * xy > -1 == 2 * xy + 1 > 0 == xy + r > 0 == xy > -r

489 r = (atan(d / r) if xy > -r else (atan(x) - atan(y))) / d

490 else:

491 r = _1_0 / r

492 return r

493

494

495 def _Dcosh(x, y):

496 return _Dsincos(x, y, sinh, sinh)

497

498

499 def _DeatanhE(x, y, E): # see .albers._Datanhee

500 # Deatanhe(x, y) = eatanhe((x - y) / (1 - e^2 * x * y)) / (x - y)

501 e = _1_0 - E.e2 * x * y

502 if e: # assert not isnear0(e)

503 d = x - y

504 e = (E._es_atanh(d / e) / d) if d else (E.e2 / e)

505 return e

506

507

508 def _DfEt(tx, ty, eF): # tangents

509 # eF = Elliptic(-E.e12) # -E.e2 / (1 - E.e2)

510 r, x, y, = _1_0, atan(tx), atan(ty)

511 d = x - y

512 if (x * y) > 0:

513 # See U{DLMF<https://DLMF.NIST.gov/19.11>}: 19.11.2 and 19.11.4

514 # letting theta -> x, phi -> -y, psi -> z

515 # (E(x) - E(y)) / d = E(z)/d - k2 * sin(x) * sin(y) * sin(z)/d

516 # tan(z/2) = (sin(x)*Delta(y) - sin(y)*Delta(x)) / (cos(x) + cos(y))

517 # = d * Dsin(x,y) * (sin(x) + sin(y))/(cos(x) + cos(y)) /

518 # (sin(x)*Delta(y) + sin(y)*Delta(x))

519 # = t = d * Dt

520 # sin(z) = 2*t/(1+t^2); cos(z) = (1-t^2)/(1+t^2)

521 # Alt (this only works for |z| <= pi/2 -- however, this conditions

522 # holds if x*y > 0):

523 # sin(z) = d * Dsin(x,y) * (sin(x) + sin(y)) /

524 # (sin(x)*cos(y)*Delta(y) + sin(y)*cos(x)*Delta(x))

525 # cos(z) = sqrt((1-sin(z))*(1+sin(z)))

526 sx, cx, sy, cy = sincos2_(x, y)

527 D = (cx + cy) * (eF.fDelta(sy, cy) * sx +

528 eF.fDelta(sx, cx) * sy)

529 D = (sx + sy) * _Dsin(x, y) / D

530 t = D * d

531 t2 = _1_0 + t**2

532 D *= _2_0 / t2

533 s = D * d

534 if s:

535 c = (t + _1_0) * (_1_0 - t) / t2

536 r = eF.fE(s, c, eF.fDelta(s, c)) / s

537 r = D * (r - eF.k2 * sx * sy)

538 elif d:

539 r = (eF.fE(x) - eF.fE(y)) / d

540 return r

541

542

543 def _Dgd(x, y):

544 return _Datan(sinh(x), sinh(y)) * _Dsinh(x, y)

545

546

547 def _Dgdinv(x, y): # x, y are tangents

548 return _Dasinh(x, y) / _Datan(x, y)

549

550

551 def _Dlog(x, y):

552 d = (x - y) * _0_5

553 # Changed atanh(t / (x + y)) to asinh(t / (2 * sqrt(x*y))) to

554 # avoid taking atanh(1) when x is large and y is 1. This also

555 # fixes bogus results being returned for the area when an endpoint

556 # is at a pole. N.B. this routine is invoked with positive x

557 # and y, so the sqrt is always taken of a positive quantity.

558 return (asinh(d / sqrt(x * y)) / d) if d else (_1_0 / x)

559

560

561 def _Dsin(x, y):

562 return _Dsincos(x, y, sin, cos)

563

564

565 def _Dsincos(x, y, sin_, cos_):

566 r = cos_((x + y) * _0_5)

567 d = (x - y) * _0_5

568 if d:

569 r *= sin_(d) / d

570 return r

571

572

573 def _Dsinh(x, y):

574 return _Dsincos(x, y, sinh, cosh)

575

576

577 def _Dtan(x, y): # PYCHOK no cover

578 return _Dtant(x - y, tan(x), tan(y))

579

580

581 def _Dtant(dxy, tx, ty):

582 txy = tx * ty

583 r = txy + _1_0

584 if dxy: # 2 * txy > -1 == 2 * txy + 1 > 0 == txy + r > 0 == txy > -r

585 r = ((tan(dxy) * r) if txy > -r else (tx - ty)) / dxy

586 return r

587

588

589 def _Eaux4(E_aux, mu_psi_x, mu_psi_y): # degrees

590 # get inverse auxiliary lats in radians and tangents

591 phix = radians(E_aux(mu_psi_x, inverse=True))

592 phiy = radians(E_aux(mu_psi_y, inverse=True))

593 return phix, phiy, tan(phix), tan(phiy)

594

595

596 def _gd(x):

597 return atan(sinh(x))

598

599

600 def _sincosSeries(sinp, x, y, C, n):

601 # N.B. C[] has n+1 elements of which

602 # C[0] is ignored and n >= 0

603 # Use Clenshaw summation to evaluate

604 # m = (g(x) + g(y)) / 2 -- mean value

605 # s = (g(x) - g(y)) / (x - y) -- average slope

606 # where

607 # g(x) = sum(C[j] * SC(2 * j * x), j = 1..n)

608 # SC = sinp ? sin : cos

609 # CS = sinp ? cos : sin

610 # ...

611 d, _neg = (x - y), neg

612 sp, cp, sd, cd = sincos2_(x + y, d)

613 sd = (sd / d) if d else _1_0

614 s = _neg(sp * sd) # negative

615 # 2x2 matrices in row-major order

616 a1 = s * d**2

617 a2 = s * _4_0

618 a0 = a3 = _2_0 * cp * cd # m

619 b2 = b1 = _0_0s(4)

620 if n > 0:

621 b1 = C[n], _0_0, _0_0, C[n]

622

623 _fsum = _MODS.fsums.fsum1f_

624 for j in range(n - 1, 0, -1): # C[0] unused

625 b1, b2, Cj = b2, b1, C[j]

626 # b1 = a * b2 - b1 + C[j] * I

627 m0, m1, m2, m3 = b2

628 n0, n1, n2, n3 = map(_neg, b1)

629 b1 = (_fsum(a0 * m0, a1 * m2, n0, Cj),

630 _fsum(a0 * m1, a1 * m3, n1),

631 _fsum(a2 * m0, a3 * m2, n2),

632 _fsum(a2 * m1, a3 * m3, n3, Cj))

633 # Here are the full expressions for m and s

634 # f01, f02, f11, f12 = (0, 0, cd * sp, 2 * sd * cp) if sinp else \

635 # (1, 0, cd * cp, -2 * sd * sp)

636 # m = -b2[1] * f02 + (C[0] - b2[0]) * f01 + b1[0] * f11 + b1[1] * f12

637 # s = -b2[2] * f01 + (C[0] - b2[3]) * f02 + b1[2] * f11 + b1[3] * f12

638 cd *= b1[2]

639 sd *= b1[3] * _2_0

640 s = _fsum(cd * sp, sd * cp) if sinp else \

641 _fsum(cd * cp, _neg(sd * sp), _neg(b2[2]))

642 return s

643

644

645_RACoeffs = { # Generated by Maxima on 2015-05-15 08:24:04-04:00

646 4: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 4

647 691, 7860, -20160, 18900, 0, 56700, # R[0]/n^0, polynomial(n), order 4

648 1772, -5340, 6930, -4725, 14175, # R[1]/n^1, polynomial(n), order 3

649 -1747, 1590, -630, 4725, # PYCHOK R[2]/n^2, polynomial(n), order 2

650 104, -31, 315, # R[3]/n^3, polynomial(n), order 1

651 -41, 420), # PYCHOK R[4]/n^4, polynomial(n), order 0, count = 20

652 5: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 5

653 -79036, 22803, 259380, -665280, 623700, 0, 1871100, # PYCHOK R[0]/n^0, polynomial(n), order 5

654 41662, 58476, -176220, 228690, -155925, 467775, # PYCHOK R[1]/n^1, polynomial(n), order 4

655 18118, -57651, 52470, -20790, 155925, # PYCHOK R[2]/n^2, polynomial(n), order 3

656 -23011, 17160, -5115, 51975, # PYCHOK R[3]/n^3, polynomial(n), order 2

657 5480, -1353, 13860, # PYCHOK R[4]/n^4, polynomial(n), order 1

658 -668, 5775), # PYCHOK R[5]/n^5, polynomial(n), order 0, count = 27

659 6: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 6

660 128346268, -107884140, 31126095, 354053700, -908107200, 851350500, 0, 2554051500, # R[0]/n^0, polynomial(n), order 6

661 -114456994, 56868630, 79819740, -240540300, 312161850, -212837625, 638512875, # PYCHOK R[1]/n^1, polynomial(n), order 5

662 51304574, 24731070, -78693615, 71621550, -28378350, 212837625, # R[2]/n^2, polynomial(n), order 4

663 1554472, -6282003, 4684680, -1396395, 14189175, # R[3]/n^3, polynomial(n), order 3

664 -4913956, 3205800, -791505, 8108100, # PYCHOK R[4]/n^4, polynomial(n), order 2

665 1092376, -234468, 2027025, # R[5]/n^5, polynomial(n), order 1

666 -313076, 2027025), # PYCHOK R[6]/n^6, polynomial(n), order 0, count = 35

667 7: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 7

668 -317195588, 385038804, -323652420, 93378285, 1062161100, -2724321600, 2554051500, 0, 7662154500, # PYCHOK R[0]/n^0, polynomial(n), order 7

669 258618446, -343370982, 170605890, 239459220, -721620900, 936485550, -638512875, 1915538625, # PYCHOK R[1]/n^1, polynomial(n), order 6

670 -248174686, 153913722, 74193210, -236080845, 214864650, -85135050, 638512875, # PYCHOK R[2]/n^2, polynomial(n), order 5

671 114450437, 23317080, -94230045, 70270200, -20945925, 212837625, # PYCHOK R[3]/n^3, polynomial(n), order 4

672 15445736, -103193076, 67321800, -16621605, 170270100, # PYCHOK R[4]/n^4, polynomial(n), order 3

673 -27766753, 16385640, -3517020, 30405375, # PYCHOK R[4]/n^4, polynomial(n), order 3

674 4892722, -939228, 6081075, # PYCHOK R[4]/n^4, polynomial(n), order 3

675 -3189007, 14189175), # PYCHOK R[7]/n^7, polynomial(n), order 0, count = 44

676 8: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 8

677 71374704821, -161769749880, 196369790040, -165062734200, 47622925350, 541702161000, -1389404016000, 1302566265000, 0, 3907698795000, # R[0]/n^0, polynomial(n), order 8

678 -13691187484, 65947703730, -87559600410, 43504501950, 61062101100, -184013329500, 238803815250, -162820783125, 488462349375, # PYCHOK R[1]/n^1, polynomial(n), order 7

679 30802104839, -63284544930, 39247999110, 18919268550, -60200615475, 54790485750, -21709437750, 162820783125, # R[2]/n^2, polynomial(n), order 6

680 -8934064508, 5836972287, 1189171080, -4805732295, 3583780200, -1068242175, 10854718875, # PYCHOK R[3]/n^3, polynomial(n), order 5

681 50072287748, 3938662680, -26314234380, 17167059000, -4238509275, 43418875500, # R[4]/n^4, polynomial(n), order 4

682 359094172, -9912730821, 5849673480, -1255576140, 10854718875, # R[5]/n^5, polynomial(n), order 3

683 -16053944387, 8733508770, -1676521980, 10854718875, # PYCHOK R[6]/n^6, polynomial(n), order 2

684 930092876, -162639357, 723647925, # R[7]/n^7, polynomial(n), order 1

685 -673429061, 1929727800) # PYCHOK R[8]/n^8, polynomial(n), order 0, count = 54

686}

687

688__all__ += _ALL_DOCS(Caps, Rhumb, RhumbLine)

689

690 if __name__ == '__main__':

691

692 from pygeodesy.lazily import printf

693

694 def _re(fmt, r3, x3):

695 e3 = []

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

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

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

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

700

701 # <https://GeographicLib.SourceForge.io/cgi-bin/RhumbSolve> version 2.0

702 rhumb = Rhumb(exact=True) # WGS84 default

703 printf('# %r\n', rhumb)

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

705 _re('# JFK NE lat2=%.8f, lon2=%.8f, S12=%.1f', (r.lat2, r.lon2, r.S12), (71.68889988, 0.25551982, 44095641862956.148438))

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

707 _re('# JFK-LHR azi12=%.8f, s12=%.3f S12=%.1f', (r.azi12, r.s12, r.S12), (77.76838971, 5771083.383328, 37395209100030.367188))

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

709 _re('# JFK-NRT azi12=%.8f, s12=%.3f S12=%.1f', (r.azi12, r.s12, r.S12), (-92.388887981699639, 12782581.0676841792, -63760642939072.492))

710

711# % python3 -m pygeodesy.rhumbx

712

713# 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)

714

715# JFK NE lat2=71.68889988, lon2=0.25551982, S12=44095641862956.1 rel errors: 4e-11, 2e-08, 5e-16

716# JFK-LHR azi12=77.76838971, s12=5771083.383 S12=37395209100030.4 rel errors: 3e-12, 5e-15, 0

717# JFK-NRT azi12=-92.38888798, s12=12782581.068 S12=-63760642939072.5 rel errors: 2e-16, 3e-16, 0

718

719# **) MIT License

720#

721# Copyright (C) 2022-2023 -- mrJean1 at Gmail -- All Rights Reserved.

722#

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

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

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

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

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

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

729#

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

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

732#

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

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

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

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

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

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

739# OTHER DEALINGS IN THE SOFTWARE.