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 isscalar # from .fsums

10# from pygeodesy.cartesianBase import CartesianBase # _MODS

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

12 _umod_PI2, float0_, isnon0, remainder, \

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

14 _4_0, _32_0, _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 _xkwds, _xkwds_pop

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

22 from pygeodesy.fsums import fsumf_, isscalar

23 from pygeodesy.interns import NN, _delta_, _distant_, _SPACE_, _too_

24 from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS

25 from pygeodesy.named import _NamedTuple, _xnamed, Fmt, unstr

26 from pygeodesy.namedTuples import Bearing2Tuple, Distance4Tuple, \

27 Intersection3Tuple, LatLon2Tuple, \

28 PhiLam2Tuple, Vector3Tuple

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

30# from pygeodesy.triaxials import _hartzell3d2 # _MODS

31 from pygeodesy.units import Bearing, Degrees_, Distance, Distance_, Height, \

32 Lam_, Lat, Lon, Meter_, Phi_, Radians, Radians_, \

33 Radius, Radius_, Scalar, _100km

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

35 tan_2, sincos2, sincos2_, sincos2d_, _Wrap

36# from pygeodesy.vector3d import _otherV3d # _MODS

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

38# sphericalNvector, sphericalTrigonometry # _MODS

39

40 from contextlib import contextmanager

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

42

43__all__ = _ALL_LAZY.formy

44__version__ = '23.09.06'

45

46_D2_R2 = (PI / _180_0)**2 # degrees- to radians-squared

47_ratio_ = 'ratio'

48_xline_ = 'xline'

49

50

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

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

53 '''

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

55 if r == a:

56 r = -r

57 b += n

58 if fabs(b) > n:

59 b = remainder(b, n2)

60 return float0_(r, b)

61

62

63 def antipode(lat, lon, name=NN):

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

65 to a given point in C{degrees}.

66

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

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

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

70

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

72

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

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

75 '''

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

77

78

79 def antipode_(phi, lam, name=NN):

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

81 to a given point in C{radians}.

82

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

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

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

86

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

88

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

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

91 '''

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

93

94

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

96 '''Compute the initial or final bearing (forward or reverse

97 azimuth) between a (spherical) start and end point.

98

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

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

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

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

103 @kwarg final_wrap: Optional keyword arguments for function

104 L{pygeodesy.bearing_}.

105

106 @return: Initial or final bearing (compass C{degrees360}) or

107 zero if start and end point coincide.

108 '''

109 r = bearing_(Phi_(lat1=lat1), Lam_(lon1=lon1),

110 Phi_(lat2=lat2), Lam_(lon2=lon2), **final_wrap)

111 return degrees(r)

112

113

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

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

116 between a (spherical) start and end point.

117

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

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

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

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

122 @kwarg final: Return final bearing if C{True}, initial otherwise (C{bool}).

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

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

125

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

127 and end point coincide.

128

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

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

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

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

133 '''

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

135 if final: # swap plus PI

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

137 r = PI3

138 else:

139 r = PI2

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

141

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

143 y = sdb * ca2

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

145

146

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

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

149 '''

150 try: # for LatLon_ and ellipsoidal LatLon

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

152 except AttributeError:

153 pass

154 # XXX spherical version, OK for ellipsoidal ispolar?

155 a1, b1 = p1.philam

156 a2, b2 = p2.philam

157 return Bearing2Tuple(degrees(bearing_(a1, b1, a2, b2, final=False, wrap=wrap)),

158 degrees(bearing_(a1, b1, a2, b2, final=True, wrap=wrap)),

159 name=_bearingTo2.__name__)

160

161

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

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

164 lat2 - lat1)} between two points.

165

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

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

168

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

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

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

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

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

174 mean latitude (C{bool}).

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

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

177

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

179

180 @note: Courtesy of Martin Schultz.

181

182 @see: U{Local, flat earth approximation

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

184 '''

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

186 if adjust: # scale delta lon

187 d_lon *= _scale_deg(lat1, lat2)

188 return atan2b(d_lon, lat2 - lat1)

189

190

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

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

193 U{Andoyer-Lambert correction<https://NavLib.net/wp-content/uploads/2013/10/

194 admiralty-manual-of-navigation-vol-1-1964-english501c.pdf>} of the U{Law of

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

196

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

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

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

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

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

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

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

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

205

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

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

208

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

210

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

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

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

214 L{Ellipsoid.distance2}.

215 '''

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

217

218

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

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

221 U{Andoyer-Lambert correction<https://NavLib.net/wp-content/uploads/2013/10/

222 admiralty-manual-of-navigation-vol-1-1964-english501c.pdf>} of the U{Law of

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

224

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

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

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

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

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

230

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

232

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

234

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

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

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

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

239 AndoyerLambert.php>}.

240 '''

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

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

243 E = _ellipsoidal(datum, cosineAndoyerLambert_)

244 if E.f: # ellipsoidal

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

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

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

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

249 if r:

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

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

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

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

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

255 return r

256

257

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

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

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

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

262 formula.

263

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

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

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

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

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

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

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

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

272

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

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

275

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

277

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

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

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

281 L{Ellipsoid.distance2}.

282 '''

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

284

285

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

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

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

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

290 formula.

291

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

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

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

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

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

297

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

299

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

301

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

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

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

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

306 Distance/ForsytheCorrection.php>}.

307 '''

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

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

310 E = _ellipsoidal(datum, cosineForsytheAndoyerLambert_)

311 if E.f: # ellipsoidal

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

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

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

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

316 x = s + t

317 y = s - t

318

319 s = 8 * r**2 / sr

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

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

322 b = -2 * d

323 e = 30 * s2r

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

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

326

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

328 return r

329

330

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

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

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

334 formula.

335

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

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

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

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

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

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

342 L{a_f2Tuple}) to use.

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

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

345

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

347 ellipsoid or datum axes).

348

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

350

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

352 L{cosineForsytheAndoyerLambert}, L{equirectangular}, L{euclidean},

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

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

355

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

357 '''

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

359

360

361 def cosineLaw_(phi2, phi1, lam21):

362 '''Compute the I{angular} distance between two points using the U{spherical

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

364 formula.

365

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

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

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

369

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

371

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

373 L{cosineForsytheAndoyerLambert_}, L{equirectangular_},

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

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

376

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

378 '''

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

380

381

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

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

384 '''

385 if wrap:

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

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

388 else: # for backward compaibility

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

390

391

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

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

394 '''

395 E = _ellipsoidal(earth, func_)

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

397 return r * E.a

398

399

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

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

402 '''

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

404 if radius is not R_M:

405 _, lat1, _, lat2, _ = wrap_lls

406 radius = _mean_radius(radius, lat1, lat2)

407 return r * radius

408

409

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

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

412 '''

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

414 if radius:

415 _, lat1, _, lat2, _ = wrap_lls

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

417 return r

418

419

420 def _ellipsoidal(earth, where):

421 '''(INTERNAL) Helper for distances.

422 '''

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

424 earth if isinstance(earth, Ellipsoid) else

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

426 _ellipsoidal_datum(earth, name=where.__name__)).ellipsoid)

427

428

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

430 '''Compute the distance between two points using

431 the U{Equirectangular Approximation / Projection

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

433

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

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

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

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

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

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

440 L{a_f2Tuple}).

441 @kwarg adjust_limit_wrap: Optional keyword arguments for

442 function L{equirectangular_}.

443

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

445 the ellipsoid or datum axes).

446

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

448

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

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

451 approximate or accurate distance functions.

452 '''

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

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

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

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

457

458

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

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

461 and L{hausdorff._HausdorffMeterRedians} classes.

462 '''

463 return equirectangular_(lat1, lon1, lat2, lon2, **adjust_limit_wrap).distance2 * _D2_R2

464

465

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

467 '''Compute the distance between two points using the U{Equirectangular

468 Approximation / Projection

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

470

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

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

473

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

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

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

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

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

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

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

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

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

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

484

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

486 unroll_lon2)} in C{degrees squared}.

487

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

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

490 set to C{True}.

491

492 @see: U{Local, flat earth approximation

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

494 L{equirectangular}, L{cosineAndoyerLambert},

495 L{cosineForsytheAndoyerLambert}, L{cosineLaw}, L{euclidean},

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

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

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

499 '''

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

501 d_lat = lat2 - lat1

502

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

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

505 if d > limit:

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

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

508 limit=limit, wrap=wrap)

509 raise LimitError(s, txt=t)

510

511 if adjust: # scale delta lon

512 d_lon *= _scale_deg(lat1, lat2)

513

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

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

516

517

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

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

520

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

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

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

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

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

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

527 L{a_f2Tuple}) to use.

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

529 the mean latitude (C{bool}).

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

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

532

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

534 ellipsoid or datum axes).

535

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

537

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

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

540 L{euclidean_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert},

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

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

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

544 '''

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

546

547

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

549 '''Approximate the I{angular} C{Euclidean} distance between two

550 (spherical) points.

551

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

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

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

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

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

557

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

559

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

561 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{equirectangular_},

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

563 and L{vincentys_}.

564 '''

565 if adjust:

566 lam21 *= _scale_rad(phi2, phi1)

567 return euclid(phi2 - phi1, lam21)

568

569

570 def excessAbc_(A, b, c):

571 '''Compute the I{spherical excess} C{E} of a (spherical) triangle

572 from two sides and the included (small) angle.

573

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

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

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

577

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

579

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

581

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

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

584 '''

585 A = Radians_(A=A)

586 b = Radians_(b=b) * _0_5

587 c = Radians_(c=c) * _0_5

588

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

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

591

592

593 def excessCagnoli_(a, b, c):

594 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using

595 U{Cagnoli's<https://Zenodo.org/record/35392>} (D.34) formula.

596

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

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

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

600

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

602

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

604

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

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

607 '''

608 a = Radians_(a=a)

609 b = Radians_(b=b)

610 c = Radians_(c=c)

611

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

613 r = sin(s) * sin(s - a) * sin(s - b) * sin(s - c)

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

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

616 return Radians(Cagnoli=r * _2_0)

617

618

619 def excessGirard_(A, B, C):

620 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using

621 U{Girard's<https://MathWorld.Wolfram.com/GirardsSphericalExcessFormula.html>}

622 formula.

623

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

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

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

627

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

629

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

631

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

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

634 '''

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

636 Radians_(B=B),

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

638

639

640 def excessLHuilier_(a, b, c):

641 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using

642 U{L'Huilier's<https://MathWorld.Wolfram.com/LHuiliersTheorem.html>}

643 Theorem.

644

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

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

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

648

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

650

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

652

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

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

655 '''

656 a = Radians_(a=a)

657 b = Radians_(b=b)

658 c = Radians_(c=c)

659

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

661 r = tan_2(s) * tan_2(s - a) * tan_2(s - b) * tan_2(s - c)

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

663 return Radians(LHuilier=r * _4_0)

664

665

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

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

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

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

670 method.

671

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

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

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

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

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

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

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

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

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

681

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

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

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

685

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

687

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

689

690 @raise ValueError: Semi-circular longitudinal delta.

691

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

693 '''

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

695

696

697 def excessKarney_(phi2, phi1, lam21):

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

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

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

701 method.

702

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

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

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

706

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

708

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

710

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

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

713 '''

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

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

716 #

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

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

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

720 #

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

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

723 t2 = tan_2(phi2)

724 t1 = tan_2(phi1)

725 t = tan_2(lam21, lam21=None)

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

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

728

729

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

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

732#

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

734#

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

736#

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

738#

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

740#

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

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

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

744#

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

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

747#

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

749#

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

751# inverse Gudermannian) function

752#

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

754#

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

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

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

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

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

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

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

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

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

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

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

766# - Charles Karney

767

768

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

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

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

772

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

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

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

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

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

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

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

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

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

782

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

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

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

786

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

788

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

790

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

792 '''

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

794

795

796 def excessQuad_(phi2, phi1, lam21):

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

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

799

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

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

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

803

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

805

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

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

808 '''

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

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

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

812

813

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

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

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

817 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}

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

819

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

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

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

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

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

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

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

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

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

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

830

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

832 ellipsoid axes).

833

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

835

836 @note: The meridional and prime_vertical radii of curvature

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

838

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

840 L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert},

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

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

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

844 '''

845 E = _ellipsoidal(datum, flatLocal)

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

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

848

849 hubeny = flatLocal # PYCHOK for Karl Hubeny

850

851

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

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

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

855 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}

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

857

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

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

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

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

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

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

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

865

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

867

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

869

870 @note: The meridional and prime_vertical radii of curvature

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

872

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

874 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{flatPolar_},

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

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

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

878 '''

879 E = _ellipsoidal(datum, flatLocal_)

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

881

882 hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny

883

884

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

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

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

888 Geographical_distance#Polar_coordinate_flat-Earth_formula>}

889 formula.

890

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

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

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

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

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

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

897 L{a_f2Tuple}) to use.

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

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

900

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

902 ellipsoid or datum axes).

903

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

905

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

907 L{cosineForsytheAndoyerLambert},L{cosineLaw},

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

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

910 L{vincentys}.

911 '''

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

913

914

915 def flatPolar_(phi2, phi1, lam21):

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

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

918 Geographical_distance#Polar_coordinate_flat-Earth_formula>}

919 formula.

920

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

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

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

924

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

926

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

928 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_},

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

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

931 '''

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

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

934 if a < b:

935 a, b = b, a

936 if a < EPS0:

937 a = _0_0

938 elif b > 0:

939 b = b / a # /= chokes PyChecker

940 c = b * cos(lam21) * _2_0

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

942 a *= sqrt0(c)

943 return a

944

945

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

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

948 from a Point-Of-View in space.

949

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

951 or L{Vector3d}).

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

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

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

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

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

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

958 point plus C{LatLon} keyword arguments, include

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

960

961 @return: The intersection point (L{Vector3d}, C{Cartesian type} of

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

963

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

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

966 the earth or points in an opposite direction.

967

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

969

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

971 method L{Ellipsoid.hartzell4} and U{I{Satellite Line-of-Sight

972 Intersection with Earth}<https://StephenHartzell.Medium.com/

973 satellite-line-of-sight-intersection-with-earth-d786b4a6a9b6>}.

974 '''

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

976 _spherical_datum(earth, name=hartzell.__name__)

977 try:

978 r, _ = _MODS.triaxials._hartzell3d2(pov, los, D.ellipsoid._triaxial)

979 except Exception as x:

980 raise IntersectionError(pov=pov, los=los, earth=earth, cause=x)

981

982# else:

983# E = D.ellipsoid

984# # Triaxial(a, b, c) == (E.a, E.a, E.b)

985#

986# def _Error(txt):

987# return IntersectionError(pov=pov, los=los, earth=earth, txt=txt)

988#

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

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

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

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

993#

994# V3 = _MODS.vector3d._otherV3d

995# p3 = V3(pov=pov)

996# u3 = V3(los=los) if los else p3.negate()

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

998#

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

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

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

1002#

1003# t = c2, c2, b2

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

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

1006# raise _Error(_near_(_null_))

1007#

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

1009# r = fsumf_(b2 * w2, c2 * v2, -v2 * z2, vy * wz * 2,

1010# c2 * u2, -u2 * z2, -w2 * x2, ux * wz * 2,

1011# -w2 * y2, -u2 * y2 * q2, -v2 * x2 * q2, ux * vy * 2 * q2)

1012# if r > 0:

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

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

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

1016#

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

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

1019# s = fsumf_(_1_0, x2 / a2, y2 / b2, z2 / c2, _N_2_0) # like _sideOf

1020# raise _Error(_outside_ if s > 0 else _inside_)

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

1022# raise _Error(_too_(_distant_))

1023#

1024# r = p3.minus(u3.times(d))

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

1026

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

1028 if LatLon_and_kwds:

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

1030 r = c.toLatLon(**LatLon_and_kwds)

1031 return r

1032

1033

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

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

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

1037 formula.

1038

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

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

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

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

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

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

1045 L{a_f2Tuple}) to use.

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

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

1048

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

1050

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

1052

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

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

1055 L{cosineLaw}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert},

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

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

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

1059

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

1061 '''

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

1063

1064

1065 def haversine_(phi2, phi1, lam21):

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

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

1068 formula.

1069

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

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

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

1073

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

1075

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

1077 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_},

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

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

1080

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

1082 '''

1083 def _hsin(rad):

1084 return sin(rad * _0_5)**2

1085

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

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

1088

1089

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

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

1092 traveling along a straight line at a given tilt.

1093

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

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

1096 B{C{radius}}).

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

1098

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

1100

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

1102

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

1104 (U{Shapiro et al. 2009, JTECH

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

1106 and U{Potvin et al. 2012, JTECH

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

1108 '''

1109 r = h = Radius(radius)

1110 d = fabs(Distance(distance))

1111 if d > h:

1112 d, h = h, d

1113

1114 if d > EPS0: # and h > EPS0

1115 d = d / h # /= h chokes PyChecker

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

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

1118 if s > 0:

1119 return h * sqrt(s) - r

1120

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

1122

1123

1124 def heightOrthometric(h_ll, N):

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

1126

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

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

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

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

1131

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

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

1134

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

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

1137 6 and module L{pygeodesy.geoids}.

1138 '''

1139 h = h_ll if isscalar(h_ll) else _xattr(h_ll, height=_xattr(h_ll, h=0))

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

1141

1142

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

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

1145 above the (spherical) earth.

1146

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

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

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

1150

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

1152

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

1154

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

1156 '''

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

1158 if min(h, r) < 0:

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

1160

1161 if refraction:

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

1163 else:

1164 d2 = h * fsumf_(r, r, h)

1165 return sqrt0(d2)

1166

1167

1168 class _idllmn6(object): # see also .geodesicw._wargs, .vector2d._numpy

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

1170 '''

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

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

1173 try:

1174 if wrap:

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

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

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

1178 n = (intersections2 if s else intersection2).__name__

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

1180 d, m = None, _MODS.vector3d

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

1182 elif isscalar(datum) and datum < 0 and not s:

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

1184 m = _MODS.sphericalNvector

1185 _i = m.intersection

1186 else:

1187 d = _spherical_datum(datum, name=n)

1188 if d.isSpherical:

1189 m = _MODS.sphericalTrigonometry

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

1191 elif d.isEllipsoidal:

1192 try:

1193 if d.ellipsoid.geodesic:

1194 pass

1195 m = _MODS.ellipsoidalKarney

1196 except ImportError:

1197 m = _MODS.ellipsoidalExact

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

1199 else:

1200 raise _TypeError(datum=datum)

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

1202

1203 except (TypeError, ValueError) as x:

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

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

1206

1207_idllmn6 = _idllmn6() # PYCHOK singleton

1208

1209

1210 def intersection2(lat1, lon1, bearing1,

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

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

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

1214

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

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

1217

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

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

1220 in C{meter} or ...

1221

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

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

1224

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

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

1227 is installed, otherwise ...

1228

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

1230

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

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

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

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

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

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

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

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

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

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

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

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

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

1244

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

1246 longitude of the intersection point.

1247

1248 @raise IntersectionError: Ambiguous or infinite intersection

1249 or colinear, parallel or otherwise

1250 non-intersecting lines.

1251

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

1253

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

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

1256

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

1258

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

1260 '''

1261 b1 = Bearing(bearing1=bearing1)

1262 b2 = Bearing(bearing2=bearing2)

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

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

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

1266 if d is None:

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

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

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

1270

1271 else:

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

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

1274 if isinstance(t, Intersection3Tuple): # ellipsoidal

1275 t, _, _ = t

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

1277 return t

1278

1279

1280 def intersections2(lat1, lon1, radius1,

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

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

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

1284

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

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

1287

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

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

1290 in C{meter} or ...

1291

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

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

1294 is installed, otherwise ...

1295

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

1297

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

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

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

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

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

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

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

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

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

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

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

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

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

1311

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

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

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

1315

1316 @raise IntersectionError: Concentric, antipodal, invalid or

1317 non-intersecting circles or no

1318 convergence.

1319

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

1321

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

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

1324 '''

1325 r1 = Radius_(radius1=radius1)

1326 r2 = Radius_(radius2=radius2)

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

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

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

1330 if d is None:

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

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

1333

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

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

1336

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

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

1339 Vector=_V2T)

1340 else:

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

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

1343

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

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

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

1347 return t

1348

1349

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

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

1352 opposite sides of the earth.

1353

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

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

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

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

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

1359

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

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

1362

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

1364 '''

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

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

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

1368

1369

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

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

1372 opposite sides of the earth.

1373

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

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

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

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

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

1379

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

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

1382

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

1384 '''

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

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

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

1388

1389

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

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

1392 '''

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

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

1395

1396

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

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

1399 '''

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

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

1402

1403

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

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

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

1407

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

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

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

1411

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

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

1414

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

1416 '''

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

1418

1419

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

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

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

1423

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

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

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

1427

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

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

1430

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

1432 '''

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

1434

1435

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

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

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

1439

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

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

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

1443

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

1445

1446 @see: Function L{philam2n_xyz}.

1447

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

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

1450 '''

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

1452

1453

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

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

1456 '''

1457 if fabs(b) > n:

1458 b = remainder(b, n2)

1459 if fabs(a) > n_2:

1460 r = remainder(a, n)

1461 if r != a:

1462 a = -r

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

1464 return float0_(a, b)

1465

1466

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

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

1469

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

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

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

1473

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

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

1476

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

1478 '''

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

1480 name=name or normal.__name__)

1481

1482

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

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

1485

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

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

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

1489

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

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

1492

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

1494 '''

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

1496 name=name or normal_.__name__)

1497

1498

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

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

1501 '''

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

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

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

1505

1506

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

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

1509

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

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

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

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

1514

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

1516

1517 @see: Function L{n_xyz2philam}.

1518 '''

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

1520

1521

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

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

1524

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

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

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

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

1529

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

1531

1532 @see: Function L{n_xyz2latlon}.

1533 '''

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

1535

1536

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

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

1539 '''

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

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

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

1543

1544

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

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

1547

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

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

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

1551

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

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

1554

1555 @see: Function L{opposing_}.

1556 '''

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

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

1559

1560

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

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

1563

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

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

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

1567

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

1569 in similar or C{None} if in perpendicular directions.

1570

1571 @see: Function L{opposing}.

1572 '''

1573 m = Radians_(margin=margin, low=EPS0, high=PI_2)

1574 return _opposes(radians2 - radians1, m, PI, PI2)

1575

1576

1577 def philam2n_xyz(phi, lam, name=NN):

1578 '''Convert lat-, longitude to C{n-vector} (I{normal} to the

1579 earth's surface) X, Y and Z components.

1580

1581 @arg phi: Latitude (C{radians}).

1582 @arg lam: Longitude (C{radians}).

1583 @kwarg name: Optional name (C{str}).

1584

1585 @return: A L{Vector3Tuple}C{(x, y, z)}.

1586

1587 @see: Function L{latlon2n_xyz}.

1588

1589 @note: These are C{n-vector} x, y and z components,

1590 I{NOT} geocentric ECEF x, y and z coordinates!

1591 '''

1592 return _2n_xyz(name, *sincos2_(phi, lam))

1593

1594

1595 def _radical2(d, r1, r2): # in .ellipsoidalBaseDI, .sphericalTrigonometry, .vector3d

1596 # (INTERNAL) See C{radical2} below

1597 # assert d > EPS0

1598 r = fsumf_(_1_0, (r1 / d)**2, -(r2 / d)**2) * _0_5

1599 return Radical2Tuple(max(_0_0, min(_1_0, r)), r * d)

1600

1601

1602 def radical2(distance, radius1, radius2):

1603 '''Compute the I{radical ratio} and I{radical line} of two

1604 U{intersecting circles<https://MathWorld.Wolfram.com/

1605 Circle-CircleIntersection.html>}.

1606

1607 The I{radical line} is perpendicular to the axis thru the

1608 centers of the circles at C{(0, 0)} and C{(B{distance}, 0)}.

1609

1610 @arg distance: Distance between the circle centers (C{scalar}).

1611 @arg radius1: Radius of the first circle (C{scalar}).

1612 @arg radius2: Radius of the second circle (C{scalar}).

1613

1614 @return: A L{Radical2Tuple}C{(ratio, xline)} where C{0.0 <=

1615 ratio <= 1.0} and C{xline} is along the B{C{distance}}.

1616

1617 @raise IntersectionError: The B{C{distance}} exceeds the sum

1618 of B{C{radius1}} and B{C{radius2}}.

1619

1620 @raise UnitError: Invalid B{C{distance}}, B{C{radius1}} or

1621 B{C{radius2}}.

1622

1623 @see: U{Circle-Circle Intersection

1624 <https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>}.

1625 '''

1626 d = Distance_(distance, low=_0_0)

1627 r1 = Radius_(radius1=radius1)

1628 r2 = Radius_(radius2=radius2)

1629 if d > (r1 + r2):

1630 raise IntersectionError(distance=d, radius1=r1, radius2=r2,

1631 txt=_too_(_distant_))

1632 return _radical2(d, r1, r2) if d > EPS0 else \

1633 Radical2Tuple(_0_5, _0_0)

1634

1635

1636 class Radical2Tuple(_NamedTuple):

1637 '''2-Tuple C{(ratio, xline)} of the I{radical} C{ratio} and

1638 I{radical} C{xline}, both C{scalar} and C{0.0 <= ratio <= 1.0}

1639 '''

1640 _Names_ = (_ratio_, _xline_)

1641 _Units_ = ( Scalar, Scalar)

1642

1643

1644 def _radistance(inst):

1645 '''(INTERNAL) Helper for the L{frechet._FrecherMeterRadians}

1646 and L{hausdorff._HausdorffMeterRedians} classes.

1647 '''

1648 kwds_ = _xkwds(inst._kwds, wrap=False)

1649 wrap_ = _xkwds_pop(kwds_, wrap=False)

1650 func_ = inst._func_

1651 try: # calling lower-overhead C{func_}

1652 func_(0, _0_25, _0_5, **kwds_)

1653 wrap_ = _Wrap._philamop(wrap_)

1654 except TypeError:

1655 return inst.distance

1656

1657 def _philam(p):

1658 try:

1659 return p.phi, p.lam

1660 except AttributeError: # no .phi or .lam

1661 return radians(p.lat), radians(p.lon)

1662

1663 def _func_wrap(point1, point2):

1664 phi1, lam1 = wrap_(*_philam(point1))

1665 phi2, lam2 = wrap_(*_philam(point2))

1666 return func_(phi2, phi1, lam2 - lam1, **kwds_)

1667

1668 inst._units = inst._units_

1669 return _func_wrap

1670

1671

1672 def _scale_deg(lat1, lat2): # degrees

1673 # scale factor cos(mean of lats) for delta lon

1674 m = fabs(lat1 + lat2) * _0_5

1675 return cos(radians(m)) if m < 90 else _0_0

1676

1677

1678 def _scale_rad(phi1, phi2): # radians, by .frechet, .hausdorff, .heights

1679 # scale factor cos(mean of phis) for delta lam

1680 m = fabs(phi1 + phi2) * _0_5

1681 return cos(m) if m < PI_2 else _0_0

1682

1683

1684 def _sincosa6(phi2, phi1, lam21): # [4] in cosineLaw

1685 '''(INTERNAL) C{sin}es, C{cos}ines and C{acos}ine.

1686 '''

1687 s2, c2, s1, c1, _, c21 = sincos2_(phi2, phi1, lam21)

1688 return s2, c2, s1, c1, acos1(s1 * s2 + c1 * c2 * c21), c21

1689

1690

1691 def thomas(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False):

1692 '''Compute the distance between two (ellipsoidal) points using

1693 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>}

1694 formula.

1695

1696 @arg lat1: Start latitude (C{degrees}).

1697 @arg lon1: Start longitude (C{degrees}).

1698 @arg lat2: End latitude (C{degrees}).

1699 @arg lon2: End longitude (C{degrees}).

1700 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid},

1701 L{Ellipsoid2} or L{a_f2Tuple}) to use.

1702 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

1703 B{C{lat2}} and B{C{lon2}} (C{bool}).

1704

1705 @return: Distance (C{meter}, same units as the B{C{datum}}'s

1706 ellipsoid axes).

1707

1708 @raise TypeError: Invalid B{C{datum}}.

1709

1710 @see: Functions L{thomas_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert},

1711 L{cosineLaw}, L{equirectangular}, L{euclidean}, L{flatLocal}/L{hubeny},

1712 L{flatPolar}, L{haversine}, L{vincentys} and method L{Ellipsoid.distance2}.

1713 '''

1714 return _dE(thomas_, datum, wrap, lat1, lon1, lat2, lon2)

1715

1716

1717 def thomas_(phi2, phi1, lam21, datum=_WGS84):

1718 '''Compute the I{angular} distance between two (ellipsoidal) points using

1719 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>}

1720 formula.

1721

1722 @arg phi2: End latitude (C{radians}).

1723 @arg phi1: Start latitude (C{radians}).

1724 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).

1725 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid},

1726 L{Ellipsoid2} or L{a_f2Tuple}).

1727

1728 @return: Angular distance (C{radians}).

1729

1730 @raise TypeError: Invalid B{C{datum}}.

1731

1732 @see: Functions L{thomas}, L{cosineAndoyerLambert_},

1733 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_},

1734 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_},

1735 L{flatPolar_}, L{haversine_} and L{vincentys_} and U{Geodesy-PHP

1736 <https://GitHub.com/jtejido/geodesy-php/blob/master/src/Geodesy/

1737 Distance/ThomasFormula.php>}.

1738 '''

1739 s2, c2, s1, c1, r, _ = _sincosa6(phi2, phi1, lam21)

1740 if r and isnon0(c1) and isnon0(c2):

1741 E = _ellipsoidal(datum, thomas_)

1742 if E.f:

1743 r1 = atan2(E.b_a * s1, c1)

1744 r2 = atan2(E.b_a * s2, c2)

1745

1746 j = (r2 + r1) * _0_5

1747 k = (r2 - r1) * _0_5

1748 sj, cj, sk, ck, h, _ = sincos2_(j, k, lam21 * _0_5)

1749

1750 h = fsumf_(sk**2, (ck * h)**2, -(sj * h)**2)

1751 u = _1_0 - h

1752 if isnon0(u) and isnon0(h):

1753 r = atan(sqrt0(h / u)) * 2 # == acos(1 - 2 * h)

1754 sr, cr = sincos2(r)

1755 if isnon0(sr):

1756 u = 2 * (sj * ck)**2 / u

1757 h = 2 * (sk * cj)**2 / h

1758 x = u + h

1759 y = u - h

1760

1761 s = r / sr

1762 e = 4 * s**2

1763 d = 2 * cr

1764 a = e * d

1765 b = 2 * r

1766 c = s - (a - d) * _0_5

1767 f = E.f * _0_25

1768

1769 t = fsumf_(a * x, -b * y, c * x**2, -d * y**2, e * x * y)

1770 r -= fsumf_(s * x, -y, -t * f * _0_25) * f * sr

1771 return r

1772

1773

1774 def vincentys(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):

1775 '''Compute the distance between two (spherical) points using

1776 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}

1777 spherical formula.

1778

1779 @arg lat1: Start latitude (C{degrees}).

1780 @arg lon1: Start longitude (C{degrees}).

1781 @arg lat2: End latitude (C{degrees}).

1782 @arg lon2: End longitude (C{degrees}).

1783 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum})

1784 or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or

1785 L{a_f2Tuple}) to use.

1786 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

1787 B{C{lat2}} and B{C{lon2}} (C{bool}).

1788

1789 @return: Distance (C{meter}, same units as B{C{radius}}).

1790

1791 @raise UnitError: Invalid B{C{radius}}.

1792

1793 @see: Functions L{vincentys_}, L{cosineAndoyerLambert},

1794 L{cosineForsytheAndoyerLambert},L{cosineLaw}, L{equirectangular},

1795 L{euclidean}, L{flatLocal}/L{hubeny}, L{flatPolar},

1796 L{haversine} and L{thomas} and methods L{Ellipsoid.distance2},

1797 C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}.

1798

1799 @note: See note at function L{vincentys_}.

1800 '''

1801 return _dS(vincentys_, radius, wrap, lat1, lon1, lat2, lon2)

1802

1803

1804 def vincentys_(phi2, phi1, lam21):

1805 '''Compute the I{angular} distance between two (spherical) points using

1806 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}

1807 spherical formula.

1808

1809 @arg phi2: End latitude (C{radians}).

1810 @arg phi1: Start latitude (C{radians}).

1811 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).

1812

1813 @return: Angular distance (C{radians}).

1814

1815 @see: Functions L{vincentys}, L{cosineAndoyerLambert_},

1816 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_},

1817 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_},

1818 L{flatPolar_}, L{haversine_} and L{thomas_}.

1819

1820 @note: Functions L{vincentys_}, L{haversine_} and L{cosineLaw_}

1821 produce equivalent results, but L{vincentys_} is suitable

1822 for antipodal points and slightly more expensive (M{3 cos,

1823 3 sin, 1 hypot, 1 atan2, 6 mul, 2 add}) than L{haversine_}

1824 (M{2 cos, 2 sin, 2 sqrt, 1 atan2, 5 mul, 1 add}) and

1825 L{cosineLaw_} (M{3 cos, 3 sin, 1 acos, 3 mul, 1 add}).

1826 '''

1827 s1, c1, s2, c2, s21, c21 = sincos2_(phi1, phi2, lam21)

1828

1829 c = c2 * c21

1830 x = s1 * s2 + c1 * c

1831 y = c1 * s2 - s1 * c

1832 return atan2(hypot(c2 * s21, y), x)

1833

1834# **) MIT License

1835#

1836# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.

1837#

1838# Permission is hereby granted, free of charge, to any person obtaining a

1839# copy of this software and associated documentation files (the "Software"),

1840# to deal in the Software without restriction, including without limitation

1841# the rights to use, copy, modify, merge, publish, distribute, sublicense,

1842# and/or sell copies of the Software, and to permit persons to whom the

1843# Software is furnished to do so, subject to the following conditions:

1844#

1845# The above copyright notice and this permission notice shall be included

1846# in all copies or substantial portions of the Software.

1847#

1848# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

1849# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

1850# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

1851# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

1852# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

1853# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

1854# OTHER DEALINGS IN THE SOFTWARE.