Coverage for pygeodesy/formy.py: 98%

445 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-12-12 17:20 -0500

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 

9# from pygeodesy.basics import _args_kwds_count2, _copysign # from .constants 

10# from pygeodesy.cartesianBase import CartesianBase # _MODS 

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

12 _0_0s, float0_, isnon0, remainder, _umod_PI2, \ 

13 _0_0, _0_125, _0_25, _0_5, _1_0, _2_0, _4_0, \ 

14 _32_0, _90_0, _180_0, _360_0, _copysign 

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

16 _mean_radius, _spherical_datum, _WGS84, _EWGS84 

17# from pygeodesy.ellipsoids import Ellipsoid, _EWGS84 # from .datums 

18from pygeodesy.errors import IntersectionError, LimitError, limiterrors, \ 

19 _TypeError, _ValueError, _xattr, _xError, \ 

20 _xcallable,_xkwds, _xkwds_pop2 

21from pygeodesy.fmath import euclid, hypot, hypot2, sqrt0 

22from pygeodesy.fsums import fsumf_, Fmt, unstr 

23# from pygeodesy.internals import _DUNDER_nameof # from .named 

24from pygeodesy.interns import _delta_, _distant_, _inside_, _SPACE_, _too_ 

25from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS 

26from pygeodesy.named import _name__, _name2__, _NamedTuple, _xnamed, \ 

27 _DUNDER_nameof 

28from pygeodesy.namedTuples import Bearing2Tuple, Distance4Tuple, LatLon2Tuple, \ 

29 Intersection3Tuple, PhiLam2Tuple 

30# from pygeodesy.streprs import Fmt, unstr # from .fsums 

31# from pygeodesy.triaxials import _hartzell3 # _MODS 

32from pygeodesy.units import _isDegrees, _isHeight, _isRadius, Bearing, Degrees_, \ 

33 Distance, Distance_, Height, Lamd, Lat, Lon, Meter_, \ 

34 Phid, Radians, Radians_, Radius, Radius_, Scalar, _100km 

35from pygeodesy.utily import acos1, atan2, atan2b, degrees2m, _loneg, m2degrees, \ 

36 tan_2, sincos2, sincos2_, _Wrap 

37# from pygeodesy.vector3d import _otherV3d # _MODS 

38# from pygeodesy.vector3dBase import _xyz_y_z3 # _MODS 

39# from pygeodesy import ellipsoidalExact, ellipsoidalKarney, vector3d, \ 

40# sphericalNvector, sphericalTrigonometry # _MODS 

41 

42from contextlib import contextmanager 

43from math import asin, atan, cos, degrees, fabs, radians, sin, sqrt # pow 

44 

45__all__ = _ALL_LAZY.formy 

46__version__ = '24.12.06' 

47 

48_RADIANS2 = (PI / _180_0)**2 # degrees- to radians-squared 

49_ratio_ = 'ratio' 

50_xline_ = 'xline' 

51 

52 

53def angle2chord(rad, radius=R_M): 

54 '''Get the chord length of a (central) angle or I{angular} distance. 

55 

56 @arg rad: Central angle (C{radians}). 

57 @kwarg radius: Mean earth radius (C{meter}, conventionally), datum (L{Datum}) or ellipsoid 

58 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use or C{None}. 

59 

60 @return: Chord length (C{meter}, same units as B{C{radius}} or if C{B{radius} is None}, C{radians}). 

61 

62 @see: Function L{chord2angle}, method L{intermediateChordTo<sphericalNvector.LatLon.intermediateChordTo>} and 

63 U{great-circle-distance<https://WikiPedia.org/wiki/Great-circle_distance#Relation_between_central_angle_and_chord_length>}. 

64 ''' 

65 d = _isDegrees(rad, iscalar=False) 

66 r = sin((radians(rad) if d else rad) / _2_0) * _2_0 

67 return (degrees(r) if d else r) if radius is None else (_mean_radius(radius) * r) 

68 

69 

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

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

72 ''' 

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

74 if r == a: 

75 r = -r 

76 b += n 

77 if fabs(b) > n: 

78 b = remainder(b, n2) 

79 return float0_(r, b) 

80 

81 

82def antipode(lat, lon, **name): 

83 '''Return the antipode, the point diametrically opposite to a given 

84 point in C{degrees}. 

85 

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

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

88 @kwarg name: Optional C{B{name}=NN} (C{str}). 

89 

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

91 

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

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

94 ''' 

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

96 

97 

98def antipode_(phi, lam, **name): 

99 '''Return the antipode, the point diametrically opposite to a given 

100 point in C{radians}. 

101 

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

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

104 @kwarg name: Optional C{B{name}=NN} (C{str}). 

105 

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

107 

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

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

110 ''' 

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

112 

113 

114def bearing(lat1, lon1, lat2, lon2, **final_wrap): 

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

116 between two (spherical) points. 

117 

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

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

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

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

122 @kwarg final_wrap: Optional keyword arguments for function 

123 L{pygeodesy.bearing_}. 

124 

125 @return: Initial or final bearing (compass C{degrees360}) or zero if 

126 both points coincide. 

127 ''' 

128 r = bearing_(Phid(lat1=lat1), Lamd(lon1=lon1), 

129 Phid(lat2=lat2), Lamd(lon2=lon2), **final_wrap) 

130 return degrees(r) 

131 

132 

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

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

135 two (spherical) points. 

136 

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

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

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

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

141 @kwarg final: If C{True}, return the final, otherwise the initial bearing 

142 (C{bool}). 

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

144 B{C{lam2}} (C{bool}). 

145 

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

147 are coincident. 

148 

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

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

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

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

153 ''' 

154 db, phi2, lam2 = _Wrap.philam3(lam1, phi2, lam2, wrap) 

155 if final: # swap plus PI 

156 phi1, lam1, phi2, lam2, db = phi2, lam2, phi1, lam1, -db 

157 r = PI3 

158 else: 

159 r = PI2 

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

161 

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

163 y = sdb * ca2 

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

165 

166 

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

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

169 ''' 

170 try: # for LatLon_ and ellipsoidal LatLon 

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

172 except AttributeError: 

173 pass 

174 # XXX spherical version, OK for ellipsoidal ispolar? 

175 t = p1.philam + p2.philam 

176 return Bearing2Tuple(degrees(bearing_(*t, final=False, wrap=wrap)), 

177 degrees(bearing_(*t, final=True, wrap=wrap)), 

178 name__=_bearingTo2) 

179 

180 

181def chord2angle(chord, radius=R_M): 

182 '''Get the (central) angle from a chord length or distance. 

183 

184 @arg chord: Length or distance (C{meter}, same units as B{C{radius}}). 

185 @kwarg radius: Mean earth radius (C{meter}, conventionally), datum (L{Datum}) or 

186 ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use. 

187 

188 @return: Angle (C{radians} with sign of B{C{chord}}) or C{0} if C{B{radius}=0}. 

189 

190 @note: The angle will exceed C{PI} if C{B{chord} > B{radius} * 2}. 

191 

192 @see: Function L{angle2chord}. 

193 ''' 

194 m = _mean_radius(radius) 

195 r = fabs(chord / (m * _2_0)) if m > 0 else _0_0 

196 if r: 

197 i = int(r) 

198 if i > 0: 

199 r -= i 

200 i *= PI 

201 r = asin(r) + i 

202 return _copysign(r * _2_0, chord) 

203 

204 

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

206 '''Return the angle from North for the direction vector M{(lon2 - lon1, 

207 lat2 - lat1)} between two points. 

208 

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

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

211 

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

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

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

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

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

217 mean latitude (C{bool}). 

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

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

220 

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

222 

223 @note: Courtesy of Martin Schultz. 

224 

225 @see: U{Local, flat earth approximation 

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

227 ''' 

228 d_lon, lat2, lon2 = _Wrap.latlon3(lon1, lat2, lon2, wrap) 

229 if adjust: # scale delta lon 

230 d_lon *= _scale_deg(lat1, lat2) 

231 return atan2b(d_lon, lat2 - lat1) 

232 

233 

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

235 '''Compute the distance between two (ellipsoidal) points using the U{Andoyer-Lambert 

236 <https://books.google.com/books?id=x2UiAQAAIAAJ>} correction of the U{Law of 

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

238 

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

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

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

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

243 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

244 L{a_f2Tuple}) to use. 

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

246 B{C{lon2}} (C{bool}). 

247 

248 @return: Distance (C{meter}, same units as the B{C{datum}}'s ellipsoid axes or 

249 C{radians} if C{B{datum} is None}). 

250 

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

252 

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

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

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

256 L{Ellipsoid.distance2}. 

257 ''' 

258 return _dE(cosineAndoyerLambert_, datum, wrap, lat1, lon1, lat2, lon2) 

259 

260 

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

262 '''Compute the I{angular} distance between two (ellipsoidal) points using the U{Andoyer-Lambert 

263 <https://books.google.com/books?id=x2UiAQAAIAAJ>} correction of the U{Law of 

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

265 

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

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

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

269 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

270 L{a_f2Tuple}) to use. 

271 

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

273 

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

275 

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

277 L{cosineLaw_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, L{flatPolar_}, 

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

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

280 AndoyerLambert.php>}. 

281 ''' 

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

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

284 E = _ellipsoidal(datum, cosineAndoyerLambert_) 

285 if E.f: # ellipsoidal 

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

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

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

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

290 if r: 

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

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

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

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

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

296 return r 

297 

298 

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

300 '''Compute the distance between two (ellipsoidal) points using the U{Forsythe-Andoyer-Lambert 

301 <https://www2.UNB.Ca/gge/Pubs/TR77.pdf>} correction of the U{Law of Cosines 

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

303 

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

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

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

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

308 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

309 L{a_f2Tuple}) to use. 

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

311 B{C{lon2}} (C{bool}). 

312 

313 @return: Distance (C{meter}, same units as the B{C{datum}}'s ellipsoid axes or 

314 C{radians} if C{B{datum} is None}). 

315 

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

317 

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

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

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

321 L{Ellipsoid.distance2}. 

322 ''' 

323 return _dE(cosineForsytheAndoyerLambert_, datum, wrap, lat1, lon1, lat2, lon2) 

324 

325 

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

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

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

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

330 formula. 

331 

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

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

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

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

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

337 

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

339 

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

341 

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

343 L{cosineLaw_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, L{flatPolar_}, 

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

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

346 Distance/ForsytheCorrection.php>}. 

347 ''' 

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

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

350 E = _ellipsoidal(datum, cosineForsytheAndoyerLambert_) 

351 if E.f: # ellipsoidal 

352 sr, cr, s2r, _ = sincos2_(r, r * 2) 

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

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

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

356 x = s + t 

357 y = s - t 

358 

359 s = 8 * r**2 / sr 

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

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

362 b = -2 * d 

363 e = 30 * s2r 

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

365 t = fsumf_( a * x, e * y**2, b * y, -c * x**2, d * x * y) 

366 

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

368 return r 

369 

370 

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

372 '''Compute the distance between two points using the U{spherical Law of Cosines 

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

374 

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

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

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

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

379 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) 

380 or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

381 L{a_f2Tuple}) to use. 

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

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

384 

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

386 ellipsoid or datum axes). 

387 

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

389 

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

391 L{cosineForsytheAndoyerLambert}, L{equirectangular}, L{euclidean}, 

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

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

394 

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

396 ''' 

397 return _dS(cosineLaw_, radius, wrap, lat1, lon1, lat2, lon2) 

398 

399 

400def cosineLaw_(phi2, phi1, lam21): 

401 '''Compute the I{angular} distance between two points using the U{spherical Law of 

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

403 

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

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

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

407 

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

409 

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

411 L{cosineForsytheAndoyerLambert_}, L{euclidean_}, 

412 L{flatLocal_}/L{hubeny_}, L{flatPolar_}, L{haversine_}, 

413 L{thomas_} and L{vincentys_}. 

414 

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

416 ''' 

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

418 

419 

420def _d3(wrap, lat1, lon1, lat2, lon2): 

421 '''(INTERNAL) Helper for _dE, _dS and _eA. 

422 ''' 

423 if wrap: 

424 d_lon, lat2, _ = _Wrap.latlon3(lon1, lat2, lon2, wrap) 

425 return radians(lat2), Phid(lat1=lat1), radians(d_lon) 

426 else: # for backward compaibility 

427 return Phid(lat2=lat2), Phid(lat1=lat1), Phid(d_lon=lon2 - lon1) 

428 

429 

430def _dE(func_, earth, *wrap_lls): 

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

432 ''' 

433 E = _ellipsoidal(earth, func_) 

434 r = func_(*_d3(*wrap_lls), datum=E) 

435 return r * E.a 

436 

437 

438def _dS(func_, radius, *wrap_lls, **adjust): 

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

440 ''' 

441 r = func_(*_d3(*wrap_lls), **adjust) 

442 if radius is not R_M: 

443 _, lat1, _, lat2, _ = wrap_lls 

444 radius = _mean_radius(radius, lat1, lat2) 

445 return r * radius 

446 

447 

448def _eA(excess_, radius, *wrap_lls): 

449 '''(INTERNAL) Helper for spherical excess or area. 

450 ''' 

451 r = excess_(*_d3(*wrap_lls)) 

452 if radius: 

453 _, lat1, _, lat2, _ = wrap_lls 

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

455 return r 

456 

457 

458def _ellipsoidal(earth, where): 

459 '''(INTERNAL) Helper for distances. 

460 ''' 

461 return _EWGS84 if earth in (_WGS84, _EWGS84) else ( 

462 earth if isinstance(earth, Ellipsoid) else 

463 (earth if isinstance(earth, Datum) else # PYCHOK indent 

464 _ellipsoidal_datum(earth, name__=where)).ellipsoid) 

465 

466 

467def equirectangular(lat1, lon1, lat2, lon2, radius=R_M, **adjust_limit_wrap): 

468 '''Approximate the distance between two points using the U{Equirectangular Approximation 

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

470 

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

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

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

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

475 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid 

476 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}). 

477 @kwarg adjust_limit_wrap: Optional keyword arguments for function L{equirectangular4}. 

478 

479 @return: Distance (C{meter}, same units as B{C{radius}} or the ellipsoid or datum axes). 

480 

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

482 

483 @see: Function L{equirectangular4} for more details, the available B{C{options}}, 

484 errors, restrictions and other, approximate or accurate distance functions. 

485 ''' 

486 d = sqrt(equirectangular4(Lat(lat1=lat1), Lon(lon1=lon1), 

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

488 **adjust_limit_wrap).distance2) # PYCHOK 4 vs 2-3 

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

490 

491 

492def _equirectangular(lat1, lon1, lat2, lon2, **adjust_limit_wrap): 

493 '''(INTERNAL) Helper for classes L{frechet._FrechetMeterRadians} and 

494 L{hausdorff._HausdorffMeterRedians}. 

495 ''' 

496 return equirectangular4(lat1, lon1, lat2, lon2, **adjust_limit_wrap).distance2 * _RADIANS2 

497 

498 

499def equirectangular4(lat1, lon1, lat2, lon2, adjust=True, limit=45, wrap=False): 

500 '''Approximate the distance between two points using the U{Equirectangular Approximation 

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

502 

503 This approximation is valid for short distance of several hundred Km or Miles, see 

504 the B{C{limit}} keyword argument and L{LimitError}. 

505 

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

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

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

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

510 @kwarg adjust: Adjust the wrapped, unrolled longitudinal delta by the cosine of the mean 

511 latitude (C{bool}). 

512 @kwarg limit: Optional limit for lat- and longitudinal deltas (C{degrees}) or C{None} 

513 or C{0} for unlimited. 

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

515 (C{bool}). 

516 

517 @return: A L{Distance4Tuple}C{(distance2, delta_lat, delta_lon, unroll_lon2)} with 

518 C{distance2} in C{degrees squared}. 

519 

520 @raise LimitError: The lat- or longitudinal delta exceeds the B{C{-limit..limit}} 

521 range and L{limiterrors<pygeodesy.limiterrors>} is C{True}. 

522 

523 @see: U{Local, flat earth approximation<https://www.EdWilliams.org/avform.htm#flat>}, 

524 functions L{equirectangular}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

525 L{cosineLaw}, L{euclidean}, L{flatLocal}/L{hubeny}, L{flatPolar}, L{haversine}, 

526 L{thomas} and L{vincentys} and methods L{Ellipsoid.distance2}, C{LatLon.distanceTo*} 

527 and C{LatLon.equirectangularTo}. 

528 ''' 

529 if wrap: 

530 d_lon, lat2, ulon2 = _Wrap.latlon3(lon1, lat2, lon2, wrap) 

531 else: 

532 d_lon, ulon2 = (lon2 - lon1), lon2 

533 d_lat = lat2 - lat1 

534 

535 if limit and limit > 0 and limiterrors(): 

536 d = max(fabs(d_lat), fabs(d_lon)) 

537 if d > limit: 

538 t = _SPACE_(_delta_, Fmt.PAREN_g(d), Fmt.exceeds_limit(limit)) 

539 s = unstr(equirectangular4, lat1, lon1, lat2, lon2, 

540 limit=limit, wrap=wrap) 

541 raise LimitError(s, txt=t) 

542 

543 if adjust: # scale delta lon 

544 d_lon *= _scale_deg(lat1, lat2) 

545 

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

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

548 

549 

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

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

552 

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

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

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

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

557 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) 

558 or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

559 L{a_f2Tuple}) to use. 

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

561 the mean latitude (C{bool}). 

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

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

564 

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

566 ellipsoid or datum axes). 

567 

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

569 

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

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

572 L{euclidean_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

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

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

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

576 ''' 

577 return _dS(euclidean_, radius, wrap, lat1, lon1, lat2, lon2, adjust=adjust) 

578 

579 

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

581 '''Approximate the I{angular} C{Euclidean} distance between two (spherical) points. 

582 

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

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

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

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

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

588 

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

590 

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

592 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

593 L{flatLocal_}/L{hubeny_}, L{flatPolar_}, L{haversine_}, 

594 L{thomas_} and L{vincentys_}. 

595 ''' 

596 if adjust: 

597 lam21 *= _scale_rad(phi2, phi1) 

598 return euclid(phi2 - phi1, lam21) 

599 

600 

601def excessAbc_(A, b, c): 

602 '''Compute the I{spherical excess} C{E} of a (spherical) triangle from two sides 

603 and the included (small) angle. 

604 

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

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

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

608 

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

610 

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

612 

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

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

615 ''' 

616 A = Radians_(A=A) 

617 b = Radians_(b=b) * _0_5 

618 c = Radians_(c=c) * _0_5 

619 

620 sA, cA, sb, cb, sc, cc = sincos2_(A, b, c) 

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

622 

623 

624def excessCagnoli_(a, b, c): 

625 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using U{Cagnoli's 

626 <https://Zenodo.org/record/35392>} (D.34) formula. 

627 

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

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

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

631 

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

633 

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

635 

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

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

638 ''' 

639 a = Radians_(a=a) 

640 b = Radians_(b=b) 

641 c = Radians_(c=c) 

642 

643 s = fsumf_(a, b, c) * _0_5 

644 _s = sin 

645 r = _s(s) * _s(s - a) * _s(s - b) * _s(s - c) 

646 c = cos(a * _0_5) * cos(b * _0_5) * cos(c * _0_5) 

647 r = asin(sqrt(r) * _0_5 / c) if c and r > 0 else _0_0 

648 return Radians(Cagnoli=r * _2_0) 

649 

650 

651def excessGirard_(A, B, C): 

652 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using U{Girard's 

653 <https://MathWorld.Wolfram.com/GirardsSphericalExcessFormula.html>} formula. 

654 

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

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

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

658 

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

660 

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

662 

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

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

665 ''' 

666 return Radians(Girard=fsumf_(Radians_(A=A), 

667 Radians_(B=B), 

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

669 

670 

671def excessLHuilier_(a, b, c): 

672 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using U{L'Huilier's 

673 <https://MathWorld.Wolfram.com/LHuiliersTheorem.html>}'s Theorem. 

674 

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

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

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

678 

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

680 

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

682 

683 @see: Function L{excessCagnoli_}, L{excessGirard_} and U{Spherical 

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

685 ''' 

686 a = Radians_(a=a) 

687 b = Radians_(b=b) 

688 c = Radians_(c=c) 

689 

690 s = fsumf_(a, b, c) * _0_5 

691 _t = tan_2 

692 r = _t(s) * _t(s - a) * _t(s - b) * _t(s - c) 

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

694 return Radians(LHuilier=r * _4_0) 

695 

696 

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

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

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

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

701 method. 

702 

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

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

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

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

707 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) 

708 or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

709 L{a_f2Tuple}) or C{None}. 

710 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll 

711 B{C{lat2}} and B{C{lon2}} (C{bool}). 

712 

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

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

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

716 

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

718 

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

720 

721 @raise ValueError: Semi-circular longitudinal delta. 

722 

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

724 ''' 

725 return _eA(excessKarney_, radius, wrap, lat1, lon1, lat2, lon2) 

726 

727 

728def excessKarney_(phi2, phi1, lam21): 

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

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

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

732 method. 

733 

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

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

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

737 

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

739 

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

741 

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

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

744 ''' 

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

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

747 # 

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

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

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

751 # 

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

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

754 _t = tan_2 

755 t2 = _t(phi2) 

756 t1 = _t(phi1) 

757 t = _t(lam21, lam21=None) 

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

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

760 

761 

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

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

764# 

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

766# 

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

768# 

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

770# 

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

772# 

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

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

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

776# 

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

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

779# 

780# = tan(lambda21 / 2) * tanh((Lamb(phi1) + Lamb(phi2)) / 2) 

781# 

782# where lambda21 = lambda2 - lambda1 and Lamb(x) is the Lambertian (or the 

783# inverse Gudermannian) function 

784# 

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

786# 

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

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

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

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

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

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

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

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

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

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

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

798# - Charles Karney 

799 

800 

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

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

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

804 

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

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

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

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

809 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) 

810 or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

811 L{a_f2Tuple}) or C{None}. 

812 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll 

813 B{C{lat2}} and B{C{lon2}} (C{bool}). 

814 

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

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

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

818 

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

820 

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

822 

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

824 ''' 

825 return _eA(excessQuad_, radius, wrap, lat1, lon1, lat2, lon2) 

826 

827 

828def excessQuad_(phi2, phi1, lam21): 

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

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

831 

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

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

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

835 

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

837 

838 @see: Function L{excessQuad} and U{Spherical trigonometry 

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

840 ''' 

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

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

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

844 

845 

846def flatLocal(lat1, lon1, lat2, lon2, datum=_WGS84, scaled=True, wrap=False): 

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

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

849 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>} 

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

851 

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

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

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

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

856 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, 

857 L{Ellipsoid2} or L{a_f2Tuple}) to use. 

858 @kwarg scaled: Scale prime_vertical by C{cos(B{phi})} (C{bool}), 

859 see method L{pygeodesy.Ellipsoid.roc2_}. 

860 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll 

861 B{C{lat2}} and B{C{lon2}} (C{bool}). 

862 

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

864 ellipsoid axes). 

865 

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

867 

868 @note: The meridional and prime_vertical radii of curvature 

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

870 

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

872 L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

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

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

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

876 ''' 

877 E = _ellipsoidal(datum, flatLocal) 

878 return E._hubeny_2(*_d3(wrap, lat1, lon1, lat2, lon2), 

879 scaled=scaled, squared=False) * E.a 

880 

881hubeny = flatLocal # PYCHOK for Karl Hubeny 

882 

883 

884def flatLocal_(phi2, phi1, lam21, datum=_WGS84, scaled=True): 

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

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

887 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>} 

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

889 

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

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

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

893 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, 

894 L{Ellipsoid2} or L{a_f2Tuple}) to use. 

895 @kwarg scaled: Scale prime_vertical by C{cos(B{phi})} (C{bool}), 

896 see method L{pygeodesy.Ellipsoid.roc2_}. 

897 

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

899 

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

901 

902 @note: The meridional and prime_vertical radii of curvature 

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

904 

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

906 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{flatPolar_}, 

907 L{euclidean_}, L{haversine_}, L{thomas_} and L{vincentys_} and 

908 U{local, flat earth approximation 

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

910 ''' 

911 E = _ellipsoidal(datum, flatLocal_) 

912 return E._hubeny_2(phi2, phi1, lam21, scaled=scaled, squared=False) 

913 

914hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny 

915 

916 

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

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

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

920 Geographical_distance#Polar_coordinate_flat-Earth_formula>} 

921 formula. 

922 

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

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

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

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

927 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) 

928 or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

929 L{a_f2Tuple}) to use. 

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

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

932 

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

934 ellipsoid or datum axes). 

935 

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

937 

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

939 L{cosineForsytheAndoyerLambert},L{cosineLaw}, 

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

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

942 L{vincentys}. 

943 ''' 

944 return _dS(flatPolar_, radius, wrap, lat1, lon1, lat2, lon2) 

945 

946 

947def flatPolar_(phi2, phi1, lam21): 

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

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

950 Geographical_distance#Polar_coordinate_flat-Earth_formula>} 

951 formula. 

952 

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

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

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

956 

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

958 

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

960 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

961 L{euclidean_}, L{flatLocal_}/L{hubeny_}, L{haversine_}, 

962 L{thomas_} and L{vincentys_}. 

963 ''' 

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

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

966 if a < b: 

967 a, b = b, a 

968 if a < EPS0: 

969 a = _0_0 

970 elif b > 0: 

971 b = b / a # /= chokes PyChecker 

972 c = b * cos(lam21) * _2_0 

973 c = fsumf_(_1_0, b**2, -fabs(c)) 

974 a *= sqrt0(c) 

975 return a 

976 

977 

978def _hartzell(pov, los, earth, **kwds): 

979 '''(INTERNAL) Helper for C{CartesianBase.hartzell} and C{LatLonBase.hartzell}. 

980 ''' 

981 if earth is None: 

982 earth = pov.datum 

983 else: 

984 earth = _spherical_datum(earth, name__=hartzell) 

985 pov = pov.toDatum(earth) 

986 h = pov.height 

987 if h < 0: # EPS0 

988 t = _SPACE_(Fmt.PARENSPACED(height=h), _inside_) 

989 raise IntersectionError(pov=pov, earth=earth, txt=t) 

990 return hartzell(pov, los=los, earth=earth, **kwds) if h > 0 else pov # EPS0 

991 

992 

993def hartzell(pov, los=False, earth=_WGS84, **name_LatLon_and_kwds): 

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

995 a Point-Of-View in space. 

996 

997 @arg pov: Point-Of-View outside the earth (C{LatLon}, C{Cartesian}, 

998 L{Ecef9Tuple} or L{Vector3d}). 

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

1000 C{True} for the I{normal, plumb} onto the surface or C{False} 

1001 or C{None} to point to the center of the earth. 

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

1003 L{a_f2Tuple} or a C{scalar} earth radius in C{meter}). 

1004 @kwarg name_LatLon_and_kwds: Optional, overriding C{B{name}="hartzell"} 

1005 (C{str}), class C{B{LatLon}=None} to return the intersection 

1006 plus additional C{LatLon} keyword arguments, include the 

1007 B{C{datum}} if different and to convert from B{C{earth}}. 

1008 

1009 @return: The intersection (L{Vector3d}, B{C{pov}}'s C{cartesian type} or the 

1010 given B{C{LatLon}} instance) with attribute C{height} set to the 

1011 distance to the B{C{pov}}. 

1012 

1013 @raise IntersectionError: Invalid B{C{pov}} or B{C{pov}} inside the earth or 

1014 invalid B{C{los}} or B{C{los}} points outside or 

1015 away from the earth. 

1016 

1017 @raise TypeError: Invalid B{C{earth}}, C{ellipsoid} or C{datum}. 

1018 

1019 @see: Class L{Los}, functions L{tyr3d} and L{hartzell4} and methods 

1020 L{Ellipsoid.hartzell4} and any C{Cartesian.hartzell} and C{LatLon.hartzell}. 

1021 ''' 

1022 n, LatLon_and_kwds = _name2__(name_LatLon_and_kwds, name__=hartzell) 

1023 try: 

1024 D = _spherical_datum(earth, name__=hartzell) 

1025 r, h, i = _MODS.triaxials._hartzell3(pov, los, D.ellipsoid._triaxial) 

1026 

1027 C = _MODS.cartesianBase.CartesianBase 

1028 if LatLon_and_kwds: 

1029 c = C(r, datum=D) 

1030 r = c.toLatLon(**_xkwds(LatLon_and_kwds, height=h)) 

1031 elif isinstance(r, C): 

1032 r.height = h 

1033 if i: 

1034 r._iteration = i 

1035 except Exception as x: 

1036 raise IntersectionError(pov=pov, los=los, earth=earth, cause=x, 

1037 **LatLon_and_kwds) 

1038 return _xnamed(r, n) if n else r 

1039 

1040 

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

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

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

1044 

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

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

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

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

1049 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid 

1050 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use. 

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

1052 B{C{lon2}} (C{bool}). 

1053 

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

1055 

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

1057 

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

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

1060 L{cosineLaw}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

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

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

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

1064 

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

1066 ''' 

1067 return _dS(haversine_, radius, wrap, lat1, lon1, lat2, lon2) 

1068 

1069 

1070def haversine_(phi2, phi1, lam21): 

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

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

1073 

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

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

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

1077 

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

1079 

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

1081 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

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

1083 L{thomas_} and L{vincentys_}. 

1084 

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

1086 ''' 

1087 def _hsin(rad): 

1088 return sin(rad * _0_5)**2 

1089 

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

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

1092 

1093 

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

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

1096 traveling along a straight line at a given tilt. 

1097 

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

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

1100 B{C{radius}}). 

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

1102 

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

1104 

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

1106 

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

1108 (U{Shapiro et al. 2009, JTECH 

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

1110 and U{Potvin et al. 2012, JTECH 

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

1112 ''' 

1113 r = h = Radius(radius) 

1114 d = fabs(Distance(distance)) 

1115 if d > h: 

1116 d, h = h, d 

1117 

1118 if d > EPS0: # and h > EPS0 

1119 d = d / h # /= h chokes PyChecker 

1120 s = sin(Phid(angle=angle, clip=_180_0)) 

1121 s = fsumf_(_1_0, s * d * _2_0, d**2) 

1122 if s > 0: 

1123 return h * sqrt(s) - r 

1124 

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

1126 

1127 

1128def heightOrthometric(h_ll, N): 

1129 '''Get the I{orthometric} height B{H}, the height above the geoid, earth surface. 

1130 

1131 @arg h_ll: The height above the ellipsoid (C{meter}) or an I{ellipsoidal} 

1132 location (C{LatLon} with a C{height} or C{h} attribute). 

1133 @arg N: The I{geoid} height (C{meter}), the height of the geoid above the 

1134 ellipsoid at the same B{C{h_ll}} location. 

1135 

1136 @return: I{Orthometric} height C{B{H} = B{h} - B{N}} (C{meter}, same units 

1137 as B{C{h}} and B{C{N}}). 

1138 

1139 @see: U{Ellipsoid, Geoid, and Othometric Heights<https://www.NGS.NOAA.gov/ 

1140 GEOID/PRESENTATIONS/2007_02_24_CCPS/Roman_A_PLSC2007notes.pdf>}, page 

1141 6 and module L{pygeodesy.geoids}. 

1142 ''' 

1143 h = h_ll if _isHeight(h_ll) else _xattr(h_ll, height=_xattr(h_ll, h=0)) 

1144 return Height(H=Height(h=h) - Height(N=N)) 

1145 

1146 

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

1148 '''Determine the distance to the horizon from a given altitude above the 

1149 (spherical) earth. 

1150 

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

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

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

1154 

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

1156 

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

1158 

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

1160 ''' 

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

1162 if min(h, r) < 0: 

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

1164 

1165 d2 = ((r * 2.415750694528) if refraction else # 2.0 / 0.8279 

1166 fsumf_(r, r, h)) * h 

1167 return sqrt0(d2) 

1168 

1169 

1170class _idllmn6(object): # see also .geodesicw._wargs, .latlonBase._toCartesian3, .vector2d._numpy 

1171 '''(INTERNAL) Helper for C{intersection2} and C{intersections2}. 

1172 ''' 

1173 @contextmanager # <https://www.Python.org/dev/peps/pep-0343/> Examples 

1174 def __call__(self, datum, lat1, lon1, lat2, lon2, small, wrap, s, **kwds): 

1175 try: 

1176 if wrap: 

1177 _, lat2, lon2 = _Wrap.latlon3(lon1, lat2, lon2, wrap) 

1178 kwds = _xkwds(kwds, wrap=wrap) # for _xError 

1179 m = small if small is _100km else Meter_(small=small) 

1180 n = _DUNDER_nameof(intersections2 if s else intersection2) 

1181 if datum is None or euclidean(lat1, lon1, lat2, lon2) < m: 

1182 d, m = None, _MODS.vector3d 

1183 _i = m._intersects2 if s else m._intersect3d3 

1184 elif _isRadius(datum) and datum < 0 and not s: 

1185 d = _spherical_datum(-datum, name=n) 

1186 m = _MODS.sphericalNvector 

1187 _i = m.intersection 

1188 else: 

1189 d = _spherical_datum(datum, name=n) 

1190 if d.isSpherical: 

1191 m = _MODS.sphericalTrigonometry 

1192 _i = m._intersects2 if s else m._intersect 

1193 elif d.isEllipsoidal: 

1194 try: 

1195 if d.ellipsoid.geodesic: 

1196 pass 

1197 m = _MODS.ellipsoidalKarney 

1198 except ImportError: 

1199 m = _MODS.ellipsoidalExact 

1200 _i = m._intersections2 if s else m._intersection3 # ellipsoidalBaseDI 

1201 else: 

1202 raise _TypeError(datum=datum) 

1203 yield _i, d, lat2, lon2, m, n 

1204 

1205 except (TypeError, ValueError) as x: 

1206 raise _xError(x, lat1=lat1, lon1=lon1, datum=datum, 

1207 lat2=lat2, lon2=lon2, small=small, **kwds) 

1208 

1209_idllmn6 = _idllmn6() # PYCHOK singleton 

1210 

1211 

1212def intersection2(lat1, lon1, bearing1, 

1213 lat2, lon2, bearing2, datum=None, wrap=False, small=_100km): # was=True 

1214 '''I{Conveniently} compute the intersection of two lines each defined 

1215 by a (geodetic) point and a bearing from North, using either ... 

1216 

1217 1) L{vector3d.intersection3d3} for B{C{small}} distances (below 100 Km 

1218 or about 0.88 degrees) or if I{no} B{C{datum}} is specified, or ... 

1219 

1220 2) L{sphericalTrigonometry.intersection} for a spherical B{C{datum}} 

1221 or a C{scalar B{datum}} representing the earth radius, conventionally 

1222 in C{meter} or ... 

1223 

1224 3) L{sphericalNvector.intersection} if B{C{datum}} is a I{negative} 

1225 C{scalar}, (negative) earth radius, conventionally in C{meter} or ... 

1226 

1227 4) L{ellipsoidalKarney.intersection3} for an ellipsoidal B{C{datum}} 

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

1229 is installed, otherwise ... 

1230 

1231 5) L{ellipsoidalExact.intersection3}, provided B{C{datum}} is ellipsoidal. 

1232 

1233 @arg lat1: Latitude of the first point (C{degrees}). 

1234 @arg lon1: Longitude of the first point (C{degrees}). 

1235 @arg bearing1: Bearing at the first point (compass C{degrees}). 

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

1237 @arg lon2: Longitude of the second point (C{degrees}). 

1238 @arg bearing2: Bearing at the second point (compass C{degrees}). 

1239 @kwarg datum: Optional datum (L{Datum}) or ellipsoid (L{Ellipsoid}, 

1240 L{Ellipsoid2} or L{a_f2Tuple}) or C{scalar} earth 

1241 radius (C{meter}, same units as B{C{radius1}} and 

1242 B{C{radius2}}) or C{None}. 

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

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

1245 @kwarg small: Upper limit for small distances (C{meter}). 

1246 

1247 @return: A L{LatLon2Tuple}C{(lat, lon)} with the lat- and 

1248 longitude of the intersection point. 

1249 

1250 @raise IntersectionError: Ambiguous or infinite intersection 

1251 or colinear, parallel or otherwise 

1252 non-intersecting lines. 

1253 

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

1255 

1256 @raise UnitError: Invalid B{C{lat1}}, B{C{lon1}}, B{C{bearing1}}, 

1257 B{C{lat2}}, B{C{lon2}} or B{C{bearing2}}. 

1258 

1259 @see: Method L{RhumbLine.intersection2}. 

1260 

1261 @note: The returned intersections may be near-antipodal. 

1262 ''' 

1263 b1 = Bearing(bearing1=bearing1) 

1264 b2 = Bearing(bearing2=bearing2) 

1265 with _idllmn6(datum, lat1, lon1, lat2, lon2, 

1266 small, wrap, False, bearing1=b1, bearing2=b2) as t: 

1267 _i, d, lat2, lon2, m, n = t 

1268 if d is None: 

1269 t, _, _ = _i(m.Vector3d(lon1, lat1, 0), b1, 

1270 m.Vector3d(lon2, lat2, 0), b2, useZ=False) 

1271 t = LatLon2Tuple(t.y, t.x, name=n) 

1272 

1273 else: 

1274 t = _i(m.LatLon(lat1, lon1, datum=d), b1, 

1275 m.LatLon(lat2, lon2, datum=d), b2, 

1276 LatLon=None, height=0, wrap=False) 

1277 if isinstance(t, Intersection3Tuple): # ellipsoidal 

1278 t, _, _ = t 

1279 t = LatLon2Tuple(t.lat, t.lon, name=n) 

1280 return t 

1281 

1282 

1283def intersections2(lat1, lon1, radius1, 

1284 lat2, lon2, radius2, datum=None, wrap=False, small=_100km): # was=True 

1285 '''I{Conveniently} compute the intersections of two circles each defined 

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

1287 

1288 1) L{vector3d.intersections2} for B{C{small}} distances (below 100 Km 

1289 or about 0.88 degrees) or if I{no} B{C{datum}} is specified, or ... 

1290 

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

1292 or a C{scalar B{datum}} representing the earth radius, conventionally 

1293 in C{meter} or ... 

1294 

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

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

1297 is installed, otherwise ... 

1298 

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

1300 

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

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

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

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

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

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

1307 @kwarg datum: Optional datum (L{Datum}) or ellipsoid (L{Ellipsoid}, 

1308 L{Ellipsoid2} or L{a_f2Tuple}) or C{scalar} earth 

1309 radius (C{meter}, same units as B{C{radius1}} and 

1310 B{C{radius2}}) or C{None}. 

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

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

1313 @kwarg small: Upper limit for small distances (C{meter}). 

1314 

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

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

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

1318 

1319 @raise IntersectionError: Concentric, antipodal, invalid or 

1320 non-intersecting circles or no 

1321 convergence. 

1322 

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

1324 

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

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

1327 ''' 

1328 r1 = Radius_(radius1=radius1) 

1329 r2 = Radius_(radius2=radius2) 

1330 with _idllmn6(datum, lat1, lon1, lat2, lon2, 

1331 small, wrap, True, radius1=r1, radius2=r2) as t: 

1332 _i, d, lat2, lon2, m, n = t 

1333 if d is None: 

1334 r1 = m2degrees(r1, radius=R_M, lat=lat1) 

1335 r2 = m2degrees(r2, radius=R_M, lat=lat2) 

1336 

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

1338 return LatLon2Tuple(y, x, name=n) 

1339 

1340 t = _i(m.Vector3d(lon1, lat1, 0), r1, 

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

1342 Vector=_V2T) 

1343 else: 

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

1345 return LatLon2Tuple(lat, lon, name=n) 

1346 

1347 t = _i(m.LatLon(lat1, lon1, datum=d), r1, 

1348 m.LatLon(lat2, lon2, datum=d), r2, 

1349 LatLon=_LL2T, height=0, wrap=False) 

1350 return t 

1351 

1352 

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

1354 '''Check whether two points are I{antipodal}, on diametrically 

1355 opposite sides of the earth. 

1356 

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

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

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

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

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

1362 

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

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

1365 

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

1367 ''' 

1368 return (fabs(lat1 + lat2) <= eps and 

1369 fabs(lon1 + lon2) <= eps) or _isequalTo( 

1370 normal(lat1, lon1), antipode(lat2, lon2), eps) 

1371 

1372 

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

1374 '''Check whether two points are I{antipodal}, on diametrically 

1375 opposite sides of the earth. 

1376 

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

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

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

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

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

1382 

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

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

1385 

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

1387 ''' 

1388 return (fabs(phi1 + phi2) <= eps and 

1389 fabs(lam1 + lam2) <= eps) or _isequalTo_( 

1390 normal_(phi1, lam1), antipode_(phi2, lam2), eps) 

1391 

1392 

1393def _isequalTo(p1, p2, eps=EPS): 

1394 '''Compare 2 point lat-/lons ignoring C{class}. 

1395 ''' 

1396 return (fabs(p1.lat - p2.lat) <= eps and 

1397 fabs(p1.lon - p2.lon) <= eps) if eps else (p1.latlon == p2.latlon) 

1398 

1399 

1400def _isequalTo_(p1, p2, eps=EPS): 

1401 '''(INTERNAL) Compare 2 point phi-/lams ignoring C{class}. 

1402 ''' 

1403 return (fabs(p1.phi - p2.phi) <= eps and 

1404 fabs(p1.lam - p2.lam) <= eps) if eps else (p1.philam == p2.philam) 

1405 

1406 

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

1408 '''Check whether B{C{lat}} I{and} B{C{lon}} are within their 

1409 respective I{normal} range in C{degrees}. 

1410 

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

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

1413 @kwarg eps: Optional tolerance C{degrees}). 

1414 

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

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

1417 

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

1419 ''' 

1420 return (_90_0 - fabs(lat)) >= eps and _loneg(fabs(lon)) >= eps 

1421 

1422 

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

1424 '''Check whether B{C{phi}} I{and} B{C{lam}} are within their 

1425 respective I{normal} range in C{radians}. 

1426 

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

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

1429 @kwarg eps: Optional tolerance C{radians}). 

1430 

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

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

1433 

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

1435 ''' 

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

1437 

1438 

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

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

1441 ''' 

1442 if fabs(b) > n: 

1443 b = remainder(b, n2) 

1444 if fabs(a) > n_2: 

1445 r = remainder(a, n) 

1446 if r != a: 

1447 a = -r 

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

1449 return float0_(a, b) 

1450 

1451 

1452def normal(lat, lon, **name): 

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

1454 

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

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

1457 @kwarg name: Optional, overriding C{B{name}="normal"} (C{str}). 

1458 

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

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

1461 

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

1463 ''' 

1464 return LatLon2Tuple(*_normal2(lat, lon, _90_0, _180_0, _360_0), 

1465 name=_name__(name, name__=normal)) 

1466 

1467 

1468def normal_(phi, lam, **name): 

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

1470 

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

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

1473 @kwarg name: Optional, overriding C{B{name}="normal_"} (C{str}). 

1474 

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

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

1477 

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

1479 ''' 

1480 return PhiLam2Tuple(*_normal2(phi, lam, PI_2, PI, PI2), 

1481 name=_name__(name, name__=normal_)) 

1482 

1483 

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

1485 '''(INTERNAL) Helper for C{opposing} and C{opposing_}. 

1486 ''' 

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

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

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

1490 

1491 

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

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

1494 

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

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

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

1498 

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

1500 in similar or C{None} if in I{perpendicular} directions. 

1501 

1502 @see: Function L{opposing_}. 

1503 ''' 

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

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

1506 

1507 

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

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

1510 

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

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

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

1514 

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

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

1517 

1518 @see: Function L{opposing}. 

1519 ''' 

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

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

1522 

1523 

1524def _Propy(func, nargs, kwds): 

1525 '''(INTERNAL) Helper for the C{frechet.[-]Frechet**} and 

1526 C{hausdorff.[-]Hausdorff*} classes. 

1527 ''' 

1528 try: 

1529 _xcallable(distance=func) 

1530 # assert _args_kwds_count2(func)[0] == nargs + int(ismethod(func)) 

1531 _ = func(*_0_0s(nargs), **kwds) 

1532 except Exception as x: 

1533 t = unstr(func, **kwds) 

1534 raise _TypeError(t, cause=x) 

1535 return func 

1536 

1537 

1538def _radical2(d, r1, r2, **name): # in .ellipsoidalBaseDI, .sphericalTrigonometry, .vector3d 

1539 # (INTERNAL) See C{radical2} below 

1540 # assert d > EPS0 

1541 r = fsumf_(_1_0, (r1 / d)**2, -(r2 / d)**2) * _0_5 

1542 n = _name__(name, name__=radical2) 

1543 return Radical2Tuple(max(_0_0, min(_1_0, r)), r * d, name=n) 

1544 

1545 

1546def radical2(distance, radius1, radius2, **name): 

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

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

1549 Circle-CircleIntersection.html>}. 

1550 

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

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

1553 

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

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

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

1557 @kwarg name: Optional C{B{name}=NN} (C{str}). 

1558 

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

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

1561 

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

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

1564 

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

1566 B{C{radius2}}. 

1567 

1568 @see: U{Circle-Circle Intersection 

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

1570 ''' 

1571 d = Distance_(distance, low=_0_0) 

1572 r1 = Radius_(radius1=radius1) 

1573 r2 = Radius_(radius2=radius2) 

1574 if d > (r1 + r2): 

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

1576 txt=_too_(_distant_)) 

1577 return _radical2(d, r1, r2, **name) if d > EPS0 else \ 

1578 Radical2Tuple(_0_5, _0_0, **name) 

1579 

1580 

1581class Radical2Tuple(_NamedTuple): 

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

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

1584 ''' 

1585 _Names_ = (_ratio_, _xline_) 

1586 _Units_ = ( Scalar, Scalar) 

1587 

1588 

1589def _radistance(inst): 

1590 '''(INTERNAL) Helper for the L{frechet._FrechetMeterRadians} 

1591 and L{hausdorff._HausdorffMeterRedians} classes. 

1592 ''' 

1593 wrap_, kwds_ = _xkwds_pop2(inst._kwds, wrap=False) 

1594 func_ = inst._func_ 

1595 try: # calling lower-overhead C{func_} 

1596 func_(0, _0_25, _0_5, **kwds_) 

1597 wrap_ = _Wrap._philamop(wrap_) 

1598 except TypeError: 

1599 return inst.distance 

1600 

1601 def _philam(p): 

1602 try: 

1603 return p.phi, p.lam 

1604 except AttributeError: # no .phi or .lam 

1605 return radians(p.lat), radians(p.lon) 

1606 

1607 def _func_wrap(point1, point2): 

1608 phi1, lam1 = wrap_(*_philam(point1)) 

1609 phi2, lam2 = wrap_(*_philam(point2)) 

1610 return func_(phi2, phi1, lam2 - lam1, **kwds_) 

1611 

1612 inst._units = inst._units_ 

1613 return _func_wrap 

1614 

1615 

1616def _scale_deg(lat1, lat2): # degrees 

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

1618 m = fabs(lat1 + lat2) * _0_5 

1619 return cos(radians(m)) if m < _90_0 else _0_0 

1620 

1621 

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

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

1624 m = fabs(phi1 + phi2) * _0_5 

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

1626 

1627 

1628def _sincosa6(phi2, phi1, lam21): # [4] in cosineLaw 

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

1630 ''' 

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

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

1633 

1634 

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

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

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

1638 formula. 

1639 

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

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

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

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

1644 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, 

1645 L{Ellipsoid2} or L{a_f2Tuple}) to use. 

1646 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll 

1647 B{C{lat2}} and B{C{lon2}} (C{bool}). 

1648 

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

1650 ellipsoid axes). 

1651 

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

1653 

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

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

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

1657 ''' 

1658 return _dE(thomas_, datum, wrap, lat1, lon1, lat2, lon2) 

1659 

1660 

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

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

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

1664 formula. 

1665 

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

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

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

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

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

1671 

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

1673 

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

1675 

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

1677 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

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

1679 L{haversine_} and L{vincentys_} and U{Geodesy-PHP 

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

1681 Distance/ThomasFormula.php>}. 

1682 ''' 

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

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

1685 E = _ellipsoidal(datum, thomas_) 

1686 if E.f: 

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

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

1689 

1690 j = (r2 + r1) * _0_5 

1691 k = (r2 - r1) * _0_5 

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

1693 

1694 h = fsumf_(sk**2, (ck * h)**2, -(sj * h)**2) 

1695 u = _1_0 - h 

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

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

1698 sr, cr = sincos2(r) 

1699 if isnon0(sr): 

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

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

1702 x = u + h 

1703 y = u - h 

1704 

1705 s = r / sr 

1706 e = 4 * s**2 

1707 d = 2 * cr 

1708 a = e * d 

1709 b = 2 * r 

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

1711 f = E.f * _0_25 

1712 

1713 t = fsumf_(a * x, -b * y, c * x**2, -d * y**2, e * x * y) 

1714 r -= fsumf_(s * x, -y, -t * f * _0_25) * f * sr 

1715 return r 

1716 

1717 

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

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

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

1721 spherical formula. 

1722 

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

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

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

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

1727 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) 

1728 or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

1729 L{a_f2Tuple}) to use. 

1730 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll 

1731 B{C{lat2}} and B{C{lon2}} (C{bool}). 

1732 

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

1734 

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

1736 

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

1738 L{cosineForsytheAndoyerLambert},L{cosineLaw}, L{equirectangular}, 

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

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

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

1742 

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

1744 ''' 

1745 return _dS(vincentys_, radius, wrap, lat1, lon1, lat2, lon2) 

1746 

1747 

1748def vincentys_(phi2, phi1, lam21): 

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

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

1751 spherical formula. 

1752 

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

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

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

1756 

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

1758 

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

1760 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

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

1762 L{haversine_} and L{thomas_}. 

1763 

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

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

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

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

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

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

1770 ''' 

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

1772 

1773 c = c2 * c21 

1774 x = s1 * s2 + c1 * c 

1775 y = c1 * s2 - s1 * c 

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

1777 

1778# **) MIT License 

1779# 

1780# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved. 

1781# 

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

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

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

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

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

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

1788# 

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

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

1791# 

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

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

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

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

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

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

1798# OTHER DEALINGS IN THE SOFTWARE.