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 _ # PYCHOK semicolon

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, typename

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

28 Intersection3Tuple, PhiLam2Tuple

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

30# from pygeodesy.triaxials import _hartzell3 # _MODS

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

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

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

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

35 m2degrees, tan_2, sincos2, sincos2_, _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

41 from contextlib import contextmanager

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

43

44__all__ = _ALL_LAZY.formy

45__version__ = '25.04.14'

46

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

48_ratio_ = 'ratio'

49_xline_ = 'xline'

50

51

52 def angle2chord(rad, radius=R_M):

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

54

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

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

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

58

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

60

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

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

63 '''

64 d = _isDegrees(rad, iscalar=False)

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

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

67

68

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

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

71 '''

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

73 if r == a:

74 r = -r

75 b += n

76 if fabs(b) > n:

77 b = remainder(b, n2)

78 return float0_(r, b)

79

80

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

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

83 point in C{degrees}.

84

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

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

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

88

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

90

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

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

93 '''

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

95

96

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

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

99 point in C{radians}.

100

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

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

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

104

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

106

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

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

109 '''

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

111

112

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

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

115 (spherical) points.

116

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

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

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

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

121 @kwarg final_wrap: Optional keyword arguments for function L{pygeodesy.bearing_}.

122

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

124 coincide.

125 '''

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

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

128 return degrees(r)

129

130

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

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

133 (spherical) points.

134

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

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

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

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

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

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

141 (C{bool}).

142

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

144 coincide.

145

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

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

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

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

150 '''

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

152 if final: # swap plus PI

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

154 r = PI3

155 else:

156 r = PI2

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

158

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

160 y = sdb * ca2

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

162

163

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

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

166 '''

167 try: # for LatLon_ and ellipsoidal LatLon

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

169 except AttributeError:

170 pass

171 # XXX spherical version, OK for ellipsoidal ispolar?

172 t = p1.philam + p2.philam

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

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

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

176 name__=_bearingTo2)

177

178

179 def chord2angle(chord, radius=R_M):

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

181

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

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

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

185

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

187

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

189

190 @see: Function L{angle2chord}.

191 '''

192 m = _mean_radius(radius)

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

194 if r:

195 i = int(r)

196 if i > 0:

197 r -= i

198 i *= PI

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

200 return _copysign(r, chord)

201

202

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

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

205 lat2 - lat1)} between two points.

206

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

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

209

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

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

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

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

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

215 latitude (C{bool}).

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

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

218

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

220

221 @note: Courtesy of Martin Schultz.

222

223 @see: U{Local, flat earth approximation

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

225 '''

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

227 if adjust: # scale delta lon

228 d_lon *= _scale_deg(lat1, lat2)

229 return atan2b(d_lon, lat2 - lat1)

230

231

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

233 datum=_WGS84, radius=R_M):

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

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

236 formula, optionally corrected.

237

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

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

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

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

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

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

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

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

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

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

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

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

250 (C{bool}).

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

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

253

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

255 ellipsoid axes).

256

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

258

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

260

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

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

263 method L{Ellipsoid.distance2}.

264

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

266 '''

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

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

269

270

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

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

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

274 optionally corrected.

275

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

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

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

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

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

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

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

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

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

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

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

287

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

289

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

291

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

293

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

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

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

297 AndoyerLambert.php>}.

298 '''

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

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

301 E = _ellipsoidal(earth or datum, cosineLaw_)

302 f = _0_25 * E.f

303 if f: # ellipsoidal

304 if corr == 1: # Andoyer-Lambert

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

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

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

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

309 if r:

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

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

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

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

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

315

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

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

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

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

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

321 x = s + t

322 y = s - t

323

324 s = 8 * r**2 / sr

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

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

327 b = -2 * d

328 e = 30 * s2r

329

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

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

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

333 else:

334 raise _ValueError(corr=corr)

335 return r

336

337

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

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

340 '''

341 if wrap:

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

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

344 else: # for backward compaibility

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

346

347

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

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

350 '''

351 E = _ellipsoidal(earth, fun_)

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

353 return r * E.a

354

355

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

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

358 '''

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

360 if radius is not R_M:

361 try: # datum?

362 radius = radius.ellipsoid.R1

363 except AttributeError:

364 pass # scalar?

365 lat1, _, lat2, _ = lls

366 radius = _mean_radius(radius, lat1, lat2)

367 return r * radius

368

369

370 def _ellipsoidal(earth, where):

371 '''(INTERNAL) Helper for distances.

372 '''

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

374 earth if isinstance(earth, Ellipsoid) else

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

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

377

378

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

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

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

382

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

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

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

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

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

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

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

390

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

392 ellipsoid axes).

393

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

395

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

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

398 '''

399 r = _mean_radius(radius, lat1, lat2)

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

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

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

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

404

405

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

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

408 L{hausdorff._HausdorffMeterRedians}.

409 '''

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

411 return t.distance2 * _RADIANS2

412

413

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

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

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

417

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

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

420

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

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

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

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

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

426 mean latitude (C{bool}).

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

428 or C{0} for unlimited.

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

430 (C{bool}).

431

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

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

434

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

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

437

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

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

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

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

442 '''

443 if wrap:

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

445 else:

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

447 d_lat = lat2 - lat1

448

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

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

451 if d > limit:

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

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

454 limit=limit, wrap=wrap)

455 raise LimitError(s, txt=t)

456

457 if adjust: # scale delta lon

458 d_lon *= _scale_deg(lat1, lat2)

459

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

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

462

463

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

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

466

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

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

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

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

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

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

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

474 latitude (C{bool}).

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

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

477

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

479 or datum axes).

480

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

482

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

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

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

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

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

488 C{LatLon.equirectangularTo}.

489 '''

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

491

492

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

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

495

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

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

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

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

500 latitude (C{bool}).

501

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

503

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

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

506 '''

507 if adjust:

508 lam21 *= _scale_rad(phi2, phi1)

509 return euclid(phi2 - phi1, lam21)

510

511

512 def excessAbc_(A, b, c):

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

514 and the included (small) angle.

515

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

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

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

519

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

521

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

523

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

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

526 '''

527 A = Radians_(A=A)

528 b = Radians_(b=b) * _0_5

529 c = Radians_(c=c) * _0_5

530

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

532 s = sA * sb * sc

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

534 return atan2(s, c) * _2_0

535

536

537 def excessCagnoli_(a, b, c):

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

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

540

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

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

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

544

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

546

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

548

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

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

551 '''

552 a = Radians_(a=a)

553 b = Radians_(b=b)

554 c = Radians_(c=c)

555

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

557 if r:

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

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

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

561 return Radians(Cagnoli=r * _2_0)

562

563

564 def excessGirard_(A, B, C):

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

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

567

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

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

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

571

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

573

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

575

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

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

578 '''

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

580 Radians_(B=B),

581 Radians_(C=C), -PI)

582 return Radians(Girard=r)

583

584

585 def excessLHuilier_(a, b, c):

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

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

588

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

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

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

592

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

594

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

596

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

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

599 '''

600 a = Radians_(a=a)

601 b = Radians_(b=b)

602 c = Radians_(c=c)

603

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

605 r = _maprod(tan_2, s, s - a, s - b, s - c)

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

607 return Radians(LHuilier=r * _4_0)

608

609

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

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

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

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

614 method.

615

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

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

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

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

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

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

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

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

624

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

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

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

628

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

630

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

632

633 @raise ValueError: Semi-circular longitudinal delta.

634

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

636 '''

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

638 if radius:

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

640 return r

641

642

643 def excessKarney_(phi2, phi1, lam21):

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

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

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

647 method.

648

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

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

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

652

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

654

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

656

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

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

659 '''

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

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

662 #

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

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

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

666 #

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

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

669 t2 = tan_2(phi2)

670 t1 = tan_2(phi1)

671 c = (t1 * t2) + _1_0

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

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

674

675

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

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

678#

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

680#

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

682#

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

684#

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

686#

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

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

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

690#

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

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

693#

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

695#

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

697# inverse Gudermannian) function

698#

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

700#

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

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

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

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

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

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

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

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

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

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

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

712# - Charles Karney

713

714

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

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

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

718

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

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

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

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

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

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

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

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

727

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

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

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

731

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

733

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

735

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

737 '''

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

739 if radius:

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

741 return r

742

743

744 def excessQuad_(phi2, phi1, lam21):

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

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

747

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

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

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

751

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

753

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

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

756 '''

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

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

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

760

761

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

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

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

765 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}

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

767

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

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

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

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

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

773 or L{a_f2Tuple}) to use.

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

775 method L{pygeodesy.Ellipsoid.roc2_}.

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

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

778

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

780

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

782

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

784 scaled at the mean of both latitude.

785

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

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

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

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

790 '''

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

792 E = _ellipsoidal(datum, flatLocal)

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

794

795 hubeny = flatLocal # PYCHOK for Karl Hubeny

796

797

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

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

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

801 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}

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

803

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

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

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

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

808 or L{a_f2Tuple}) to use.

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

810 method L{pygeodesy.Ellipsoid.roc2_}.

811

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

813

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

815

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

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

818

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

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

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

822 '''

823 E = _ellipsoidal(datum, flatLocal_)

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

825

826 hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny

827

828

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

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

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

832 #Polar_coordinate_flat-Earth_formula>} formula.

833

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

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

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

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

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

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

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

841 (C{bool}).

842

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

844 ellipsoid axes).

845

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

847

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

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

850 '''

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

852

853

854 def flatPolar_(phi2, phi1, lam21):

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

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

857 #Polar_coordinate_flat-Earth_formula>} formula.

858

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

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

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

862

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

864

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

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

867 '''

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

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

870 if a < b:

871 a, b = b, a

872 if a < EPS0:

873 a = _0_0

874 elif b > 0:

875 b = b / a # /= chokes PyChecker

876 c = b * cos(lam21) * _2_0

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

878 a *= sqrt0(c)

879 return a

880

881

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

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

884 '''

885 if earth is None:

886 earth = pov.datum

887 else:

888 earth = _spherical_datum(earth, name__=hartzell)

889 pov = pov.toDatum(earth)

890 h = pov.height

891 if h < 0: # EPS0

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

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

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

895

896

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

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

899 a Point-Of-View in space.

900

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

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

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

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

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

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

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

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

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

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

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

912

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

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

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

916

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

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

919 away from the earth.

920

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

922

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

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

925 '''

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

927 try:

928 D = _spherical_datum(earth, name__=hartzell)

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

930

931 C = _MODS.cartesianBase.CartesianBase

932 if kwds:

933 c = C(r, datum=D)

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

935 elif isinstance(r, C):

936 r.height = h

937 if i:

938 r._iteration = i

939 except Exception as x:

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

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

942

943

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

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

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

947

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

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

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

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

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

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

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

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

956

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

958

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

960

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

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

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

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

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

966

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

968 '''

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

970

971

972 def haversine_(phi2, phi1, lam21):

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

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

975

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

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

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

979

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

981

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

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

984

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

986 '''

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

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

989

990

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

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

993 traveling along a straight line at a given tilt.

994

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

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

997 B{C{radius}}).

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

999

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

1001

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

1003

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

1005 (U{Shapiro et al. 2009, JTECH

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

1007 and U{Potvin et al. 2012, JTECH

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

1009 '''

1010 r = h = Radius(radius)

1011 d = fabs(Distance(distance))

1012 if d > h:

1013 d, h = h, d

1014

1015 if d > EPS0: # and h > EPS0

1016 d = d / h # /= h chokes PyChecker

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

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

1019 if s > 0:

1020 return h * sqrt(s) - r

1021

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

1023

1024

1025 def heightOrthometric(h_loc, N):

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

1027

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

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

1030 attribute), otherwise C{0 meter}.

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

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

1033

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

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

1036

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

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

1039 6 and module L{pygeodesy.geoids}.

1040 '''

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

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

1043

1044

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

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

1047 (spherical) earth.

1048

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

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

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

1052

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

1054

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

1056

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

1058 '''

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

1060 if min(h, r) < 0:

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

1062

1063 if refraction:

1064 r *= 2.415750694528 # 2.0 / 0.8279

1065 else:

1066 r += r + h

1067 return sqrt0(r * h)

1068

1069

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

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

1072 '''

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

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

1075 try:

1076 if wrap:

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

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

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

1080 n = typename(intersections2 if s else intersection2)

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

1082 d, m = None, _MODS.vector3d

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

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

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

1086 m = _MODS.sphericalNvector

1087 _i = m.intersection

1088 else:

1089 d = _spherical_datum(datum, name=n)

1090 if d.isSpherical:

1091 m = _MODS.sphericalTrigonometry

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

1093 elif d.isEllipsoidal:

1094 try:

1095 if d.ellipsoid.geodesic:

1096 pass

1097 m = _MODS.ellipsoidalKarney

1098 except ImportError:

1099 m = _MODS.ellipsoidalExact

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

1101 else:

1102 raise _TypeError(datum=datum)

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

1104

1105 except (TypeError, ValueError) as x:

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

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

1108

1109_idllmn6 = _idllmn6() # PYCHOK singleton

1110

1111

1112 def intersection2(lat1, lon1, bearing1,

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

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

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

1116

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

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

1119

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

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

1122 in C{meter} or ...

1123

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

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

1126

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

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

1129 is installed, otherwise ...

1130

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

1132

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

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

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

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

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

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

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

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

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

1142 or C{None}.

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

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

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

1146

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

1148

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

1150 parallel or otherwise non-intersecting lines.

1151

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

1153

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

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

1156

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

1158 '''

1159 b1 = Bearing(bearing1=bearing1)

1160 b2 = Bearing(bearing2=bearing2)

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

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

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

1164 if d is None:

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

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

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

1168

1169 else:

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

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

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

1173 if isinstance(t, Intersection3Tuple): # ellipsoidal

1174 t, _, _ = t

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

1176 return t

1177

1178

1179 def intersections2(lat1, lon1, radius1,

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

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

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

1183

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

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

1186

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

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

1189 in C{meter} or ...

1190

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

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

1193 is installed, otherwise ...

1194

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

1196

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

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

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

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

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

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

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

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

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

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

1207 (C{bool}).

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

1209

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

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

1212 circles are abutting

1213

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

1215 circles or no convergence.

1216

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

1218

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

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

1221 '''

1222 r1 = Radius_(radius1=radius1)

1223 r2 = Radius_(radius2=radius2)

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

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

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

1227 if d is None:

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

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

1230

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

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

1233

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

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

1236 Vector=_V2T)

1237 else:

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

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

1240

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

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

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

1244 return t

1245

1246

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

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

1249 opposite sides of the earth.

1250

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

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

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

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

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

1256

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

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

1259

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

1261 '''

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

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

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

1265

1266

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

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

1269 opposite sides of the earth.

1270

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

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

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

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

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

1276

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

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

1279

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

1281 '''

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

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

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

1285

1286

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

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

1289 '''

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

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

1292

1293

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

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

1296 '''

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

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

1299

1300

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

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

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

1304

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

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

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

1308

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

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

1311

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

1313 '''

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

1315

1316

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

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

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

1320

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

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

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

1324

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

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

1327

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

1329 '''

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

1331

1332

1333 def _maprod(fun_, *ts):

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

1335 '''

1336 return fprod(map(fun_, ts))

1337

1338

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

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

1341 '''

1342 if fabs(b) > n:

1343 b = remainder(b, n2)

1344 if fabs(a) > n_2:

1345 r = remainder(a, n)

1346 if r != a:

1347 a = -r

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

1349 return float0_(a, b)

1350

1351

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

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

1354

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

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

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

1358

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

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

1361

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

1363 '''

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

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

1366

1367

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

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

1370

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

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

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

1374

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

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

1377

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

1379 '''

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

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

1382

1383

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

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

1386 '''

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

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

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

1390

1391

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

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

1394

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

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

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

1398

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

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

1401

1402 @see: Function L{opposing_}.

1403 '''

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

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

1406

1407

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

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

1410

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

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

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

1414

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

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

1417

1418 @see: Function L{opposing}.

1419 '''

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

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

1422

1423

1424 def _Propy(func, nargs, kwds):

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

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

1427 '''

1428 try:

1429 _xcallable(distance=func)

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

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

1432 except Exception as x:

1433 t = unstr(func, **kwds)

1434 raise _TypeError(t, cause=x)

1435 return func

1436

1437

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

1439 # (INTERNAL) See C{radical2} below

1440 # assert d > EPS0

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

1442 n = _name__(name, name__=radical2)

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

1444

1445

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

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

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

1449

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

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

1452

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

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

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

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

1457

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

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

1460

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

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

1463

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

1465 '''

1466 d = Distance_(distance, low=_0_0)

1467 r1 = Radius_(radius1=radius1)

1468 r2 = Radius_(radius2=radius2)

1469 if d > (r1 + r2):

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

1471 txt=_too_(_distant_))

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

1473 Radical2Tuple(_0_5, _0_0, **name)

1474

1475

1476 class Radical2Tuple(_NamedTuple):

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

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

1479 '''

1480 _Names_ = (_ratio_, _xline_)

1481 _Units_ = ( Scalar, Scalar)

1482

1483

1484 def _radistance(inst):

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

1486 and L{hausdorff._HausdorffMeterRedians} classes.

1487 '''

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

1489 func_ = inst._func_

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

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

1492 wrap_ = _Wrap._philamop(wrap_)

1493 except TypeError:

1494 return inst.distance

1495

1496 def _philam(p):

1497 try:

1498 return p.phi, p.lam

1499 except AttributeError: # no .phi or .lam

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

1501

1502 def _func_wrap(point1, point2):

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

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

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

1506

1507 inst._units = inst._units_

1508 return _func_wrap

1509

1510

1511 def _scale_deg(lat1, lat2): # degrees

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

1513 m = fabs(lat1 + lat2) * _0_5

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

1515

1516

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

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

1519 m = fabs(phi1 + phi2) * _0_5

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

1521

1522

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

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

1525 '''

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

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

1528

1529

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

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

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

1533

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

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

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

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

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

1539 or L{a_f2Tuple}) to use.

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

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

1542

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

1544

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

1546

1547 @see: Functions L{thomas_}, L{cosineLaw}, L{equirectangular}, L{euclidean},

1548 L{flatLocal} / L{hubeny}, L{flatPolar}, L{haversine}, L{vincentys} and

1549 method L{Ellipsoid.distance2}.

1550 '''

1551 return _dE(thomas_, datum, wrap, lat1, lon1, lat2, lon2)

1552

1553

1554 def thomas_(phi2, phi1, lam21, datum=_WGS84):

1555 '''Compute the I{angular} distance between two (ellipsoidal) points using

1556 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} formula.

1557

1558 @arg phi2: End latitude (C{radians}).

1559 @arg phi1: Start latitude (C{radians}).

1560 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).

1561 @kwarg datum: Datum (L{Datum}) ?or ellipsoid to use (L{Ellipsoid},

1562 L{Ellipsoid2} or L{a_f2Tuple}).

1563

1564 @return: Angular distance (C{radians}).

1565

1566 @raise TypeError: Invalid B{C{datum}}.

1567

1568 @see: Functions L{thomas}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /

1569 L{hubeny_}, L{flatPolar_}, L{haversine_} and L{vincentys_} and

1570 U{Geodesy-PHP<https://GitHub.com/jtejido/geodesy-php/blob/master/

1571 src/Geodesy/Distance/ThomasFormula.php>}.

1572 '''

1573 s2, c2, s1, c1, r, _ = _sincosa6(phi2, phi1, lam21)

1574 if r and isnon0(c1) and isnon0(c2):

1575 E = _ellipsoidal(datum, thomas_)

1576 f = E.f * _0_25

1577 if f: # ellipsoidal

1578 r1 = atan2(E.b_a * s1, c1)

1579 r2 = atan2(E.b_a * s2, c2)

1580

1581 j = (r2 + r1) * _0_5

1582 k = (r2 - r1) * _0_5

1583 sj, cj, sk, ck, h, _ = sincos2_(j, k, lam21 * _0_5)

1584

1585 h = fsumf_(sk**2, (ck * h)**2, -(sj * h)**2)

1586 u = _1_0 - h

1587 if isnon0(u) and isnon0(h):

1588 r = atan(sqrt0(h / u)) * 2 # == acos(1 - 2 * h)

1589 sr, cr = sincos2(r)

1590 if isnon0(sr):

1591 u = (sj * ck)**2 * 2 / u

1592 h = (sk * cj)**2 * 2 / h

1593 x = u + h

1594 y = u - h

1595

1596 b = r * 2

1597 s = r / sr

1598 e = 4 * s**2

1599 d = 2 * cr

1600 a = e * d

1601 c = s - (a - d) * _0_5

1602

1603 t = fdot_(a, x, -b, y, -d, y**2, c, x**2, e, x * y) * _0_25

1604 r -= fdot_(s, x, -1, y, -t, f) * f * sr

1605 return r

1606

1607

1608 def vincentys(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):

1609 '''Compute the distance between two (spherical) points using

1610 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}

1611 spherical formula.

1612

1613 @arg lat1: Start latitude (C{degrees}).

1614 @arg lon1: Start longitude (C{degrees}).

1615 @arg lat2: End latitude (C{degrees}).

1616 @arg lon2: End longitude (C{degrees}).

1617 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or

1618 ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple})

1619 to use.

1620 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and

1621 B{C{lon2}} (C{bool}).

1622

1623 @return: Distance (C{meter}, same units as B{C{radius}}).

1624

1625 @raise UnitError: Invalid B{C{radius}}.

1626

1627 @see: Functions L{vincentys_}, L{cosineLaw}, L{equirectangular}, L{euclidean},

1628 L{flatLocal}/L{hubeny}, L{flatPolar}, L{haversine} and L{thomas} and

1629 methods L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and

1630 C{LatLon.equirectangularTo}.

1631

1632 @note: See note at function L{vincentys_}.

1633 '''

1634 return _dS(vincentys_, radius, wrap, lat1, lon1, lat2, lon2)

1635

1636

1637 def vincentys_(phi2, phi1, lam21):

1638 '''Compute the I{angular} distance between two (spherical) points using

1639 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}

1640 spherical formula.

1641

1642 @arg phi2: End latitude (C{radians}).

1643 @arg phi1: Start latitude (C{radians}).

1644 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).

1645

1646 @return: Angular distance (C{radians}).

1647

1648 @see: Functions L{vincentys}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /

1649 L{hubeny_}, L{flatPolar_}, L{haversine_} and L{thomas_}.

1650

1651 @note: Functions L{vincentys_}, L{haversine_} and L{cosineLaw_} produce

1652 equivalent results, but L{vincentys_} is suitable for antipodal

1653 points and slightly more expensive (M{3 cos, 3 sin, 1 hypot, 1 atan2,

1654 6 mul, 2 add}) than L{haversine_} (M{2 cos, 2 sin, 2 sqrt, 1 atan2, 5

1655 mul, 1 add}) and L{cosineLaw_} (M{3 cos, 3 sin, 1 acos, 3 mul, 1 add}).

1656 '''

1657 s1, c1, s2, c2, s21, c21 = sincos2_(phi1, phi2, lam21)

1658

1659 c = c2 * c21

1660 x = s1 * s2 + c1 * c

1661 y = c1 * s2 - s1 * c

1662 return atan2(hypot(c2 * s21, y), x)

1663

1664# **) MIT License

1665#

1666# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.

1667#

1668# Permission is hereby granted, free of charge, to any person obtaining a

1669# copy of this software and associated documentation files (the "Software"),

1670# to deal in the Software without restriction, including without limitation

1671# the rights to use, copy, modify, merge, publish, distribute, sublicense,

1672# and/or sell copies of the Software, and to permit persons to whom the

1673# Software is furnished to do so, subject to the following conditions:

1674#

1675# The above copyright notice and this permission notice shall be included

1676# in all copies or substantial portions of the Software.

1677#

1678# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

1679# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

1680# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

1681# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

1682# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

1683# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

1684# OTHER DEALINGS IN THE SOFTWARE.