Coverage for pygeodesy/formy.py: 98%

446 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-05-25 12:04 -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 

9# from pygeodesy.cartesianBase import CartesianBase # _MODS 

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

11 _umod_PI2, float0_, isnon0, remainder, \ 

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

13 _32_0, _90_0, _180_0, _360_0 

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

15 _mean_radius, _spherical_datum, _WGS84, _EWGS84 

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

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

18 _TypeError, _ValueError, _xattr, _xError, \ 

19 _xkwds, _xkwds_pop2 

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

21from pygeodesy.fsums import fsumf_, Fmt, unstr 

22# from pygeodesy.internals import _dunder_nameof # from .named 

23from pygeodesy.interns import _delta_, _distant_, _inside_, _not_, _SPACE_, _too_ 

24from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS 

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

26 _dunder_nameof 

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

28 Intersection3Tuple, PhiLam2Tuple, Vector3Tuple 

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

30# from pygeodesy.triaxials import _hartzell2 # _MODS 

31from pygeodesy.units import _isHeight, _isRadius, Bearing, Degrees_, Distance, \ 

32 Distance_, Height, Lam_, Lat, Lon, Meter_, Phi_, \ 

33 Radians, Radians_, Radius, Radius_, Scalar, _100km 

34from pygeodesy.utily import acos1, atan2b, atan2d, degrees2m, _loneg, m2degrees, \ 

35 tan_2, sincos2, sincos2_, sincos2d_, _Wrap 

36# from pygeodesy.vector3d import _otherV3d # _MODS 

37# from pygeodesy.vector3dBase import _xyz_y_z3 # _MODS 

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

39# sphericalNvector, sphericalTrigonometry # _MODS 

40 

41from contextlib import contextmanager 

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

43 

44__all__ = _ALL_LAZY.formy 

45__version__ = '24.05.24' 

46 

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

48_ratio_ = 'ratio' 

49_xline_ = 'xline' 

50 

51 

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

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

54 ''' 

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

56 if r == a: 

57 r = -r 

58 b += n 

59 if fabs(b) > n: 

60 b = remainder(b, n2) 

61 return float0_(r, b) 

62 

63 

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

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

66 to a given point in C{degrees}. 

67 

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

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

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

71 

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

73 

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

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

76 ''' 

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

78 

79 

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

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

82 to a given point in C{radians}. 

83 

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

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

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

87 

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

89 

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

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

92 ''' 

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

94 

95 

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

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 final_wrap: 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), **final_wrap) 

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: If C{True}, return the final, otherwise the initial 

124 bearing (C{bool}). 

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

126 and B{C{lam2}} (C{bool}). 

127 

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

129 start and end point coincide. 

130 

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

132 U{Course between two points<https://www.EdWilliams.org/avform147.htm#Crs>} 

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

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

135 ''' 

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

137 if final: # swap plus PI 

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

139 r = PI3 

140 else: 

141 r = PI2 

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

143 

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

145 y = sdb * ca2 

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

147 

148 

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

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

151 ''' 

152 try: # for LatLon_ and ellipsoidal LatLon 

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

154 except AttributeError: 

155 pass 

156 # XXX spherical version, OK for ellipsoidal ispolar? 

157 t = p1.philam + p2.philam 

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

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

160 name__=_bearingTo2) 

161 

162 

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

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

165 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: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} 

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

178 

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

180 

181 @note: Courtesy of Martin Schultz. 

182 

183 @see: U{Local, flat earth approximation 

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

185 ''' 

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

187 if adjust: # scale delta lon 

188 d_lon *= _scale_deg(lat1, lat2) 

189 return atan2b(d_lon, lat2 - lat1) 

190 

191 

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

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

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

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

196 

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

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

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

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

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

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

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

204 B{C{lat2}} and B{C{lon2}} (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 _dE(cosineAndoyerLambert_, datum, wrap, lat1, lon1, lat2, lon2) 

217 

218 

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

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

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

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

223 

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

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

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

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

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

229 

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

231 

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

233 

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

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

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

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

238 AndoyerLambert.php>}. 

239 ''' 

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

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

242 E = _ellipsoidal(datum, cosineAndoyerLambert_) 

243 if E.f: # ellipsoidal 

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

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

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

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

248 if r: 

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

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

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

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

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

254 return r 

255 

256 

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

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

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

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

261 

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

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

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

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

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

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

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

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

270 

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

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

273 

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

275 

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

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

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

279 L{Ellipsoid.distance2}. 

280 ''' 

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

282 

283 

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

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

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

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

288 formula. 

289 

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

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

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

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

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

295 

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

297 

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

299 

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

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

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

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

304 Distance/ForsytheCorrection.php>}. 

305 ''' 

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

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

308 E = _ellipsoidal(datum, cosineForsytheAndoyerLambert_) 

309 if E.f: # ellipsoidal 

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

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

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

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

314 x = s + t 

315 y = s - t 

316 

317 s = 8 * r**2 / sr 

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

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

320 b = -2 * d 

321 e = 30 * s2r 

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

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

324 

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

326 return r 

327 

328 

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

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

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

332 

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

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

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

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

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

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

339 L{a_f2Tuple}) to use. 

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

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

342 

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

344 ellipsoid or datum axes). 

345 

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

347 

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

349 L{cosineForsytheAndoyerLambert}, L{equirectangular}, L{euclidean}, 

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

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

352 

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

354 ''' 

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

356 

357 

358def cosineLaw_(phi2, phi1, lam21): 

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

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

361 

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

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

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

365 

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

367 

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

369 L{cosineForsytheAndoyerLambert_}, L{equirectangular_}, 

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

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

372 

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

374 ''' 

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

376 

377 

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

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

380 ''' 

381 if wrap: 

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

383 return radians(lat2), Phi_(lat1=lat1), radians(d_lon) 

384 else: # for backward compaibility 

385 return Phi_(lat2=lat2), Phi_(lat1=lat1), Phi_(d_lon=lon2 - lon1) 

386 

387 

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

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

390 ''' 

391 E = _ellipsoidal(earth, func_) 

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

393 return r * E.a 

394 

395 

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

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

398 ''' 

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

400 if radius is not R_M: 

401 _, lat1, _, lat2, _ = wrap_lls 

402 radius = _mean_radius(radius, lat1, lat2) 

403 return r * radius 

404 

405 

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

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

408 ''' 

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

410 if radius: 

411 _, lat1, _, lat2, _ = wrap_lls 

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

413 return r 

414 

415 

416def _ellipsoidal(earth, where): 

417 '''(INTERNAL) Helper for distances. 

418 ''' 

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

420 earth if isinstance(earth, Ellipsoid) else 

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

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

423 

424 

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

426 '''Compute the distance between two points using the U{Equirectangular Approximation 

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

428 

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

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

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

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

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

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

435 L{a_f2Tuple}). 

436 @kwarg adjust_limit_wrap: Optional keyword arguments for 

437 function L{equirectangular_}. 

438 

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

440 the ellipsoid or datum axes). 

441 

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

443 

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

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

446 approximate or accurate distance functions. 

447 ''' 

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

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

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

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

452 

453 

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

455 '''(INTERNAL) Helper for the L{frechet._FrecherMeterRadians} 

456 and L{hausdorff._HausdorffMeterRedians} classes. 

457 ''' 

458 return equirectangular_(lat1, lon1, lat2, lon2, **adjust_limit_wrap).distance2 * _RADIANS2 

459 

460 

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

462 '''Compute the distance between two points using the U{Equirectangular Approximation 

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

464 

465 This approximation is valid for short distance of several hundred Km 

466 or Miles, see the B{C{limit}} keyword argument and L{LimitError}. 

467 

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

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

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

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

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

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

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

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

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

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

478 

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

480 unroll_lon2)} in C{degrees squared}. 

481 

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

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

484 set to C{True}. 

485 

486 @see: U{Local, flat earth approximation 

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

488 L{equirectangular}, L{cosineAndoyerLambert}, 

489 L{cosineForsytheAndoyerLambert}, L{cosineLaw}, L{euclidean}, 

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

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

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

493 ''' 

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

495 d_lat = lat2 - lat1 

496 

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

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

499 if d > limit: 

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

501 s = unstr(equirectangular_, lat1, lon1, lat2, lon2, 

502 limit=limit, wrap=wrap) 

503 raise LimitError(s, txt=t) 

504 

505 if adjust: # scale delta lon 

506 d_lon *= _scale_deg(lat1, lat2) 

507 

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

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

510 

511 

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

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

514 

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

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

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

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

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

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

521 L{a_f2Tuple}) to use. 

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

523 the mean latitude (C{bool}). 

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

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

526 

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

528 ellipsoid or datum axes). 

529 

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

531 

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

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

534 L{euclidean_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

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

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

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

538 ''' 

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

540 

541 

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

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

544 

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

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

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

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

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

550 

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

552 

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

554 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{equirectangular_}, 

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

556 and L{vincentys_}. 

557 ''' 

558 if adjust: 

559 lam21 *= _scale_rad(phi2, phi1) 

560 return euclid(phi2 - phi1, lam21) 

561 

562 

563def excessAbc_(A, b, c): 

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

565 and the included (small) angle. 

566 

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

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

569 @arg c: Second adjacent triangle side (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: Functions L{excessGirard_}, L{excessLHuilier_} and U{Spherical 

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

577 ''' 

578 A = Radians_(A=A) 

579 b = Radians_(b=b) * _0_5 

580 c = Radians_(c=c) * _0_5 

581 

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

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

584 

585 

586def excessCagnoli_(a, b, c): 

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

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

589 

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

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

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

593 

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

595 

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

597 

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

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

600 ''' 

601 a = Radians_(a=a) 

602 b = Radians_(b=b) 

603 c = Radians_(c=c) 

604 

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

606 _s = sin 

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

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

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

610 return Radians(Cagnoli=r * _2_0) 

611 

612 

613def excessGirard_(A, B, C): 

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

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

616 

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

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

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

620 

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

622 

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

624 

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

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

627 ''' 

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

629 Radians_(B=B), 

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

631 

632 

633def excessLHuilier_(a, b, c): 

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

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

636 

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

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

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

640 

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

642 

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

644 

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

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

647 ''' 

648 a = Radians_(a=a) 

649 b = Radians_(b=b) 

650 c = Radians_(c=c) 

651 

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

653 _t = tan_2 

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

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

656 return Radians(LHuilier=r * _4_0) 

657 

658 

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

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

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

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

663 method. 

664 

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

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

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

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

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

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

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

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

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

674 

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

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

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

678 

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

680 

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

682 

683 @raise ValueError: Semi-circular longitudinal delta. 

684 

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

686 ''' 

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

688 

689 

690def excessKarney_(phi2, phi1, lam21): 

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

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

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

694 method. 

695 

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

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

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

699 

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

701 

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

703 

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

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

706 ''' 

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

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

709 # 

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

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

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

713 # 

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

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

716 _t = tan_2 

717 t2 = _t(phi2) 

718 t1 = _t(phi1) 

719 t = _t(lam21, lam21=None) 

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

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

722 

723 

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

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

726# 

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

728# 

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

730# 

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

732# 

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

734# 

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

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

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

738# 

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

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

741# 

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

743# 

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

745# inverse Gudermannian) function 

746# 

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

748# 

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

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

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

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

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

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

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

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

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

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

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

760# - Charles Karney 

761 

762 

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

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

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

766 

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

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

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

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

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

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

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

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

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

776 

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

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

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

780 

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

782 

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

784 

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

786 ''' 

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

788 

789 

790def excessQuad_(phi2, phi1, lam21): 

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

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

793 

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

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

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

797 

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

799 

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

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

802 ''' 

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

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

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

806 

807 

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

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

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

811 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>} 

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

813 

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

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

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

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

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

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

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

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

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

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

824 

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

826 ellipsoid axes). 

827 

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

829 

830 @note: The meridional and prime_vertical radii of curvature 

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

832 

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

834 L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

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

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

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

838 ''' 

839 E = _ellipsoidal(datum, flatLocal) 

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

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

842 

843hubeny = flatLocal # PYCHOK for Karl Hubeny 

844 

845 

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

847 '''Compute the I{angular} 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 phi2: End latitude (C{radians}). 

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

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

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

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

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

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

859 

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

861 

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

863 

864 @note: The meridional and prime_vertical radii of curvature 

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

866 

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

868 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{flatPolar_}, 

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

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

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

872 ''' 

873 E = _ellipsoidal(datum, flatLocal_) 

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

875 

876hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny 

877 

878 

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

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

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

882 Geographical_distance#Polar_coordinate_flat-Earth_formula>} 

883 formula. 

884 

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

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

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

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

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

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

891 L{a_f2Tuple}) to use. 

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

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

894 

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

896 ellipsoid or datum axes). 

897 

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

899 

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

901 L{cosineForsytheAndoyerLambert},L{cosineLaw}, 

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

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

904 L{vincentys}. 

905 ''' 

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

907 

908 

909def flatPolar_(phi2, phi1, lam21): 

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

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

912 Geographical_distance#Polar_coordinate_flat-Earth_formula>} 

913 formula. 

914 

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

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

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

918 

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

920 

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

922 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

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

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

925 ''' 

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

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

928 if a < b: 

929 a, b = b, a 

930 if a < EPS0: 

931 a = _0_0 

932 elif b > 0: 

933 b = b / a # /= chokes PyChecker 

934 c = b * cos(lam21) * _2_0 

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

936 a *= sqrt0(c) 

937 return a 

938 

939 

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

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

942 ''' 

943 if earth is None: 

944 earth = pov.datum 

945 else: 

946 earth = _spherical_datum(earth, name__=hartzell) 

947 pov = pov.toDatum(earth) 

948 h = pov.height 

949 if h < 0: # EPS0 

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

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

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

953 

954 

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

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

957 a Point-Of-View in space. 

958 

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

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

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

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

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

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

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

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

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

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

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

970 

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

972 given B{C{LatLon}} instance) with attribute C{heigth} set to the 

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

974 

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

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

977 away from the earth. 

978 

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

980 

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

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

983 ''' 

984 D = _spherical_datum(earth, name__=hartzell) 

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

986 try: 

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

988 r = _xnamed(r, n) 

989 

990 C = _MODS.cartesianBase.CartesianBase 

991 if LatLon_and_kwds: 

992 c = C(r, datum=D, name=r.name) 

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

994 elif isinstance(r, C): 

995 r.height = h 

996 if i: 

997 r._iteration = i 

998 except Exception as x: 

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

1000 **LatLon_and_kwds) 

1001 return r 

1002 

1003 

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

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

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

1007 formula. 

1008 

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

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

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

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

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

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

1015 L{a_f2Tuple}) to use. 

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

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

1018 

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

1020 

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

1022 

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

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

1025 L{cosineLaw}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

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

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

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

1029 

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

1031 ''' 

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

1033 

1034 

1035def haversine_(phi2, phi1, lam21): 

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

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

1038 formula. 

1039 

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

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

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

1043 

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

1045 

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

1047 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

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

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

1050 

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

1052 ''' 

1053 def _hsin(rad): 

1054 return sin(rad * _0_5)**2 

1055 

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

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

1058 

1059 

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

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

1062 traveling along a straight line at a given tilt. 

1063 

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

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

1066 B{C{radius}}). 

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

1068 

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

1070 

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

1072 

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

1074 (U{Shapiro et al. 2009, JTECH 

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

1076 and U{Potvin et al. 2012, JTECH 

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

1078 ''' 

1079 r = h = Radius(radius) 

1080 d = fabs(Distance(distance)) 

1081 if d > h: 

1082 d, h = h, d 

1083 

1084 if d > EPS0: # and h > EPS0 

1085 d = d / h # /= h chokes PyChecker 

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

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

1088 if s > 0: 

1089 return h * sqrt(s) - r 

1090 

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

1092 

1093 

1094def heightOrthometric(h_ll, N): 

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

1096 

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

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

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

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

1101 

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

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

1104 

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

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

1107 6 and module L{pygeodesy.geoids}. 

1108 ''' 

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

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

1111 

1112 

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

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

1115 above the (spherical) earth. 

1116 

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

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

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

1120 

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

1122 

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

1124 

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

1126 ''' 

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

1128 if min(h, r) < 0: 

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

1130 

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

1132 fsumf_(r, r, h)) * h 

1133 return sqrt0(d2) 

1134 

1135 

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

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

1138 ''' 

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

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

1141 try: 

1142 if wrap: 

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

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

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

1146 n = _dunder_nameof(intersections2 if s else intersection2) 

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

1148 d, m = None, _MODS.vector3d 

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

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

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

1152 m = _MODS.sphericalNvector 

1153 _i = m.intersection 

1154 else: 

1155 d = _spherical_datum(datum, name=n) 

1156 if d.isSpherical: 

1157 m = _MODS.sphericalTrigonometry 

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

1159 elif d.isEllipsoidal: 

1160 try: 

1161 if d.ellipsoid.geodesic: 

1162 pass 

1163 m = _MODS.ellipsoidalKarney 

1164 except ImportError: 

1165 m = _MODS.ellipsoidalExact 

1166 _i = m._intersections2 if s else m._intersection3 # ellispoidalBaseDI 

1167 else: 

1168 raise _TypeError(datum=datum) 

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

1170 

1171 except (TypeError, ValueError) as x: 

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

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

1174 

1175_idllmn6 = _idllmn6() # PYCHOK singleton 

1176 

1177 

1178def intersection2(lat1, lon1, bearing1, 

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

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

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

1182 

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

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

1185 

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

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

1188 in C{meter} or ... 

1189 

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

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

1192 

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

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

1195 is installed, otherwise ... 

1196 

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

1198 

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

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

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

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

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

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

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

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

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

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

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

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

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

1212 

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

1214 longitude of the intersection point. 

1215 

1216 @raise IntersectionError: Ambiguous or infinite intersection 

1217 or colinear, parallel or otherwise 

1218 non-intersecting lines. 

1219 

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

1221 

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

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

1224 

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

1226 

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

1228 ''' 

1229 b1 = Bearing(bearing1=bearing1) 

1230 b2 = Bearing(bearing2=bearing2) 

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

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

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

1234 if d is None: 

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

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

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

1238 

1239 else: 

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

1241 m.LatLon(lat2, lon2, datum=d), b2, height=0, wrap=False) 

1242 if isinstance(t, Intersection3Tuple): # ellipsoidal 

1243 t, _, _ = t 

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

1245 return t 

1246 

1247 

1248def intersections2(lat1, lon1, radius1, 

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

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

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

1252 

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

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

1255 

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

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

1258 in C{meter} or ... 

1259 

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

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

1262 is installed, otherwise ... 

1263 

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

1265 

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

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

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

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

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

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

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

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

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

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

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

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

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

1279 

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

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

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

1283 

1284 @raise IntersectionError: Concentric, antipodal, invalid or 

1285 non-intersecting circles or no 

1286 convergence. 

1287 

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

1289 

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

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

1292 ''' 

1293 r1 = Radius_(radius1=radius1) 

1294 r2 = Radius_(radius2=radius2) 

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

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

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

1298 if d is None: 

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

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

1301 

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

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

1304 

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

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

1307 Vector=_V2T) 

1308 else: 

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

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

1311 

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

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

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

1315 return t 

1316 

1317 

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

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

1320 opposite sides of the earth. 

1321 

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

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

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

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

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

1327 

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

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

1330 

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

1332 ''' 

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

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

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

1336 

1337 

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

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

1340 opposite sides of the earth. 

1341 

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

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

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

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

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

1347 

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

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

1350 

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

1352 ''' 

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

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

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

1356 

1357 

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

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

1360 ''' 

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

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

1363 

1364 

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

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

1367 ''' 

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

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

1370 

1371 

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

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

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

1375 

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

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

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

1379 

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

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

1382 

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

1384 ''' 

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

1386 

1387 

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

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

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

1391 

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

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

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

1395 

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

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

1398 

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

1400 ''' 

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

1402 

1403 

1404def latlon2n_xyz(lat, lon, **name): 

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

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

1407 

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

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

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

1411 

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

1413 

1414 @see: Function L{philam2n_xyz}. 

1415 

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

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

1418 ''' 

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

1420 

1421 

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

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

1424 ''' 

1425 if fabs(b) > n: 

1426 b = remainder(b, n2) 

1427 if fabs(a) > n_2: 

1428 r = remainder(a, n) 

1429 if r != a: 

1430 a = -r 

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

1432 return float0_(a, b) 

1433 

1434 

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

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

1437 

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

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

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

1441 

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

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

1444 

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

1446 ''' 

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

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

1449 

1450 

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

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

1453 

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

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

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

1457 

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

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

1460 

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

1462 ''' 

1463 return PhiLam2Tuple(*_normal2(phi, lam, PI_2, PI, PI2), 

1464 name=_name__(name, name__=normal_)) 

1465 

1466 

1467def _2n_xyz(name, sa, ca, sb, cb): # name always **name 

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

1469 ''' 

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

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

1472 return Vector3Tuple(ca * cb, ca * sb, sa, **name) 

1473 

1474 

1475def n_xyz2latlon(x, y, z, **name): 

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

1477 

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

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

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

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

1482 

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

1484 

1485 @see: Function L{n_xyz2philam}. 

1486 ''' 

1487 return LatLon2Tuple(atan2d(z, hypot(x, y)), atan2d(y, x), **name) 

1488 

1489 

1490def n_xyz2philam(x, y, z, **name): 

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

1492 

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

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

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

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

1497 

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

1499 

1500 @see: Function L{n_xyz2latlon}. 

1501 ''' 

1502 return PhiLam2Tuple(atan2(z, hypot(x, y)), atan2(y, x), **name) 

1503 

1504 

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

1506 '''(INTERNAL) Helper for C{opposing} and C{opposing_}. 

1507 ''' 

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

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

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

1511 

1512 

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

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

1515 

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

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

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

1519 

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

1521 in similar or C{None} if in I{perpendicular} directions. 

1522 

1523 @see: Function L{opposing_}. 

1524 ''' 

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

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

1527 

1528 

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

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

1531 

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

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

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

1535 

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

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

1538 

1539 @see: Function L{opposing}. 

1540 ''' 

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

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

1543 

1544 

1545def philam2n_xyz(phi, lam, **name): 

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

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

1548 

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

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

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

1552 

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

1554 

1555 @see: Function L{latlon2n_xyz}. 

1556 

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

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

1559 ''' 

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

1561 

1562 

1563def _Propy(inst, nargs, **_prop_func): 

1564 '''(INTERNAL) Helper for the C{frechet.[-]Frechet**} and 

1565 C{hausdorff.[-]Hausdorff*} classes. 

1566 ''' 

1567 _prop, func = _prop_func.popitem() # _xkwds_item2(_func_func) 

1568 if func is None: # getter 

1569 try: 

1570 return inst.__dict__[_prop] 

1571 except KeyError: 

1572 inst._notOverloaded(**inst.kwds) 

1573 else: # setter 

1574 try: 

1575 if not callable(func): 

1576 raise TypeError(_not_(callable.__name__)) 

1577 args = (0,) * nargs 

1578 _ = func(*args, **inst.kwds) 

1579 except Exception as x: 

1580 t = unstr(func, **inst.kwds) 

1581 raise _TypeError(t, cause=x) 

1582 inst.__dict__[_prop] = func 

1583# return func 

1584 

1585 

1586def _radical2(d, r1, r2, **name): # in .ellipsoidalBaseDI, .sphericalTrigonometry, .vector3d 

1587 # (INTERNAL) See C{radical2} below 

1588 # assert d > EPS0 

1589 r = fsumf_(_1_0, (r1 / d)**2, -(r2 / d)**2) * _0_5 

1590 n = _name__(name, name__=radical2) 

1591 return Radical2Tuple(max(_0_0, min(_1_0, r)), r * d, name=n) 

1592 

1593 

1594def radical2(distance, radius1, radius2, **name): 

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

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

1597 Circle-CircleIntersection.html>}. 

1598 

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

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

1601 

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

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

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

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

1606 

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

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

1609 

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

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

1612 

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

1614 B{C{radius2}}. 

1615 

1616 @see: U{Circle-Circle Intersection 

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

1618 ''' 

1619 d = Distance_(distance, low=_0_0) 

1620 r1 = Radius_(radius1=radius1) 

1621 r2 = Radius_(radius2=radius2) 

1622 if d > (r1 + r2): 

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

1624 txt=_too_(_distant_)) 

1625 return _radical2(d, r1, r2, **name) if d > EPS0 else \ 

1626 Radical2Tuple(_0_5, _0_0, **name) 

1627 

1628 

1629class Radical2Tuple(_NamedTuple): 

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

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

1632 ''' 

1633 _Names_ = (_ratio_, _xline_) 

1634 _Units_ = ( Scalar, Scalar) 

1635 

1636 

1637def _radistance(inst): 

1638 '''(INTERNAL) Helper for the L{frechet._FrechetMeterRadians} 

1639 and L{hausdorff._HausdorffMeterRedians} classes. 

1640 ''' 

1641 wrap_, kwds_ = _xkwds_pop2(inst._kwds, wrap=False) 

1642 func_ = inst._func_ 

1643 try: # calling lower-overhead C{func_} 

1644 func_(0, _0_25, _0_5, **kwds_) 

1645 wrap_ = _Wrap._philamop(wrap_) 

1646 except TypeError: 

1647 return inst.distance 

1648 

1649 def _philam(p): 

1650 try: 

1651 return p.phi, p.lam 

1652 except AttributeError: # no .phi or .lam 

1653 return radians(p.lat), radians(p.lon) 

1654 

1655 def _func_wrap(point1, point2): 

1656 phi1, lam1 = wrap_(*_philam(point1)) 

1657 phi2, lam2 = wrap_(*_philam(point2)) 

1658 return func_(phi2, phi1, lam2 - lam1, **kwds_) 

1659 

1660 inst._units = inst._units_ 

1661 return _func_wrap 

1662 

1663 

1664def _scale_deg(lat1, lat2): # degrees 

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

1666 m = fabs(lat1 + lat2) * _0_5 

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

1668 

1669 

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

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

1672 m = fabs(phi1 + phi2) * _0_5 

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

1674 

1675 

1676def _sincosa6(phi2, phi1, lam21): # [4] in cosineLaw 

1677 '''(INTERNAL) C{sin}es, C{cos}ines and C{acos}ine. 

1678 ''' 

1679 s2, c2, s1, c1, _, c21 = sincos2_(phi2, phi1, lam21) 

1680 return s2, c2, s1, c1, acos1(s1 * s2 + c1 * c2 * c21), c21 

1681 

1682 

1683def thomas(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False): 

1684 '''Compute the distance between two (ellipsoidal) points using 

1685 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} 

1686 formula. 

1687 

1688 @arg lat1: Start latitude (C{degrees}). 

1689 @arg lon1: Start longitude (C{degrees}). 

1690 @arg lat2: End latitude (C{degrees}). 

1691 @arg lon2: End longitude (C{degrees}). 

1692 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, 

1693 L{Ellipsoid2} or L{a_f2Tuple}) to use. 

1694 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll 

1695 B{C{lat2}} and B{C{lon2}} (C{bool}). 

1696 

1697 @return: Distance (C{meter}, same units as the B{C{datum}}'s 

1698 ellipsoid axes). 

1699 

1700 @raise TypeError: Invalid B{C{datum}}. 

1701 

1702 @see: Functions L{thomas_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert}, 

1703 L{cosineLaw}, L{equirectangular}, L{euclidean}, L{flatLocal}/L{hubeny}, 

1704 L{flatPolar}, L{haversine}, L{vincentys} and method L{Ellipsoid.distance2}. 

1705 ''' 

1706 return _dE(thomas_, datum, wrap, lat1, lon1, lat2, lon2) 

1707 

1708 

1709def thomas_(phi2, phi1, lam21, datum=_WGS84): 

1710 '''Compute the I{angular} distance between two (ellipsoidal) points using 

1711 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} 

1712 formula. 

1713 

1714 @arg phi2: End latitude (C{radians}). 

1715 @arg phi1: Start latitude (C{radians}). 

1716 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

1717 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid}, 

1718 L{Ellipsoid2} or L{a_f2Tuple}). 

1719 

1720 @return: Angular distance (C{radians}). 

1721 

1722 @raise TypeError: Invalid B{C{datum}}. 

1723 

1724 @see: Functions L{thomas}, L{cosineAndoyerLambert_}, 

1725 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

1726 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, 

1727 L{flatPolar_}, L{haversine_} and L{vincentys_} and U{Geodesy-PHP 

1728 <https://GitHub.com/jtejido/geodesy-php/blob/master/src/Geodesy/ 

1729 Distance/ThomasFormula.php>}. 

1730 ''' 

1731 s2, c2, s1, c1, r, _ = _sincosa6(phi2, phi1, lam21) 

1732 if r and isnon0(c1) and isnon0(c2): 

1733 E = _ellipsoidal(datum, thomas_) 

1734 if E.f: 

1735 r1 = atan2(E.b_a * s1, c1) 

1736 r2 = atan2(E.b_a * s2, c2) 

1737 

1738 j = (r2 + r1) * _0_5 

1739 k = (r2 - r1) * _0_5 

1740 sj, cj, sk, ck, h, _ = sincos2_(j, k, lam21 * _0_5) 

1741 

1742 h = fsumf_(sk**2, (ck * h)**2, -(sj * h)**2) 

1743 u = _1_0 - h 

1744 if isnon0(u) and isnon0(h): 

1745 r = atan(sqrt0(h / u)) * 2 # == acos(1 - 2 * h) 

1746 sr, cr = sincos2(r) 

1747 if isnon0(sr): 

1748 u = 2 * (sj * ck)**2 / u 

1749 h = 2 * (sk * cj)**2 / h 

1750 x = u + h 

1751 y = u - h 

1752 

1753 s = r / sr 

1754 e = 4 * s**2 

1755 d = 2 * cr 

1756 a = e * d 

1757 b = 2 * r 

1758 c = s - (a - d) * _0_5 

1759 f = E.f * _0_25 

1760 

1761 t = fsumf_(a * x, -b * y, c * x**2, -d * y**2, e * x * y) 

1762 r -= fsumf_(s * x, -y, -t * f * _0_25) * f * sr 

1763 return r 

1764 

1765 

1766def vincentys(lat1, lon1, lat2, lon2, radius=R_M, wrap=False): 

1767 '''Compute the distance between two (spherical) points using 

1768 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>} 

1769 spherical formula. 

1770 

1771 @arg lat1: Start latitude (C{degrees}). 

1772 @arg lon1: Start longitude (C{degrees}). 

1773 @arg lat2: End latitude (C{degrees}). 

1774 @arg lon2: End longitude (C{degrees}). 

1775 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) 

1776 or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or 

1777 L{a_f2Tuple}) to use. 

1778 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll 

1779 B{C{lat2}} and B{C{lon2}} (C{bool}). 

1780 

1781 @return: Distance (C{meter}, same units as B{C{radius}}). 

1782 

1783 @raise UnitError: Invalid B{C{radius}}. 

1784 

1785 @see: Functions L{vincentys_}, L{cosineAndoyerLambert}, 

1786 L{cosineForsytheAndoyerLambert},L{cosineLaw}, L{equirectangular}, 

1787 L{euclidean}, L{flatLocal}/L{hubeny}, L{flatPolar}, 

1788 L{haversine} and L{thomas} and methods L{Ellipsoid.distance2}, 

1789 C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}. 

1790 

1791 @note: See note at function L{vincentys_}. 

1792 ''' 

1793 return _dS(vincentys_, radius, wrap, lat1, lon1, lat2, lon2) 

1794 

1795 

1796def vincentys_(phi2, phi1, lam21): 

1797 '''Compute the I{angular} distance between two (spherical) points using 

1798 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>} 

1799 spherical formula. 

1800 

1801 @arg phi2: End latitude (C{radians}). 

1802 @arg phi1: Start latitude (C{radians}). 

1803 @arg lam21: Longitudinal delta, M{end-start} (C{radians}). 

1804 

1805 @return: Angular distance (C{radians}). 

1806 

1807 @see: Functions L{vincentys}, L{cosineAndoyerLambert_}, 

1808 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, 

1809 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_}, 

1810 L{flatPolar_}, L{haversine_} and L{thomas_}. 

1811 

1812 @note: Functions L{vincentys_}, L{haversine_} and L{cosineLaw_} 

1813 produce equivalent results, but L{vincentys_} is suitable 

1814 for antipodal points and slightly more expensive (M{3 cos, 

1815 3 sin, 1 hypot, 1 atan2, 6 mul, 2 add}) than L{haversine_} 

1816 (M{2 cos, 2 sin, 2 sqrt, 1 atan2, 5 mul, 1 add}) and 

1817 L{cosineLaw_} (M{3 cos, 3 sin, 1 acos, 3 mul, 1 add}). 

1818 ''' 

1819 s1, c1, s2, c2, s21, c21 = sincos2_(phi1, phi2, lam21) 

1820 

1821 c = c2 * c21 

1822 x = s1 * s2 + c1 * c 

1823 y = c1 * s2 - s1 * c 

1824 return atan2(hypot(c2 * s21, y), x) 

1825 

1826# **) MIT License 

1827# 

1828# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved. 

1829# 

1830# Permission is hereby granted, free of charge, to any person obtaining a 

1831# copy of this software and associated documentation files (the "Software"), 

1832# to deal in the Software without restriction, including without limitation 

1833# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

1834# and/or sell copies of the Software, and to permit persons to whom the 

1835# Software is furnished to do so, subject to the following conditions: 

1836# 

1837# The above copyright notice and this permission notice shall be included 

1838# in all copies or substantial portions of the Software. 

1839# 

1840# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

1841# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

1842# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

1843# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

1844# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

1845# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

1846# OTHER DEALINGS IN THE SOFTWARE.