Coverage for pygeodesy/formy.py: 98%
447 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-06 12:20 -0500
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-06 12:20 -0500
2# -*- coding: utf-8 -*-
4u'''Formulary of basic geodesy functions and approximations.
5'''
6# make sure int/int division yields float quotient, see .basics
7from __future__ import division as _; del _ # PYCHOK semicolon
9# from pygeodesy.basics import _args_kwds_count2, _copysign # from .constants
10# from pygeodesy.cartesianBase import CartesianBase # _MODS
11from pygeodesy.constants import EPS, EPS0, EPS1, PI, PI2, PI3, PI_2, R_M, \
12 _0_0s, float0_, isnon0, remainder, _umod_PI2, \
13 _0_0, _0_125, _0_25, _0_5, _1_0, _2_0, _4_0, \
14 _90_0, _180_0, _360_0, _copysign
15from pygeodesy.datums import Datum, Ellipsoid, _ellipsoidal_datum, \
16 _mean_radius, _spherical_datum, _WGS84, _EWGS84
17# from pygeodesy.ellipsoids import Ellipsoid, _EWGS84 # from .datums
18from pygeodesy.errors import IntersectionError, LimitError, limiterrors, \
19 _TypeError, _ValueError, _xattr, _xError, \
20 _xcallable,_xkwds, _xkwds_pop2
21from pygeodesy.fmath import euclid, fdot_, fprod, hypot, hypot2, sqrt0
22from pygeodesy.fsums import fsumf_, Fmt, unstr
23# from pygeodesy.internals import _DUNDER_nameof # from .named
24from pygeodesy.interns import _delta_, _distant_, _inside_, _SPACE_, _too_
25from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS
26from pygeodesy.named import _name__, _name2__, _NamedTuple, _xnamed, \
27 _DUNDER_nameof
28from pygeodesy.namedTuples import Bearing2Tuple, Distance4Tuple, LatLon2Tuple, \
29 Intersection3Tuple, PhiLam2Tuple
30# from pygeodesy.streprs import Fmt, unstr # from .fsums
31# from pygeodesy.triaxials import _hartzell3 # _MODS
32from pygeodesy.units import _isDegrees, _isHeight, _isRadius, Bearing, Degrees_, \
33 Distance, Distance_, Height, Lamd, Lat, Lon, Meter_, \
34 Phid, Radians, Radians_, Radius, Radius_, Scalar, _100km
35from pygeodesy.utily import acos1, asin1, atan2, atan2b, degrees2m, hav, _loneg, \
36 m2degrees, tan_2, sincos2, sincos2_, _Wrap
37# from pygeodesy.vector3d import _otherV3d # _MODS
38# from pygeodesy.vector3dBase import _xyz_y_z3 # _MODS
39# from pygeodesy import ellipsoidalExact, ellipsoidalKarney, vector3d, \
40# sphericalNvector, sphericalTrigonometry # _MODS
42from contextlib import contextmanager
43from math import atan, cos, degrees, fabs, radians, sin, sqrt # pow
45__all__ = _ALL_LAZY.formy
46__version__ = '25.01.05'
48_RADIANS2 = radians(_1_0)**2 # degree to radians-squared
49_ratio_ = 'ratio'
50_xline_ = 'xline'
53def angle2chord(rad, radius=R_M):
54 '''Get the chord length of a (central) angle or I{angular} distance.
56 @arg rad: Central angle (C{radians}).
57 @kwarg radius: Mean earth radius (C{meter}, conventionally), datum (L{Datum}) or ellipsoid
58 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use or C{None}.
60 @return: Chord length (C{meter}, same units as B{C{radius}} or if C{B{radius} is None}, C{radians}).
62 @see: Function L{chord2angle}, method L{intermediateChordTo<sphericalNvector.LatLon.intermediateChordTo>} and
63 U{great-circle-distance<https://WikiPedia.org/wiki/Great-circle_distance#Relation_between_central_angle_and_chord_length>}.
64 '''
65 d = _isDegrees(rad, iscalar=False)
66 r = sin((radians(rad) if d else rad) / _2_0) * _2_0
67 return (degrees(r) if d else r) if radius is None else (_mean_radius(radius) * r)
70def _anti2(a, b, n_2, n, n2):
71 '''(INTERNAL) Helper for C{antipode} and C{antipode_}.
72 '''
73 r = remainder(a, n) if fabs(a) > n_2 else a
74 if r == a:
75 r = -r
76 b += n
77 if fabs(b) > n:
78 b = remainder(b, n2)
79 return float0_(r, b)
82def antipode(lat, lon, **name):
83 '''Return the antipode, the point diametrically opposite to a given
84 point in C{degrees}.
86 @arg lat: Latitude (C{degrees}).
87 @arg lon: Longitude (C{degrees}).
88 @kwarg name: Optional C{B{name}=NN} (C{str}).
90 @return: A L{LatLon2Tuple}C{(lat, lon)}.
92 @see: Functions L{antipode_} and L{normal} and U{Geosphere
93 <https://CRAN.R-Project.org/web/packages/geosphere/geosphere.pdf>}.
94 '''
95 return LatLon2Tuple(*_anti2(lat, lon, _90_0, _180_0, _360_0), **name)
98def antipode_(phi, lam, **name):
99 '''Return the antipode, the point diametrically opposite to a given
100 point in C{radians}.
102 @arg phi: Latitude (C{radians}).
103 @arg lam: Longitude (C{radians}).
104 @kwarg name: Optional C{B{name}=NN} (C{str}).
106 @return: A L{PhiLam2Tuple}C{(phi, lam)}.
108 @see: Functions L{antipode} and L{normal_} and U{Geosphere
109 <https://CRAN.R-Project.org/web/packages/geosphere/geosphere.pdf>}.
110 '''
111 return PhiLam2Tuple(*_anti2(phi, lam, PI_2, PI, PI2), **name)
114def bearing(lat1, lon1, lat2, lon2, **final_wrap):
115 '''Compute the initial or final bearing (forward or reverse azimuth) between two
116 (spherical) points.
118 @arg lat1: Start latitude (C{degrees}).
119 @arg lon1: Start longitude (C{degrees}).
120 @arg lat2: End latitude (C{degrees}).
121 @arg lon2: End longitude (C{degrees}).
122 @kwarg final_wrap: Optional keyword arguments for function L{pygeodesy.bearing_}.
124 @return: Initial or final bearing (compass C{degrees360}) or zero if both points
125 coincide.
126 '''
127 r = bearing_(Phid(lat1=lat1), Lamd(lon1=lon1),
128 Phid(lat2=lat2), Lamd(lon2=lon2), **final_wrap)
129 return degrees(r)
132def bearing_(phi1, lam1, phi2, lam2, final=False, wrap=False):
133 '''Compute the initial or final bearing (forward or reverse azimuth) between two
134 (spherical) points.
136 @arg phi1: Start latitude (C{radians}).
137 @arg lam1: Start longitude (C{radians}).
138 @arg phi2: End latitude (C{radians}).
139 @arg lam2: End longitude (C{radians}).
140 @kwarg final: If C{True}, return the final, otherwise the initial bearing (C{bool}).
141 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{phi2}} and B{C{lam2}}
142 (C{bool}).
144 @return: Initial or final bearing (compass C{radiansPI2}) or zero if both points
145 coincide.
147 @see: U{Bearing<https://www.Movable-Type.co.UK/scripts/latlong.html>}, U{Course
148 between two points<https://www.EdWilliams.org/avform147.htm#Crs>} and
149 U{Bearing Between Two Points<https://web.Archive.org/web/20020630205931/
150 https://MathForum.org/library/drmath/view/55417.html>}.
151 '''
152 db, phi2, lam2 = _Wrap.philam3(lam1, phi2, lam2, wrap)
153 if final: # swap plus PI
154 phi1, lam1, phi2, lam2, db = phi2, lam2, phi1, lam1, -db
155 r = PI3
156 else:
157 r = PI2
158 sa1, ca1, sa2, ca2, sdb, cdb = sincos2_(phi1, phi2, db)
160 x = ca1 * sa2 - sa1 * ca2 * cdb
161 y = sdb * ca2
162 return _umod_PI2(atan2(y, x) + r) # .utily.wrapPI2
165def _bearingTo2(p1, p2, wrap=False): # for points.ispolar, sphericalTrigonometry.areaOf
166 '''(INTERNAL) Compute initial and final bearing.
167 '''
168 try: # for LatLon_ and ellipsoidal LatLon
169 return p1.bearingTo2(p2, wrap=wrap)
170 except AttributeError:
171 pass
172 # XXX spherical version, OK for ellipsoidal ispolar?
173 t = p1.philam + p2.philam
174 i = bearing_(*t, final=False, wrap=wrap)
175 f = bearing_(*t, final=True, wrap=wrap)
176 return Bearing2Tuple(degrees(i), degrees(f),
177 name__=_bearingTo2)
180def chord2angle(chord, radius=R_M):
181 '''Get the (central) angle from a chord length or distance.
183 @arg chord: Length or distance (C{meter}, same units as B{C{radius}}).
184 @kwarg radius: Mean earth radius (C{meter}, conventionally), datum (L{Datum}) or
185 ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
187 @return: Angle (C{radians} with sign of B{C{chord}}) or C{0} if C{B{radius}=0}.
189 @note: The angle will exceed C{PI} if C{B{chord} > B{radius} * 2}.
191 @see: Function L{angle2chord}.
192 '''
193 m = _mean_radius(radius)
194 r = fabs(chord / (m * _2_0)) if m > 0 else _0_0
195 if r:
196 i = int(r)
197 if i > 0:
198 r -= i
199 i *= PI
200 r = (asin1(r) + i) * _2_0
201 return _copysign(r, chord)
204def compassAngle(lat1, lon1, lat2, lon2, adjust=True, wrap=False):
205 '''Return the angle from North for the direction vector M{(lon2 - lon1,
206 lat2 - lat1)} between two points.
208 Suitable only for short, not near-polar vectors up to a few hundred
209 Km or Miles. Use function L{pygeodesy.bearing} for longer vectors.
211 @arg lat1: From latitude (C{degrees}).
212 @arg lon1: From longitude (C{degrees}).
213 @arg lat2: To latitude (C{degrees}).
214 @arg lon2: To longitude (C{degrees}).
215 @kwarg adjust: Adjust the longitudinal delta by the cosine of the mean
216 latitude (C{bool}).
217 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
218 B{C{lon2}} (C{bool}).
220 @return: Compass angle from North (C{degrees360}).
222 @note: Courtesy of Martin Schultz.
224 @see: U{Local, flat earth approximation
225 <https://www.EdWilliams.org/avform.htm#flat>}.
226 '''
227 d_lon, lat2, lon2 = _Wrap.latlon3(lon1, lat2, lon2, wrap)
228 if adjust: # scale delta lon
229 d_lon *= _scale_deg(lat1, lat2)
230 return atan2b(d_lon, lat2 - lat1)
233def cosineLaw(lat1, lon1, lat2, lon2, corr=0, earth=None, wrap=False,
234 datum=_WGS84, radius=R_M):
235 '''Compute the distance between two points using the U{Law of Cosines
236 <https://www.Movable-Type.co.UK/scripts/latlong.html#cosine-law>}
237 formula, optionally corrected.
239 @arg lat1: Start latitude (C{degrees}).
240 @arg lon1: Start longitude (C{degrees}).
241 @arg lat2: End latitude (C{degrees}).
242 @arg lon2: End longitude (C{degrees}).
243 @kwarg corr: Use C{B{corr}=2} to apply the U{Forsythe-Andoyer-Lambert
244 <https://www2.UNB.CA/gge/Pubs/TR77.pdf>}, C{B{corr}=1} for the
245 U{Andoyer-Lambert<https://Books.Google.com/books?id=x2UiAQAAIAAJ>}
246 corrected (ellipsoidal) or keep C{B{corr}=0} for the uncorrected
247 (spherical) C{Law of Cosines} formula (C{int}).
248 @kwarg earth: Mean earth radius (C{meter}) or datum (L{Datum}) or ellipsoid
249 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
250 @kwarg wrap: If C{True}, wrap or I{normalize} and B{C{lat2}} and B{C{lon2}}
251 (C{bool}).
252 @kwarg datum: Default ellipsiodal B{C{earth}} (and for backward compatibility).
253 @kwarg radius: Default spherical B{C{earth}} (and for backward compatibility).
255 @return: Distance (C{meter}, same units as B{C{radius}} or the datum's or
256 ellipsoid axes).
258 @raise TypeError: Invalid B{C{earth}}, B{C{datum}} or B{C{radius}}.
260 @raise ValueError: Invalid B{C{corr}}.
262 @see: Functions L{cosineLaw_}, L{equirectangular}, L{euclidean}, L{flatLocal} /
263 L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys} and
264 method L{Ellipsoid.distance2}.
266 @note: See note at function L{vincentys_}.
267 '''
268 return _dE(cosineLaw_, earth or datum, wrap, lat1, lon1, lat2, lon2, corr=corr) if corr else \
269 _dS(cosineLaw_, earth or radius, wrap, lat1, lon1, lat2, lon2)
272def cosineLaw_(phi2, phi1, lam21, corr=0, earth=None, datum=_WGS84):
273 '''Compute the I{angular} distance between two points using the U{Law of Cosines
274 <https://www.Movable-Type.co.UK/scripts/latlong.html#cosine-law>} formula,
275 optionally corrected.
277 @arg phi2: End latitude (C{radians}).
278 @arg phi1: Start latitude (C{radians}).
279 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
280 @kwarg corr: Use C{B{corr}=2} to apply the U{Forsythe-Andoyer-Lambert
281 <https://www2.UNB.CA/gge/Pubs/TR77.pdf>}, C{B{corr}=1} for the
282 U{Andoyer-Lambert<https://Books.Google.com/books?id=x2UiAQAAIAAJ>}
283 corrected (ellipsoidal) or keep C{B{corr}=0} for the uncorrected
284 (spherical) C{Law of Cosines} formula (C{int}).
285 @kwarg earth: Mean earth radius (C{meter}) or datum (L{Datum}) or ellipsoid
286 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
287 @kwarg datum: Default ellipsoidal B{C{earth}} (and for backward compatibility).
289 @return: Angular distance (C{radians}).
291 @raise TypeError: Invalid B{C{earth}} or B{C{datum}}.
293 @raise ValueError: Invalid B{C{corr}}.
295 @see: Functions L{cosineLaw}, L{euclidean_}, L{flatLocal_} / L{hubeny_},
296 L{flatPolar_}, L{haversine_}, L{thomas_} and L{vincentys_} and U{Geodesy-PHP
297 <https://GitHub.com/jtejido/geodesy-php/blob/master/src/Geodesy/Distance/
298 AndoyerLambert.php>}.
299 '''
300 s2, c2, s1, c1, r, c21 = _sincosa6(phi2, phi1, lam21)
301 if corr and isnon0(c1) and isnon0(c2):
302 E = _ellipsoidal(earth or datum, cosineLaw_)
303 f = _0_25 * E.f
304 if f: # ellipsoidal
305 if corr == 1: # Andoyer-Lambert
306 r2 = atan2(E.b_a * s2, c2)
307 r1 = atan2(E.b_a * s1, c1)
308 s2, c2, s1, c1 = sincos2_(r2, r1)
309 r = acos1(s1 * s2 + c1 * c2 * c21)
310 if r:
311 sr, _, sr_2, cr_2 = sincos2_(r, r * _0_5)
312 if isnon0(sr_2) and isnon0(cr_2):
313 s = (sr + r) * ((s1 - s2) / sr_2)**2
314 c = (sr - r) * ((s1 + s2) / cr_2)**2
315 r += (c - s) * _0_5 * f
317 elif corr == 2: # Forsythe-Andoyer-Lambert
318 sr, cr, s2r, _ = sincos2_(r, r * 2)
319 if isnon0(sr) and fabs(cr) < EPS1:
320 s = (s1 + s2)**2 / (_1_0 + cr)
321 t = (s1 - s2)**2 / (_1_0 - cr)
322 x = s + t
323 y = s - t
325 s = 8 * r**2 / sr
326 a = 64 * r + s * cr * 2 # 16 * r**2 / tan(r)
327 d = 48 * sr + s # 8 * r**2 / tan(r)
328 b = -2 * d
329 e = 30 * s2r
331 c = fdot_(30, r, cr, s, e, _0_5) # 8 * r**2 / tan(r)
332 t = fdot_( a, x, b, y, e, y**2, -c, x**2, d, x * y) * _0_125
333 r += fdot_(-r, x, sr, y * 3, t, f) * f
334 else:
335 raise _ValueError(corr=corr)
336 return r
339def _d3(wrap, lat1, lon1, lat2, lon2):
340 '''(INTERNAL) Helper for _dE, _dS, ....
341 '''
342 if wrap:
343 d_lon, lat2, _ = _Wrap.latlon3(lon1, lat2, lon2, wrap)
344 return radians(lat2), Phid(lat1=lat1), radians(d_lon)
345 else: # for backward compaibility
346 return Phid(lat2=lat2), Phid(lat1=lat1), radians(lon2 - lon1)
349def _dE(fun_, earth, wrap, *lls, **corr):
350 '''(INTERNAL) Helper for ellipsoidal distances.
351 '''
352 E = _ellipsoidal(earth, fun_)
353 r = fun_(*_d3(wrap, *lls), datum=E, **corr)
354 return r * E.a
357def _dS(fun_, radius, wrap, *lls, **adjust):
358 '''(INTERNAL) Helper for spherical distances.
359 '''
360 r = fun_(*_d3(wrap, *lls), **adjust)
361 if radius is not R_M:
362 try: # datum?
363 radius = radius.ellipsoid.R1
364 except AttributeError:
365 pass # scalar?
366 lat1, _, lat2, _ = lls
367 radius = _mean_radius(radius, lat1, lat2)
368 return r * radius
371def _ellipsoidal(earth, where):
372 '''(INTERNAL) Helper for distances.
373 '''
374 return _EWGS84 if earth in (_WGS84, _EWGS84) else (
375 earth if isinstance(earth, Ellipsoid) else
376 (earth if isinstance(earth, Datum) else # PYCHOK indent
377 _ellipsoidal_datum(earth, name__=where)).ellipsoid)
380def equirectangular(lat1, lon1, lat2, lon2, radius=R_M, **adjust_limit_wrap):
381 '''Approximate the distance between two points using the U{Equirectangular Approximation
382 / Projection<https://www.Movable-Type.co.UK/scripts/latlong.html#equirectangular>}.
384 @arg lat1: Start latitude (C{degrees}).
385 @arg lon1: Start longitude (C{degrees}).
386 @arg lat2: End latitude (C{degrees}).
387 @arg lon2: End longitude (C{degrees}).
388 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
389 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}).
390 @kwarg adjust_limit_wrap: Optionally, keyword arguments for function L{equirectangular4}.
392 @return: Distance (C{meter}, same units as B{C{radius}} or the datum's
393 ellipsoid axes).
395 @raise TypeError: Invalid B{C{radius}}.
397 @see: Function L{equirectangular4} for more details, the available B{C{options}},
398 errors, restrictions and other, approximate or accurate distance functions.
399 '''
400 r = _mean_radius(radius, lat1, lat2)
401 t = equirectangular4(Lat(lat1=lat1), Lon(lon1=lon1),
402 Lat(lat2=lat2), Lon(lon2=lon2),
403 **adjust_limit_wrap) # PYCHOK 4 vs 2-3
404 return degrees2m(sqrt(t.distance2), radius=r)
407def _equirectangular(lat1, lon1, lat2, lon2, **adjust_limit_wrap):
408 '''(INTERNAL) Helper for classes L{frechet._FrechetMeterRadians} and
409 L{hausdorff._HausdorffMeterRedians}.
410 '''
411 t = equirectangular4(lat1, lon1, lat2, lon2, **adjust_limit_wrap)
412 return t.distance2 * _RADIANS2
415def equirectangular4(lat1, lon1, lat2, lon2, adjust=True, limit=45, wrap=False):
416 '''Approximate the distance between two points using the U{Equirectangular Approximation
417 / Projection<https://www.Movable-Type.co.UK/scripts/latlong.html#equirectangular>}.
419 This approximation is valid for short distance of several hundred Km or Miles, see
420 the B{C{limit}} keyword argument and L{LimitError}.
422 @arg lat1: Start latitude (C{degrees}).
423 @arg lon1: Start longitude (C{degrees}).
424 @arg lat2: End latitude (C{degrees}).
425 @arg lon2: End longitude (C{degrees}).
426 @kwarg adjust: Adjust the wrapped, unrolled longitudinal delta by the cosine of the
427 mean latitude (C{bool}).
428 @kwarg limit: Optional limit for lat- and longitudinal deltas (C{degrees}) or C{None}
429 or C{0} for unlimited.
430 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and B{C{lon2}}
431 (C{bool}).
433 @return: A L{Distance4Tuple}C{(distance2, delta_lat, delta_lon, unroll_lon2)} with
434 C{distance2} in C{degrees squared}.
436 @raise LimitError: The lat- or longitudinal delta exceeds the B{C{-limit..limit}}
437 range and L{limiterrors<pygeodesy.limiterrors>} is C{True}.
439 @see: U{Local, flat earth approximation<https://www.EdWilliams.org/avform.htm#flat>},
440 functions L{equirectangular}, L{cosineLaw}, L{euclidean}, L{flatLocal} /
441 L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys} and methods
442 L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}.
443 '''
444 if wrap:
445 d_lon, lat2, ulon2 = _Wrap.latlon3(lon1, lat2, lon2, wrap)
446 else:
447 d_lon, ulon2 = (lon2 - lon1), lon2
448 d_lat = lat2 - lat1
450 if limit and limit > 0 and limiterrors():
451 d = max(fabs(d_lat), fabs(d_lon))
452 if d > limit:
453 t = _SPACE_(_delta_, Fmt.PAREN_g(d), Fmt.exceeds_limit(limit))
454 s = unstr(equirectangular4, lat1, lon1, lat2, lon2,
455 limit=limit, wrap=wrap)
456 raise LimitError(s, txt=t)
458 if adjust: # scale delta lon
459 d_lon *= _scale_deg(lat1, lat2)
461 d2 = hypot2(d_lat, d_lon) # degrees squared!
462 return Distance4Tuple(d2, d_lat, d_lon, ulon2 - lon2)
465def euclidean(lat1, lon1, lat2, lon2, radius=R_M, adjust=True, wrap=False):
466 '''Approximate the C{Euclidean} distance between two (spherical) points.
468 @arg lat1: Start latitude (C{degrees}).
469 @arg lon1: Start longitude (C{degrees}).
470 @arg lat2: End latitude (C{degrees}).
471 @arg lon2: End longitude (C{degrees}).
472 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
473 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
474 @kwarg adjust: Adjust the longitudinal delta by the cosine of the mean
475 latitude (C{bool}).
476 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
477 B{C{lon2}} (C{bool}).
479 @return: Distance (C{meter}, same units as B{C{radius}} or the ellipsoid
480 or datum axes).
482 @raise TypeError: Invalid B{C{radius}}.
484 @see: U{Distance between two (spherical) points
485 <https://www.EdWilliams.org/avform.htm#Dist>}, functions L{euclid},
486 L{euclidean_}, L{cosineLaw}, L{equirectangular}, L{flatLocal} /
487 L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys}
488 and methods L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and
489 C{LatLon.equirectangularTo}.
490 '''
491 return _dS(euclidean_, radius, wrap, lat1, lon1, lat2, lon2, adjust=adjust)
494def euclidean_(phi2, phi1, lam21, adjust=True):
495 '''Approximate the I{angular} C{Euclidean} distance between two (spherical) points.
497 @arg phi2: End latitude (C{radians}).
498 @arg phi1: Start latitude (C{radians}).
499 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
500 @kwarg adjust: Adjust the longitudinal delta by the cosine of the mean
501 latitude (C{bool}).
503 @return: Angular distance (C{radians}).
505 @see: Functions L{euclid}, L{euclidean}, L{cosineLaw_}, L{flatLocal_} /
506 L{hubeny_}, L{flatPolar_}, L{haversine_}, L{thomas_} and L{vincentys_}.
507 '''
508 if adjust:
509 lam21 *= _scale_rad(phi2, phi1)
510 return euclid(phi2 - phi1, lam21)
513def excessAbc_(A, b, c):
514 '''Compute the I{spherical excess} C{E} of a (spherical) triangle from two sides
515 and the included (small) angle.
517 @arg A: An interior triangle angle (C{radians}).
518 @arg b: Frist adjacent triangle side (C{radians}).
519 @arg c: Second adjacent triangle side (C{radians}).
521 @return: Spherical excess (C{radians}).
523 @raise UnitError: Invalid B{C{A}}, B{C{b}} or B{C{c}}.
525 @see: Functions L{excessGirard_}, L{excessLHuilier_} and U{Spherical
526 trigonometry<https://WikiPedia.org/wiki/Spherical_trigonometry>}.
527 '''
528 A = Radians_(A=A)
529 b = Radians_(b=b) * _0_5
530 c = Radians_(c=c) * _0_5
532 sA, cA, sb, cb, sc, cc = sincos2_(A, b, c)
533 s = sA * sb * sc
534 c = cA * sb * sc + cc * cb
535 return atan2(s, c) * _2_0
538def excessCagnoli_(a, b, c):
539 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using U{Cagnoli's
540 <https://Zenodo.org/record/35392>} (D.34) formula.
542 @arg a: First triangle side (C{radians}).
543 @arg b: Second triangle side (C{radians}).
544 @arg c: Third triangle side (C{radians}).
546 @return: Spherical excess (C{radians}).
548 @raise UnitError: Invalid B{C{a}}, B{C{b}} or B{C{c}}.
550 @see: Function L{excessLHuilier_} and U{Spherical trigonometry
551 <https://WikiPedia.org/wiki/Spherical_trigonometry>}.
552 '''
553 a = Radians_(a=a)
554 b = Radians_(b=b)
555 c = Radians_(c=c)
557 r = _maprod(cos, a * _0_5, b * _0_5, c * _0_5)
558 if r:
559 s = fsumf_(a, b, c) * _0_5
560 t = _maprod(sin, s, s - a, s - b, s - c)
561 r = asin1(sqrt(t) * _0_5 / r) if t > 0 else _0_0
562 return Radians(Cagnoli=r * _2_0)
565def excessGirard_(A, B, C):
566 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using U{Girard's
567 <https://MathWorld.Wolfram.com/GirardsSphericalExcessFormula.html>} formula.
569 @arg A: First interior triangle angle (C{radians}).
570 @arg B: Second interior triangle angle (C{radians}).
571 @arg C: Third interior triangle angle (C{radians}).
573 @return: Spherical excess (C{radians}).
575 @raise UnitError: Invalid B{C{A}}, B{C{B}} or B{C{C}}.
577 @see: Function L{excessLHuilier_} and U{Spherical trigonometry
578 <https://WikiPedia.org/wiki/Spherical_trigonometry>}.
579 '''
580 r = fsumf_(Radians_(A=A),
581 Radians_(B=B),
582 Radians_(C=C), -PI)
583 return Radians(Girard=r)
586def excessLHuilier_(a, b, c):
587 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using U{L'Huilier's
588 <https://MathWorld.Wolfram.com/LHuiliersTheorem.html>}'s Theorem.
590 @arg a: First triangle side (C{radians}).
591 @arg b: Second triangle side (C{radians}).
592 @arg c: Third triangle side (C{radians}).
594 @return: Spherical excess (C{radians}).
596 @raise UnitError: Invalid B{C{a}}, B{C{b}} or B{C{c}}.
598 @see: Function L{excessCagnoli_}, L{excessGirard_} and U{Spherical
599 trigonometry<https://WikiPedia.org/wiki/Spherical_trigonometry>}.
600 '''
601 a = Radians_(a=a)
602 b = Radians_(b=b)
603 c = Radians_(c=c)
605 s = fsumf_(a, b, c) * _0_5
606 r = _maprod(tan_2, s, s - a, s - b, s - c)
607 r = atan(sqrt(r)) if r > 0 else _0_0
608 return Radians(LHuilier=r * _4_0)
611def excessKarney(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
612 '''Compute the surface area of a (spherical) quadrilateral bounded by a
613 segment of a great circle, two meridians and the equator using U{Karney's
614 <https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}
615 method.
617 @arg lat1: Start latitude (C{degrees}).
618 @arg lon1: Start longitude (C{degrees}).
619 @arg lat2: End latitude (C{degrees}).
620 @arg lon2: End longitude (C{degrees}).
621 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
622 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) or C{None}.
623 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
624 B{C{lon2}} (C{bool}).
626 @return: Surface area, I{signed} (I{square} C{meter} or the same units as
627 B{C{radius}} I{squared}) or the I{spherical excess} (C{radians})
628 if C{B{radius}=0} or C{None}.
630 @raise TypeError: Invalid B{C{radius}}.
632 @raise UnitError: Invalid B{C{lat2}} or B{C{lat1}}.
634 @raise ValueError: Semi-circular longitudinal delta.
636 @see: Functions L{excessKarney_} and L{excessQuad}.
637 '''
638 r = excessKarney_(*_d3(wrap, lat1, lon1, lat2, lon2))
639 if radius:
640 r *= _mean_radius(radius, lat1, lat2)**2
641 return r
644def excessKarney_(phi2, phi1, lam21):
645 '''Compute the I{spherical excess} C{E} of a (spherical) quadrilateral bounded by
646 a segment of a great circle, two meridians and the equator using U{Karney's
647 <https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}
648 method.
650 @arg phi2: End latitude (C{radians}).
651 @arg phi1: Start latitude (C{radians}).
652 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
654 @return: Spherical excess, I{signed} (C{radians}).
656 @raise ValueError: Semi-circular longitudinal delta B{C{lam21}}.
658 @see: Function L{excessKarney} and U{Area of a spherical polygon
659 <https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}.
660 '''
661 # from: Veness <https://www.Movable-Type.co.UK/scripts/latlong.html> Area
662 # method due to Karney: for each edge of the polygon,
663 #
664 # tan(Δλ / 2) · (tan(φ1 / 2) + tan(φ2 / 2))
665 # tan(E / 2) = -----------------------------------------
666 # 1 + tan(φ1 / 2) · tan(φ2 / 2)
667 #
668 # where E is the spherical excess of the trapezium obtained by extending
669 # the edge to the equator-circle vector for each edge (see also ***).
670 t2 = tan_2(phi2)
671 t1 = tan_2(phi1)
672 c = (t1 * t2) + _1_0
673 s = (t1 + t2) * tan_2(lam21, lam21=None)
674 return Radians(Karney=atan2(s, c) * _2_0)
677# ***) Original post no longer available, following is a copy of the main part
678# <http://OSGeo-org.1560.x6.Nabble.com/Area-of-a-spherical-polygon-td3841625.html>
679#
680# The area of a polygon on a (unit) sphere is given by the spherical excess
681#
682# A = 2 * pi - sum(exterior angles)
683#
684# However this is badly conditioned if the polygon is small. In this case, use
685#
686# A = sum(S12{i, i+1}) over the edges of the polygon
687#
688# where S12 is the area of the quadrilateral bounded by an edge of the polygon,
689# two meridians and the equator, i.e. with vertices (phi1, lambda1), (phi2,
690# lambda2), (0, lambda1) and (0, lambda2). S12 is given by
691#
692# tan(S12 / 2) = tan(lambda21 / 2) * (tan(phi1 / 2) + tan(phi2 / 2)) /
693# (tan(phi1 / 2) * tan(phi2 / 2) + 1)
694#
695# = tan(lambda21 / 2) * tanh((Lamb(phi1) + Lamb(phi2)) / 2)
696#
697# where lambda21 = lambda2 - lambda1 and Lamb(x) is the Lambertian (or the
698# inverse Gudermannian) function
699#
700# Lambertian(x) = asinh(tan(x)) = atanh(sin(x)) = 2 * atanh(tan(x / 2))
701#
702# Notes: The formula for S12 is exact, except that...
703# - it is indeterminate if an edge is a semi-circle
704# - the formula for A applies only if the polygon does not include a pole
705# (if it does, then add +/- 2 * pi to the result)
706# - in the limit of small phi and lambda, S12 reduces to the trapezoidal
707# formula, S12 = (lambda2 - lambda1) * (phi1 + phi2) / 2
708# - I derived this result from the equation for the area of a spherical
709# triangle in terms of two edges and the included angle given by, e.g.
710# U{Todhunter, I. - Spherical Trigonometry (1871), Sec. 103, Eq. (2)
711# <http://Books.Google.com/books?id=3uBHAAAAIAAJ&pg=PA71>}
712# - I would be interested to know if this formula for S12 is already known
713# - Charles Karney
716def excessQuad(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
717 '''Compute the surface area of a (spherical) quadrilateral bounded by a segment
718 of a great circle, two meridians and the equator.
720 @arg lat1: Start latitude (C{degrees}).
721 @arg lon1: Start longitude (C{degrees}).
722 @arg lat2: End latitude (C{degrees}).
723 @arg lon2: End longitude (C{degrees}).
724 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
725 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) or C{None}.
726 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
727 B{C{lon2}} (C{bool}).
729 @return: Surface area, I{signed} (I{square} C{meter} or the same units as
730 B{C{radius}} I{squared}) or the I{spherical excess} (C{radians})
731 if C{B{radius}=0} or C{None}.
733 @raise TypeError: Invalid B{C{radius}}.
735 @raise UnitError: Invalid B{C{lat2}} or B{C{lat1}}.
737 @see: Function L{excessQuad_} and L{excessKarney}.
738 '''
739 r = excessQuad_(*_d3(wrap, lat1, lon1, lat2, lon2))
740 if radius:
741 r *= _mean_radius(radius, lat1, lat2)**2
742 return r
745def excessQuad_(phi2, phi1, lam21):
746 '''Compute the I{spherical excess} C{E} of a (spherical) quadrilateral bounded
747 by a segment of a great circle, two meridians and the equator.
749 @arg phi2: End latitude (C{radians}).
750 @arg phi1: Start latitude (C{radians}).
751 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
753 @return: Spherical excess, I{signed} (C{radians}).
755 @see: Function L{excessQuad} and U{Spherical trigonometry
756 <https://WikiPedia.org/wiki/Spherical_trigonometry>}.
757 '''
758 c = cos((phi2 - phi1) * _0_5)
759 s = sin((phi2 + phi1) * _0_5) * tan_2(lam21)
760 return Radians(Quad=atan2(s, c) * _2_0)
763def flatLocal(lat1, lon1, lat2, lon2, datum=_WGS84, scaled=True, wrap=False):
764 '''Compute the distance between two (ellipsoidal) points using
765 the U{ellipsoidal Earth to plane projection<https://WikiPedia.org/
766 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}
767 aka U{Hubeny<https://www.OVG.AT/de/vgi/files/pdf/3781/>} formula.
769 @arg lat1: Start latitude (C{degrees}).
770 @arg lon1: Start longitude (C{degrees}).
771 @arg lat2: End latitude (C{degrees}).
772 @arg lon2: End longitude (C{degrees}).
773 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2}
774 or L{a_f2Tuple}) to use.
775 @kwarg scaled: Scale prime_vertical by C{cos(B{phi})} (C{bool}), see
776 method L{pygeodesy.Ellipsoid.roc2_}.
777 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
778 B{C{lon2}} (C{bool}).
780 @return: Distance (C{meter}, same units as the B{C{datum}}'s or ellipsoid axes).
782 @raise TypeError: Invalid B{C{datum}}.
784 @note: The meridional and prime_vertical radii of curvature are taken and
785 scaled at the mean of both latitude.
787 @see: Functions L{flatLocal_} or L{hubeny_}, L{cosineLaw}, L{equirectangular},
788 L{euclidean}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys}, method
789 L{Ellipsoid.distance2} and U{local, flat earth approximation
790 <https://www.EdWilliams.org/avform.htm#flat>}.
791 '''
792 t = _d3(wrap, lat1, lon1, lat2, lon2)
793 E = _ellipsoidal(datum, flatLocal)
794 return E._hubeny_2(*t, scaled=scaled, squared=False) * E.a
796hubeny = flatLocal # PYCHOK for Karl Hubeny
799def flatLocal_(phi2, phi1, lam21, datum=_WGS84, scaled=True):
800 '''Compute the I{angular} distance between two (ellipsoidal) points using
801 the U{ellipsoidal Earth to plane projection<https://WikiPedia.org/
802 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}
803 aka U{Hubeny<https://www.OVG.AT/de/vgi/files/pdf/3781/>} formula.
805 @arg phi2: End latitude (C{radians}).
806 @arg phi1: Start latitude (C{radians}).
807 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
808 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2}
809 or L{a_f2Tuple}) to use.
810 @kwarg scaled: Scale prime_vertical by C{cos(B{phi})} (C{bool}), see
811 method L{pygeodesy.Ellipsoid.roc2_}.
813 @return: Angular distance (C{radians}).
815 @raise TypeError: Invalid B{C{datum}}.
817 @note: The meridional and prime_vertical radii of curvature are taken and
818 scaled I{at the mean of both latitude}.
820 @see: Functions L{flatLocal} or L{hubeny}, L{cosineLaw_}, L{flatPolar_},
821 L{euclidean_}, L{haversine_}, L{thomas_} and L{vincentys_} and U{local,
822 flat earth approximation<https://www.EdWilliams.org/avform.htm#flat>}.
823 '''
824 E = _ellipsoidal(datum, flatLocal_)
825 return E._hubeny_2(phi2, phi1, lam21, scaled=scaled, squared=False)
827hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny
830def flatPolar(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
831 '''Compute the distance between two (spherical) points using the U{polar
832 coordinate flat-Earth<https://WikiPedia.org/wiki/Geographical_distance
833 #Polar_coordinate_flat-Earth_formula>} formula.
835 @arg lat1: Start latitude (C{degrees}).
836 @arg lon1: Start longitude (C{degrees}).
837 @arg lat2: End latitude (C{degrees}).
838 @arg lon2: End longitude (C{degrees}).
839 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
840 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
841 @kwarg wrap: If C{True}, wrap or I{normalize} and B{C{lat2}} and B{C{lon2}}
842 (C{bool}).
844 @return: Distance (C{meter}, same units as B{C{radius}} or the datum's or
845 ellipsoid axes).
847 @raise TypeError: Invalid B{C{radius}}.
849 @see: Functions L{flatPolar_}, L{cosineLaw}, L{flatLocal} / L{hubeny},
850 L{equirectangular}, L{euclidean}, L{haversine}, L{thomas} and L{vincentys}.
851 '''
852 return _dS(flatPolar_, radius, wrap, lat1, lon1, lat2, lon2)
855def flatPolar_(phi2, phi1, lam21):
856 '''Compute the I{angular} distance between two (spherical) points using the
857 U{polar coordinate flat-Earth<https://WikiPedia.org/wiki/Geographical_distance
858 #Polar_coordinate_flat-Earth_formula>} formula.
860 @arg phi2: End latitude (C{radians}).
861 @arg phi1: Start latitude (C{radians}).
862 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
864 @return: Angular distance (C{radians}).
866 @see: Functions L{flatPolar}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /
867 L{hubeny_}, L{haversine_}, L{thomas_} and L{vincentys_}.
868 '''
869 a = fabs(PI_2 - phi1) # co-latitude
870 b = fabs(PI_2 - phi2) # co-latitude
871 if a < b:
872 a, b = b, a
873 if a < EPS0:
874 a = _0_0
875 elif b > 0:
876 b = b / a # /= chokes PyChecker
877 c = b * cos(lam21) * _2_0
878 c = fsumf_(_1_0, b**2, -fabs(c))
879 a *= sqrt0(c)
880 return a
883def _hartzell(pov, los, earth, **kwds):
884 '''(INTERNAL) Helper for C{CartesianBase.hartzell} and C{LatLonBase.hartzell}.
885 '''
886 if earth is None:
887 earth = pov.datum
888 else:
889 earth = _spherical_datum(earth, name__=hartzell)
890 pov = pov.toDatum(earth)
891 h = pov.height
892 if h < 0: # EPS0
893 t = _SPACE_(Fmt.PARENSPACED(height=h), _inside_)
894 raise IntersectionError(pov=pov, earth=earth, txt=t)
895 return hartzell(pov, los=los, earth=earth, **kwds) if h > 0 else pov # EPS0
898def hartzell(pov, los=False, earth=_WGS84, **name_LatLon_and_kwds):
899 '''Compute the intersection of the earth's surface and a Line-Of-Sight from
900 a Point-Of-View in space.
902 @arg pov: Point-Of-View outside the earth (C{LatLon}, C{Cartesian},
903 L{Ecef9Tuple} or L{Vector3d}).
904 @kwarg los: Line-Of-Sight, I{direction} to earth (L{Los}, L{Vector3d}),
905 C{True} for the I{normal, plumb} onto the surface or C{False}
906 or C{None} to point to the center of the earth.
907 @kwarg earth: The earth model (L{Datum}, L{Ellipsoid}, L{Ellipsoid2},
908 L{a_f2Tuple} or a C{scalar} earth radius in C{meter}).
909 @kwarg name_LatLon_and_kwds: Optional C{B{name}="hartzell"} (C{str}), class
910 C{B{LatLon}=None} to return the intersection and optionally,
911 additional C{LatLon} keyword arguments, include the B{C{datum}}
912 if different from and to convert from B{C{earth}}.
914 @return: The intersection (L{Vector3d}, B{C{pov}}'s C{cartesian type} or
915 the given B{C{LatLon}} instance) with attribute C{height} set to
916 the distance to the B{C{pov}}.
918 @raise IntersectionError: Invalid B{C{pov}} or B{C{pov}} inside the earth or
919 invalid B{C{los}} or B{C{los}} points outside or
920 away from the earth.
922 @raise TypeError: Invalid B{C{earth}}, C{ellipsoid} or C{datum}.
924 @see: Class L{Los}, functions L{tyr3d} and L{hartzell4} and methods
925 L{Ellipsoid.hartzell4}, any C{Cartesian.hartzell} and C{LatLon.hartzell}.
926 '''
927 n, kwds = _name2__(name_LatLon_and_kwds, name__=hartzell)
928 try:
929 D = _spherical_datum(earth, name__=hartzell)
930 r, h, i = _MODS.triaxials._hartzell3(pov, los, D.ellipsoid._triaxial)
932 C = _MODS.cartesianBase.CartesianBase
933 if kwds:
934 c = C(r, datum=D)
935 r = c.toLatLon(**_xkwds(kwds, height=h))
936 elif isinstance(r, C):
937 r.height = h
938 if i:
939 r._iteration = i
940 except Exception as x:
941 raise IntersectionError(pov=pov, los=los, earth=earth, cause=x, **kwds)
942 return _xnamed(r, n) if n else r
945def haversine(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
946 '''Compute the distance between two (spherical) points using the U{Haversine
947 <https://www.Movable-Type.co.UK/scripts/latlong.html>} formula.
949 @arg lat1: Start latitude (C{degrees}).
950 @arg lon1: Start longitude (C{degrees}).
951 @arg lat2: End latitude (C{degrees}).
952 @arg lon2: End longitude (C{degrees}).
953 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
954 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
955 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
956 B{C{lon2}} (C{bool}).
958 @return: Distance (C{meter}, same units as B{C{radius}}).
960 @raise TypeError: Invalid B{C{radius}}.
962 @see: U{Distance between two (spherical) points
963 <https://www.EdWilliams.org/avform.htm#Dist>}, functions L{cosineLaw},
964 L{equirectangular}, L{euclidean}, L{flatLocal} / L{hubeny}, L{flatPolar},
965 L{thomas} and L{vincentys} and methods L{Ellipsoid.distance2},
966 C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}.
968 @note: See note at function L{vincentys_}.
969 '''
970 return _dS(haversine_, radius, wrap, lat1, lon1, lat2, lon2)
973def haversine_(phi2, phi1, lam21):
974 '''Compute the I{angular} distance between two (spherical) points using the
975 U{Haversine<https://www.Movable-Type.co.UK/scripts/latlong.html>} formula.
977 @arg phi2: End latitude (C{radians}).
978 @arg phi1: Start latitude (C{radians}).
979 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
981 @return: Angular distance (C{radians}).
983 @see: Functions L{haversine}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /
984 L{hubeny_}, L{flatPolar_}, L{thomas_} and L{vincentys_}.
986 @note: See note at function L{vincentys_}.
987 '''
988 h = hav(phi2 - phi1) + cos(phi1) * cos(phi2) * hav(lam21) # haversine
989 return atan2(sqrt0(h), sqrt0(_1_0 - h)) * _2_0 # == asin1(sqrt(h)) * 2
992def heightOf(angle, distance, radius=R_M):
993 '''Determine the height above the (spherical) earth' surface after
994 traveling along a straight line at a given tilt.
996 @arg angle: Tilt angle above horizontal (C{degrees}).
997 @arg distance: Distance along the line (C{meter} or same units as
998 B{C{radius}}).
999 @kwarg radius: Optional mean earth radius (C{meter}).
1001 @return: Height (C{meter}, same units as B{C{distance}} and B{C{radius}}).
1003 @raise ValueError: Invalid B{C{angle}}, B{C{distance}} or B{C{radius}}.
1005 @see: U{MultiDop geog_lib.GeogBeamHt<https://GitHub.com/NASA/MultiDop>}
1006 (U{Shapiro et al. 2009, JTECH
1007 <https://Journals.AMetSoc.org/doi/abs/10.1175/2009JTECHA1256.1>}
1008 and U{Potvin et al. 2012, JTECH
1009 <https://Journals.AMetSoc.org/doi/abs/10.1175/JTECH-D-11-00019.1>}).
1010 '''
1011 r = h = Radius(radius)
1012 d = fabs(Distance(distance))
1013 if d > h:
1014 d, h = h, d
1016 if d > EPS0: # and h > EPS0
1017 d = d / h # /= h chokes PyChecker
1018 s = sin(Phid(angle=angle, clip=_180_0))
1019 s = fsumf_(_1_0, s * d * _2_0, d**2)
1020 if s > 0:
1021 return h * sqrt(s) - r
1023 raise _ValueError(angle=angle, distance=distance, radius=radius)
1026def heightOrthometric(h_loc, N):
1027 '''Get the I{orthometric} height B{H}, the height above the geoid, earth surface.
1029 @arg h_loc: The height above the ellipsoid (C{meter}) or an I{ellipsoidal}
1030 location (C{LatLon} or C{Cartesian} with a C{height} or C{h}
1031 attribute), otherwise C{0 meter}.
1032 @arg N: The I{geoid} height (C{meter}), the height of the geoid above the
1033 ellipsoid at the same B{C{h_loc}} location.
1035 @return: I{Orthometric} height C{B{H} = B{h} - B{N}} (C{meter}, same units
1036 as B{C{h}} and B{C{N}}).
1038 @see: U{Ellipsoid, Geoid, and Orthometric Heights<https://www.NGS.NOAA.gov/
1039 GEOID/PRESENTATIONS/2007_02_24_CCPS/Roman_A_PLSC2007notes.pdf>}, page
1040 6 and module L{pygeodesy.geoids}.
1041 '''
1042 h = h_loc if _isHeight(h_loc) else _xattr(h_loc, height=_xattr(h_loc, h=0))
1043 return Height(H=Height(h=h) - Height(N=N))
1046def horizon(height, radius=R_M, refraction=False):
1047 '''Determine the distance to the horizon from a given altitude above the
1048 (spherical) earth.
1050 @arg height: Altitude (C{meter} or same units as B{C{radius}}).
1051 @kwarg radius: Optional mean earth radius (C{meter}).
1052 @kwarg refraction: Consider atmospheric refraction (C{bool}).
1054 @return: Distance (C{meter}, same units as B{C{height}} and B{C{radius}}).
1056 @raise ValueError: Invalid B{C{height}} or B{C{radius}}.
1058 @see: U{Distance to horizon<https://www.EdWilliams.org/avform.htm#Horizon>}.
1059 '''
1060 h, r = Height(height), Radius(radius)
1061 if min(h, r) < 0:
1062 raise _ValueError(height=height, radius=radius)
1064 if refraction:
1065 r *= 2.415750694528 # 2.0 / 0.8279
1066 else:
1067 r += r + h
1068 return sqrt0(r * h)
1071class _idllmn6(object): # see also .geodesicw._wargs, .latlonBase._toCartesian3, .vector2d._numpy
1072 '''(INTERNAL) Helper for C{intersection2} and C{intersections2}.
1073 '''
1074 @contextmanager # <https://www.Python.org/dev/peps/pep-0343/> Examples
1075 def __call__(self, datum, lat1, lon1, lat2, lon2, small, wrap, s, **kwds):
1076 try:
1077 if wrap:
1078 _, lat2, lon2 = _Wrap.latlon3(lon1, lat2, lon2, wrap)
1079 kwds = _xkwds(kwds, wrap=wrap) # for _xError
1080 m = small if small is _100km else Meter_(small=small)
1081 n = _DUNDER_nameof(intersections2 if s else intersection2)
1082 if datum is None or euclidean(lat1, lon1, lat2, lon2) < m:
1083 d, m = None, _MODS.vector3d
1084 _i = m._intersects2 if s else m._intersect3d3
1085 elif _isRadius(datum) and datum < 0 and not s:
1086 d = _spherical_datum(-datum, name=n)
1087 m = _MODS.sphericalNvector
1088 _i = m.intersection
1089 else:
1090 d = _spherical_datum(datum, name=n)
1091 if d.isSpherical:
1092 m = _MODS.sphericalTrigonometry
1093 _i = m._intersects2 if s else m._intersect
1094 elif d.isEllipsoidal:
1095 try:
1096 if d.ellipsoid.geodesic:
1097 pass
1098 m = _MODS.ellipsoidalKarney
1099 except ImportError:
1100 m = _MODS.ellipsoidalExact
1101 _i = m._intersections2 if s else m._intersection3 # ellipsoidalBaseDI
1102 else:
1103 raise _TypeError(datum=datum)
1104 yield _i, d, lat2, lon2, m, n
1106 except (TypeError, ValueError) as x:
1107 raise _xError(x, lat1=lat1, lon1=lon1, datum=datum,
1108 lat2=lat2, lon2=lon2, small=small, **kwds)
1110_idllmn6 = _idllmn6() # PYCHOK singleton
1113def intersection2(lat1, lon1, bearing1,
1114 lat2, lon2, bearing2, datum=None, wrap=False, small=_100km): # was=True
1115 '''I{Conveniently} compute the intersection of two lines each defined by
1116 a (geodetic) point and a bearing from North, using either ...
1118 1) L{vector3d.intersection3d3} for B{C{small}} distances (below 100 Km
1119 or about 0.88 degrees) or if I{no} B{C{datum}} is specified, or ...
1121 2) L{sphericalTrigonometry.intersection} for a spherical B{C{datum}}
1122 or a C{scalar B{datum}} representing the earth radius, conventionally
1123 in C{meter} or ...
1125 3) L{sphericalNvector.intersection} if B{C{datum}} is a I{negative}
1126 C{scalar}, (negative) earth radius, conventionally in C{meter} or ...
1128 4) L{ellipsoidalKarney.intersection3} for an ellipsoidal B{C{datum}}
1129 and if I{Karney}'s U{geographiclib<https://PyPI.org/project/geographiclib>}
1130 is installed, otherwise ...
1132 5) L{ellipsoidalExact.intersection3}, provided B{C{datum}} is ellipsoidal.
1134 @arg lat1: Latitude of the first point (C{degrees}).
1135 @arg lon1: Longitude of the first point (C{degrees}).
1136 @arg bearing1: Bearing at the first point (compass C{degrees360}).
1137 @arg lat2: Latitude of the second point (C{degrees}).
1138 @arg lon2: Longitude of the second point (C{degrees}).
1139 @arg bearing2: Bearing at the second point (compass C{degrees360}).
1140 @kwarg datum: Optional datum (L{Datum}) or ellipsoid (L{Ellipsoid},
1141 L{Ellipsoid2} or L{a_f2Tuple}) or C{scalar} earth radius
1142 (C{meter}, same units as B{C{radius1}} and B{C{radius2}})
1143 or C{None}.
1144 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
1145 B{C{lon2}} (C{bool}).
1146 @kwarg small: Upper limit for small distances (C{meter}).
1148 @return: Intersection point (L{LatLon2Tuple}C{(lat, lon)}).
1150 @raise IntersectionError: No or an ambiguous intersection or colinear,
1151 parallel or otherwise non-intersecting lines.
1153 @raise TypeError: Invalid B{C{datum}}.
1155 @raise UnitError: Invalid B{C{lat1}}, B{C{lon1}}, B{C{bearing1}}, B{C{lat2}},
1156 B{C{lon2}} or B{C{bearing2}}.
1158 @see: Method L{RhumbLine.intersection2}.
1159 '''
1160 b1 = Bearing(bearing1=bearing1)
1161 b2 = Bearing(bearing2=bearing2)
1162 with _idllmn6(datum, lat1, lon1, lat2, lon2,
1163 small, wrap, False, bearing1=b1, bearing2=b2) as t:
1164 _i, d, lat2, lon2, m, n = t
1165 if d is None:
1166 t, _, _ = _i(m.Vector3d(lon1, lat1, 0), b1,
1167 m.Vector3d(lon2, lat2, 0), b2, useZ=False)
1168 t = LatLon2Tuple(t.y, t.x, name=n)
1170 else:
1171 t = _i(m.LatLon(lat1, lon1, datum=d), b1,
1172 m.LatLon(lat2, lon2, datum=d), b2,
1173 LatLon=None, height=0, wrap=False)
1174 if isinstance(t, Intersection3Tuple): # ellipsoidal
1175 t, _, _ = t
1176 t = LatLon2Tuple(t.lat, t.lon, name=n)
1177 return t
1180def intersections2(lat1, lon1, radius1,
1181 lat2, lon2, radius2, datum=None, wrap=False, small=_100km): # was=True
1182 '''I{Conveniently} compute the intersections of two circles each defined
1183 by a (geodetic) center point and a radius, using either ...
1185 1) L{vector3d.intersections2} for B{C{small}} distances (below 100 Km
1186 or about 0.88 degrees) or if I{no} B{C{datum}} is specified, or ...
1188 2) L{sphericalTrigonometry.intersections2} for a spherical B{C{datum}}
1189 or a C{scalar B{datum}} representing the earth radius, conventionally
1190 in C{meter} or ...
1192 3) L{ellipsoidalKarney.intersections2} for an ellipsoidal B{C{datum}}
1193 and if I{Karney}'s U{geographiclib<https://PyPI.org/project/geographiclib>}
1194 is installed, otherwise ...
1196 4) L{ellipsoidalExact.intersections2}, provided B{C{datum}} is ellipsoidal.
1198 @arg lat1: Latitude of the first circle center (C{degrees}).
1199 @arg lon1: Longitude of the first circle center (C{degrees}).
1200 @arg radius1: Radius of the first circle (C{meter}, conventionally).
1201 @arg lat2: Latitude of the second circle center (C{degrees}).
1202 @arg lon2: Longitude of the second circle center (C{degrees}).
1203 @arg radius2: Radius of the second circle (C{meter}, same units as B{C{radius1}}).
1204 @kwarg datum: Optional datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2}
1205 or L{a_f2Tuple}) or C{scalar} earth radius (C{meter}, same units as
1206 B{C{radius1}} and B{C{radius2}}) or C{None}.
1207 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and B{C{lon2}}
1208 (C{bool}).
1209 @kwarg small: Upper limit for small distances (C{meter}).
1211 @return: 2-Tuple of the intersection points, each a L{LatLon2Tuple}C{(lat, lon)}.
1212 Both points are the same instance, aka the I{radical center} if the
1213 circles are abutting
1215 @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting
1216 circles or no convergence.
1218 @raise TypeError: Invalid B{C{datum}}.
1220 @raise UnitError: Invalid B{C{lat1}}, B{C{lon1}}, B{C{radius1}}, B{C{lat2}},
1221 B{C{lon2}} or B{C{radius2}}.
1222 '''
1223 r1 = Radius_(radius1=radius1)
1224 r2 = Radius_(radius2=radius2)
1225 with _idllmn6(datum, lat1, lon1, lat2, lon2,
1226 small, wrap, True, radius1=r1, radius2=r2) as t:
1227 _i, d, lat2, lon2, m, n = t
1228 if d is None:
1229 r1 = m2degrees(r1, radius=R_M, lat=lat1)
1230 r2 = m2degrees(r2, radius=R_M, lat=lat2)
1232 def _V2T(x, y, _, **unused): # _ == z unused
1233 return LatLon2Tuple(y, x, name=n)
1235 t = _i(m.Vector3d(lon1, lat1, 0), r1,
1236 m.Vector3d(lon2, lat2, 0), r2, sphere=False,
1237 Vector=_V2T)
1238 else:
1239 def _LL2T(lat, lon, **unused):
1240 return LatLon2Tuple(lat, lon, name=n)
1242 t = _i(m.LatLon(lat1, lon1, datum=d), r1,
1243 m.LatLon(lat2, lon2, datum=d), r2,
1244 LatLon=_LL2T, height=0, wrap=False)
1245 return t
1248def isantipode(lat1, lon1, lat2, lon2, eps=EPS):
1249 '''Check whether two points are I{antipodal}, on diametrically
1250 opposite sides of the earth.
1252 @arg lat1: Latitude of one point (C{degrees}).
1253 @arg lon1: Longitude of one point (C{degrees}).
1254 @arg lat2: Latitude of the other point (C{degrees}).
1255 @arg lon2: Longitude of the other point (C{degrees}).
1256 @kwarg eps: Tolerance for near-equality (C{degrees}).
1258 @return: C{True} if points are antipodal within the
1259 B{C{eps}} tolerance, C{False} otherwise.
1261 @see: Functions L{isantipode_} and L{antipode}.
1262 '''
1263 return (fabs(lat1 + lat2) <= eps and
1264 fabs(lon1 + lon2) <= eps) or _isequalTo(
1265 normal(lat1, lon1), antipode(lat2, lon2), eps)
1268def isantipode_(phi1, lam1, phi2, lam2, eps=EPS):
1269 '''Check whether two points are I{antipodal}, on diametrically
1270 opposite sides of the earth.
1272 @arg phi1: Latitude of one point (C{radians}).
1273 @arg lam1: Longitude of one point (C{radians}).
1274 @arg phi2: Latitude of the other point (C{radians}).
1275 @arg lam2: Longitude of the other point (C{radians}).
1276 @kwarg eps: Tolerance for near-equality (C{radians}).
1278 @return: C{True} if points are antipodal within the
1279 B{C{eps}} tolerance, C{False} otherwise.
1281 @see: Functions L{isantipode} and L{antipode_}.
1282 '''
1283 return (fabs(phi1 + phi2) <= eps and
1284 fabs(lam1 + lam2) <= eps) or _isequalTo_(
1285 normal_(phi1, lam1), antipode_(phi2, lam2), eps)
1288def _isequalTo(p1, p2, eps=EPS):
1289 '''Compare 2 point lat-/lons ignoring C{class}.
1290 '''
1291 return (fabs(p1.lat - p2.lat) <= eps and
1292 fabs(p1.lon - p2.lon) <= eps) if eps else (p1.latlon == p2.latlon)
1295def _isequalTo_(p1, p2, eps=EPS): # underscore_!
1296 '''(INTERNAL) Compare 2 point phi-/lams ignoring C{class}.
1297 '''
1298 return (fabs(p1.phi - p2.phi) <= eps and
1299 fabs(p1.lam - p2.lam) <= eps) if eps else (p1.philam == p2.philam)
1302def isnormal(lat, lon, eps=0):
1303 '''Check whether B{C{lat}} I{and} B{C{lon}} are within their
1304 respective I{normal} range in C{degrees}.
1306 @arg lat: Latitude (C{degrees}).
1307 @arg lon: Longitude (C{degrees}).
1308 @kwarg eps: Optional tolerance C{degrees}).
1310 @return: C{True} if C{(abs(B{lat}) + B{eps}) <= 90} and
1311 C{(abs(B{lon}) + B{eps}) <= 180}, C{False} otherwise.
1313 @see: Functions L{isnormal_} and L{normal}.
1314 '''
1315 return _loneg(fabs(lon)) >= eps and (_90_0 - fabs(lat)) >= eps # co-latitude
1318def isnormal_(phi, lam, eps=0):
1319 '''Check whether B{C{phi}} I{and} B{C{lam}} are within their
1320 respective I{normal} range in C{radians}.
1322 @arg phi: Latitude (C{radians}).
1323 @arg lam: Longitude (C{radians}).
1324 @kwarg eps: Optional tolerance C{radians}).
1326 @return: C{True} if C{(abs(B{phi}) + B{eps}) <= PI/2} and
1327 C{(abs(B{lam}) + B{eps}) <= PI}, C{False} othwerwise.
1329 @see: Functions L{isnormal} and L{normal_}.
1330 '''
1331 return (PI_2 - fabs(phi)) >= eps and (PI - fabs(lam)) >= eps
1334def _maprod(fun_, *ts):
1335 '''(INTERNAL) Helper for C{excessCagnoli_} and C{excessLHuilier_}.
1336 '''
1337 return fprod(map(fun_, ts))
1340def _normal2(a, b, n_2, n, n2):
1341 '''(INTERNAL) Helper for C{normal} and C{normal_}.
1342 '''
1343 if fabs(b) > n:
1344 b = remainder(b, n2)
1345 if fabs(a) > n_2:
1346 r = remainder(a, n)
1347 if r != a:
1348 a = -r
1349 b -= n if b > 0 else -n
1350 return float0_(a, b)
1353def normal(lat, lon, **name):
1354 '''Normalize a lat- I{and} longitude pair in C{degrees}.
1356 @arg lat: Latitude (C{degrees}).
1357 @arg lon: Longitude (C{degrees}).
1358 @kwarg name: Optional C{B{name}="normal"} (C{str}).
1360 @return: L{LatLon2Tuple}C{(lat, lon)} with C{-90 <= lat <= 90}
1361 and C{-180 <= lon <= 180}.
1363 @see: Functions L{normal_} and L{isnormal}.
1364 '''
1365 return LatLon2Tuple(*_normal2(lat, lon, _90_0, _180_0, _360_0),
1366 name=_name__(name, name__=normal))
1369def normal_(phi, lam, **name):
1370 '''Normalize a lat- I{and} longitude pair in C{radians}.
1372 @arg phi: Latitude (C{radians}).
1373 @arg lam: Longitude (C{radians}).
1374 @kwarg name: Optional C{B{name}="normal_"} (C{str}).
1376 @return: L{PhiLam2Tuple}C{(phi, lam)} with C{abs(phi) <= PI/2}
1377 and C{abs(lam) <= PI}.
1379 @see: Functions L{normal} and L{isnormal_}.
1380 '''
1381 return PhiLam2Tuple(*_normal2(phi, lam, PI_2, PI, PI2),
1382 name=_name__(name, name__=normal_))
1385def _opposes(d, m, n, n2):
1386 '''(INTERNAL) Helper for C{opposing} and C{opposing_}.
1387 '''
1388 d = d % n2 # -20 % 360 == 340, -1 % PI2 == PI2 - 1
1389 return False if d < m or d > (n2 - m) else (
1390 True if (n - m) < d < (n + m) else None)
1393def opposing(bearing1, bearing2, margin=_90_0):
1394 '''Compare the direction of two bearings given in C{degrees}.
1396 @arg bearing1: First bearing (compass C{degrees}).
1397 @arg bearing2: Second bearing (compass C{degrees}).
1398 @kwarg margin: Optional, interior angle bracket (C{degrees}).
1400 @return: C{True} if both bearings point in opposite, C{False} if
1401 in similar or C{None} if in I{perpendicular} directions.
1403 @see: Function L{opposing_}.
1404 '''
1405 m = Degrees_(margin=margin, low=EPS0, high=_90_0)
1406 return _opposes(bearing2 - bearing1, m, _180_0, _360_0)
1409def opposing_(radians1, radians2, margin=PI_2):
1410 '''Compare the direction of two bearings given in C{radians}.
1412 @arg radians1: First bearing (C{radians}).
1413 @arg radians2: Second bearing (C{radians}).
1414 @kwarg margin: Optional, interior angle bracket (C{radians}).
1416 @return: C{True} if both bearings point in opposite, C{False} if
1417 in similar or C{None} if in perpendicular directions.
1419 @see: Function L{opposing}.
1420 '''
1421 m = Radians_(margin=margin, low=EPS0, high=PI_2)
1422 return _opposes(radians2 - radians1, m, PI, PI2)
1425def _Propy(func, nargs, kwds):
1426 '''(INTERNAL) Helper for the C{frechet.[-]Frechet**} and
1427 C{hausdorff.[-]Hausdorff*} classes.
1428 '''
1429 try:
1430 _xcallable(distance=func)
1431 # assert _args_kwds_count2(func)[0] == nargs + int(ismethod(func))
1432 _ = func(*_0_0s(nargs), **kwds)
1433 except Exception as x:
1434 t = unstr(func, **kwds)
1435 raise _TypeError(t, cause=x)
1436 return func
1439def _radical2(d, r1, r2, **name): # in .ellipsoidalBaseDI, .sphericalTrigonometry, .vector3d
1440 # (INTERNAL) See C{radical2} below
1441 # assert d > EPS0
1442 r = fsumf_(_1_0, (r1 / d)**2, -(r2 / d)**2) * _0_5
1443 n = _name__(name, name__=radical2)
1444 return Radical2Tuple(max(_0_0, min(_1_0, r)), r * d, name=n)
1447def radical2(distance, radius1, radius2, **name):
1448 '''Compute the I{radical ratio} and I{radical line} of two U{intersecting
1449 circles<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>}.
1451 The I{radical line} is perpendicular to the axis thru the centers of
1452 the circles at C{(0, 0)} and C{(B{distance}, 0)}.
1454 @arg distance: Distance between the circle centers (C{scalar}).
1455 @arg radius1: Radius of the first circle (C{scalar}).
1456 @arg radius2: Radius of the second circle (C{scalar}).
1457 @kwarg name: Optional C{B{name}=NN} (C{str}).
1459 @return: A L{Radical2Tuple}C{(ratio, xline)} where C{0.0 <= ratio <=
1460 1.0} and C{xline} is along the B{C{distance}}.
1462 @raise IntersectionError: The B{C{distance}} exceeds the sum of
1463 B{C{radius1}} and B{C{radius2}}.
1465 @raise UnitError: Invalid B{C{distance}}, B{C{radius1}} or B{C{radius2}}.
1466 '''
1467 d = Distance_(distance, low=_0_0)
1468 r1 = Radius_(radius1=radius1)
1469 r2 = Radius_(radius2=radius2)
1470 if d > (r1 + r2):
1471 raise IntersectionError(distance=d, radius1=r1, radius2=r2,
1472 txt=_too_(_distant_))
1473 return _radical2(d, r1, r2, **name) if d > EPS0 else \
1474 Radical2Tuple(_0_5, _0_0, **name)
1477class Radical2Tuple(_NamedTuple):
1478 '''2-Tuple C{(ratio, xline)} of the I{radical} C{ratio} and
1479 I{radical} C{xline}, both C{scalar} and C{0.0 <= ratio <= 1.0}
1480 '''
1481 _Names_ = (_ratio_, _xline_)
1482 _Units_ = ( Scalar, Scalar)
1485def _radistance(inst):
1486 '''(INTERNAL) Helper for the L{frechet._FrechetMeterRadians}
1487 and L{hausdorff._HausdorffMeterRedians} classes.
1488 '''
1489 wrap_, kwds_ = _xkwds_pop2(inst._kwds, wrap=False)
1490 func_ = inst._func_
1491 try: # calling lower-overhead C{func_}
1492 func_(0, _0_25, _0_5, **kwds_)
1493 wrap_ = _Wrap._philamop(wrap_)
1494 except TypeError:
1495 return inst.distance
1497 def _philam(p):
1498 try:
1499 return p.phi, p.lam
1500 except AttributeError: # no .phi or .lam
1501 return radians(p.lat), radians(p.lon)
1503 def _func_wrap(point1, point2):
1504 phi1, lam1 = wrap_(*_philam(point1))
1505 phi2, lam2 = wrap_(*_philam(point2))
1506 return func_(phi2, phi1, lam2 - lam1, **kwds_)
1508 inst._units = inst._units_
1509 return _func_wrap
1512def _scale_deg(lat1, lat2): # degrees
1513 # scale factor cos(mean of lats) for delta lon
1514 m = fabs(lat1 + lat2) * _0_5
1515 return cos(radians(m)) if m < _90_0 else _0_0
1518def _scale_rad(phi1, phi2): # radians, by .frechet, .hausdorff, .heights
1519 # scale factor cos(mean of phis) for delta lam
1520 m = fabs(phi1 + phi2) * _0_5
1521 return cos(m) if m < PI_2 else _0_0
1524def _sincosa6(phi2, phi1, lam21): # [4] in cosineLaw
1525 '''(INTERNAL) C{sin}es, C{cos}ines and C{acos}ine.
1526 '''
1527 s2, c2, s1, c1, _, c21 = sincos2_(phi2, phi1, lam21)
1528 return s2, c2, s1, c1, acos1(s1 * s2 + c1 * c2 * c21), c21
1531def thomas(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False):
1532 '''Compute the distance between two (ellipsoidal) points using U{Thomas'
1533 <https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} formula.
1535 @arg lat1: Start latitude (C{degrees}).
1536 @arg lon1: Start longitude (C{degrees}).
1537 @arg lat2: End latitude (C{degrees}).
1538 @arg lon2: End longitude (C{degrees}).
1539 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2}
1540 or L{a_f2Tuple}) to use.
1541 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
1542 B{C{lon2}} (C{bool}).
1544 @return: Distance (C{meter}, same units as the B{C{datum}}'s or ellipsoid axes).
1546 @raise TypeError: Invalid B{C{datum}}.
1548 @see: Functions L{thomas_}, L{cosineLaw}, L{equirectangular}, L{euclidean},
1549 L{flatLocal} / L{hubeny}, L{flatPolar}, L{haversine}, L{vincentys} and
1550 method L{Ellipsoid.distance2}.
1551 '''
1552 return _dE(thomas_, datum, wrap, lat1, lon1, lat2, lon2)
1555def thomas_(phi2, phi1, lam21, datum=_WGS84):
1556 '''Compute the I{angular} distance between two (ellipsoidal) points using
1557 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} formula.
1559 @arg phi2: End latitude (C{radians}).
1560 @arg phi1: Start latitude (C{radians}).
1561 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
1562 @kwarg datum: Datum (L{Datum}) ?or ellipsoid to use (L{Ellipsoid},
1563 L{Ellipsoid2} or L{a_f2Tuple}).
1565 @return: Angular distance (C{radians}).
1567 @raise TypeError: Invalid B{C{datum}}.
1569 @see: Functions L{thomas}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /
1570 L{hubeny_}, L{flatPolar_}, L{haversine_} and L{vincentys_} and
1571 U{Geodesy-PHP<https://GitHub.com/jtejido/geodesy-php/blob/master/
1572 src/Geodesy/Distance/ThomasFormula.php>}.
1573 '''
1574 s2, c2, s1, c1, r, _ = _sincosa6(phi2, phi1, lam21)
1575 if r and isnon0(c1) and isnon0(c2):
1576 E = _ellipsoidal(datum, thomas_)
1577 f = E.f * _0_25
1578 if f: # ellipsoidal
1579 r1 = atan2(E.b_a * s1, c1)
1580 r2 = atan2(E.b_a * s2, c2)
1582 j = (r2 + r1) * _0_5
1583 k = (r2 - r1) * _0_5
1584 sj, cj, sk, ck, h, _ = sincos2_(j, k, lam21 * _0_5)
1586 h = fsumf_(sk**2, (ck * h)**2, -(sj * h)**2)
1587 u = _1_0 - h
1588 if isnon0(u) and isnon0(h):
1589 r = atan(sqrt0(h / u)) * 2 # == acos(1 - 2 * h)
1590 sr, cr = sincos2(r)
1591 if isnon0(sr):
1592 u = (sj * ck)**2 * 2 / u
1593 h = (sk * cj)**2 * 2 / h
1594 x = u + h
1595 y = u - h
1597 b = r * 2
1598 s = r / sr
1599 e = 4 * s**2
1600 d = 2 * cr
1601 a = e * d
1602 c = s - (a - d) * _0_5
1604 t = fdot_(a, x, -b, y, -d, y**2, c, x**2, e, x * y) * _0_25
1605 r -= fdot_(s, x, -1, y, -t, f) * f * sr
1606 return r
1609def vincentys(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
1610 '''Compute the distance between two (spherical) points using
1611 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}
1612 spherical formula.
1614 @arg lat1: Start latitude (C{degrees}).
1615 @arg lon1: Start longitude (C{degrees}).
1616 @arg lat2: End latitude (C{degrees}).
1617 @arg lon2: End longitude (C{degrees}).
1618 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or
1619 ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple})
1620 to use.
1621 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
1622 B{C{lon2}} (C{bool}).
1624 @return: Distance (C{meter}, same units as B{C{radius}}).
1626 @raise UnitError: Invalid B{C{radius}}.
1628 @see: Functions L{vincentys_}, L{cosineLaw}, L{equirectangular}, L{euclidean},
1629 L{flatLocal}/L{hubeny}, L{flatPolar}, L{haversine} and L{thomas} and
1630 methods L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and
1631 C{LatLon.equirectangularTo}.
1633 @note: See note at function L{vincentys_}.
1634 '''
1635 return _dS(vincentys_, radius, wrap, lat1, lon1, lat2, lon2)
1638def vincentys_(phi2, phi1, lam21):
1639 '''Compute the I{angular} distance between two (spherical) points using
1640 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}
1641 spherical formula.
1643 @arg phi2: End latitude (C{radians}).
1644 @arg phi1: Start latitude (C{radians}).
1645 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
1647 @return: Angular distance (C{radians}).
1649 @see: Functions L{vincentys}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /
1650 L{hubeny_}, L{flatPolar_}, L{haversine_} and L{thomas_}.
1652 @note: Functions L{vincentys_}, L{haversine_} and L{cosineLaw_} produce
1653 equivalent results, but L{vincentys_} is suitable for antipodal
1654 points and slightly more expensive (M{3 cos, 3 sin, 1 hypot, 1 atan2,
1655 6 mul, 2 add}) than L{haversine_} (M{2 cos, 2 sin, 2 sqrt, 1 atan2, 5
1656 mul, 1 add}) and L{cosineLaw_} (M{3 cos, 3 sin, 1 acos, 3 mul, 1 add}).
1657 '''
1658 s1, c1, s2, c2, s21, c21 = sincos2_(phi1, phi2, lam21)
1660 c = c2 * c21
1661 x = s1 * s2 + c1 * c
1662 y = c1 * s2 - s1 * c
1663 return atan2(hypot(c2 * s21, y), x)
1665# **) MIT License
1666#
1667# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.
1668#
1669# Permission is hereby granted, free of charge, to any person obtaining a
1670# copy of this software and associated documentation files (the "Software"),
1671# to deal in the Software without restriction, including without limitation
1672# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1673# and/or sell copies of the Software, and to permit persons to whom the
1674# Software is furnished to do so, subject to the following conditions:
1675#
1676# The above copyright notice and this permission notice shall be included
1677# in all copies or substantial portions of the Software.
1678#
1679# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1680# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1681# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1682# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1683# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1684# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1685# OTHER DEALINGS IN THE SOFTWARE.