Coverage for pygeodesy/formy.py: 98%

358 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-31 10:52 -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.03.19' 

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_(func_, lat1, lat2, radius, d_lon, unused): 

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

88 ''' 

89 r = func_(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: Function L{excessGirard}, L{excessLHuilier}, 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}, 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}, 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 units of B{C{radius}} 

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

625 C{None} or C{0}. 

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: Function 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}, 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((lam(phi1) + lam(phi2)) / 2) 

691# 

692# where lambda21 = lambda2 - lambda1 and lam(x) is the Lambertian (or inverse 

693# Gudermannian) function 

694# 

695# lam(x) = asinh(tan(x)) = atanh(sin(x)) = 2 * atanh(tan(x / 2)) 

696# 

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

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

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

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

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

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

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

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

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

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

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

708# - Charles Karney 

709 

710 

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

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

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

714 

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

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

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

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

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

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

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

722 

723 @return: Surface area, I{signed} (I{square} C{meter}, or units of B{C{radius}} 

724 I{squared}) or I{spherical excess} (C{radians}) if B{C{radius}} is 

725 C{None} or C{0}. 

726 

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

728 

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

730 

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

732 ''' 

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

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

735 

736 

737def excessQuad_(phi2, phi1, lam21): 

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

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

740 

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

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

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

744 

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

746 

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

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

749 ''' 

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

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

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

753 

754 

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

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

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

758 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>} 

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

760 

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

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

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

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

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

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

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

768 

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

770 ellipsoid axes). 

771 

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

773 

774 @note: The meridional and prime_vertical radii of curvature 

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

776 

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

778 L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

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

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

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

782 ''' 

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

784 return flatLocal_(Phi_(lat2=lat2), 

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

786 

787hubeny = flatLocal # PYCHOK for Karl Hubeny 

788 

789 

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

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

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

793 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>} 

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

795 

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

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

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

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

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

801 

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

803 

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

805 

806 @note: The meridional and prime_vertical radii of curvature 

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

808 

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

810 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{flatPolar_}, 

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

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

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

814 ''' 

815 E = _ellipsoidal(datum, flatLocal_) 

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

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

818 

819hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny 

820 

821 

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

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

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

825 Geographical_distance#Polar_coordinate_flat-Earth_formula>} 

826 formula. 

827 

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

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

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

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

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

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

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

835 

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

837 ellipsoid or datum axes). 

838 

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

840 

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

842 L{cosineForsytheAndoyerLambert},L{cosineLaw}, 

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

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

845 L{vincentys}. 

846 ''' 

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

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

849 

850 

851def flatPolar_(phi2, phi1, lam21): 

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

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

854 Geographical_distance#Polar_coordinate_flat-Earth_formula>} 

855 formula. 

856 

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

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

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

860 

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

862 

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

864 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

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

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

867 ''' 

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

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

870 if a < b: 

871 a, b = b, a 

872 if a < EPS0: 

873 a = _0_0 

874 elif b > 0: 

875 b = b / a # /= chokes PyChecker 

876 c = b * cos(lam21) * _2_0 

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

878 a *= sqrt0(c) 

879 return a 

880 

881 

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

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

884 from a Point-Of-View in space. 

885 

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

887 or L{Vector3d}). 

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

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

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

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

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

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

894 point plus C{LatLon} keyword arguments, include 

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

896 

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

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

899 

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

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

902 the earth or points in an opposite direction. 

903 

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

905 

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

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

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

909 ''' 

910 def _Error(txt): 

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

912 

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

914 _spherical_datum(earth, name=hartzell.__name__) 

915 E = D.ellipsoid 

916 

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

918 try: 

919 t = _MODS.triaxials 

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

921 except Exception as x: 

922 raise _Error(str(x)) 

923 else: 

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

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

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

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

928 

929 V3 = _MODS.vector3d._otherV3d 

930 p3 = V3(pov=pov) 

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

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

933 

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

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

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

937 

938 t = c2, c2, b2 

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

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

941 raise _Error(_near_(_null_)) 

942 

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

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

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

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

947 if r > 0: 

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

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

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

951 

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

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

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

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

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

957 raise _Error(_too_(_distant_)) 

958 

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

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

961 

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

963 if LatLon_and_kwds: 

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

965 r = c.toLatLon(**LatLon_and_kwds) 

966 return r 

967 

968 

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

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

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

972 formula. 

973 

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

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

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

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

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

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

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

981 

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

983 

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

985 

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

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

988 L{cosineLaw}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

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

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

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

992 

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

994 ''' 

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

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

997 

998 

999def haversine_(phi2, phi1, lam21): 

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

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

1002 formula. 

1003 

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

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

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

1007 

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

1009 

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

1011 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

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

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

1014 

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

1016 ''' 

1017 def _hsin(rad): 

1018 return sin(rad * _0_5)**2 

1019 

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

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

1022 

1023 

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

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

1026 traveling along a straight line at a given tilt. 

1027 

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

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

1030 B{C{radius}}). 

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

1032 

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

1034 

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

1036 

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

1038 (U{Shapiro et al. 2009, JTECH 

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

1040 and U{Potvin et al. 2012, JTECH 

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

1042 ''' 

1043 r = h = Radius(radius) 

1044 d = fabs(Distance(distance)) 

1045 if d > h: 

1046 d, h = h, d 

1047 

1048 if d > EPS0: # and h > EPS0 

1049 d = d / h # /= h chokes PyChecker 

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

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

1052 if s > 0: 

1053 return h * sqrt(s) - r 

1054 

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

1056 

1057 

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

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

1060 above the (spherical) earth. 

1061 

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

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

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

1065 

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

1067 

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

1069 

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

1071 ''' 

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

1073 if min(h, r) < 0: 

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

1075 

1076 if refraction: 

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

1078 else: 

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

1080 return sqrt0(d2) 

1081 

1082 

1083def intersections2(lat1, lon1, radius1, 

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

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

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

1087 

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

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

1090 

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

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

1093 conventionally in C{meter} or ... 

1094 

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

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

1097 is installed, otherwise ... 

1098 

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

1100 

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

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

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

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

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

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

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

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

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

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

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

1112 

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

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

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

1116 

1117 @raise IntersectionError: Concentric, antipodal, invalid or 

1118 non-intersecting circles or no 

1119 convergence. 

1120 

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

1122 

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

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

1125 ''' 

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

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

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

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

1130 

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

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

1133 

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

1135 m = _MODS.vector3d 

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

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

1138 Vector=_V2T) 

1139 else: 

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

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

1142 

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

1144 if d.isSpherical: 

1145 m = _MODS.sphericalTrigonometry 

1146 elif d.isEllipsoidal: 

1147 try: 

1148 if d.ellipsoid.geodesic: 

1149 pass 

1150 m = _MODS.ellipsoidalKarney 

1151 except ImportError: 

1152 m = _MODS.ellipsoidalExact 

1153 else: 

1154 raise _AssertionError(datum=d) 

1155 

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

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

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

1159 return t 

1160 

1161 

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

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

1164 opposite sides of the earth. 

1165 

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

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

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

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

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

1171 

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

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

1174 

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

1176 ''' 

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

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

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

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

1181 

1182 

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

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

1185 opposite sides of the earth. 

1186 

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

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

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

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

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

1192 

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

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

1195 

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

1197 ''' 

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

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

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

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

1202 

1203 

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

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

1206 range in C{degrees}. 

1207 

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

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

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

1211 

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

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

1214 

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

1216 ''' 

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

1218 

1219 

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

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

1222 range in C{radians}. 

1223 

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

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

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

1227 

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

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

1230 

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

1232 ''' 

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

1234 

1235 

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

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

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

1239 

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

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

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

1243 

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

1245 

1246 @see: Function L{philam2n_xyz}. 

1247 

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

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

1250 ''' 

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

1252 

1253 

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

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

1256 ''' 

1257 if fabs(b) > n: 

1258 b = remainder(b, n2) 

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

1260 if r != a: 

1261 r = -r 

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

1263 return float0(r, b) 

1264 

1265 

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

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

1268 

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

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

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

1272 

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

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

1275 

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

1277 ''' 

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

1279 

1280 

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

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

1283 

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

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

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

1287 

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

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

1290 

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

1292 ''' 

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

1294 

1295 

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

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

1298 ''' 

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

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

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

1302 

1303 

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

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

1306 

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

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

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

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

1311 

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

1313 

1314 @see: Function L{n_xyz2philam}. 

1315 ''' 

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

1317 

1318 

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

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

1321 

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

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

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

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

1326 

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

1328 

1329 @see: Function L{n_xyz2latlon}. 

1330 ''' 

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

1332 

1333 

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

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

1336 ''' 

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

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

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

1340 

1341 

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

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

1344 

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

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

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

1348 

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

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

1351 

1352 @see: Function L{opposing_}. 

1353 ''' 

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

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

1356 

1357 

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

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

1360 

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

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

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

1364 

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

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

1367 

1368 @see: Function L{opposing}. 

1369 ''' 

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

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

1372 

1373 

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

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

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

1377 

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

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

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

1381 

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

1383 

1384 @see: Function L{latlon2n_xyz}. 

1385 

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

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

1388 ''' 

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

1390 

1391 

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

1393 # (INTERNAL) See C{radical2} below 

1394 # assert d > EPS0 

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

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

1397 

1398 

1399def radical2(distance, radius1, radius2): 

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

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

1402 Circle-CircleIntersection.html>}. 

1403 

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

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

1406 

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

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

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

1410 

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

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

1413 

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

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

1416 

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

1418 B{C{radius2}}. 

1419 

1420 @see: U{Circle-Circle Intersection 

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

1422 ''' 

1423 d = Distance_(distance, low=_0_0) 

1424 r1 = Radius_(radius1=radius1) 

1425 r2 = Radius_(radius2=radius2) 

1426 if d > (r1 + r2): 

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

1428 txt=_too_(_distant_)) 

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

1430 Radical2Tuple(_0_5, _0_0) 

1431 

1432 

1433class Radical2Tuple(_NamedTuple): 

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

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

1436 ''' 

1437 _Names_ = (_ratio_, _xline_) 

1438 _Units_ = ( Scalar, Scalar) 

1439 

1440 

1441def _scale_deg(lat1, lat2): # degrees 

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

1443 m = fabs(lat1 + lat2) * _0_5 

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

1445 

1446 

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

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

1449 m = fabs(phi1 + phi2) * _0_5 

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

1451 

1452 

1453def _sincosa6(phi2, phi1, lam21): 

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

1455 ''' 

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

1457 return s2, c2, s1, c1, acos1(s1 * s2 + c1 * c2 * c21), c21 

1458 

1459 

1460def thomas(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False): 

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

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

1463 formula. 

1464 

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

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

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

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

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

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

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

1472 

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

1474 ellipsoid axes). 

1475 

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

1477 

1478 @see: Functions L{thomas_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

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

1480 L{flatPolar}, L{haversine}, L{vincentys} and method L{Ellipsoid.distance2}. 

1481 ''' 

1482 return _distanceToE(thomas_, lat1, lat2, datum, 

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

1484 

1485 

1486def thomas_(phi2, phi1, lam21, datum=_WGS84): 

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

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

1489 formula. 

1490 

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

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

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

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

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

1496 

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

1498 

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

1500 

1501 @see: Functions L{thomas}, L{cosineAndoyerLambert_}, 

1502 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

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

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

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

1506 Distance/ThomasFormula.php>}. 

1507 ''' 

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

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

1510 E = _ellipsoidal(datum, thomas_) 

1511 if E.f: 

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

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

1514 

1515 j = (r2 + r1) * _0_5 

1516 k = (r2 - r1) * _0_5 

1517 sj, cj, sk, ck, h, _ = sincos2_(j, k, lam21 * _0_5) 

1518 

1519 h = fsum_(sk**2, (ck * h)**2, -(sj * h)**2) 

1520 u = _1_0 - h 

1521 if isnon0(u) and isnon0(h): 

1522 r = atan(sqrt0(h / u)) * _2_0 # == acos(1 - 2 * h) 

1523 sr, cr = sincos2(r) 

1524 if isnon0(sr): 

1525 u = 2 * (sj * ck)**2 / u 

1526 h = 2 * (sk * cj)**2 / h 

1527 x = u + h 

1528 y = u - h 

1529 

1530 s = r / sr 

1531 e = 4 * s**2 

1532 d = 2 * cr 

1533 a = e * d 

1534 b = 2 * r 

1535 c = s - (a - d) * _0_5 

1536 f = E.f * _0_25 

1537 

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

1539 r -= fsum_(s * x, -y, -t * f * _0_25) * f * sr 

1540 return r 

1541 

1542 

1543def vincentys(lat1, lon1, lat2, lon2, radius=R_M, wrap=False): 

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

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

1546 spherical formula. 

1547 

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

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

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

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

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

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

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

1555 

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

1557 

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

1559 

1560 @see: Functions L{vincentys_}, L{cosineAndoyerLambert}, 

1561 L{cosineForsytheAndoyerLambert},L{cosineLaw}, L{equirectangular}, 

1562 L{euclidean}, L{flatLocal}/L{hubeny}, L{flatPolar}, 

1563 L{haversine} and L{thomas} and methods L{Ellipsoid.distance2}, 

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

1565 

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

1567 ''' 

1568 return _distanceToS(vincentys_, lat1, lat2, radius, 

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

1570 

1571 

1572def vincentys_(phi2, phi1, lam21): 

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

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

1575 spherical formula. 

1576 

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

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

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

1580 

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

1582 

1583 @see: Functions L{vincentys}, L{cosineAndoyerLambert_}, 

1584 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

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

1586 L{flatPolar_}, L{haversine_} and L{thomas_}. 

1587 

1588 @note: Functions L{vincentys_}, L{haversine_} and L{cosineLaw_} 

1589 produce equivalent results, but L{vincentys_} is suitable 

1590 for antipodal points and slightly more expensive (M{3 cos, 

1591 3 sin, 1 hypot, 1 atan2, 6 mul, 2 add}) than L{haversine_} 

1592 (M{2 cos, 2 sin, 2 sqrt, 1 atan2, 5 mul, 1 add}) and 

1593 L{cosineLaw_} (M{3 cos, 3 sin, 1 acos, 3 mul, 1 add}). 

1594 ''' 

1595 s1, c1, s2, c2, s21, c21 = sincos2_(phi1, phi2, lam21) 

1596 

1597 c = c2 * c21 

1598 x = s1 * s2 + c1 * c 

1599 y = c1 * s2 - s1 * c 

1600 return atan2(hypot(c2 * s21, y), x) 

1601 

1602# **) MIT License 

1603# 

1604# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved. 

1605# 

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

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

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

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

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

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

1612# 

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

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

1615# 

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

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

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

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

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

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

1622# OTHER DEALINGS IN THE SOFTWARE.