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.cartesianBase import CartesianBase # _MODS

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

11 _isfinite, float0_, isnon0, remainder, _umod_PI2, \

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

13 _32_0, _90_0, _180_0, _360_0

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

15 _mean_radius, _spherical_datum, _WGS84, _EWGS84

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

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

18 _TypeError, _ValueError, _xattr, _xError, \

19 _xkwds, _xkwds_pop

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

21 from pygeodesy.fsums import fsumf_

22 from pygeodesy.interns import NN, _delta_, _distant_, _inside_, _SPACE_, _too_

23 from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS

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

25 from pygeodesy.namedTuples import Bearing2Tuple, Distance4Tuple, \

26 Intersection3Tuple, LatLon2Tuple, \

27 PhiLam2Tuple, Vector3Tuple

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

29# from pygeodesy.triaxials import _hartzell2 # _MODS

30 from pygeodesy.units import _isHeight, _isRadius, Bearing, Degrees, Degrees_, \

31 Distance, Distance_, Height, Lam_, Lat, Lon, \

32 Meter_, Phi_, Radians, Radians_, Radius, Radius_, \

33 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.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 asin, atan, atan2, cos, degrees, fabs, radians, sin, sqrt # pow

43

44__all__ = _ALL_LAZY.formy

45__version__ = '23.12.03'

46

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

48_ratio_ = 'ratio'

49_xline_ = 'xline'

50

51

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

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

54 '''

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

56 if r == a:

57 r = -r

58 b += n

59 if fabs(b) > n:

60 b = remainder(b, n2)

61 return float0_(r, b)

62

63

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

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

66 to a given point in C{degrees}.

67

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

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

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

71

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

73

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

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

76 '''

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

78

79

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

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

82 to a given point in C{radians}.

83

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

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

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

87

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

89

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

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

92 '''

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

94

95

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

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

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

99

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

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

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

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

104 @kwarg final_wrap: Optional keyword arguments for function

105 L{pygeodesy.bearing_}.

106

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

108 zero if start and end point coincide.

109 '''

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

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

112 return degrees(r)

113

114

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

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

117 between a (spherical) start and end point.

118

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

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

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

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

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

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

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

126

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

128 and end point coincide.

129

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

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

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

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

134 '''

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

136 if final: # swap plus PI

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

138 r = PI3

139 else:

140 r = PI2

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

142

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

144 y = sdb * ca2

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

146

147

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

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

150 '''

151 try: # for LatLon_ and ellipsoidal LatLon

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

153 except AttributeError:

154 pass

155 # XXX spherical version, OK for ellipsoidal ispolar?

156 t = p1.philam + p2.philam

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

158 degrees(bearing_(*t, 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 U{Andoyer-Lambert

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

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

195

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

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

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

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

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

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

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

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

204

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

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

207

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

209

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

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

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

213 L{Ellipsoid.distance2}.

214 '''

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

216

217

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

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

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

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

222

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

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

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

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

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

228

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

230

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

232

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

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

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

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

237 AndoyerLambert.php>}.

238 '''

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

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

241 E = _ellipsoidal(datum, cosineAndoyerLambert_)

242 if E.f: # ellipsoidal

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

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

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

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

247 if r:

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

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

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

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

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

253 return r

254

255

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

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

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

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

260

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

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

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

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

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

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

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

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

269

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

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

272

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

274

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

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

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

278 L{Ellipsoid.distance2}.

279 '''

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

281

282

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

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

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

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

287 formula.

288

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

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

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

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

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

294

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

296

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

298

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

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

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

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

303 Distance/ForsytheCorrection.php>}.

304 '''

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

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

307 E = _ellipsoidal(datum, cosineForsytheAndoyerLambert_)

308 if E.f: # ellipsoidal

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

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

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

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

313 x = s + t

314 y = s - t

315

316 s = 8 * r**2 / sr

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

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

319 b = -2 * d

320 e = 30 * s2r

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

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

323

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

325 return r

326

327

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

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

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

331

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

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

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

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

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

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

338 L{a_f2Tuple}) to use.

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

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

341

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

343 ellipsoid or datum axes).

344

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

346

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

348 L{cosineForsytheAndoyerLambert}, L{equirectangular}, L{euclidean},

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

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

351

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

353 '''

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

355

356

357 def cosineLaw_(phi2, phi1, lam21):

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

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

360

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

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

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

364

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

366

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

368 L{cosineForsytheAndoyerLambert_}, L{equirectangular_},

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

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

371

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

373 '''

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

375

376

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

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

379 '''

380 if wrap:

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

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

383 else: # for backward compaibility

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

385

386

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

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

389 '''

390 E = _ellipsoidal(earth, func_)

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

392 return r * E.a

393

394

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

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

397 '''

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

399 if radius is not R_M:

400 _, lat1, _, lat2, _ = wrap_lls

401 radius = _mean_radius(radius, lat1, lat2)

402 return r * radius

403

404

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

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

407 '''

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

409 if radius:

410 _, lat1, _, lat2, _ = wrap_lls

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

412 return r

413

414

415 def _ellipsoidal(earth, where):

416 '''(INTERNAL) Helper for distances.

417 '''

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

419 earth if isinstance(earth, Ellipsoid) else

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

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

422

423

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

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

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

427

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

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

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

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

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

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

434 L{a_f2Tuple}).

435 @kwarg adjust_limit_wrap: Optional keyword arguments for

436 function L{equirectangular_}.

437

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

439 the ellipsoid or datum axes).

440

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

442

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

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

445 approximate or accurate distance functions.

446 '''

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

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

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

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

451

452

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

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

455 and L{hausdorff._HausdorffMeterRedians} classes.

456 '''

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

458

459

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

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

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

463

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

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

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 adjust: Adjust the wrapped, unrolled longitudinal delta

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

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

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

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

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

477

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

479 unroll_lon2)} in C{degrees squared}.

480

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

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

483 set to C{True}.

484

485 @see: U{Local, flat earth approximation

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

487 L{equirectangular}, L{cosineAndoyerLambert},

488 L{cosineForsytheAndoyerLambert}, L{cosineLaw}, L{euclidean},

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

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

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

492 '''

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

494 d_lat = lat2 - lat1

495

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

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

498 if d > limit:

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

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

501 limit=limit, wrap=wrap)

502 raise LimitError(s, txt=t)

503

504 if adjust: # scale delta lon

505 d_lon *= _scale_deg(lat1, lat2)

506

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

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

509

510

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

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

513

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

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

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

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

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

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

520 L{a_f2Tuple}) to use.

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

522 the mean latitude (C{bool}).

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

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

525

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

527 ellipsoid or datum axes).

528

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

530

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

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

533 L{euclidean_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert},

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

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

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

537 '''

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

539

540

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

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

543

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

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

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

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

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

549

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

551

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

553 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{equirectangular_},

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

555 and L{vincentys_}.

556 '''

557 if adjust:

558 lam21 *= _scale_rad(phi2, phi1)

559 return euclid(phi2 - phi1, lam21)

560

561

562 def excessAbc_(A, b, c):

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

564 and the included (small) angle.

565

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

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

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

569

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

571

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

573

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

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

576 '''

577 A = Radians_(A=A)

578 b = Radians_(b=b) * _0_5

579 c = Radians_(c=c) * _0_5

580

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

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

583

584

585 def excessCagnoli_(a, b, c):

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

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

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{excessLHuilier_} and U{Spherical trigonometry

598 <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 _s = sin

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

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

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

609 return Radians(Cagnoli=r * _2_0)

610

611

612 def excessGirard_(A, B, C):

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

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

615

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

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

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

619

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

621

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

623

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

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

626 '''

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

628 Radians_(B=B),

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

630

631

632 def excessLHuilier_(a, b, c):

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

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

635

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

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

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

639

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

641

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

643

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

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

646 '''

647 a = Radians_(a=a)

648 b = Radians_(b=b)

649 c = Radians_(c=c)

650

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

652 _t = tan_2

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

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

655 return Radians(LHuilier=r * _4_0)

656

657

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

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

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

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

662 method.

663

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

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

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

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

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

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

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

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

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

673

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

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

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

677

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

679

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

681

682 @raise ValueError: Semi-circular longitudinal delta.

683

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

685 '''

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

687

688

689 def excessKarney_(phi2, phi1, lam21):

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

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

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

693 method.

694

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

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

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

698

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

700

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

702

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

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

705 '''

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

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

708 #

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

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

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

712 #

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

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

715 _t = tan_2

716 t2 = _t(phi2)

717 t1 = _t(phi1)

718 t = _t(lam21, lam21=None)

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

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

721

722

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

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

725#

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

727#

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

729#

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

731#

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

733#

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

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

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

737#

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

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

740#

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

742#

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

744# inverse Gudermannian) function

745#

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

747#

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

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

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

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

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

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

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

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

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

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

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

759# - Charles Karney

760

761

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

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

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

765

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

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

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

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

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

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

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

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

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

775

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

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

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

779

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

781

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

783

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

785 '''

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

787

788

789 def excessQuad_(phi2, phi1, lam21):

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

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

792

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

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

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

796

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

798

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

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

801 '''

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

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

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

805

806

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

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

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

810 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}

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

812

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

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

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

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

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

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

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

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

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

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

823

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

825 ellipsoid axes).

826

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

828

829 @note: The meridional and prime_vertical radii of curvature

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

831

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

833 L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert},

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

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

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

837 '''

838 E = _ellipsoidal(datum, flatLocal)

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

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

841

842 hubeny = flatLocal # PYCHOK for Karl Hubeny

843

844

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

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

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

848 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}

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

850

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

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

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

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

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

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

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

858

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

860

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

862

863 @note: The meridional and prime_vertical radii of curvature

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

865

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

867 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_}, L{flatPolar_},

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

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

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

871 '''

872 E = _ellipsoidal(datum, flatLocal_)

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

874

875 hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny

876

877

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

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

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

881 Geographical_distance#Polar_coordinate_flat-Earth_formula>}

882 formula.

883

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

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

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

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

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

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

890 L{a_f2Tuple}) to use.

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

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

893

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

895 ellipsoid or datum axes).

896

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

898

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

900 L{cosineForsytheAndoyerLambert},L{cosineLaw},

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

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

903 L{vincentys}.

904 '''

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

906

907

908 def flatPolar_(phi2, phi1, lam21):

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

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

911 Geographical_distance#Polar_coordinate_flat-Earth_formula>}

912 formula.

913

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

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

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

917

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

919

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

921 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_},

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

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

924 '''

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

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

927 if a < b:

928 a, b = b, a

929 if a < EPS0:

930 a = _0_0

931 elif b > 0:

932 b = b / a # /= chokes PyChecker

933 c = b * cos(lam21) * _2_0

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

935 a *= sqrt0(c)

936 return a

937

938

939 def _hartzell(inst, los, earth, **kwds):

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

941 '''

942 if earth is not None:

943 earth = _spherical_datum(earth, name=hartzell.__name__)

944 inst = inst.toDatum(earth)

945 h = inst.height

946 if h > 0: # EPS0

947 r = hartzell(inst, los=los, earth=earth or inst.datum, **kwds)

948 elif h < 0: # EPS0

949 raise IntersectionError(pov=inst, los=los, height=h, txt=_inside_)

950 else:

951 r = inst

952 return r

953

954

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

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

957 from a Point-Of-View in space.

958

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

960 C{LatLon} or L{Vector3d}).

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

962 or C{None} to point to the earth' center.

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

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

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

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

967 point plus C{LatLon} keyword arguments, include

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

969

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

971 B{C{pov}} or the given C{B{LatLon}_and_kwds}) with C{.heigth}

972 set to the distance to the B{C{pov}}.

973

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

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

976 the earth or points in an opposite direction.

977

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

979

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

981 methods L{Ellipsoid.hartzell4}, C{Cartesian.hartzell}, C{LatLon.hartzell}

982 and U{I{Satellite Line-of-Sight Intersection with Earth}<https://

983 StephenHartzell.Medium.com/satellite-line-of-sight-intersection-with-earth-d786b4a6a9b6>}.

984 '''

985 n = hartzell.__name__

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

987 _spherical_datum(earth, name=n)

988 try:

989 r, h = _MODS.triaxials._hartzell2(pov, los, D.ellipsoid._triaxial)

990 except Exception as x:

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

992

993 r = _xnamed(r, name or n)

994 C = _MODS.cartesianBase.CartesianBase

995 if LatLon_and_kwds:

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

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

998 elif isinstance(r, C):

999 r.height = h

1000 return r

1001

1002

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

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

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

1006 formula.

1007

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

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

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

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

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

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

1014 L{a_f2Tuple}) to use.

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

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

1017

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

1019

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

1021

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

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

1024 L{cosineLaw}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert},

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

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

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

1028

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

1030 '''

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

1032

1033

1034 def haversine_(phi2, phi1, lam21):

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

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

1037 formula.

1038

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

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

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

1042

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

1044

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

1046 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_},

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

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

1049

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

1051 '''

1052 def _hsin(rad):

1053 return sin(rad * _0_5)**2

1054

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

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

1057

1058

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

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

1061 traveling along a straight line at a given tilt.

1062

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

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

1065 B{C{radius}}).

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

1067

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

1069

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

1071

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

1073 (U{Shapiro et al. 2009, JTECH

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

1075 and U{Potvin et al. 2012, JTECH

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

1077 '''

1078 r = h = Radius(radius)

1079 d = fabs(Distance(distance))

1080 if d > h:

1081 d, h = h, d

1082

1083 if d > EPS0: # and h > EPS0

1084 d = d / h # /= h chokes PyChecker

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

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

1087 if s > 0:

1088 return h * sqrt(s) - r

1089

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

1091

1092

1093 def heightOrthometric(h_ll, N):

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

1095

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

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

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

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

1100

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

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

1103

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

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

1106 6 and module L{pygeodesy.geoids}.

1107 '''

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

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

1110

1111

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

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

1114 above the (spherical) earth.

1115

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

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

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

1119

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

1121

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

1123

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

1125 '''

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

1127 if min(h, r) < 0:

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

1129

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

1131 fsumf_(r, r, h)) * h

1132 return sqrt0(d2)

1133

1134

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

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

1137 '''

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

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

1140 try:

1141 if wrap:

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

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

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

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

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

1147 d, m = None, _MODS.vector3d

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

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

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

1151 m = _MODS.sphericalNvector

1152 _i = m.intersection

1153 else:

1154 d = _spherical_datum(datum, name=n)

1155 if d.isSpherical:

1156 m = _MODS.sphericalTrigonometry

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

1158 elif d.isEllipsoidal:

1159 try:

1160 if d.ellipsoid.geodesic:

1161 pass

1162 m = _MODS.ellipsoidalKarney

1163 except ImportError:

1164 m = _MODS.ellipsoidalExact

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

1166 else:

1167 raise _TypeError(datum=datum)

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

1169

1170 except (TypeError, ValueError) as x:

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

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

1173

1174_idllmn6 = _idllmn6() # PYCHOK singleton

1175

1176

1177 def intersection2(lat1, lon1, bearing1,

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

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

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

1181

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

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

1184

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

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

1187 in C{meter} or ...

1188

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

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

1191

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

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

1194 is installed, otherwise ...

1195

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

1197

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

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

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

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

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

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

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

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

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

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

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

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

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

1211

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

1213 longitude of the intersection point.

1214

1215 @raise IntersectionError: Ambiguous or infinite intersection

1216 or colinear, parallel or otherwise

1217 non-intersecting lines.

1218

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

1220

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

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

1223

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

1225

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

1227 '''

1228 b1 = Bearing(bearing1=bearing1)

1229 b2 = Bearing(bearing2=bearing2)

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

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

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

1233 if d is None:

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

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

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

1237

1238 else:

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

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

1241 if isinstance(t, Intersection3Tuple): # ellipsoidal

1242 t, _, _ = t

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

1244 return t

1245

1246

1247 def intersections2(lat1, lon1, radius1,

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

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

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

1251

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

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

1254

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

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

1257 in C{meter} or ...

1258

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

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

1261 is installed, otherwise ...

1262

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

1264

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

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

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

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

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

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

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

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

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

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

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

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

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

1278

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

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

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

1282

1283 @raise IntersectionError: Concentric, antipodal, invalid or

1284 non-intersecting circles or no

1285 convergence.

1286

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

1288

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

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

1291 '''

1292 r1 = Radius_(radius1=radius1)

1293 r2 = Radius_(radius2=radius2)

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

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

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

1297 if d is None:

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

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

1300

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

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

1303

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

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

1306 Vector=_V2T)

1307 else:

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

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

1310

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

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

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

1314 return t

1315

1316

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

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

1319 opposite sides of the earth.

1320

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

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

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

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

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

1326

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

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

1329

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

1331 '''

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

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

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

1335

1336

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

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

1339 opposite sides of the earth.

1340

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

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

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

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

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

1346

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

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

1349

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

1351 '''

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

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

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

1355

1356

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

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

1359 '''

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

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

1362

1363

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

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

1366 '''

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

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

1369

1370

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

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

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

1374

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

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

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

1378

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

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

1381

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

1383 '''

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

1385

1386

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

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

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

1390

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

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

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

1394

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

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

1397

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

1399 '''

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

1401

1402

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

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

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

1406

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

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

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

1410

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

1412

1413 @see: Function L{philam2n_xyz}.

1414

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

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

1417 '''

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

1419

1420

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

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

1423 '''

1424 if fabs(b) > n:

1425 b = remainder(b, n2)

1426 if fabs(a) > n_2:

1427 r = remainder(a, n)

1428 if r != a:

1429 a = -r

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

1431 return float0_(a, b)

1432

1433

1434 def normal(lat, lon, name=NN):

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

1436

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

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

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

1440

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

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

1443

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

1445 '''

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

1447 name=name or normal.__name__)

1448

1449

1450 def normal_(phi, lam, name=NN):

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

1452

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

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

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

1456

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

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

1459

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

1461 '''

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

1463 name=name or normal_.__name__)

1464

1465

1466 def _2n_xyz(name, sa, ca, sb, cb):

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

1468 '''

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

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

1471 return Vector3Tuple(ca * cb, ca * sb, sa, name=name)

1472

1473

1474 def n_xyz2latlon(x, y, z, name=NN):

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

1476

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

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

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

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

1481

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

1483

1484 @see: Function L{n_xyz2philam}.

1485 '''

1486 return LatLon2Tuple(atan2d(z, hypot(x, y)), atan2d(y, x), name=name)

1487

1488

1489 def n_xyz2philam(x, y, z, name=NN):

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

1491

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

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

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

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

1496

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

1498

1499 @see: Function L{n_xyz2latlon}.

1500 '''

1501 return PhiLam2Tuple(atan2(z, hypot(x, y)), atan2(y, x), name=name)

1502

1503

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

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

1506 '''

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

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

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

1510

1511

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

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

1514

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

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

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

1518

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

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

1521

1522 @see: Function L{opposing_}.

1523 '''

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

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

1526

1527

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

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

1530

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

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

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

1534

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

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

1537

1538 @see: Function L{opposing}.

1539 '''

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

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

1542

1543

1544 def philam2n_xyz(phi, lam, name=NN):

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

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

1547

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

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

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

1551

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

1553

1554 @see: Function L{latlon2n_xyz}.

1555

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

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

1558 '''

1559 return _2n_xyz(name, *sincos2_(phi, lam))

1560

1561

1562 def _radical2(d, r1, r2): # in .ellipsoidalBaseDI, .sphericalTrigonometry, .vector3d

1563 # (INTERNAL) See C{radical2} below

1564 # assert d > EPS0

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

1566 return Radical2Tuple(max(_0_0, min(_1_0, r)), r * d)

1567

1568

1569 def radical2(distance, radius1, radius2):

1570 '''Compute the I{radical ratio} and I{radical line} of two

1571 U{intersecting circles<https://MathWorld.Wolfram.com/

1572 Circle-CircleIntersection.html>}.

1573

1574 The I{radical line} is perpendicular to the axis thru the

1575 centers of the circles at C{(0, 0)} and C{(B{distance}, 0)}.

1576

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

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

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

1580

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

1582 ratio <= 1.0} and C{xline} is along the B{C{distance}}.

1583

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

1585 of B{C{radius1}} and B{C{radius2}}.

1586

1587 @raise UnitError: Invalid B{C{distance}}, B{C{radius1}} or

1588 B{C{radius2}}.

1589

1590 @see: U{Circle-Circle Intersection

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

1592 '''

1593 d = Distance_(distance, low=_0_0)

1594 r1 = Radius_(radius1=radius1)

1595 r2 = Radius_(radius2=radius2)

1596 if d > (r1 + r2):

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

1598 txt=_too_(_distant_))

1599 return _radical2(d, r1, r2) if d > EPS0 else \

1600 Radical2Tuple(_0_5, _0_0)

1601

1602

1603 class Radical2Tuple(_NamedTuple):

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

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

1606 '''

1607 _Names_ = (_ratio_, _xline_)

1608 _Units_ = ( Scalar, Scalar)

1609

1610

1611 def _radistance(inst):

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

1613 and L{hausdorff._HausdorffMeterRedians} classes.

1614 '''

1615 kwds_ = _xkwds(inst._kwds, wrap=False)

1616 wrap_ = _xkwds_pop(kwds_, wrap=False)

1617 func_ = inst._func_

1618 try: # calling lower-overhead C{func_}

1619 func_(0, _0_25, _0_5, **kwds_)

1620 wrap_ = _Wrap._philamop(wrap_)

1621 except TypeError:

1622 return inst.distance

1623

1624 def _philam(p):

1625 try:

1626 return p.phi, p.lam

1627 except AttributeError: # no .phi or .lam

1628 return radians(p.lat), radians(p.lon)

1629

1630 def _func_wrap(point1, point2):

1631 phi1, lam1 = wrap_(*_philam(point1))

1632 phi2, lam2 = wrap_(*_philam(point2))

1633 return func_(phi2, phi1, lam2 - lam1, **kwds_)

1634

1635 inst._units = inst._units_

1636 return _func_wrap

1637

1638

1639 def rtp2xyz(r, theta, phi):

1640 '''Convert spherical C{(r, theta, phi)} to cartesian C{(x, y, z)} coordinates.

1641

1642 @arg r: Radial distance (C{scalar}, conventially C{meter}).

1643 @arg theta: Inclination (C{degrees} with respect to the positive z-axis).

1644 @arg phi: Azimuthal angle (C{degrees}).

1645

1646 @return: L{Vector3Tuple}C{(x, y, z)} in C{meter}, same units as C{r}.

1647

1648 @see: Functions L{rtp2xyz_} and L{xyz2rtp}.

1649 '''

1650 return rtp2xyz_(r, radians(theta), radians(phi))

1651

1652

1653 def rtp2xyz_(r, theta, phi):

1654 '''Convert spherical C{(r, theta, phi)} to cartesian C{(x, y, z)} coordinates.

1655

1656 @arg r: Radial distance (C{scalar}, conventially C{meter}).

1657 @arg theta: Inclination (C{radians} with respect to the positive z-axis).

1658 @arg phi: Azimuthal angle (C{radians}).

1659

1660 @return: L{Vector3Tuple}C{(x, y, z)} in C{meter}, same units as C{r}.

1661

1662 @see: U{Physics<https://WikiPedia.org/wiki/Spherical_coordinate_system>}

1663 convention (ISO 80000-2:2019).

1664 '''

1665 if r and _isfinite(r):

1666 s, z, y, x = sincos2_(theta, phi)

1667 s *= r

1668 x *= s

1669 y *= s

1670 z *= r

1671 else:

1672 x = y = z = r

1673 return Vector3Tuple(x, y, z)

1674

1675

1676 def _scale_deg(lat1, lat2): # degrees

1677 # scale factor cos(mean of lats) for delta lon

1678 m = fabs(lat1 + lat2) * _0_5

1679 return cos(radians(m)) if m < 90 else _0_0

1680

1681

1682 def _scale_rad(phi1, phi2): # radians, by .frechet, .hausdorff, .heights

1683 # scale factor cos(mean of phis) for delta lam

1684 m = fabs(phi1 + phi2) * _0_5

1685 return cos(m) if m < PI_2 else _0_0

1686

1687

1688 def _sincosa6(phi2, phi1, lam21): # [4] in cosineLaw

1689 '''(INTERNAL) C{sin}es, C{cos}ines and C{acos}ine.

1690 '''

1691 s2, c2, s1, c1, _, c21 = sincos2_(phi2, phi1, lam21)

1692 return s2, c2, s1, c1, acos1(s1 * s2 + c1 * c2 * c21), c21

1693

1694

1695 def thomas(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False):

1696 '''Compute the distance between two (ellipsoidal) points using

1697 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>}

1698 formula.

1699

1700 @arg lat1: Start latitude (C{degrees}).

1701 @arg lon1: Start longitude (C{degrees}).

1702 @arg lat2: End latitude (C{degrees}).

1703 @arg lon2: End longitude (C{degrees}).

1704 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid},

1705 L{Ellipsoid2} or L{a_f2Tuple}) to use.

1706 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

1707 B{C{lat2}} and B{C{lon2}} (C{bool}).

1708

1709 @return: Distance (C{meter}, same units as the B{C{datum}}'s

1710 ellipsoid axes).

1711

1712 @raise TypeError: Invalid B{C{datum}}.

1713

1714 @see: Functions L{thomas_}, L{cosineAndoyerLambert}, L{cosineForsytheAndoyerLambert},

1715 L{cosineLaw}, L{equirectangular}, L{euclidean}, L{flatLocal}/L{hubeny},

1716 L{flatPolar}, L{haversine}, L{vincentys} and method L{Ellipsoid.distance2}.

1717 '''

1718 return _dE(thomas_, datum, wrap, lat1, lon1, lat2, lon2)

1719

1720

1721 def thomas_(phi2, phi1, lam21, datum=_WGS84):

1722 '''Compute the I{angular} distance between two (ellipsoidal) points using

1723 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>}

1724 formula.

1725

1726 @arg phi2: End latitude (C{radians}).

1727 @arg phi1: Start latitude (C{radians}).

1728 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).

1729 @kwarg datum: Datum or ellipsoid to use (L{Datum}, L{Ellipsoid},

1730 L{Ellipsoid2} or L{a_f2Tuple}).

1731

1732 @return: Angular distance (C{radians}).

1733

1734 @raise TypeError: Invalid B{C{datum}}.

1735

1736 @see: Functions L{thomas}, L{cosineAndoyerLambert_},

1737 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_},

1738 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_},

1739 L{flatPolar_}, L{haversine_} and L{vincentys_} and U{Geodesy-PHP

1740 <https://GitHub.com/jtejido/geodesy-php/blob/master/src/Geodesy/

1741 Distance/ThomasFormula.php>}.

1742 '''

1743 s2, c2, s1, c1, r, _ = _sincosa6(phi2, phi1, lam21)

1744 if r and isnon0(c1) and isnon0(c2):

1745 E = _ellipsoidal(datum, thomas_)

1746 if E.f:

1747 r1 = atan2(E.b_a * s1, c1)

1748 r2 = atan2(E.b_a * s2, c2)

1749

1750 j = (r2 + r1) * _0_5

1751 k = (r2 - r1) * _0_5

1752 sj, cj, sk, ck, h, _ = sincos2_(j, k, lam21 * _0_5)

1753

1754 h = fsumf_(sk**2, (ck * h)**2, -(sj * h)**2)

1755 u = _1_0 - h

1756 if isnon0(u) and isnon0(h):

1757 r = atan(sqrt0(h / u)) * 2 # == acos(1 - 2 * h)

1758 sr, cr = sincos2(r)

1759 if isnon0(sr):

1760 u = 2 * (sj * ck)**2 / u

1761 h = 2 * (sk * cj)**2 / h

1762 x = u + h

1763 y = u - h

1764

1765 s = r / sr

1766 e = 4 * s**2

1767 d = 2 * cr

1768 a = e * d

1769 b = 2 * r

1770 c = s - (a - d) * _0_5

1771 f = E.f * _0_25

1772

1773 t = fsumf_(a * x, -b * y, c * x**2, -d * y**2, e * x * y)

1774 r -= fsumf_(s * x, -y, -t * f * _0_25) * f * sr

1775 return r

1776

1777

1778 def vincentys(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):

1779 '''Compute the distance between two (spherical) points using

1780 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}

1781 spherical formula.

1782

1783 @arg lat1: Start latitude (C{degrees}).

1784 @arg lon1: Start longitude (C{degrees}).

1785 @arg lat2: End latitude (C{degrees}).

1786 @arg lon2: End longitude (C{degrees}).

1787 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum})

1788 or ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or

1789 L{a_f2Tuple}) to use.

1790 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll

1791 B{C{lat2}} and B{C{lon2}} (C{bool}).

1792

1793 @return: Distance (C{meter}, same units as B{C{radius}}).

1794

1795 @raise UnitError: Invalid B{C{radius}}.

1796

1797 @see: Functions L{vincentys_}, L{cosineAndoyerLambert},

1798 L{cosineForsytheAndoyerLambert},L{cosineLaw}, L{equirectangular},

1799 L{euclidean}, L{flatLocal}/L{hubeny}, L{flatPolar},

1800 L{haversine} and L{thomas} and methods L{Ellipsoid.distance2},

1801 C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}.

1802

1803 @note: See note at function L{vincentys_}.

1804 '''

1805 return _dS(vincentys_, radius, wrap, lat1, lon1, lat2, lon2)

1806

1807

1808 def vincentys_(phi2, phi1, lam21):

1809 '''Compute the I{angular} distance between two (spherical) points using

1810 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}

1811 spherical formula.

1812

1813 @arg phi2: End latitude (C{radians}).

1814 @arg phi1: Start latitude (C{radians}).

1815 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).

1816

1817 @return: Angular distance (C{radians}).

1818

1819 @see: Functions L{vincentys}, L{cosineAndoyerLambert_},

1820 L{cosineForsytheAndoyerLambert_}, L{cosineLaw_},

1821 L{equirectangular_}, L{euclidean_}, L{flatLocal_}/L{hubeny_},

1822 L{flatPolar_}, L{haversine_} and L{thomas_}.

1823

1824 @note: Functions L{vincentys_}, L{haversine_} and L{cosineLaw_}

1825 produce equivalent results, but L{vincentys_} is suitable

1826 for antipodal points and slightly more expensive (M{3 cos,

1827 3 sin, 1 hypot, 1 atan2, 6 mul, 2 add}) than L{haversine_}

1828 (M{2 cos, 2 sin, 2 sqrt, 1 atan2, 5 mul, 1 add}) and

1829 L{cosineLaw_} (M{3 cos, 3 sin, 1 acos, 3 mul, 1 add}).

1830 '''

1831 s1, c1, s2, c2, s21, c21 = sincos2_(phi1, phi2, lam21)

1832

1833 c = c2 * c21

1834 x = s1 * s2 + c1 * c

1835 y = c1 * s2 - s1 * c

1836 return atan2(hypot(c2 * s21, y), x)

1837

1838

1839 def xyz2rtp(x_xyz, *y_z):

1840 '''Convert cartesian C{(x, y, z)} to spherical C{(r, theta, phi)} coordinates.

1841

1842 @return: 3-Tuple C{(r, theta, phi)} in C{degrees}.

1843

1844 @see: Function L{xyz2rtp_}.

1845 '''

1846 r, t, p = xyz2rtp_(x_xyz, *y_z)

1847 return r, Degrees(theta=degrees(t)), Degrees(phi=degrees(p))

1848

1849

1850 def xyz2rtp_(x_xyz, *y_z):

1851 '''Convert cartesian C{(x, y, z)} to spherical C{(r, theta, phi)} coordinates.

1852

1853 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian}, L{Ecef9Tuple},

1854 C{Nvector}, L{Vector3d}, L{Vector3Tuple}, L{Vector4Tuple} or a

1855 C{tuple} or C{list} of 3+ C{scalar} items) if no C{y_z} specified.

1856 @arg y_z: Y and Z component (C{scalar}s), ignored if C{x_xyz} is not a C{scalar}.

1857

1858 @return: 3-Tuple C{(r, theta, phi)} with radial distance C{r} (C{meter}, same

1859 units as C{x}, C{y} and C{z}), inclination C{theta} (with respect to

1860 the positive z-axis) and azimuthal angle C{phi} in C{radians}.

1861

1862 @see: U{Physics<https://WikiPedia.org/wiki/Spherical_coordinate_system>}

1863 convention (ISO 80000-2:2019).

1864 '''

1865 x, y, z = _MODS.vector3dBase._xyz3(xyz2rtp, x_xyz, *y_z)

1866 r = hypot_(x, y, z)

1867 if r:

1868 t = acos1(z / r)

1869 p = atan2(y, x)

1870 if p < 0:

1871 p += PI2

1872 else:

1873 t = p = _0_0

1874 return r, Radians(theta=t), Radians(phi=p)

1875

1876# **) MIT License

1877#

1878# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.

1879#

1880# Permission is hereby granted, free of charge, to any person obtaining a

1881# copy of this software and associated documentation files (the "Software"),

1882# to deal in the Software without restriction, including without limitation

1883# the rights to use, copy, modify, merge, publish, distribute, sublicense,

1884# and/or sell copies of the Software, and to permit persons to whom the

1885# Software is furnished to do so, subject to the following conditions:

1886#

1887# The above copyright notice and this permission notice shall be included

1888# in all copies or substantial portions of the Software.

1889#

1890# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

1891# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

1892# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

1893# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

1894# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

1895# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

1896# OTHER DEALINGS IN THE SOFTWARE.