Coverage for pygeodesy/formy.py: 98%

1

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

3

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

5'''

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

7 from __future__ import division as _; del _ # noqa: E702 ;

8

9 from pygeodesy.basics import _copysign, _isin # _args_kwds_count2

10# from pygeodesy.cartesianBase import CartesianBase # _MODS

11 from 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 _90_0, _180_0, _360_0

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

16 _mean_radius, _spherical_datum, _WGS84, _EWGS84

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

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

19 _TypeError, _ValueError, _xattr, _xError, \

20 _xcallable, _xkwds, _xkwds_pop2

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

22 from pygeodesy.fsums import fsumf_, Fmt, unstr

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

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

25 from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS

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

27 typename

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

29 Intersection3Tuple, PhiLam2Tuple

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

31# from pygeodesy.triaxials.triaxial5 import _hartzell3 # _MODS

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

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

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

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

36 m2degrees, 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

42 from contextlib import contextmanager

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

44

45 __all__ = _ALL_LAZY.formy

46 __version__ = '26.01.06'

47

48 _RADIANS2 = radians(_1_0)**2 # degree to radians-squared

49 _ratio_ = 'ratio'

50 _xline_ = 'xline'

51

52

53 def 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

70 def _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

82 def 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

98 def 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

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

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

116 (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 L{pygeodesy.bearing_}.

123

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

125 coincide.

126 '''

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

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

129 return degrees(r)

130

131

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

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

134 (spherical) points.

135

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

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

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

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

140 @kwarg final: If C{True}, return the final, otherwise the initial bearing (C{bool}).

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

142 (C{bool}).

143

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

145 coincide.

146

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

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

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

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

151 '''

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

153 if final: # swap plus PI

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

155 r = PI3

156 else:

157 r = PI2

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

159

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

161 y = sdb * ca2

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

163

164

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

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

167 '''

168 try: # for LatLon_ and ellipsoidal LatLon

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

170 except AttributeError:

171 pass

172 # XXX spherical version, OK for ellipsoidal ispolar?

173 t = p1.philam + p2.philam

174 i = bearing_(*t, final=False, wrap=wrap)

175 f = bearing_(*t, final=True, wrap=wrap)

176 return Bearing2Tuple(degrees(i), degrees(f),

177 name__=_bearingTo2)

178

179

180 def chord2angle(chord, radius=R_M):

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

182

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

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

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

186

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

188

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

190

191 @see: Function L{angle2chord}.

192 '''

193 m = _mean_radius(radius)

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

195 if r:

196 i = int(r)

197 if i > 0:

198 r -= i

199 i *= PI

200 r = (asin1(r) + i) * _2_0

201 return _copysign(r, chord)

202

203

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

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

206 lat2 - lat1)} between two points.

207

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

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

210

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

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

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

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

215 @kwarg adjust: Adjust the longitudinal delta by the cosine of the mean

216 latitude (C{bool}).

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

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

219

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

221

222 @note: Courtesy of Martin Schultz.

223

224 @see: U{Local, flat earth approximation

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

226 '''

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

228 if adjust: # scale delta lon

229 d_lon *= _scale_deg(lat1, lat2)

230 return atan2b(d_lon, lat2 - lat1)

231

232

233 def cosineLaw(lat1, lon1, lat2, lon2, corr=0, earth=None, wrap=False,

234 datum=_WGS84, radius=R_M):

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

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

237 formula, optionally corrected.

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 corr: Use C{B{corr}=2} to apply the U{Forsythe-Andoyer-Lambert

244 <https://www2.UNB.CA/gge/Pubs/TR77.pdf>}, C{B{corr}=1} for the

245 U{Andoyer-Lambert<https://Books.Google.com/books?id=x2UiAQAAIAAJ>}

246 corrected (ellipsoidal) or keep C{B{corr}=0} for the uncorrected

247 (spherical) C{Law of Cosines} formula (C{int}).

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

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

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

251 (C{bool}).

252 @kwarg datum: Default ellipsiodal B{C{earth}} (and for backward compatibility).

253 @kwarg radius: Default spherical B{C{earth}} (and for backward compatibility).

254

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

256 ellipsoid axes).

257

258 @raise TypeError: Invalid B{C{earth}}, B{C{datum}} or B{C{radius}}.

259

260 @raise ValueError: Invalid B{C{corr}}.

261

262 @see: Functions L{cosineLaw_}, L{equirectangular}, L{euclidean}, L{flatLocal} /

263 L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys} and

264 method L{Ellipsoid.distance2}.

265

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

267 '''

268 return _dE(cosineLaw_, earth or datum, wrap, lat1, lon1, lat2, lon2, corr=corr) if corr else \

269 _dS(cosineLaw_, earth or radius, wrap, lat1, lon1, lat2, lon2)

270

271

272 def cosineLaw_(phi2, phi1, lam21, corr=0, earth=None, datum=_WGS84):

273 '''Compute the I{angular} distance between two points using the U{Law of Cosines

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

275 optionally corrected.

276

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

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

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

280 @kwarg corr: Use C{B{corr}=2} to apply the U{Forsythe-Andoyer-Lambert

281 <https://www2.UNB.CA/gge/Pubs/TR77.pdf>}, C{B{corr}=1} for the

282 U{Andoyer-Lambert<https://Books.Google.com/books?id=x2UiAQAAIAAJ>}

283 corrected (ellipsoidal) or keep C{B{corr}=0} for the uncorrected

284 (spherical) C{Law of Cosines} formula (C{int}).

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

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

287 @kwarg datum: Default ellipsoidal B{C{earth}} (and for backward compatibility).

288

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

290

291 @raise TypeError: Invalid B{C{earth}} or B{C{datum}}.

292

293 @raise ValueError: Invalid B{C{corr}}.

294

295 @see: Functions L{cosineLaw}, L{euclidean_}, L{flatLocal_} / L{hubeny_},

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

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

298 AndoyerLambert.php>}.

299 '''

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

301 if corr and isnon0(c1) and isnon0(c2):

302 E = _ellipsoidal(earth or datum, cosineLaw_)

303 f = _0_25 * E.f

304 if f: # ellipsoidal

305 if corr == 1: # Andoyer-Lambert

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

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

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

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

310 if r:

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

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

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

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

315 r += (c - s) * _0_5 * f

316

317 elif corr == 2: # Forsythe-Andoyer-Lambert

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

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

320 s = (s1 + s2)**2 / (_1_0 + cr)

321 t = (s1 - s2)**2 / (_1_0 - cr)

322 x = s + t

323 y = s - t

324

325 s = 8 * r**2 / sr

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

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

328 b = -2 * d

329 e = 30 * s2r

330

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

332 t = fdot_( a, x, b, y, e, y**2, -c, x**2, d, x * y) * _0_125

333 r += fdot_(-r, x, sr, y * 3, t, f) * f

334 else:

335 raise _ValueError(corr=corr)

336 return r

337

338

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

340 '''(INTERNAL) Helper for _dE, _dS, ....

341 '''

342 if wrap:

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

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

345 else: # for backward compaibility

346 return Phid(lat2=lat2), Phid(lat1=lat1), radians(lon2 - lon1)

347

348

349 def _dE(fun_, earth, wrap, *lls, **corr):

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

351 '''

352 E = _ellipsoidal(earth, fun_)

353 r = fun_(*_d3(wrap, *lls), datum=E, **corr)

354 return r * E.a

355

356

357 def _dS(fun_, radius, wrap, *lls, **adjust):

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

359 '''

360 r = fun_(*_d3(wrap, *lls), **adjust)

361 if radius is not R_M:

362 try: # datum?

363 radius = radius.ellipsoid.R1

364 except AttributeError:

365 pass # scalar?

366 lat1, _, lat2, _ = lls

367 radius = _mean_radius(radius, lat1, lat2)

368 return r * radius

369

370

371 def _ellipsoidal(earth, where):

372 '''(INTERNAL) Helper for distances.

373 '''

374 return _EWGS84 if _isin(earth, _EWGS84, _WGS84) else (

375 earth if isinstance(earth, Ellipsoid) else

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

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

378

379

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

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

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

383

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

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

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

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

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

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

390 @kwarg adjust_limit_wrap: Optionally, keyword arguments for function L{equirectangular4}.

391

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

393 ellipsoid axes).

394

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

396

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

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

399 '''

400 r = _mean_radius(radius, lat1, lat2)

401 t = equirectangular4(Lat(lat1=lat1), Lon(lon1=lon1),

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

403 **adjust_limit_wrap) # PYCHOK 4 vs 2-3

404 return degrees2m(sqrt(t.distance2), radius=r)

405

406

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

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

409 L{hausdorff._HausdorffMeterRedians}.

410 '''

411 t = equirectangular4(lat1, lon1, lat2, lon2, **adjust_limit_wrap)

412 return t.distance2 * _RADIANS2

413

414

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

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

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

418

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

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

421

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

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

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

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

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

427 mean latitude (C{bool}).

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

429 or C{0} for unlimited.

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

431 (C{bool}).

432

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

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

435

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

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

438

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

440 functions L{equirectangular}, L{cosineLaw}, L{euclidean}, L{flatLocal} /

441 L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys} and methods

442 L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}.

443 '''

444 if wrap:

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

446 else:

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

448 d_lat = lat2 - lat1

449

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

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

452 if d > limit:

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

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

455 limit=limit, wrap=wrap)

456 raise LimitError(s, txt=t)

457

458 if adjust: # scale delta lon

459 d_lon *= _scale_deg(lat1, lat2)

460

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

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

463

464

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

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

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 radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid

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

474 @kwarg adjust: Adjust the longitudinal delta by the cosine of the mean

475 latitude (C{bool}).

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

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

478

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

480 or datum axes).

481

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

483

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

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

486 L{euclidean_}, L{cosineLaw}, L{equirectangular}, L{flatLocal} /

487 L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys}

488 and methods L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and

489 C{LatLon.equirectangularTo}.

490 '''

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

492

493

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

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

496

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

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

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

500 @kwarg adjust: Adjust the longitudinal delta by the cosine of the mean

501 latitude (C{bool}).

502

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

504

505 @see: Functions L{euclid}, L{euclidean}, L{cosineLaw_}, L{flatLocal_} /

506 L{hubeny_}, L{flatPolar_}, L{haversine_}, L{thomas_} and L{vincentys_}.

507 '''

508 if adjust:

509 lam21 *= _scale_rad(phi2, phi1)

510 return euclid(phi2 - phi1, lam21)

511

512

513 def excessAbc_(A, b, c):

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

515 and the included (small) angle.

516

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

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

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

520

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

522

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

524

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

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

527 '''

528 A = Radians_(A=A)

529 b = Radians_(b=b) * _0_5

530 c = Radians_(c=c) * _0_5

531

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

533 s = sA * sb * sc

534 c = cA * sb * sc + cc * cb

535 return atan2(s, c) * _2_0

536

537

538 def excessCagnoli_(a, b, c):

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

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

541

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

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

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

545

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

547

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

549

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

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

552 '''

553 a = Radians_(a=a)

554 b = Radians_(b=b)

555 c = Radians_(c=c)

556

557 r = _maprod(cos, a * _0_5, b * _0_5, c * _0_5)

558 if r:

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

560 t = _maprod(sin, s, s - a, s - b, s - c)

561 r = asin1(sqrt(t) * _0_5 / r) if t > 0 else _0_0

562 return Radians(Cagnoli=r * _2_0)

563

564

565 def excessGirard_(A, B, C):

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

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

568

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

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

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

572

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

574

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

576

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

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

579 '''

580 r = fsumf_(Radians_(A=A),

581 Radians_(B=B),

582 Radians_(C=C), -PI)

583 return Radians(Girard=r)

584

585

586 def excessLHuilier_(a, b, c):

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

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

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{excessCagnoli_}, L{excessGirard_} and U{Spherical

599 trigonometry<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 r = _maprod(tan_2, s, s - a, s - b, s - c)

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

608 return Radians(LHuilier=r * _4_0)

609

610

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

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

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

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

615 method.

616

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

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

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

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

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

622 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) or C{None}.

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

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

625

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

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

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

629

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

631

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

633

634 @raise ValueError: Semi-circular longitudinal delta.

635

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

637 '''

638 r = excessKarney_(*_d3(wrap, lat1, lon1, lat2, lon2))

639 if radius:

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

641 return r

642

643

644 def excessKarney_(phi2, phi1, lam21):

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

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

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

648 method.

649

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

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

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

653

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

655

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

657

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

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

660 '''

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

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

663 #

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

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

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

667 #

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

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

670 t2 = tan_2(phi2)

671 t1 = tan_2(phi1)

672 c = (t1 * t2) + _1_0

673 s = (t1 + t2) * tan_2(lam21, lam21=None)

674 return Radians(Karney=atan2(s, c) * _2_0)

675

676

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

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

679#

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

681#

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

683#

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

685#

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

687#

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

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

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

691#

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

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

694#

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

696#

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

698# inverse Gudermannian) function

699#

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

701#

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

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

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

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

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

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

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

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

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

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

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

713# - Charles Karney

714

715

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

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

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

719

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

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

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

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

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

725 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) or C{None}.

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

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

728

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

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

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

732

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

734

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

736

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

738 '''

739 r = excessQuad_(*_d3(wrap, lat1, lon1, lat2, lon2))

740 if radius:

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

742 return r

743

744

745 def excessQuad_(phi2, phi1, lam21):

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

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

748

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

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

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

752

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

754

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

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

757 '''

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

759 s = sin((phi2 + phi1) * _0_5) * tan_2(lam21)

760 return Radians(Quad=atan2(s, c) * _2_0)

761

762

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

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

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

766 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}

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

768

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

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

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

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

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

774 or L{a_f2Tuple}) to use.

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

776 method L{pygeodesy.Ellipsoid.roc2_}.

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

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

779

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

781

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

783

784 @note: The meridional and prime_vertical radii of curvature are taken and

785 scaled at the mean of both latitude.

786

787 @see: Functions L{flatLocal_} or L{hubeny_}, L{cosineLaw}, L{equirectangular},

788 L{euclidean}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys}, method

789 L{Ellipsoid.distance2} and U{local, flat earth approximation

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

791 '''

792 t = _d3(wrap, lat1, lon1, lat2, lon2)

793 E = _ellipsoidal(datum, flatLocal)

794 return E._hubeny_2(*t, scaled=scaled, squared=False) * E.a

795

796 hubeny = flatLocal # PYCHOK for Karl Hubeny

797

798

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

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

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

802 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}

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

804

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

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

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

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

809 or L{a_f2Tuple}) to use.

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

811 method L{pygeodesy.Ellipsoid.roc2_}.

812

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

814

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

816

817 @note: The meridional and prime_vertical radii of curvature are taken and

818 scaled I{at the mean of both latitude}.

819

820 @see: Functions L{flatLocal} or L{hubeny}, L{cosineLaw_}, L{flatPolar_},

821 L{euclidean_}, L{haversine_}, L{thomas_} and L{vincentys_} and U{local,

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

823 '''

824 E = _ellipsoidal(datum, flatLocal_)

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

826

827 hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny

828

829

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

831 '''Compute the distance between two (spherical) points using the U{polar

832 coordinate flat-Earth<https://WikiPedia.org/wiki/Geographical_distance

833 #Polar_coordinate_flat-Earth_formula>} formula.

834

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

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

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

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

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

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

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

842 (C{bool}).

843

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

845 ellipsoid axes).

846

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

848

849 @see: Functions L{flatPolar_}, L{cosineLaw}, L{flatLocal} / L{hubeny},

850 L{equirectangular}, L{euclidean}, L{haversine}, L{thomas} and L{vincentys}.

851 '''

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

853

854

855 def flatPolar_(phi2, phi1, lam21):

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

857 U{polar coordinate flat-Earth<https://WikiPedia.org/wiki/Geographical_distance

858 #Polar_coordinate_flat-Earth_formula>} formula.

859

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

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

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

863

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

865

866 @see: Functions L{flatPolar}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /

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

868 '''

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

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

871 if a < b:

872 a, b = b, a

873 if a < EPS0:

874 a = _0_0

875 elif b > 0:

876 b = b / a # /= chokes PyChecker

877 c = b * cos(lam21) * _2_0

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

879 a *= sqrt0(c)

880 return a

881

882

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

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

885 '''

886 if earth is None:

887 earth = pov.datum

888 else:

889 earth = _spherical_datum(earth, name__=hartzell)

890 pov = pov.toDatum(earth)

891 h = pov.height

892 if h < 0: # EPS0

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

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

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

896

897

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

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

900 a Point-Of-View in space.

901

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

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

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

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

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

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

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

909 @kwarg name_LatLon_and_kwds: Optional C{B{name}="hartzell"} (C{str}), class

910 C{B{LatLon}=None} to return the intersection and optionally,

911 additional C{LatLon} keyword arguments, include the B{C{datum}}

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

913

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

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

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

917

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

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

920 away from the earth.

921

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

923

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

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

926 '''

927 n, kwds = _name2__(name_LatLon_and_kwds, name__=hartzell)

928 try:

929 D = _spherical_datum(earth, name__=hartzell)

930 m = _MODS._triaxials_triaxial5

931 r, h, i = m._hartzell3(pov, los, D.ellipsoid._triaxial)

932

933 C = _MODS.cartesianBase.CartesianBase

934 if kwds:

935 c = C(r, datum=D)

936 r = c.toLatLon(**_xkwds(kwds, height=h))

937 elif isinstance(r, C):

938 r.height = h

939 if i:

940 r._iteration = i

941 except Exception as x:

942 raise IntersectionError(pov=pov, los=los, earth=earth, cause=x, **kwds)

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

944

945

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

947 '''Compute the distance between two (spherical) points using the U{Haversine

948 <https://www.Movable-Type.co.UK/scripts/latlong.html>} formula.

949

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

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

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

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

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

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

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

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

958

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

960

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

962

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

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

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

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

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

968

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

970 '''

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

972

973

974 def haversine_(phi2, phi1, lam21):

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

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

977

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

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

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

981

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

983

984 @see: Functions L{haversine}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /

985 L{hubeny_}, L{flatPolar_}, L{thomas_} and L{vincentys_}.

986

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

988 '''

989 h = hav(phi2 - phi1) + cos(phi1) * cos(phi2) * hav(lam21) # haversine

990 return atan2(sqrt0(h), sqrt0(_1_0 - h)) * _2_0 # == asin1(sqrt(h)) * 2

991

992

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

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

995 traveling along a straight line at a given tilt.

996

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

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

999 B{C{radius}}).

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

1001

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

1003

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

1005

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

1007 (U{Shapiro et al. 2009, JTECH

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

1009 and U{Potvin et al. 2012, JTECH

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

1011 '''

1012 r = h = Radius(radius)

1013 d = fabs(Distance(distance))

1014 if d > h:

1015 d, h = h, d

1016

1017 if d > EPS0: # and h > EPS0

1018 d = d / h # /= h chokes PyChecker

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

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

1021 if s > 0:

1022 return h * sqrt(s) - r

1023

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

1025

1026

1027 def heightOrthometric(h_loc, N):

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

1029

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

1031 location (C{LatLon} or C{Cartesian} with a C{height} or C{h}

1032 attribute), otherwise C{0 meter}.

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

1034 ellipsoid at the same B{C{h_loc}} location.

1035

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

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

1038

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

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

1041 6 and module L{pygeodesy.geoids}.

1042 '''

1043 h = h_loc if _isHeight(h_loc) else _xattr(h_loc, height=_xattr(h_loc, h=0))

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

1045

1046

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

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

1049 (spherical) earth.

1050

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

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

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

1054

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

1056

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

1058

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

1060 '''

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

1062 if min(h, r) < 0:

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

1064

1065 if refraction:

1066 r *= 2.415750694528 # 2.0 / 0.8279

1067 else:

1068 r += r + h

1069 return sqrt0(r * h)

1070

1071

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

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

1074 '''

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

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

1077 try:

1078 if wrap:

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

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

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

1082 n = typename(intersections2 if s else intersection2)

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

1084 d, m = None, _MODS.vector3d

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

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

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

1088 m = _MODS.sphericalNvector

1089 _i = m.intersection

1090 else:

1091 d = _spherical_datum(datum, name=n)

1092 if d.isSpherical:

1093 m = _MODS.sphericalTrigonometry

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

1095 elif d.isEllipsoidal:

1096 try:

1097 if d.ellipsoid.geodesic:

1098 pass

1099 m = _MODS.ellipsoidalKarney

1100 except ImportError:

1101 m = _MODS.ellipsoidalExact

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

1103 else:

1104 raise _TypeError(datum=datum)

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

1106

1107 except (TypeError, ValueError) as x:

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

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

1110

1111 _idllmn6 = _idllmn6() # PYCHOK singleton

1112

1113

1114 def intersection2(lat1, lon1, bearing1,

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

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

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

1118

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

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

1121

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

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

1124 in C{meter} or ...

1125

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

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

1128

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

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

1131 is installed, otherwise ...

1132

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

1134

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

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

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

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

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

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

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

1142 L{Ellipsoid2} or L{a_f2Tuple}) or C{scalar} earth radius

1143 (C{meter}, same units as B{C{radius1}} and B{C{radius2}})

1144 or C{None}.

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

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

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

1148

1149 @return: Intersection point (L{LatLon2Tuple}C{(lat, lon)}).

1150

1151 @raise IntersectionError: No or an ambiguous intersection or colinear,

1152 parallel or otherwise non-intersecting lines.

1153

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

1155

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

1157 B{C{lon2}} or B{C{bearing2}}.

1158

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

1160 '''

1161 b1 = Bearing(bearing1=bearing1)

1162 b2 = Bearing(bearing2=bearing2)

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

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

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

1166 if d is None:

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

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

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

1170

1171 else:

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

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

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

1175 if isinstance(t, Intersection3Tuple): # ellipsoidal

1176 t, _, _ = t

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

1178 return t

1179

1180

1181 def intersections2(lat1, lon1, radius1,

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

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

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

1185

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

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

1188

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

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

1191 in C{meter} or ...

1192

1193 3) L{ellipsoidalKarney.intersections2} 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 4) L{ellipsoidalExact.intersections2}, provided B{C{datum}} is ellipsoidal.

1198

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

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

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

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

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

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

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

1206 or L{a_f2Tuple}) or C{scalar} earth radius (C{meter}, same units as

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

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

1209 (C{bool}).

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

1211

1212 @return: 2-Tuple of the intersection points, each a L{LatLon2Tuple}C{(lat, lon)}.

1213 Both points are the same instance, aka the I{radical center} if the

1214 circles are abutting

1215

1216 @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting

1217 circles or no convergence.

1218

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

1220

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

1222 B{C{lon2}} or B{C{radius2}}.

1223 '''

1224 r1 = Radius_(radius1=radius1)

1225 r2 = Radius_(radius2=radius2)

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

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

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

1229 if d is None:

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

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

1232

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

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

1235

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

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

1238 Vector=_V2T)

1239 else:

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

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

1242

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

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

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

1246 return t

1247

1248

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

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

1251 opposite sides of the earth.

1252

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

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

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

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

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

1258

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

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

1261

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

1263 '''

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

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

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

1267

1268

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

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

1271 opposite sides of the earth.

1272

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

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

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

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

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

1278

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

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

1281

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

1283 '''

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

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

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

1287

1288

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

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

1291 '''

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

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

1294

1295

1296 def _isequalTo_(p1, p2, eps=EPS): # underscore_!

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

1298 '''

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

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

1301

1302

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

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

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

1306

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

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

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

1310

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

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

1313

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

1315 '''

1316 return _loneg(fabs(lon)) >= eps and (_90_0 - fabs(lat)) >= eps # co-latitude

1317

1318

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

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

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

1322

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

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

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

1326

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

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

1329

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

1331 '''

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

1333

1334

1335 def _maprod(fun_, *ts):

1336 '''(INTERNAL) Helper for C{excessCagnoli_} and C{excessLHuilier_}.

1337 '''

1338 return fprod(map(fun_, ts))

1339

1340

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

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

1343 '''

1344 if fabs(b) > n:

1345 b = remainder(b, n2)

1346 if fabs(a) > n_2:

1347 r = remainder(a, n)

1348 if r != a:

1349 a = -r

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

1351 return float0_(a, b)

1352

1353

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

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

1356

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

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

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

1360

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

1362 and C{-180 <= lon <= 180}.

1363

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

1365 '''

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

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

1368

1369

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

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

1372

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

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

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

1376

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

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

1379

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

1381 '''

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

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

1384

1385

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

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

1388 '''

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

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

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

1392

1393

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

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

1396

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

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

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

1400

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

1402 in similar or C{None} if in I{perpendicular} directions.

1403

1404 @see: Function L{opposing_}.

1405 '''

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

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

1408

1409

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

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

1412

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

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

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

1416

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

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

1419

1420 @see: Function L{opposing}.

1421 '''

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

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

1424

1425

1426 def _Propy(func, nargs, kwds):

1427 '''(INTERNAL) Helper for the C{frechet.[-]Frechet**} and

1428 C{hausdorff.[-]Hausdorff*} classes.

1429 '''

1430 try:

1431 _xcallable(distance=func)

1432 # assert _args_kwds_count2(func)[0] == nargs + int(ismethod(func))

1433 _ = func(*_0_0s(nargs), **kwds)

1434 except Exception as x:

1435 t = unstr(func, **kwds)

1436 raise _TypeError(t, cause=x)

1437 return func

1438

1439

1440 def _radical2(d, r1, r2, **name): # in .ellipsoidalBaseDI, .sphericalTrigonometry, .vector3d

1441 # (INTERNAL) See C{radical2} below

1442 # assert d > EPS0

1443 r = fsumf_(_1_0, (r1 / d)**2, -(r2 / d)**2) * _0_5

1444 n = _name__(name, name__=radical2)

1445 return Radical2Tuple(max(_0_0, min(_1_0, r)), r * d, name=n)

1446

1447

1448 def radical2(distance, radius1, radius2, **name):

1449 '''Compute the I{radical ratio} and I{radical line} of two U{intersecting

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

1451

1452 The I{radical line} is perpendicular to the axis thru the centers of

1453 the circles at C{(0, 0)} and C{(B{distance}, 0)}.

1454

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

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

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

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

1459

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

1461 1.0} and C{xline} is along the B{C{distance}}.

1462

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

1464 B{C{radius1}} and B{C{radius2}}.

1465

1466 @raise UnitError: Invalid B{C{distance}}, B{C{radius1}} or B{C{radius2}}.

1467 '''

1468 d = Distance_(distance, low=_0_0)

1469 r1 = Radius_(radius1=radius1)

1470 r2 = Radius_(radius2=radius2)

1471 if d > (r1 + r2):

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

1473 txt=_too_(_distant_))

1474 return _radical2(d, r1, r2, **name) if d > EPS0 else \

1475 Radical2Tuple(_0_5, _0_0, **name)

1476

1477

1478 class Radical2Tuple(_NamedTuple):

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

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

1481 '''

1482 _Names_ = (_ratio_, _xline_)

1483 _Units_ = ( Scalar, Scalar)

1484

1485

1486 def _radistance(inst):

1487 '''(INTERNAL) Helper for the L{frechet._FrechetMeterRadians}

1488 and L{hausdorff._HausdorffMeterRedians} classes.

1489 '''

1490 wrap_, kwds_ = _xkwds_pop2(inst._kwds, wrap=False)

1491 func_ = inst._func_

1492 try: # calling lower-overhead C{func_}

1493 func_(0, _0_25, _0_5, **kwds_)

1494 wrap_ = _Wrap._philamop(wrap_)

1495 except TypeError:

1496 return inst.distance

1497

1498 def _philam(p):

1499 try:

1500 return p.phi, p.lam

1501 except AttributeError: # no .phi or .lam

1502 return radians(p.lat), radians(p.lon)

1503

1504 def _func_wrap(point1, point2):

1505 phi1, lam1 = wrap_(*_philam(point1))

1506 phi2, lam2 = wrap_(*_philam(point2))

1507 return func_(phi2, phi1, lam2 - lam1, **kwds_)

1508

1509 inst._units = inst._units_

1510 return _func_wrap

1511

1512

1513 def _scale_deg(lat1, lat2): # degrees

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

1515 m = fabs(lat1 + lat2) * _0_5

1516 return cos(radians(m)) if m < _90_0 else _0_0

1517

1518

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

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

1521 m = fabs(phi1 + phi2) * _0_5

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

1523

1524

1525 def _sincosa6(phi2, phi1, lam21): # [4] in cosineLaw

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

1527 '''

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

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

1530

1531

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

1533 '''Compute the distance between two (ellipsoidal) points using U{Thomas'

1534 <https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} formula.

1535

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

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

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

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

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

1541 or L{a_f2Tuple}) to use.

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

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

1544

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

1546

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

1548

1549 @see: Functions L{thomas_}, L{cosineLaw}, L{equirectangular}, L{euclidean},

1550 L{flatLocal} / L{hubeny}, L{flatPolar}, L{haversine}, L{vincentys} and

1551 method L{Ellipsoid.distance2}.

1552 '''

1553 return _dE(thomas_, datum, wrap, lat1, lon1, lat2, lon2)

1554

1555

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

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

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

1559

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

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

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

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

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

1565

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

1567

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

1569

1570 @see: Functions L{thomas}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /

1571 L{hubeny_}, L{flatPolar_}, L{haversine_} and L{vincentys_} and

1572 U{Geodesy-PHP<https://GitHub.com/jtejido/geodesy-php/blob/master/

1573 src/Geodesy/Distance/ThomasFormula.php>}.

1574 '''

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

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

1577 E = _ellipsoidal(datum, thomas_)

1578 f = E.f * _0_25

1579 if f: # ellipsoidal

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

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

1582

1583 j = (r2 + r1) * _0_5

1584 k = (r2 - r1) * _0_5

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

1586

1587 h = fsumf_(sk**2, (ck * h)**2, -(sj * h)**2)

1588 u = _1_0 - h

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

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

1591 sr, cr = sincos2(r)

1592 if isnon0(sr):

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

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

1595 x = u + h

1596 y = u - h

1597

1598 b = r * 2

1599 s = r / sr

1600 e = 4 * s**2

1601 d = 2 * cr

1602 a = e * d

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

1604

1605 t = fdot_(a, x, -b, y, -d, y**2, c, x**2, e, x * y) * _0_25

1606 r -= fdot_(s, x, -1, y, -t, f) * f * sr

1607 return r

1608

1609

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

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

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

1613 spherical formula.

1614

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

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

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

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

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

1620 ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple})

1621 to use.

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

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

1624

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

1626

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

1628

1629 @see: Functions L{vincentys_}, L{cosineLaw}, L{equirectangular}, L{euclidean},

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

1631 methods L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and

1632 C{LatLon.equirectangularTo}.

1633

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

1635 '''

1636 return _dS(vincentys_, radius, wrap, lat1, lon1, lat2, lon2)

1637

1638

1639 def vincentys_(phi2, phi1, lam21):

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

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

1642 spherical formula.

1643

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

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

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

1647

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

1649

1650 @see: Functions L{vincentys}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /

1651 L{hubeny_}, L{flatPolar_}, L{haversine_} and L{thomas_}.

1652

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

1654 equivalent results, but L{vincentys_} is suitable for antipodal

1655 points and slightly more expensive (M{3 cos, 3 sin, 1 hypot, 1 atan2,

1656 6 mul, 2 add}) than L{haversine_} (M{2 cos, 2 sin, 2 sqrt, 1 atan2, 5

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

1658 '''

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

1660

1661 c = c2 * c21

1662 x = s1 * s2 + c1 * c

1663 y = c1 * s2 - s1 * c

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

1665

1666# **) MIT License

1667#

1668# Copyright (C) 2016-2026 -- mrJean1 at Gmail -- All Rights Reserved.

1669#

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

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

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

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

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

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

1676#

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

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

1679#

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

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

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

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

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

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

1686# OTHER DEALINGS IN THE SOFTWARE.