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:Karney@Alum.MIT.edu>} (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.ellipsoids import _EWGS84 # from .karney

29 from pygeodesy.errors import itemsorted, RhumbError, _Xorder

30 from pygeodesy.fmath import hypot, hypot1

31# from pygeodesy.fsums import fsum1f_ # _MODS

32 from pygeodesy.interns import NN, _COMMASPACE_

33 from pygeodesy.karney import _atan2d, Caps, _diff182, GDict, _GTuple, \

34 _norm180, _EWGS84

35 from pygeodesy.ktm import KTransverseMercator, _Xs, \

36 _AlpCoeffs, _BetCoeffs # PYCHOK used!

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

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

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

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

41 from pygeodesy.units import Int

42 from pygeodesy.utily import sincos2_

43

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

45

46__all__ = _ALL_LAZY.rhumbx

47__version__ = '23.08.20'

48

49

50 class Rhumb(RhumbBase):

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

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

53

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

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

56 '''

57 _mRA = 6 # see .RAorder

58

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

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

61

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

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

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

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

66 a C{scalar}, ignored otherwise.

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

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

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

70 C{exact} and C{TMorder}.

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

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

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

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

75

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

77 '''

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

79 if RA_TMorder:

80 self.orders(**RA_TMorder)

81

82 @Property_RO

83 def _A2(self): # Conformal2RectifyingCoeffs

84 m = self.TMorder

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

86

87 @Property_RO

88 def _B2(self): # Rectifying2ConformalCoeffs

89 m = self.TMorder

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

91

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

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

94

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

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

97

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

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

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

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

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

103

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

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

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

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

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

109

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

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

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

113

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

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

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

117 carried out in finite terms.

118 '''

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

120 name=self.name)

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

122

123 @deprecated_method

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

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

126

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

128 '''

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

130

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

132 E = self.ellipsoid

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

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

135

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

137 if self.exact:

138 E = self.ellipsoid

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

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

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

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

143 else:

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

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

146 return r

147

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

149 E = self.ellipsoid

150 tbetx = E.f1 * tphix

151 tbety = E.f1 * tphiy

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

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

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

155

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

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

158

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

160 E = self.ellipsoid

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

162 if self.exact:

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

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

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

166 else:

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

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

169 return r

170

171 @Property_RO

172 def _eF(self):

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

174 '''

175 # .k2 = 0.006739496742276434

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

177

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

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

180

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

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

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

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

185

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

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

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

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

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

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

192

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

194 on opposite meridians, there are two shortest rhumb lines

195 and the East-going one is chosen.

196

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

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

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

200 carried out in finite terms.

201 '''

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

203 if (outmask & Cs.AZIMUTH_DISTANCE_AREA):

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

205 E = self.ellipsoid

206 psi1 = E.auxIsometric(lat1)

207 psi2 = E.auxIsometric(lat2)

208 psi12 = psi2 - psi1

209 lon12, _ = _diff182(lon1, lon2)

210 if (outmask & Cs.AZIMUTH):

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

212 if (outmask & Cs.DISTANCE):

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

214 s12 = a12 * E._L_90

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

216 if (outmask & Cs.AREA):

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

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

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

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

221 lon12=lon12, psi1=psi1, exact=self.exact,

222 psi12=psi12, psi2=psi2)

223 return r

224

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

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

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

228#

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

230# '''

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

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

233

234 @deprecated_method

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

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

237

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

239 '''

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

241

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

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

244 if self.f:

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

246 return s

247

248 @deprecated_method

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

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

251

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

253

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

255 or 8).

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

257 5, 6, 7 or 8).

258

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

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

261 '''

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

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

264 self.RAorder = RAorder

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

266 self.TMorder = TMorder

267 return t

268

269 @Property_RO

270 def _RA2(self):

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

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

273 m = self.RAorder

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

275

276 @Property

277 def RAorder(self):

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

279 '''

280 return self._mRA

281

282 @RAorder.setter # PYCHOK setter!

283 def RAorder(self, order):

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

285 '''

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

287 if self._mRA != n:

288 _update_all_rls(self)

289 self._mRA = n

290

291 @Property_RO

292 def _RhumbLine(self):

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

294 '''

295 return RhumbLine

296

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

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

299 '''

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

301 self.ellipsoid.area) * lon12 / _720_0

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

303 return r

304

305 @Property

306 def TMorder(self):

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

308 '''

309 return self._mTM

310

311 @TMorder.setter # PYCHOK setter!

312 def TMorder(self, order):

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

314

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

316 '''

317 self.exact = self._TMorder(order)

318

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

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

321

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

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

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

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

326

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

328 '''

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

330 exact=self.exact, TMorder=self.TMorder)

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

332

333

334 class RhumbLine(RhumbLineBase):

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

336

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

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

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

340

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

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

343 rhumb line.

344

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

346 two rhumb lines.

347

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

349 the distance to a rhumb line in different ways.

350 '''

351 _Rhumb = Rhumb # rhumbx.Rhumb

352

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

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

355

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

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

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

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

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

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

362 values specifying the required capabilities. Include

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

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

365 '''

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

367

368 @Property_RO

369 def _mu1(self):

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

371 '''

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

373

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

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

376

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

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

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

380 the quantities to be returned.

381

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

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

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

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

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

387

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

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

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

391

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

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

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

395 carried out in finite terms.

396 '''

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

398 if (outmask & Cs.LATITUDE_LONGITUDE_AREA):

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

400 a12 = s12 / E._L_90

401 mu12 = self._calp * a12

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

403 if x90: # PYCHOK no cover

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

405 lon2 = NAN

406 if (outmask & Cs.AREA):

407 r.set_(S12=NAN)

408 else:

409 psi2 = self._psi1

410 if self._calp:

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

412 psi12 = R._DRectifying2Isometricd(mu2,

413 self._mu1) * mu12

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

415 psi2 += psi12

416 else: # PYCHOK no cover

417 lat2 = self.lat1

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

419 if (outmask & Cs.AREA):

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

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

422 if (outmask & Cs.LONGITUDE):

423 if (outmask & Cs.LONG_UNROLL):

424 lon2 += self.lon1

425 else:

426 lon2 = _norm180(self._lon12 + lon2)

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

428 if (outmask & Cs.LATITUDE):

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

430 if (outmask & Cs.LONGITUDE):

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

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

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

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

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

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

437 return r

438

439 @Property_RO

440 def _psi1(self):

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

442 '''

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

444

445 @property_RO

446 def RAorder(self):

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

448 '''

449 return self.rhumb.RAorder

450

451 @Property_RO

452 def _r1rad(self): # PYCHOK no cover

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

454 '''

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

456

457

458 class RhumbOrder2Tuple(_GTuple):

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

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

461 '''

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

463 _Units_ = ( Int, Int)

464

465

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

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

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

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

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

471

472 def _Dasinh(x, y):

473 hx = hypot1(x)

474 d = x - y

475 if d:

476 hx *= y

477 hy = x * hypot1(y)

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

479 r = asinh(t) / d

480 else:

481 r = _1_0 / hx

482 return r

483

484

485 def _Datan(x, y):

486 xy = x * y

487 r = xy + _1_0

488 d = x - y

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

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

491 else:

492 r = _1_0 / r

493 return r

494

495

496 def _Dcosh(x, y):

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

498

499

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

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

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

503 if e: # assert not isnear0(e)

504 d = x - y

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

506 return e

507

508

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

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

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

512 d = x - y

513 if (x * y) > 0:

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

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

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

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

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

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

520 # = t = d * Dt

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

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

523 # holds if x*y > 0):

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

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

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

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

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

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

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

531 t = D * d

532 t2 = _1_0 + t**2

533 D *= _2_0 / t2

534 s = D * d

535 if s:

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

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

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

539 elif d:

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

541 return r

542

543

544 def _Dgd(x, y):

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

546

547

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

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

550

551

552 def _Dlog(x, y):

553 d = (x - y) * _0_5

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

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

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

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

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

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

560

561

562 def _Dsin(x, y):

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

564

565

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

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

568 d = (x - y) * _0_5

569 if d:

570 r *= sin_(d) / d

571 return r

572

573

574 def _Dsinh(x, y):

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

576

577

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

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

580

581

582 def _Dtant(dxy, tx, ty):

583 txy = tx * ty

584 r = txy + _1_0

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

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

587 return r

588

589

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

591 # get inverse auxiliary lats in radians and tangents

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

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

594 return phix, phiy, tan(phix), tan(phiy)

595

596

597 def _gd(x):

598 return atan(sinh(x))

599

600

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

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

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

604 # Use Clenshaw summation to evaluate

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

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

607 # where

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

609 # SC = sinp ? sin : cos

610 # CS = sinp ? cos : sin

611 # ...

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

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

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

615 s = _neg(sp * sd) # negative

616 # 2x2 matrices in row-major order

617 a1 = s * d**2

618 a2 = s * _4_0

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

620 b2 = b1 = _0_0s(4)

621 if n > 0:

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

623

624 _fsum = _MODS.fsums.fsum1f_

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

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

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

628 m0, m1, m2, m3 = b2

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

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

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

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

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

634 # Here are the full expressions for m and s

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

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

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

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

639 cd *= b1[2]

640 sd *= b1[3] * _2_0

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

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

643 return s

644

645

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

647 4: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 4

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

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

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

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

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

653 5: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 5

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

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

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

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

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

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

660 6: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 6

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

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

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

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

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

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

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

668 7: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 7

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

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

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

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

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

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

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

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

677 8: ( # GEOGRAPHICLIB_RHUMBAREA_ORDER == 8

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

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

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

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

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

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

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

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

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

687}

688

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

690

691 if __name__ == '__main__':

692

693 from pygeodesy.lazily import printf

694

695 def _re(fmt, r3, x3):

696 e3 = []

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

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

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

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

701

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

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

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

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

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

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

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

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

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

711

712# % python3 -m pygeodesy.rhumbx

713

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

715

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

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

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

719

720# **) MIT License

721#

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

723#

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

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

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

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

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

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

730#

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

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

733#

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

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

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

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

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

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

740# OTHER DEALINGS IN THE SOFTWARE.