Coverage for pygeodesy/formy.py: 98%

359 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-04-05 15:46 -0400

1 

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

3 

4u'''Formulary of basic geodesy functions and approximations. 

5''' 

6# make sure int/int division yields float quotient, see .basics 

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

8 

9from pygeodesy.constants import EPS, EPS0, EPS1, PI, PI2, PI3, PI_2, R_M, \ 

10 _umod_PI2, float0, isnon0, remainder, _0_0, \ 

11 _0_125, _0_25, _0_5, _1_0, _2_0, _N_2_0, \ 

12 _4_0, _32_0, _90_0, _180_0, _360_0 

13from pygeodesy.datums import Datum, Ellipsoid, _ellipsoidal_datum, \ 

14 _mean_radius, _spherical_datum, _WGS84 

15# from pygeodesy.ellipsoids import Ellipsoid # from .datums 

16from pygeodesy.errors import _AssertionError, IntersectionError, \ 

17 LimitError, limiterrors, _ValueError 

18from pygeodesy.fmath import Fdot, euclid, fdot, hypot, hypot2, sqrt0 

19from pygeodesy.fsums import fsum_, unstr 

20from pygeodesy.interns import NN, _distant_, _inside_, _near_, _null_, \ 

21 _opposite_, _outside_, _too_ 

22from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS 

23from pygeodesy.named import _NamedTuple, _xnamed 

24from pygeodesy.namedTuples import Bearing2Tuple, Distance4Tuple, \ 

25 LatLon2Tuple, PhiLam2Tuple, Vector3Tuple 

26# from pygeodesy.streprs import unstr # from .fsums 

27from pygeodesy.units import Degrees_, Distance, Distance_, Height, Lam_, Lat, \ 

28 Lon, Phi_, Radians, Radians_, Radius, Radius_, \ 

29 Scalar, _100km 

30from pygeodesy.utily import acos1, atan2b, atan2d, degrees2m, m2degrees, tan_2, \ 

31 sincos2, sincos2_, sincos2d_, unroll180, unrollPI 

32 

33from math import atan, atan2, cos, degrees, fabs, radians, sin, sqrt # pow 

34 

35__all__ = _ALL_LAZY.formy 

36__version__ = '23.04.05' 

37 

38_ratio_ = 'ratio' 

39_xline_ = 'xline' 

40 

41 

42def _anti2(a, b, n_2, n, n2): 

43 '''(INTERNAL) Helper for C{antipode} and C{antipode_}. 

44 ''' 

45 r = remainder(a, n) if fabs(a) > n_2 else a 

46 if r == a: 

47 r = -r 

48 b += n 

49 if fabs(b) > n: 

50 b = remainder(b, n2) 

51 return float0(r, b) 

52 

53 

54def antipode(lat, lon, name=NN): 

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

56 to a given point in C{degrees}. 

57 

58 @arg lat: Latitude (C{degrees}). 

59 @arg lon: Longitude (C{degrees}). 

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

61 

62 @return: A L{LatLon2Tuple}C{(lat, lon)}. 

63 

64 @see: Functions L{antipode_} and L{normal} and U{Geosphere 

65 <https://CRAN.R-Project.org/web/packages/geosphere/geosphere.pdf>}. 

66 ''' 

67 return LatLon2Tuple(*_anti2(lat, lon, _90_0, _180_0, _360_0), name=name) 

68 

69 

70def antipode_(phi, lam, name=NN): 

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

72 to a given point in C{radians}. 

73 

74 @arg phi: Latitude (C{radians}). 

75 @arg lam: Longitude (C{radians}). 

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

77 

78 @return: A L{PhiLam2Tuple}C{(phi, lam)}. 

79 

80 @see: Functions L{antipode} and L{normal_} and U{Geosphere 

81 <https://CRAN.R-Project.org/web/packages/geosphere/geosphere.pdf>}. 

82 ''' 

83 return PhiLam2Tuple(*_anti2(phi, lam, PI_2, PI, PI2), name=name) 

84 

85 

86def _area_or_(excess_, lat1, lat2, radius, d_lon, unused): 

87 '''(INTERNAL) Helper for area and spherical excess. 

88 ''' 

89 r = excess_(Phi_(lat2=lat2), 

90 Phi_(lat1=lat1), radians(d_lon)) 

91 if radius: 

92 r *= _mean_radius(radius, lat1, lat2)**2 

93 return r 

94 

95 

96def bearing(lat1, lon1, lat2, lon2, **options): 

97 '''Compute the initial or final bearing (forward or reverse 

98 azimuth) between a (spherical) start and end point. 

99 

100 @arg lat1: Start latitude (C{degrees}). 

101 @arg lon1: Start longitude (C{degrees}). 

102 @arg lat2: End latitude (C{degrees}). 

103 @arg lon2: End longitude (C{degrees}). 

104 @kwarg options: Optional keyword arguments for function 

105 L{pygeodesy.bearing_}. 

106 

107 @return: Initial or final bearing (compass C{degrees360}) or 

108 zero if start and end point coincide. 

109 ''' 

110 r = bearing_(Phi_(lat1=lat1), Lam_(lon1=lon1), 

111 Phi_(lat2=lat2), Lam_(lon2=lon2), **options) 

112 return degrees(r) 

113 

114 

115def bearing_(phi1, lam1, phi2, lam2, final=False, wrap=False): 

116 '''Compute the initial or final bearing (forward or reverse azimuth) 

117 between a (spherical) start and end point. 

118 

119 @arg phi1: Start latitude (C{radians}). 

120 @arg lam1: Start longitude (C{radians}). 

121 @arg phi2: End latitude (C{radians}). 

122 @arg lam2: End longitude (C{radians}). 

123 @kwarg final: Return final bearing if C{True}, initial otherwise (C{bool}). 

124 @kwarg wrap: Wrap and L{pygeodesy.unrollPI} longitudes (C{bool}). 

125 

126 @return: Initial or final bearing (compass C{radiansPI2}) or zero if start 

127 and end point coincide. 

128 

129 @see: U{Bearing<https://www.Movable-Type.co.UK/scripts/latlong.html>}, U{Course 

130 between two points<https://www.EdWilliams.org/avform147.htm#Crs>} and 

131 U{Bearing Between Two Points<https://web.Archive.org/web/20020630205931/ 

132 https://MathForum.org/library/drmath/view/55417.html>}. 

133 ''' 

134 if final: # swap plus PI 

135 phi1, lam1, phi2, lam2 = phi2, lam2, phi1, lam1 

136 r = PI3 

137 else: 

138 r = PI2 

139 

140 db, _ = unrollPI(lam1, lam2, wrap=wrap) 

141 sa1, ca1, sa2, ca2, sdb, cdb = sincos2_(phi1, phi2, db) 

142 

143 x = ca1 * sa2 - sa1 * ca2 * cdb 

144 y = sdb * ca2 

145 return _umod_PI2(atan2(y, x) + r) # .utily.wrapPI2 

146 

147 

148def _bearingTo2(p1, p2, wrap=False): # for points.ispolar, sphericalTrigonometry.areaOf 

149 '''(INTERNAL) Compute initial and final bearing. 

150 ''' 

151 try: # for LatLon_ and ellipsoidal LatLon 

152 return p1.bearingTo2(p2, wrap=wrap) 

153 except AttributeError: 

154 pass 

155 # XXX spherical version, OK for ellipsoidal ispolar? 

156 a1, b1 = p1.philam 

157 a2, b2 = p2.philam 

158 return Bearing2Tuple(degrees(bearing_(a1, b1, a2, b2, final=False, wrap=wrap)), 

159 degrees(bearing_(a1, b1, a2, b2, final=True, wrap=wrap)), 

160 name=_bearingTo2.__name__) 

161 

162 

163def compassAngle(lat1, lon1, lat2, lon2, adjust=True, wrap=False): 

164 '''Return the angle from North for the direction vector 

165 M{(lon2 - lon1, lat2 - lat1)} between two points. 

166 

167 Suitable only for short, not near-polar vectors up to a few hundred 

168 Km or Miles. Use function L{pygeodesy.bearing} for longer vectors. 

169 

170 @arg lat1: From latitude (C{degrees}). 

171 @arg lon1: From longitude (C{degrees}). 

172 @arg lat2: To latitude (C{degrees}). 

173 @arg lon2: To longitude (C{degrees}). 

174 @kwarg adjust: Adjust the longitudinal delta by the cosine of the 

175 mean latitude (C{bool}). 

176 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

177 

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

179 

180 @note: Courtesy of Martin Schultz. 

181 

182 @see: U{Local, flat earth approximation 

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

184 ''' 

185 d_lon, _ = unroll180(lon1, lon2, wrap=wrap) 

186 if adjust: # scale delta lon 

187 d_lon *= _scale_deg(lat1, lat2) 

188 return atan2b(d_lon, lat2 - lat1) 

189 

190 

191def cosineAndoyerLambert(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False): 

192 '''Compute the distance between two (ellipsoidal) points using the 

193 U{Andoyer-Lambert correction<https://navlib.net/wp-content/uploads/ 

194 2013/10/admiralty-manual-of-navigation-vol-1-1964-english501c.pdf>} of the 

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

196 fromula. 

197 

198 @arg lat1: Start latitude (C{degrees}). 

199 @arg lon1: Start longitude (C{degrees}). 

200 @arg lat2: End latitude (C{degrees}). 

201 @arg lon2: End longitude (C{degrees}). 

202 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid}, 

203 L{Ellipsoid2} or L{a_f2Tuple}). 

204 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

205 

206 @return: Distance (C{meter}, same units as the B{C{datum}}'s 

207 ellipsoid axes or C{radians} if B{C{datum}} is C{None}). 

208 

209 @raise TypeError: Invalid B{C{datum}}. 

210 

211 @see: Functions L{cosineAndoyerLambert_}, L{cosineForsytheAndoyerLambert}, 

212 L{cosineLaw}, L{equirectangular}, L{euclidean}, L{flatLocal}/L{hubeny}, 

213 L{flatPolar}, L{haversine}, L{thomas} and L{vincentys} and method 

214 L{Ellipsoid.distance2}. 

215 ''' 

216 return _distanceToE(cosineAndoyerLambert_, lat1, lat2, datum, 

217 *unroll180(lon1, lon2, wrap=wrap)) 

218 

219 

220def cosineAndoyerLambert_(phi2, phi1, lam21, datum=_WGS84): 

221 '''Compute the I{angular} distance between two (ellipsoidal) points using the 

222 U{Andoyer-Lambert correction<https://navlib.net/wp-content/uploads/2013/10/ 

223 admiralty-manual-of-navigation-vol-1-1964-english501c.pdf>} of the U{Law 

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

225 fromula. 

226 

227 @arg phi2: End latitude (C{radians}). 

228 @arg phi1: Start latitude (C{radians}). 

229 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

230 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid}, 

231 L{Ellipsoid2} or L{a_f2Tuple}). 

232 

233 @return: Angular distance (C{radians}). 

234 

235 @raise TypeError: Invalid B{C{datum}}. 

236 

237 @see: Functions L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert_}, 

238 L{cosineLaw_}, L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, 

239 L{flatPolar_}, L{haversine_}, L{thomas_} and L{vincentys_} and U{Geodesy-PHP 

240 <https://GitHub.com/jtejido/geodesy-php/blob/master/src/Geodesy/ 

241 Distance/AndoyerLambert.php>}. 

242 ''' 

243 s2, c2, s1, c1, r, c21 = _sincosa6(phi2, phi1, lam21) 

244 if isnon0(c1) and isnon0(c2): 

245 E = _ellipsoidal(datum, cosineAndoyerLambert_) 

246 if E.f: # ellipsoidal 

247 r2 = atan2(E.b_a * s2, c2) 

248 r1 = atan2(E.b_a * s1, c1) 

249 s2, c2, s1, c1 = sincos2_(r2, r1) 

250 r = acos1(s1 * s2 + c1 * c2 * c21) 

251 if r: 

252 sr, _, sr_2, cr_2 = sincos2_(r, r * _0_5) 

253 if isnon0(sr_2) and isnon0(cr_2): 

254 s = (sr + r) * ((s1 - s2) / sr_2)**2 

255 c = (sr - r) * ((s1 + s2) / cr_2)**2 

256 r += (c - s) * E.f * _0_125 

257 return r 

258 

259 

260def cosineForsytheAndoyerLambert(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False): 

261 '''Compute the distance between two (ellipsoidal) points using the 

262 U{Forsythe-Andoyer-Lambert correction<https://www2.UNB.Ca/gge/Pubs/TR77.pdf>} of 

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

264 formula. 

265 

266 @arg lat1: Start latitude (C{degrees}). 

267 @arg lon1: Start longitude (C{degrees}). 

268 @arg lat2: End latitude (C{degrees}). 

269 @arg lon2: End longitude (C{degrees}). 

270 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid}, 

271 L{Ellipsoid2} or L{a_f2Tuple}). 

272 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

273 

274 @return: Distance (C{meter}, same units as the B{C{datum}}'s 

275 ellipsoid axes or C{radians} if B{C{datum}} is C{None}). 

276 

277 @raise TypeError: Invalid B{C{datum}}. 

278 

279 @see: Functions L{cosineForsytheAndoyerLambert_}, L{cosineAndoyerLambert}, 

280 L{cosineLaw}, L{equirectangular}, L{euclidean}, L{flatLocal}/L{hubeny}, 

281 L{flatPolar}, L{haversine}, L{thomas} and L{vincentys} and method 

282 L{Ellipsoid.distance2}. 

283 ''' 

284 return _distanceToE(cosineForsytheAndoyerLambert_, lat1, lat2, datum, 

285 *unroll180(lon1, lon2, wrap=wrap)) 

286 

287 

288def cosineForsytheAndoyerLambert_(phi2, phi1, lam21, datum=_WGS84): 

289 '''Compute the I{angular} distance between two (ellipsoidal) points using the 

290 U{Forsythe-Andoyer-Lambert correction<https://www2.UNB.Ca/gge/Pubs/TR77.pdf>} of 

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

292 formula. 

293 

294 @arg phi2: End latitude (C{radians}). 

295 @arg phi1: Start latitude (C{radians}). 

296 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

297 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid}, 

298 L{Ellipsoid2} or L{a_f2Tuple}). 

299 

300 @return: Angular distance (C{radians}). 

301 

302 @raise TypeError: Invalid B{C{datum}}. 

303 

304 @see: Functions L{cosineForsytheAndoyerLambert}, L{cosineAndoyerLambert_}, 

305 L{cosineLaw_}, L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, 

306 L{flatPolar_}, L{haversine_}, L{thomas_} and L{vincentys_} and U{Geodesy-PHP 

307 <https://GitHub.com/jtejido/geodesy-php/blob/master/src/Geodesy/ 

308 Distance/ForsytheCorrection.php>}. 

309 ''' 

310 s2, c2, s1, c1, r, _ = _sincosa6(phi2, phi1, lam21) 

311 if r and isnon0(c1) and isnon0(c2): 

312 E = _ellipsoidal(datum, cosineForsytheAndoyerLambert_) 

313 if E.f: # ellipsoidal 

314 sr, cr, s2r, _ = sincos2_(r, r * _2_0) 

315 if isnon0(sr) and fabs(cr) < EPS1: 

316 s = (s1 + s2)**2 / (1 + cr) 

317 t = (s1 - s2)**2 / (1 - cr) 

318 x = s + t 

319 y = s - t 

320 

321 s = 8 * r**2 / sr 

322 a = 64 * r + _2_0 * s * cr # 16 * r**2 / tan(r) 

323 d = 48 * sr + s # 8 * r**2 / tan(r) 

324 b = -2 * d 

325 e = 30 * s2r 

326 c = fsum_(30 * r, e * _0_5, s * cr) # 8 * r**2 / tan(r) 

327 

328 t = fsum_( a * x, b * y, -c * x**2, d * x * y, e * y**2) 

329 r += fsum_(-r * x, 3 * y * sr, t * E.f / _32_0) * E.f * _0_25 

330 return r 

331 

332 

333def cosineLaw(lat1, lon1, lat2, lon2, radius=R_M, wrap=False): 

334 '''Compute the distance between two points using the 

335 U{spherical Law of Cosines 

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

337 formula. 

338 

339 @arg lat1: Start latitude (C{degrees}). 

340 @arg lon1: Start longitude (C{degrees}). 

341 @arg lat2: End latitude (C{degrees}). 

342 @arg lon2: End longitude (C{degrees}). 

343 @kwarg radius: Mean earth radius, ellipsoid or datum (C{meter}, 

344 L{Ellipsoid}, L{Ellipsoid2}, L{Datum} or L{a_f2Tuple}). 

345 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

346 

347 @return: Distance (C{meter}, same units as B{C{radius}} or the 

348 ellipsoid or datum axes). 

349 

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

351 

352 @see: Functions L{cosineLaw_}, L{cosineAndoyerLambert}, 

353 L{cosineForsytheAndoyerLambert}, L{equirectangular}, L{euclidean}, 

354 L{flatLocal}/L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and 

355 L{vincentys} and method L{Ellipsoid.distance2}. 

356 

357 @note: See note at function L{vincentys_}. 

358 ''' 

359 return _distanceToS(cosineLaw_, lat1, lat2, radius, 

360 *unroll180(lon1, lon2, wrap=wrap)) 

361 

362 

363def cosineLaw_(phi2, phi1, lam21): 

364 '''Compute the I{angular} distance between two points using the 

365 U{spherical Law of Cosines 

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

367 formula. 

368 

369 @arg phi2: End latitude (C{radians}). 

370 @arg phi1: Start latitude (C{radians}). 

371 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

372 

373 @return: Angular distance (C{radians}). 

374 

375 @see: Functions L{cosineLaw}, L{cosineAndoyerLambert_}, 

376 L{cosineForsytheAndoyerLambert_}, L{equirectangular_}, 

377 L{euclidean_}, L{flatLocal_}/L{hubeny_}, L{flatPolar_}, 

378 L{haversine_}, L{thomas_} and L{vincentys_}. 

379 

380 @note: See note at function L{vincentys_}. 

381 ''' 

382 return _sincosa6(phi2, phi1, lam21)[4] 

383 

384 

385def _distanceToE(func_, lat1, lat2, earth, d_lon, unused): 

386 '''(INTERNAL) Helper for ellipsoidal distances. 

387 ''' 

388 E = _ellipsoidal(earth, func_) 

389 r = func_(Phi_(lat2=lat2), 

390 Phi_(lat1=lat1), radians(d_lon), datum=E) 

391 return r * E.a 

392 

393 

394def _distanceToS(func_, lat1, lat2, earth, d_lon, unused, **adjust): 

395 '''(INTERNAL) Helper for spherical distances. 

396 ''' 

397 r = func_(Phi_(lat2=lat2), 

398 Phi_(lat1=lat1), radians(d_lon), **adjust) 

399 return r * _mean_radius(earth, lat1, lat2) 

400 

401 

402def _ellipsoidal(earth, where): 

403 '''(INTERNAL) Helper for distances. 

404 ''' 

405 return earth if isinstance(earth, Ellipsoid) else ( 

406 earth if isinstance(earth, Datum) else 

407 _ellipsoidal_datum(earth, name=where.__name__)).ellipsoid # PYCHOK indent 

408 

409 

410def equirectangular(lat1, lon1, lat2, lon2, radius=R_M, **options): 

411 '''Compute the distance between two points using 

412 the U{Equirectangular Approximation / Projection 

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

414 

415 @arg lat1: Start latitude (C{degrees}). 

416 @arg lon1: Start longitude (C{degrees}). 

417 @arg lat2: End latitude (C{degrees}). 

418 @arg lon2: End longitude (C{degrees}). 

419 @kwarg radius: Mean earth radius, ellipsoid or datum 

420 (C{meter}, L{Ellipsoid}, L{Ellipsoid2}, 

421 L{Datum} or L{a_f2Tuple}). 

422 @kwarg options: Optional keyword arguments for function 

423 L{equirectangular_}. 

424 

425 @return: Distance (C{meter}, same units as B{C{radius}} or 

426 the ellipsoid or datum axes). 

427 

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

429 

430 @see: Function L{equirectangular_} for more details, the 

431 available B{C{options}}, errors, restrictions and other, 

432 approximate or accurate distance functions. 

433 ''' 

434 d = sqrt(equirectangular_(Lat(lat1=lat1), Lon(lon1=lon1), 

435 Lat(lat2=lat2), Lon(lon2=lon2), 

436 **options).distance2) # PYCHOK 4 vs 2-3 

437 return degrees2m(d, radius=_mean_radius(radius, lat1, lat2)) 

438 

439 

440def equirectangular_(lat1, lon1, lat2, lon2, 

441 adjust=True, limit=45, wrap=False): 

442 '''Compute the distance between two points using 

443 the U{Equirectangular Approximation / Projection 

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

445 

446 This approximation is valid for short distance of several 

447 hundred Km or Miles, see the B{C{limit}} keyword argument and 

448 the L{LimitError}. 

449 

450 @arg lat1: Start latitude (C{degrees}). 

451 @arg lon1: Start longitude (C{degrees}). 

452 @arg lat2: End latitude (C{degrees}). 

453 @arg lon2: End longitude (C{degrees}). 

454 @kwarg adjust: Adjust the wrapped, unrolled longitudinal delta 

455 by the cosine of the mean latitude (C{bool}). 

456 @kwarg limit: Optional limit for lat- and longitudinal deltas 

457 (C{degrees}) or C{None} or C{0} for unlimited. 

458 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

459 

460 @return: A L{Distance4Tuple}C{(distance2, delta_lat, delta_lon, 

461 unroll_lon2)}. 

462 

463 @raise LimitError: If the lat- and/or longitudinal delta exceeds the 

464 B{C{-limit..+limit}} range and L{pygeodesy.limiterrors} 

465 set to C{True}. 

466 

467 @see: U{Local, flat earth approximation 

468 <https://www.EdWilliams.org/avform.htm#flat>}, functions 

469 L{equirectangular}, L{cosineAndoyerLambert}, 

470 L{cosineForsytheAndoyerLambert}, L{cosineLaw}, L{euclidean}, 

471 L{flatLocal}/L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} 

472 and L{vincentys} and methods L{Ellipsoid.distance2}, 

473 C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}. 

474 ''' 

475 d_lat = lat2 - lat1 

476 d_lon, ulon2 = unroll180(lon1, lon2, wrap=wrap) 

477 

478 if limit and limit > 0 and limiterrors() and (fabs(d_lat) > limit or 

479 fabs(d_lon) > limit): 

480 t = unstr(equirectangular_, lat1, lon1, lat2, lon2, limit=limit) 

481 raise LimitError('delta exceeds limit', txt=t) 

482 

483 if adjust: # scale delta lon 

484 d_lon *= _scale_deg(lat1, lat2) 

485 

486 d2 = hypot2(d_lat, d_lon) # degrees squared! 

487 return Distance4Tuple(d2, d_lat, d_lon, ulon2 - lon2) 

488 

489 

490def euclidean(lat1, lon1, lat2, lon2, radius=R_M, adjust=True, wrap=False): 

491 '''Approximate the C{Euclidean} distance between two (spherical) points. 

492 

493 @arg lat1: Start latitude (C{degrees}). 

494 @arg lon1: Start longitude (C{degrees}). 

495 @arg lat2: End latitude (C{degrees}). 

496 @arg lon2: End longitude (C{degrees}). 

497 @kwarg radius: Mean earth radius, ellipsoid or datum (C{meter}, 

498 L{Ellipsoid}, L{Ellipsoid2}, L{Datum} or L{a_f2Tuple}). 

499 @kwarg adjust: Adjust the longitudinal delta by the cosine of the 

500 mean latitude (C{bool}). 

501 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

502 

503 @return: Distance (C{meter}, same units as B{C{radius}} or the 

504 ellipsoid or datum axes). 

505 

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

507 

508 @see: U{Distance between two (spherical) points 

509 <https://www.EdWilliams.org/avform.htm#Dist>}, functions L{euclid}, 

510 L{euclidean_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

511 L{cosineLaw}, L{equirectangular}, L{flatLocal}/L{hubeny}, L{flatPolar}, 

512 L{haversine}, L{thomas} and L{vincentys} and methods L{Ellipsoid.distance2}, 

513 C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}. 

514 ''' 

515 return _distanceToS(euclidean_, lat1, lat2, radius, 

516 *unroll180(lon1, lon2, wrap=wrap), 

517 adjust=adjust) 

518 

519 

520def euclidean_(phi2, phi1, lam21, adjust=True): 

521 '''Approximate the I{angular} C{Euclidean} distance between two 

522 (spherical) points. 

523 

524 @arg phi2: End latitude (C{radians}). 

525 @arg phi1: Start latitude (C{radians}). 

526 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

527 @kwarg adjust: Adjust the longitudinal delta by the cosine 

528 of the mean latitude (C{bool}). 

529 

530 @return: Angular distance (C{radians}). 

531 

532 @see: Functions L{euclid}, L{euclidean}, L{cosineAndoyerLambert_}, 

533 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{equirectangular_}, 

534 L{flatLocal_}/L{hubeny_}, L{flatPolar_}, L{haversine_}, L{thomas_} 

535 and L{vincentys_}. 

536 ''' 

537 if adjust: 

538 lam21 *= _scale_rad(phi2, phi1) 

539 return euclid(phi2 - phi1, lam21) 

540 

541 

542def excessAbc_(A, b, c): 

543 '''Compute the I{spherical excess} C{E} of a (spherical) triangle 

544 from two sides and the included angle. 

545 

546 @arg A: An interior triangle angle (C{radians}). 

547 @arg b: Frist adjacent triangle side (C{radians}). 

548 @arg c: Second adjacent triangle side (C{radians}). 

549 

550 @return: Spherical excess (C{radians}). 

551 

552 @raise UnitError: Invalid B{C{A}}, B{C{b}} or B{C{c}}. 

553 

554 @see: Functions L{excessGirard_}, L{excessLHuilier_} and U{Spherical 

555 trigonometry<https://WikiPedia.org/wiki/Spherical_trigonometry>}. 

556 ''' 

557 sA, cA, sb, cb, sc, cc = sincos2_(Radians_(A=A), Radians_(b=b) * _0_5, 

558 Radians_(c=c) * _0_5) 

559 return atan2(sA * sb * sc, cb * cc + cA * sb * sc) * _2_0 

560 

561 

562def excessGirard_(A, B, C): 

563 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using 

564 U{Girard's<https://MathWorld.Wolfram.com/GirardsSphericalExcessFormula.html>} 

565 formula. 

566 

567 @arg A: First interior triangle angle (C{radians}). 

568 @arg B: Second interior triangle angle (C{radians}). 

569 @arg C: Third interior triangle angle (C{radians}). 

570 

571 @return: Spherical excess (C{radians}). 

572 

573 @raise UnitError: Invalid B{C{A}}, B{C{B}} or B{C{C}}. 

574 

575 @see: Function L{excessLHuilier_} and U{Spherical trigonometry 

576 <https://WikiPedia.org/wiki/Spherical_trigonometry>}. 

577 ''' 

578 return Radians(Girard=fsum_(Radians_(A=A), 

579 Radians_(B=B), 

580 Radians_(C=C), -PI)) 

581 

582 

583def excessLHuilier_(a, b, c): 

584 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using 

585 U{L'Huilier's<https://MathWorld.Wolfram.com/LHuiliersTheorem.html>} 

586 Theorem. 

587 

588 @arg a: First triangle side (C{radians}). 

589 @arg b: Second triangle side (C{radians}). 

590 @arg c: Third triangle side (C{radians}). 

591 

592 @return: Spherical excess (C{radians}). 

593 

594 @raise UnitError: Invalid B{C{a}}, B{C{b}} or B{C{c}}. 

595 

596 @see: Function L{excessGirard_} and U{Spherical trigonometry 

597 <https://WikiPedia.org/wiki/Spherical_trigonometry>}. 

598 ''' 

599 a = Radians_(a=a) 

600 b = Radians_(b=b) 

601 c = Radians_(c=c) 

602 

603 s = fsum_(a, b, c) * _0_5 

604 r = tan_2(s) * tan_2(s - a) * tan_2(s - b) * tan_2(s - c) 

605 r = atan(sqrt(r)) if r > 0 else _0_0 

606 return Radians(LHuilier=r * _4_0) 

607 

608 

609def excessKarney(lat1, lon1, lat2, lon2, radius=R_M, wrap=False): 

610 '''Compute the surface area of a (spherical) quadrilateral bounded by a 

611 segment of a great circle, two meridians and the equator using U{Karney's 

612 <https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>} 

613 method. 

614 

615 @arg lat1: Start latitude (C{degrees}). 

616 @arg lon1: Start longitude (C{degrees}). 

617 @arg lat2: End latitude (C{degrees}). 

618 @arg lon2: End longitude (C{degrees}). 

619 @kwarg radius: Mean earth radius, ellipsoid or datum (C{meter}, L{Ellipsoid}, 

620 L{Ellipsoid2}, L{Datum} or L{a_f2Tuple}) or C{None}. 

621 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

622 

623 @return: Surface area, I{signed} (I{square} C{meter} or the same units as 

624 B{C{radius}} I{squared}) or the I{spherical excess} (C{radians}) 

625 if C{B{radius}=0} or C{None}. 

626 

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

628 

629 @raise UnitError: Invalid B{C{lat2}} or B{C{lat1}}. 

630 

631 @raise ValueError: Semi-circular longitudinal delta. 

632 

633 @see: Functions L{excessKarney_} and L{excessQuad}. 

634 ''' 

635 return _area_or_(excessKarney_, lat1, lat2, radius, 

636 *unroll180(lon1, lon2, wrap=wrap)) 

637 

638 

639def excessKarney_(phi2, phi1, lam21): 

640 '''Compute the I{spherical excess} C{E} of a (spherical) quadrilateral bounded 

641 by a segment of a great circle, two meridians and the equator using U{Karney's 

642 <https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>} 

643 method. 

644 

645 @arg phi2: End latitude (C{radians}). 

646 @arg phi1: Start latitude (C{radians}). 

647 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

648 

649 @return: Spherical excess, I{signed} (C{radians}). 

650 

651 @raise ValueError: Semi-circular longitudinal delta B{C{lam21}}. 

652 

653 @see: Function L{excessKarney} and U{Area of a spherical polygon 

654 <https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}. 

655 ''' 

656 # from: Veness <https://www.Movable-Type.co.UK/scripts/latlong.html> Area 

657 # method due to Karney: for each edge of the polygon, 

658 # 

659 # tan(Δλ / 2) · (tan(φ1 / 2) + tan(φ2 / 2)) 

660 # tan(E / 2) = ----------------------------------------- 

661 # 1 + tan(φ1 / 2) · tan(φ2 / 2) 

662 # 

663 # where E is the spherical excess of the trapezium obtained by extending 

664 # the edge to the equator-circle vector for each edge (see also ***). 

665 t2 = tan_2(phi2) 

666 t1 = tan_2(phi1) 

667 t = tan_2(lam21, lam21=None) 

668 return Radians(Karney=atan2(t * (t1 + t2), 

669 _1_0 + (t1 * t2)) * _2_0) 

670 

671 

672# ***) Original post no longer available, following is a copy of the main part 

673# <http://OSGeo-org.1560.x6.Nabble.com/Area-of-a-spherical-polygon-td3841625.html> 

674# 

675# The area of a polygon on a (unit) sphere is given by the spherical excess 

676# 

677# A = 2 * pi - sum(exterior angles) 

678# 

679# However this is badly conditioned if the polygon is small. In this case, use 

680# 

681# A = sum(S12{i, i+1}) over the edges of the polygon 

682# 

683# where S12 is the area of the quadrilateral bounded by an edge of the polygon, 

684# two meridians and the equator, i.e. with vertices (phi1, lambda1), (phi2, 

685# lambda2), (0, lambda1) and (0, lambda2). S12 is given by 

686# 

687# tan(S12 / 2) = tan(lambda21 / 2) * (tan(phi1 / 2) + tan(phi2 / 2)) / 

688# (tan(phi1 / 2) * tan(phi2 / 2) + 1) 

689# 

690# = tan(lambda21 / 2) * tanh((Lambertian(phi1) + 

691# Lambertian(phi2)) / 2) 

692# 

693# where lambda21 = lambda2 - lambda1 and lamb(x) is the Lambertian (or 

694# inverse Gudermannian) function 

695# 

696# Lambertian(x) = asinh(tan(x)) = atanh(sin(x)) = 2 * atanh(tan(x / 2)) 

697# 

698# Notes: The formula for S12 is exact, except that... 

699# - it is indeterminate if an edge is a semi-circle 

700# - the formula for A applies only if the polygon does not include a pole 

701# (if it does, then add +/- 2 * pi to the result) 

702# - in the limit of small phi and lambda, S12 reduces to the trapezoidal 

703# formula, S12 = (lambda2 - lambda1) * (phi1 + phi2) / 2 

704# - I derived this result from the equation for the area of a spherical 

705# triangle in terms of two edges and the included angle given by, e.g. 

706# U{Todhunter, I. - Spherical Trigonometry (1871), Sec. 103, Eq. (2) 

707# <http://Books.Google.com/books?id=3uBHAAAAIAAJ&pg=PA71>} 

708# - I would be interested to know if this formula for S12 is already known 

709# - Charles Karney 

710 

711 

712def excessQuad(lat1, lon1, lat2, lon2, radius=R_M, wrap=False): 

713 '''Compute the surface area of a (spherical) quadrilateral bounded by a segment 

714 of a great circle, two meridians and the equator. 

715 

716 @arg lat1: Start latitude (C{degrees}). 

717 @arg lon1: Start longitude (C{degrees}). 

718 @arg lat2: End latitude (C{degrees}). 

719 @arg lon2: End longitude (C{degrees}). 

720 @kwarg radius: Mean earth radius, ellipsoid or datum (C{meter}, 

721 L{Ellipsoid}, L{Ellipsoid2}, L{Datum} or L{a_f2Tuple}) or C{None}. 

722 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

723 

724 @return: Surface area, I{signed} (I{square} C{meter} or the same units as 

725 B{C{radius}} I{squared}) or the I{spherical excess} (C{radians}) 

726 if C{B{radius}=0} or C{None}. 

727 

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

729 

730 @raise UnitError: Invalid B{C{lat2}} or B{C{lat1}}. 

731 

732 @see: Function L{excessQuad_} and L{excessKarney}. 

733 ''' 

734 return _area_or_(excessQuad_, lat1, lat2, radius, 

735 *unroll180(lon1, lon2, wrap=wrap)) 

736 

737 

738def excessQuad_(phi2, phi1, lam21): 

739 '''Compute the I{spherical excess} C{E} of a (spherical) quadrilateral bounded 

740 by a segment of a great circle, two meridians and the equator. 

741 

742 @arg phi2: End latitude (C{radians}). 

743 @arg phi1: Start latitude (C{radians}). 

744 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

745 

746 @return: Spherical excess, I{signed} (C{radians}). 

747 

748 @see: Function L{excessQuad}, U{Spherical trigonometry 

749 <https://WikiPedia.org/wiki/Spherical_trigonometry>}. 

750 ''' 

751 s = sin((phi2 + phi1) * _0_5) 

752 c = cos((phi2 - phi1) * _0_5) 

753 return Radians(Quad=atan2(tan_2(lam21) * s, c) * _2_0) 

754 

755 

756def flatLocal(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False): 

757 '''Compute the distance between two (ellipsoidal) points using 

758 the U{ellipsoidal Earth to plane projection<https://WikiPedia.org/ 

759 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>} 

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

761 

762 @arg lat1: Start latitude (C{degrees}). 

763 @arg lon1: Start longitude (C{degrees}). 

764 @arg lat2: End latitude (C{degrees}). 

765 @arg lon2: End longitude (C{degrees}). 

766 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid}, 

767 L{Ellipsoid2} or L{a_f2Tuple}). 

768 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

769 

770 @return: Distance (C{meter}, same units as the B{C{datum}}'s 

771 ellipsoid axes). 

772 

773 @raise TypeError: Invalid B{C{datum}}. 

774 

775 @note: The meridional and prime_vertical radii of curvature 

776 are taken and scaled at the mean of both latitude. 

777 

778 @see: Functions L{flatLocal_} or L{hubeny_}, L{cosineLaw}, L{flatPolar}, 

779 L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

780 L{equirectangular}, L{euclidean}, L{haversine}, L{thomas}, 

781 L{vincentys}, method L{Ellipsoid.distance2} and U{local, flat 

782 earth approximation<https://www.EdWilliams.org/avform.htm#flat>}. 

783 ''' 

784 d, _ = unroll180(lon1, lon2, wrap=wrap) 

785 return flatLocal_(Phi_(lat2=lat2), 

786 Phi_(lat1=lat1), radians(d), datum=datum) 

787 

788hubeny = flatLocal # PYCHOK for Karl Hubeny 

789 

790 

791def flatLocal_(phi2, phi1, lam21, datum=_WGS84): 

792 '''Compute the I{angular} distance between two (ellipsoidal) points using 

793 the U{ellipsoidal Earth to plane projection<https://WikiPedia.org/ 

794 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>} 

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

796 

797 @arg phi2: End latitude (C{radians}). 

798 @arg phi1: Start latitude (C{radians}). 

799 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

800 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid}, 

801 L{Ellipsoid2} or L{a_f2Tuple}). 

802 

803 @return: Angular distance (C{radians}). 

804 

805 @raise TypeError: Invalid B{C{datum}}. 

806 

807 @note: The meridional and prime_vertical radii of curvature 

808 are taken and scaled I{at the mean of both latitude}. 

809 

810 @see: Functions L{flatLocal} or L{hubeny}, L{cosineAndoyerLambert_}, 

811 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{flatPolar_}, 

812 L{equirectangular_}, L{euclidean_}, L{haversine_}, L{thomas_} 

813 and L{vincentys_} and U{local, flat earth approximation 

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

815 ''' 

816 E = _ellipsoidal(datum, flatLocal_) 

817 m, n = E.roc2_((phi2 + phi1) * _0_5, scaled=True) 

818 return hypot(m * (phi2 - phi1), n * lam21) 

819 

820hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny 

821 

822 

823def flatPolar(lat1, lon1, lat2, lon2, radius=R_M, wrap=False): 

824 '''Compute the distance between two (spherical) points using 

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

826 Geographical_distance#Polar_coordinate_flat-Earth_formula>} 

827 formula. 

828 

829 @arg lat1: Start latitude (C{degrees}). 

830 @arg lon1: Start longitude (C{degrees}). 

831 @arg lat2: End latitude (C{degrees}). 

832 @arg lon2: End longitude (C{degrees}). 

833 @kwarg radius: Mean earth radius, ellipsoid or datum (C{meter}, 

834 L{Ellipsoid}, L{Ellipsoid2}, L{Datum} or L{a_f2Tuple}). 

835 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

836 

837 @return: Distance (C{meter}, same units as B{C{radius}} or the 

838 ellipsoid or datum axes). 

839 

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

841 

842 @see: Functions L{flatPolar_}, L{cosineAndoyerLambert}, 

843 L{cosineForsytheAndoyerLambert},L{cosineLaw}, 

844 L{flatLocal}/L{hubeny}, L{equirectangular}, 

845 L{euclidean}, L{haversine}, L{thomas} and 

846 L{vincentys}. 

847 ''' 

848 return _distanceToS(flatPolar_, lat1, lat2, radius, 

849 *unroll180(lon1, lon2, wrap=wrap)) 

850 

851 

852def flatPolar_(phi2, phi1, lam21): 

853 '''Compute the I{angular} distance between two (spherical) points 

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

855 Geographical_distance#Polar_coordinate_flat-Earth_formula>} 

856 formula. 

857 

858 @arg phi2: End latitude (C{radians}). 

859 @arg phi1: Start latitude (C{radians}). 

860 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

861 

862 @return: Angular distance (C{radians}). 

863 

864 @see: Functions L{flatPolar}, L{cosineAndoyerLambert_}, 

865 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

866 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, 

867 L{haversine_}, L{thomas_} and L{vincentys_}. 

868 ''' 

869 a = fabs(PI_2 - phi1) # co-latitude 

870 b = fabs(PI_2 - phi2) # co-latitude 

871 if a < b: 

872 a, b = b, a 

873 if a < EPS0: 

874 a = _0_0 

875 elif b > 0: 

876 b = b / a # /= chokes PyChecker 

877 c = b * cos(lam21) * _2_0 

878 c = fsum_(_1_0, b**2, -fabs(c)) 

879 a *= sqrt0(c) 

880 return a 

881 

882 

883def hartzell(pov, los=None, earth=_WGS84, name=NN, **LatLon_and_kwds): 

884 '''Compute the intersection of the earth's surface and a Line-Of-Sight 

885 from a Point-Of-View in space. 

886 

887 @arg pov: Point-Of-View outside the earth (C{Cartesian}, L{Ecef9Tuple} 

888 or L{Vector3d}). 

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

890 C{None} to point to the earth' center. 

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

892 L{a_f2Tuple} or C{scalar} radius in C{meter}). 

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

894 @kwarg LatLon_and_kwds: Optional C{LatLon} class for the intersection 

895 point plus C{LatLon} keyword arguments, include 

896 B{C{datum}} if different from B{C{earth}}. 

897 

898 @return: The earth intersection (L{Vector3d}, C{Cartesian type} of 

899 B{C{pov}} or B{C{LatLon}}). 

900 

901 @raise IntersectionError: Null B{C{pov}} or B{C{los}} vector, B{C{pov}} 

902 is inside the earth or B{C{los}} points outside 

903 the earth or points in an opposite direction. 

904 

905 @raise TypeError: Invalid B{C{pov}}, B{C{los}} or B{C{earth}}. 

906 

907 @see: Function L{pygeodesy.hartzell4}, L{pygeodesy.tyr3d} for B{C{los}} and 

908 U{I{Satellite Line-of-Sight Intersection with Earth}<https://StephenHartzell. 

909 Medium.com/satellite-line-of-sight-intersection-with-earth-d786b4a6a9b6>}. 

910 ''' 

911 def _Error(txt): 

912 return IntersectionError(pov=pov, los=los, earth=earth, txt=txt) 

913 

914 D = earth if isinstance(earth, Datum) else \ 

915 _spherical_datum(earth, name=hartzell.__name__) 

916 E = D.ellipsoid 

917 

918 if E.b > E.a: # PYCHOK no cover 

919 try: 

920 t = _MODS.triaxials 

921 r, _ = t._hartzell3d2(pov, los, t.Triaxial_(E.a, E.a, E.b)) 

922 except Exception as x: 

923 raise _Error(str(x)) 

924 else: 

925 a2 = b2 = E.a2 # earth' x, y, ... 

926 c2 = E.b2 # ... z semi-axis squared 

927 q2 = E.b2_a2 # == c2 / a2 

928 bc = E.a * E.b # == b * c 

929 

930 V3 = _MODS.vector3d._otherV3d 

931 p3 = V3(pov=pov) 

932 u3 = V3(los=los) if los else p3.negate() 

933 u3 = u3.unit() # unit vector, opposing signs 

934 

935 x2, y2, z2 = p3.x2y2z2 # p3.times_(p3).xyz 

936 ux, vy, wz = u3.times_(p3).xyz 

937 u2, v2, w2 = u3.x2y2z2 # u3.times_(u3).xyz 

938 

939 t = c2, c2, b2 

940 m = fdot(t, u2, v2, w2) # a2 factored out 

941 if m < EPS0: # zero or near-null LOS vector 

942 raise _Error(_near_(_null_)) 

943 

944 # a2 and b2 factored out, b2 == a2 and b2 / a2 == 1 

945 r = fsum_(b2 * w2, c2 * v2, -v2 * z2, vy * wz * 2, 

946 c2 * u2, -u2 * z2, -w2 * x2, ux * wz * 2, 

947 -w2 * y2, -u2 * y2 * q2, -v2 * x2 * q2, ux * vy * 2 * q2, floats=True) 

948 if r > 0: 

949 r = sqrt(r) * bc # == a * a * b * c / a2 

950 elif r < 0: # LOS pointing away from or missing the earth 

951 raise _Error(_opposite_ if max(ux, vy, wz) > 0 else _outside_) 

952 

953 d = Fdot(t, ux, vy, wz).fadd_(r).fover(m) # -r for antipode, a2 factored out 

954 if d > 0: # POV inside or LOS missing, outside the earth 

955 s = fsum_(_1_0, x2 / a2, y2 / b2, z2 / c2, _N_2_0, floats=True) # like _sideOf 

956 raise _Error(_outside_ if s > 0 else _inside_) 

957 elif fsum_(x2, y2, z2) < d**2: # d past earth center 

958 raise _Error(_too_(_distant_)) 

959 

960 r = p3.minus(u3.times(d)) 

961# h = p3.minus(r).length # distance to ellipsoid 

962 

963 r = _xnamed(r, name or hartzell.__name__) 

964 if LatLon_and_kwds: 

965 c = _MODS.cartesianBase.CartesianBase(r, datum=D, name=r.name) 

966 r = c.toLatLon(**LatLon_and_kwds) 

967 return r 

968 

969 

970def haversine(lat1, lon1, lat2, lon2, radius=R_M, wrap=False): 

971 '''Compute the distance between two (spherical) points using the 

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

973 formula. 

974 

975 @arg lat1: Start latitude (C{degrees}). 

976 @arg lon1: Start longitude (C{degrees}). 

977 @arg lat2: End latitude (C{degrees}). 

978 @arg lon2: End longitude (C{degrees}). 

979 @kwarg radius: Mean earth radius, ellipsoid or datum (C{meter}, 

980 L{Ellipsoid}, L{Ellipsoid2}, L{Datum} or L{a_f2Tuple}). 

981 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

982 

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

984 

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

986 

987 @see: U{Distance between two (spherical) points 

988 <https://www.EdWilliams.org/avform.htm#Dist>}, functions 

989 L{cosineLaw}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

990 L{equirectangular}, L{euclidean}, L{flatLocal}/L{hubeny}, L{flatPolar}, 

991 L{thomas} and L{vincentys} and methods L{Ellipsoid.distance2}, 

992 C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}. 

993 

994 @note: See note at function L{vincentys_}. 

995 ''' 

996 return _distanceToS(haversine_, lat1, lat2, radius, 

997 *unroll180(lon1, lon2, wrap=wrap)) 

998 

999 

1000def haversine_(phi2, phi1, lam21): 

1001 '''Compute the I{angular} distance between two (spherical) points 

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

1003 formula. 

1004 

1005 @arg phi2: End latitude (C{radians}). 

1006 @arg phi1: Start latitude (C{radians}). 

1007 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

1008 

1009 @return: Angular distance (C{radians}). 

1010 

1011 @see: Functions L{haversine}, L{cosineAndoyerLambert_}, 

1012 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

1013 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, 

1014 L{flatPolar_}, L{thomas_} and L{vincentys_}. 

1015 

1016 @note: See note at function L{vincentys_}. 

1017 ''' 

1018 def _hsin(rad): 

1019 return sin(rad * _0_5)**2 

1020 

1021 h = _hsin(phi2 - phi1) + cos(phi1) * cos(phi2) * _hsin(lam21) # haversine 

1022 return atan2(sqrt0(h), sqrt0(_1_0 - h)) * _2_0 # == asin(sqrt(h)) * 2 

1023 

1024 

1025def heightOf(angle, distance, radius=R_M): 

1026 '''Determine the height above the (spherical) earth' surface after 

1027 traveling along a straight line at a given tilt. 

1028 

1029 @arg angle: Tilt angle above horizontal (C{degrees}). 

1030 @arg distance: Distance along the line (C{meter} or same units as 

1031 B{C{radius}}). 

1032 @kwarg radius: Optional mean earth radius (C{meter}). 

1033 

1034 @return: Height (C{meter}, same units as B{C{distance}} and B{C{radius}}). 

1035 

1036 @raise ValueError: Invalid B{C{angle}}, B{C{distance}} or B{C{radius}}. 

1037 

1038 @see: U{MultiDop geog_lib.GeogBeamHt<https://GitHub.com/NASA/MultiDop>} 

1039 (U{Shapiro et al. 2009, JTECH 

1040 <https://Journals.AMetSoc.org/doi/abs/10.1175/2009JTECHA1256.1>} 

1041 and U{Potvin et al. 2012, JTECH 

1042 <https://Journals.AMetSoc.org/doi/abs/10.1175/JTECH-D-11-00019.1>}). 

1043 ''' 

1044 r = h = Radius(radius) 

1045 d = fabs(Distance(distance)) 

1046 if d > h: 

1047 d, h = h, d 

1048 

1049 if d > EPS0: # and h > EPS0 

1050 d = d / h # /= h chokes PyChecker 

1051 s = sin(Phi_(angle=angle, clip=_180_0)) 

1052 s = fsum_(_1_0, _2_0 * s * d, d**2) 

1053 if s > 0: 

1054 return h * sqrt(s) - r 

1055 

1056 raise _ValueError(angle=angle, distance=distance, radius=radius) 

1057 

1058 

1059def horizon(height, radius=R_M, refraction=False): 

1060 '''Determine the distance to the horizon from a given altitude 

1061 above the (spherical) earth. 

1062 

1063 @arg height: Altitude (C{meter} or same units as B{C{radius}}). 

1064 @kwarg radius: Optional mean earth radius (C{meter}). 

1065 @kwarg refraction: Consider atmospheric refraction (C{bool}). 

1066 

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

1068 

1069 @raise ValueError: Invalid B{C{height}} or B{C{radius}}. 

1070 

1071 @see: U{Distance to horizon<https://www.EdWilliams.org/avform.htm#Horizon>}. 

1072 ''' 

1073 h, r = Height(height), Radius(radius) 

1074 if min(h, r) < 0: 

1075 raise _ValueError(height=height, radius=radius) 

1076 

1077 if refraction: 

1078 d2 = 2.415750694528 * h * r # 2.0 / 0.8279 

1079 else: 

1080 d2 = h * fsum_(r, r, h) 

1081 return sqrt0(d2) 

1082 

1083 

1084def intersections2(lat1, lon1, radius1, 

1085 lat2, lon2, radius2, datum=None, wrap=True): 

1086 '''Conveniently compute the intersections of two circles each defined 

1087 by a (geodetic) center point and a radius, using either ... 

1088 

1089 1) L{vector3d.intersections2} for small distances (below 100 KM or 

1090 about 0.9 degrees) or if no B{C{datum}} is specified, or ... 

1091 

1092 2) L{sphericalTrigonometry.intersections2} for a spherical B{C{datum}} 

1093 or if B{C{datum}} is a C{scalar} representing the earth radius, 

1094 conventionally in C{meter} or ... 

1095 

1096 3) L{ellipsoidalKarney.intersections2} for an ellipsoidal B{C{datum}} 

1097 and if I{Karney}'s U{geographiclib<https://PyPI.org/project/geographiclib>} 

1098 is installed, otherwise ... 

1099 

1100 4) L{ellipsoidalExact.intersections2}, provided B{C{datum}} is ellipsoidal. 

1101 

1102 @arg lat1: Latitude of the first circle center (C{degrees}). 

1103 @arg lon1: Longitude of the first circle center (C{degrees}). 

1104 @arg radius1: Radius of the first circle (C{meter}, conventionally). 

1105 @arg lat2: Latitude of the second circle center (C{degrees}). 

1106 @arg lon2: Longitude of the second circle center (C{degrees}). 

1107 @arg radius2: Radius of the second circle (C{meter}, same units as B{C{radius1}}). 

1108 @kwarg datum: Optional ellipsoidal or spherical datum (L{Datum}, 

1109 L{Ellipsoid}, L{Ellipsoid2}, L{a_f2Tuple} or 

1110 C{scalar} earth radius in C{meter}, same units as 

1111 B{C{radius1}} and B{C{radius2}}) or C{None}. 

1112 @kwarg wrap: Wrap and unroll longitudes (C{bool}). 

1113 

1114 @return: 2-Tuple of the intersection points, each a 

1115 L{LatLon2Tuple}C{(lat, lon)}. For abutting circles, 

1116 the points are the same instance, aka I{radical center}. 

1117 

1118 @raise IntersectionError: Concentric, antipodal, invalid or 

1119 non-intersecting circles or no 

1120 convergence. 

1121 

1122 @raise TypeError: Invalid B{C{datum}}. 

1123 

1124 @raise UnitError: Invalid B{C{lat1}}, B{C{lon1}}, B{C{radius1}}, 

1125 B{C{lat2}}, B{C{lon2}} or B{C{radius2}}. 

1126 ''' 

1127 if datum is None or euclidean(lat1, lon1, lat2, lon2, radius=R_M, 

1128 adjust=True, wrap=wrap) < _100km: 

1129 def _V2T(x, y, _, **unused): # _ == z unused 

1130 return LatLon2Tuple(y, x, name=intersections2.__name__) 

1131 

1132 r1 = m2degrees(Radius_(radius1=radius1), radius=R_M, lat=lat1) 

1133 r2 = m2degrees(Radius_(radius2=radius2), radius=R_M, lat=lat2) 

1134 

1135 _, lon2 = unroll180(lon1, lon2, wrap=wrap) 

1136 m = _MODS.vector3d 

1137 t = m.intersections2(m.Vector3d(lon1, lat1, 0), r1, 

1138 m.Vector3d(lon2, lat2, 0), r2, sphere=False, 

1139 Vector=_V2T) 

1140 else: 

1141 def _LL2T(lat, lon, **unused): 

1142 return LatLon2Tuple(lat, lon, name=intersections2.__name__) 

1143 

1144 d = _spherical_datum(datum, name=intersections2.__name__) 

1145 if d.isSpherical: 

1146 m = _MODS.sphericalTrigonometry 

1147 elif d.isEllipsoidal: 

1148 try: 

1149 if d.ellipsoid.geodesic: 

1150 pass 

1151 m = _MODS.ellipsoidalKarney 

1152 except ImportError: 

1153 m = _MODS.ellipsoidalExact 

1154 else: 

1155 raise _AssertionError(datum=d) 

1156 

1157 t = m.intersections2(m.LatLon(lat1, lon1, datum=d), radius1, 

1158 m.LatLon(lat2, lon2, datum=d), radius2, 

1159 LatLon=_LL2T, height=0, wrap=wrap) 

1160 return t 

1161 

1162 

1163def isantipode(lat1, lon1, lat2, lon2, eps=EPS): 

1164 '''Check whether two points are antipodal, on diametrically 

1165 opposite sides of the earth. 

1166 

1167 @arg lat1: Latitude of one point (C{degrees}). 

1168 @arg lon1: Longitude of one point (C{degrees}). 

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

1170 @arg lon2: Longitude of the other point (C{degrees}). 

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

1172 

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

1174 B{C{eps}} tolerance, C{False} otherwise. 

1175 

1176 @see: Functions L{isantipode_} and L{antipode}. 

1177 ''' 

1178 return True if (fabs(lat1 + lat2) <= eps and 

1179 fabs(lon1 + lon2) <= eps) else \ 

1180 _MODS.latlonBase._isequalTo(antipode(lat1, lon1), 

1181 normal(lat2, lon2), eps=eps) 

1182 

1183 

1184def isantipode_(phi1, lam1, phi2, lam2, eps=EPS): 

1185 '''Check whether two points are antipodal, on diametrically 

1186 opposite sides of the earth. 

1187 

1188 @arg phi1: Latitude of one point (C{radians}). 

1189 @arg lam1: Longitude of one point (C{radians}). 

1190 @arg phi2: Latitude of the other point (C{radians}). 

1191 @arg lam2: Longitude of the other point (C{radians}). 

1192 @kwarg eps: Tolerance for near-equality (C{radians}). 

1193 

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

1195 B{C{eps}} tolerance, C{False} otherwise. 

1196 

1197 @see: Functions L{isantipode} and L{antipode_}. 

1198 ''' 

1199 return True if (fabs(phi1 + phi2) <= eps and 

1200 fabs(lam1 + lam2) <= eps) else \ 

1201 _MODS.latlonBase._isequalTo_(antipode_(phi1, lam1), 

1202 normal_(phi2, lam2), eps=eps) 

1203 

1204 

1205def isnormal(lat, lon, eps=0): 

1206 '''Check whether B{C{lat}} I{and} B{C{lon}} are within the I{normal} 

1207 range in C{degrees}. 

1208 

1209 @arg lat: Latitude (C{degrees}). 

1210 @arg lon: Longitude (C{degrees}). 

1211 @kwarg eps: Optional tolerance below C{90} and C{180 degrees}. 

1212 

1213 @return: C{True} if C{(abs(B{lat}) + B{eps}) <= 90} and 

1214 C{(abs(B{lon}) + B{eps}) <= 180}, C{False} othwerwise. 

1215 

1216 @see: Functions L{isnormal_} and L{normal}. 

1217 ''' 

1218 return (_90_0 - fabs(lat)) >= eps and (_180_0 - fabs(lon)) >= eps 

1219 

1220 

1221def isnormal_(phi, lam, eps=0): 

1222 '''Check whether B{C{phi}} I{and} B{C{lam}} are within the I{normal} 

1223 range in C{radians}. 

1224 

1225 @arg phi: Latitude (C{radians}). 

1226 @arg lam: Longitude (C{radians}). 

1227 @kwarg eps: Optional tolerance below C{PI/2} and C{PI radians}. 

1228 

1229 @return: C{True} if C{(abs(B{phi}) + B{eps}) <= PI/2} and 

1230 C{(abs(B{lam}) + B{eps}) <= PI}, C{False} othwerwise. 

1231 

1232 @see: Functions L{isnormal} and L{normal_}. 

1233 ''' 

1234 return (PI_2 - fabs(phi)) >= eps and (PI - fabs(lam)) >= eps 

1235 

1236 

1237def latlon2n_xyz(lat, lon, name=NN): 

1238 '''Convert lat-, longitude to C{n-vector} (I{normal} to the 

1239 earth's surface) X, Y and Z components. 

1240 

1241 @arg lat: Latitude (C{degrees}). 

1242 @arg lon: Longitude (C{degrees}). 

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

1244 

1245 @return: A L{Vector3Tuple}C{(x, y, z)}. 

1246 

1247 @see: Function L{philam2n_xyz}. 

1248 

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

1250 I{NOT} geocentric ECEF x, y and z coordinates! 

1251 ''' 

1252 return _2n_xyz(name, *sincos2d_(lat, lon)) 

1253 

1254 

1255def _normal2(a, b, n_2, n, n2): 

1256 '''(INTERNAL) Helper for C{normal} and C{normal_}. 

1257 ''' 

1258 if fabs(b) > n: 

1259 b = remainder(b, n2) 

1260 r = remainder(a, n) if fabs(a) > n_2 else a 

1261 if r != a: 

1262 r = -r 

1263 b -= n if b > 0 else -n 

1264 return float0(r, b) 

1265 

1266 

1267def normal(lat, lon, name=NN): 

1268 '''Normalize a lat- I{and} longitude pair in C{degrees}. 

1269 

1270 @arg lat: Latitude (C{degrees}). 

1271 @arg lon: Longitude (C{degrees}). 

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

1273 

1274 @return: L{LatLon2Tuple}C{(lat, lon)} with C{abs(lat) <= 90} 

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

1276 

1277 @see: Functions L{normal_} and L{isnormal}. 

1278 ''' 

1279 return LatLon2Tuple(*_normal2(lat, lon, _90_0, _180_0, _360_0), name=name) 

1280 

1281 

1282def normal_(phi, lam, name=NN): 

1283 '''Normalize a lat- I{and} longitude pair in C{radians}. 

1284 

1285 @arg phi: Latitude (C{radians}). 

1286 @arg lam: Longitude (C{radians}). 

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

1288 

1289 @return: L{PhiLam2Tuple}C{(phi, lam)} with C{abs(phi) <= PI/2} 

1290 and C{abs(lam) <= PI}. 

1291 

1292 @see: Functions L{normal} and L{isnormal_}. 

1293 ''' 

1294 return PhiLam2Tuple(*_normal2(phi, lam, PI_2, PI, PI2), name=name) 

1295 

1296 

1297def _2n_xyz(name, sa, ca, sb, cb): 

1298 '''(INTERNAL) Helper for C{latlon2n_xyz} and C{philam2n_xyz}. 

1299 ''' 

1300 # Kenneth Gade eqn 3, but using right-handed 

1301 # vector x -> 0°E,0°N, y -> 90°E,0°N, z -> 90°N 

1302 return Vector3Tuple(ca * cb, ca * sb, sa, name=name) 

1303 

1304 

1305def n_xyz2latlon(x, y, z, name=NN): 

1306 '''Convert C{n-vector} components to lat- and longitude in C{degrees}. 

1307 

1308 @arg x: X component (C{scalar}). 

1309 @arg y: Y component (C{scalar}). 

1310 @arg z: Z component (C{scalar}). 

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

1312 

1313 @return: A L{LatLon2Tuple}C{(lat, lon)}. 

1314 

1315 @see: Function L{n_xyz2philam}. 

1316 ''' 

1317 return LatLon2Tuple(atan2d(z, hypot(x, y)), atan2d(y, x), name=name) 

1318 

1319 

1320def n_xyz2philam(x, y, z, name=NN): 

1321 '''Convert C{n-vector} components to lat- and longitude in C{radians}. 

1322 

1323 @arg x: X component (C{scalar}). 

1324 @arg y: Y component (C{scalar}). 

1325 @arg z: Z component (C{scalar}). 

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

1327 

1328 @return: A L{PhiLam2Tuple}C{(phi, lam)}. 

1329 

1330 @see: Function L{n_xyz2latlon}. 

1331 ''' 

1332 return PhiLam2Tuple(atan2(z, hypot(x, y)), atan2(y, x), name=name) 

1333 

1334 

1335def _opposes(d, m, n, n2): 

1336 '''(INETNAL) Helper for C{opposing} and C{opposing_}. 

1337 ''' 

1338 d = d % n2 # -20 % 360 == 340, -1 % PI2 == PI2 - 1 

1339 return False if d < m or d > (n2 - m) else ( 

1340 True if (n - m) < d < (n + m) else None) 

1341 

1342 

1343def opposing(bearing1, bearing2, margin=_90_0): 

1344 '''Compare the direction of two bearings given in C{degrees}. 

1345 

1346 @arg bearing1: First bearing (compass C{degrees}). 

1347 @arg bearing2: Second bearing (compass C{degrees}). 

1348 @kwarg margin: Optional, interior angle bracket (C{degrees}). 

1349 

1350 @return: C{True} if both bearings point in opposite, C{False} if 

1351 in similar or C{None} if in perpendicular directions. 

1352 

1353 @see: Function L{opposing_}. 

1354 ''' 

1355 m = Degrees_(margin=margin, low=EPS0, high=_90_0) 

1356 return _opposes(bearing2 - bearing1, m,_180_0, _360_0) 

1357 

1358 

1359def opposing_(radians1, radians2, margin=PI_2): 

1360 '''Compare the direction of two bearings given in C{radians}. 

1361 

1362 @arg radians1: First bearing (C{radians}). 

1363 @arg radians2: Second bearing (C{radians}). 

1364 @kwarg margin: Optional, interior angle bracket (C{radians}). 

1365 

1366 @return: C{True} if both bearings point in opposite, C{False} if 

1367 in similar or C{None} if in perpendicular directions. 

1368 

1369 @see: Function L{opposing}. 

1370 ''' 

1371 m = Radians_(margin=margin, low=EPS0, high=PI_2) 

1372 return _opposes(radians2 - radians1, m, PI, PI2) 

1373 

1374 

1375def philam2n_xyz(phi, lam, name=NN): 

1376 '''Convert lat-, longitude to C{n-vector} (I{normal} to the 

1377 earth's surface) X, Y and Z components. 

1378 

1379 @arg phi: Latitude (C{radians}). 

1380 @arg lam: Longitude (C{radians}). 

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

1382 

1383 @return: A L{Vector3Tuple}C{(x, y, z)}. 

1384 

1385 @see: Function L{latlon2n_xyz}. 

1386 

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

1388 I{NOT} geocentric ECEF x, y and z coordinates! 

1389 ''' 

1390 return _2n_xyz(name, *sincos2_(phi, lam)) 

1391 

1392 

1393def _radical2(d, r1, r2): # in .ellipsoidalBaseDI, .sphericalTrigonometry, .vector3d 

1394 # (INTERNAL) See C{radical2} below 

1395 # assert d > EPS0 

1396 r = fsum_(_1_0, (r1 / d)**2, -(r2 / d)**2) * _0_5 

1397 return Radical2Tuple(max(_0_0, min(_1_0, r)), r * d) 

1398 

1399 

1400def radical2(distance, radius1, radius2): 

1401 '''Compute the I{radical ratio} and I{radical line} of two 

1402 U{intersecting circles<https://MathWorld.Wolfram.com/ 

1403 Circle-CircleIntersection.html>}. 

1404 

1405 The I{radical line} is perpendicular to the axis thru the 

1406 centers of the circles at C{(0, 0)} and C{(B{distance}, 0)}. 

1407 

1408 @arg distance: Distance between the circle centers (C{scalar}). 

1409 @arg radius1: Radius of the first circle (C{scalar}). 

1410 @arg radius2: Radius of the second circle (C{scalar}). 

1411 

1412 @return: A L{Radical2Tuple}C{(ratio, xline)} where C{0.0 <= 

1413 ratio <= 1.0} and C{xline} is along the B{C{distance}}. 

1414 

1415 @raise IntersectionError: The B{C{distance}} exceeds the sum 

1416 of B{C{radius1}} and B{C{radius2}}. 

1417 

1418 @raise UnitError: Invalid B{C{distance}}, B{C{radius1}} or 

1419 B{C{radius2}}. 

1420 

1421 @see: U{Circle-Circle Intersection 

1422 <https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>}. 

1423 ''' 

1424 d = Distance_(distance, low=_0_0) 

1425 r1 = Radius_(radius1=radius1) 

1426 r2 = Radius_(radius2=radius2) 

1427 if d > (r1 + r2): 

1428 raise IntersectionError(distance=d, radius1=r1, radius2=r2, 

1429 txt=_too_(_distant_)) 

1430 return _radical2(d, r1, r2) if d > EPS0 else \ 

1431 Radical2Tuple(_0_5, _0_0) 

1432 

1433 

1434class Radical2Tuple(_NamedTuple): 

1435 '''2-Tuple C{(ratio, xline)} of the I{radical} C{ratio} and 

1436 I{radical} C{xline}, both C{scalar} and C{0.0 <= ratio <= 1.0} 

1437 ''' 

1438 _Names_ = (_ratio_, _xline_) 

1439 _Units_ = ( Scalar, Scalar) 

1440 

1441 

1442def _scale_deg(lat1, lat2): # degrees 

1443 # scale factor cos(mean of lats) for delta lon 

1444 m = fabs(lat1 + lat2) * _0_5 

1445 return cos(radians(m)) if m < 90 else _0_0 

1446 

1447 

1448def _scale_rad(phi1, phi2): # radians, by .frechet, .hausdorff, .heights 

1449 # scale factor cos(mean of phis) for delta lam 

1450 m = fabs(phi1 + phi2) * _0_5 

1451 return cos(m) if m < PI_2 else _0_0 

1452 

1453 

1454def _sincosa6(phi2, phi1, lam21): 

1455 '''(INTERNAL) C{sin}es, C{cos}ines and C{acos}ine. 

1456 ''' 

1457 s2, c2, s1, c1, _, c21 = sincos2_(phi2, phi1, lam21) 

1458 return s2, c2, s1, c1, acos1(s1 * s2 + c1 * c2 * c21), c21 

1459 

1460 

1461def thomas(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False): 

1462 '''Compute the distance between two (ellipsoidal) points using 

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

1464 formula. 

1465 

1466 @arg lat1: Start latitude (C{degrees}). 

1467 @arg lon1: Start longitude (C{degrees}). 

1468 @arg lat2: End latitude (C{degrees}). 

1469 @arg lon2: End longitude (C{degrees}). 

1470 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid}, 

1471 L{Ellipsoid2} or L{a_f2Tuple}). 

1472 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

1473 

1474 @return: Distance (C{meter}, same units as the B{C{datum}}'s 

1475 ellipsoid axes). 

1476 

1477 @raise TypeError: Invalid B{C{datum}}. 

1478 

1479 @see: Functions L{thomas_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

1480 L{cosineLaw}, L{equirectangular}, L{euclidean}, L{flatLocal}/L{hubeny}, 

1481 L{flatPolar}, L{haversine}, L{vincentys} and method L{Ellipsoid.distance2}. 

1482 ''' 

1483 return _distanceToE(thomas_, lat1, lat2, datum, 

1484 *unroll180(lon1, lon2, wrap=wrap)) 

1485 

1486 

1487def thomas_(phi2, phi1, lam21, datum=_WGS84): 

1488 '''Compute the I{angular} distance between two (ellipsoidal) points using 

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

1490 formula. 

1491 

1492 @arg phi2: End latitude (C{radians}). 

1493 @arg phi1: Start latitude (C{radians}). 

1494 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

1495 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid}, 

1496 L{Ellipsoid2} or L{a_f2Tuple}). 

1497 

1498 @return: Angular distance (C{radians}). 

1499 

1500 @raise TypeError: Invalid B{C{datum}}. 

1501 

1502 @see: Functions L{thomas}, L{cosineAndoyerLambert_}, 

1503 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

1504 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, 

1505 L{flatPolar_}, L{haversine_} and L{vincentys_} and U{Geodesy-PHP 

1506 <https://GitHub.com/jtejido/geodesy-php/blob/master/src/Geodesy/ 

1507 Distance/ThomasFormula.php>}. 

1508 ''' 

1509 s2, c2, s1, c1, r, _ = _sincosa6(phi2, phi1, lam21) 

1510 if r and isnon0(c1) and isnon0(c2): 

1511 E = _ellipsoidal(datum, thomas_) 

1512 if E.f: 

1513 r1 = atan2(E.b_a * s1, c1) 

1514 r2 = atan2(E.b_a * s2, c2) 

1515 

1516 j = (r2 + r1) * _0_5 

1517 k = (r2 - r1) * _0_5 

1518 sj, cj, sk, ck, h, _ = sincos2_(j, k, lam21 * _0_5) 

1519 

1520 h = fsum_(sk**2, (ck * h)**2, -(sj * h)**2) 

1521 u = _1_0 - h 

1522 if isnon0(u) and isnon0(h): 

1523 r = atan(sqrt0(h / u)) * _2_0 # == acos(1 - 2 * h) 

1524 sr, cr = sincos2(r) 

1525 if isnon0(sr): 

1526 u = 2 * (sj * ck)**2 / u 

1527 h = 2 * (sk * cj)**2 / h 

1528 x = u + h 

1529 y = u - h 

1530 

1531 s = r / sr 

1532 e = 4 * s**2 

1533 d = 2 * cr 

1534 a = e * d 

1535 b = 2 * r 

1536 c = s - (a - d) * _0_5 

1537 f = E.f * _0_25 

1538 

1539 t = fsum_(a * x, -b * y, c * x**2, -d * y**2, e * x * y) 

1540 r -= fsum_(s * x, -y, -t * f * _0_25) * f * sr 

1541 return r 

1542 

1543 

1544def vincentys(lat1, lon1, lat2, lon2, radius=R_M, wrap=False): 

1545 '''Compute the distance between two (spherical) points using 

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

1547 spherical formula. 

1548 

1549 @arg lat1: Start latitude (C{degrees}). 

1550 @arg lon1: Start longitude (C{degrees}). 

1551 @arg lat2: End latitude (C{degrees}). 

1552 @arg lon2: End longitude (C{degrees}). 

1553 @kwarg radius: Mean earth radius, ellipsoid or datum (C{meter}, 

1554 L{Ellipsoid}, L{Ellipsoid2}, L{Datum} or L{a_f2Tuple}). 

1555 @kwarg wrap: Wrap and L{pygeodesy.unroll180} longitudes (C{bool}). 

1556 

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

1558 

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

1560 

1561 @see: Functions L{vincentys_}, L{cosineAndoyerLambert}, 

1562 L{cosineForsytheAndoyerLambert},L{cosineLaw}, L{equirectangular}, 

1563 L{euclidean}, L{flatLocal}/L{hubeny}, L{flatPolar}, 

1564 L{haversine} and L{thomas} and methods L{Ellipsoid.distance2}, 

1565 C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}. 

1566 

1567 @note: See note at function L{vincentys_}. 

1568 ''' 

1569 return _distanceToS(vincentys_, lat1, lat2, radius, 

1570 *unroll180(lon1, lon2, wrap=wrap)) 

1571 

1572 

1573def vincentys_(phi2, phi1, lam21): 

1574 '''Compute the I{angular} distance between two (spherical) points using 

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

1576 spherical formula. 

1577 

1578 @arg phi2: End latitude (C{radians}). 

1579 @arg phi1: Start latitude (C{radians}). 

1580 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

1581 

1582 @return: Angular distance (C{radians}). 

1583 

1584 @see: Functions L{vincentys}, L{cosineAndoyerLambert_}, 

1585 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

1586 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, 

1587 L{flatPolar_}, L{haversine_} and L{thomas_}. 

1588 

1589 @note: Functions L{vincentys_}, L{haversine_} and L{cosineLaw_} 

1590 produce equivalent results, but L{vincentys_} is suitable 

1591 for antipodal points and slightly more expensive (M{3 cos, 

1592 3 sin, 1 hypot, 1 atan2, 6 mul, 2 add}) than L{haversine_} 

1593 (M{2 cos, 2 sin, 2 sqrt, 1 atan2, 5 mul, 1 add}) and 

1594 L{cosineLaw_} (M{3 cos, 3 sin, 1 acos, 3 mul, 1 add}). 

1595 ''' 

1596 s1, c1, s2, c2, s21, c21 = sincos2_(phi1, phi2, lam21) 

1597 

1598 c = c2 * c21 

1599 x = s1 * s2 + c1 * c 

1600 y = c1 * s2 - s1 * c 

1601 return atan2(hypot(c2 * s21, y), x) 

1602 

1603# **) MIT License 

1604# 

1605# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved. 

1606# 

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

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

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

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

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

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

1613# 

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

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

1616# 

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

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

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

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

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

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

1623# OTHER DEALINGS IN THE SOFTWARE.