Coverage for pygeodesy/formy.py: 98%
448 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-29 12:40 -0400
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-29 12:40 -0400
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 _ # noqa: E702 ;
9from pygeodesy.basics import _copysign, _isin # _args_kwds_count2
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
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 typename # 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, typename
27from pygeodesy.namedTuples import Bearing2Tuple, Distance4Tuple, LatLon2Tuple, \
28 Intersection3Tuple, PhiLam2Tuple
29# from pygeodesy.streprs import Fmt, unstr # from .fsums
30# from pygeodesy.triaxials import _hartzell3 # _MODS
31from pygeodesy.units import _isDegrees, _isHeight, _isRadius, Bearing, Degrees_, \
32 Distance, Distance_, Height, Lamd, Lat, Lon, Meter_, \
33 Phid, Radians, Radians_, Radius, Radius_, Scalar, _100km
34from pygeodesy.utily import acos1, asin1, atan2, atan2b, degrees2m, hav, _loneg, \
35 m2degrees, tan_2, sincos2, sincos2_, _Wrap
36# from pygeodesy.vector3d import _otherV3d # _MODS
37# from pygeodesy.vector3dBase import _xyz_y_z3 # _MODS
38# from pygeodesy import ellipsoidalExact, ellipsoidalKarney, vector3d, \
39# sphericalNvector, sphericalTrigonometry # _MODS
41from contextlib import contextmanager
42from math import atan, cos, degrees, fabs, radians, sin, sqrt # pow
44__all__ = _ALL_LAZY.formy
45__version__ = '25.05.12'
47_RADIANS2 = radians(_1_0)**2 # degree to radians-squared
48_ratio_ = 'ratio'
49_xline_ = 'xline'
52def angle2chord(rad, radius=R_M):
53 '''Get the chord length of a (central) angle or I{angular} distance.
55 @arg rad: Central angle (C{radians}).
56 @kwarg radius: Mean earth radius (C{meter}, conventionally), datum (L{Datum}) or ellipsoid
57 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use or C{None}.
59 @return: Chord length (C{meter}, same units as B{C{radius}} or if C{B{radius} is None}, C{radians}).
61 @see: Function L{chord2angle}, method L{intermediateChordTo<sphericalNvector.LatLon.intermediateChordTo>} and
62 U{great-circle-distance<https://WikiPedia.org/wiki/Great-circle_distance#Relation_between_central_angle_and_chord_length>}.
63 '''
64 d = _isDegrees(rad, iscalar=False)
65 r = sin((radians(rad) if d else rad) / _2_0) * _2_0
66 return (degrees(r) if d else r) if radius is None else (_mean_radius(radius) * r)
69def _anti2(a, b, n_2, n, n2):
70 '''(INTERNAL) Helper for C{antipode} and C{antipode_}.
71 '''
72 r = remainder(a, n) if fabs(a) > n_2 else a
73 if r == a:
74 r = -r
75 b += n
76 if fabs(b) > n:
77 b = remainder(b, n2)
78 return float0_(r, b)
81def antipode(lat, lon, **name):
82 '''Return the antipode, the point diametrically opposite to a given
83 point in C{degrees}.
85 @arg lat: Latitude (C{degrees}).
86 @arg lon: Longitude (C{degrees}).
87 @kwarg name: Optional C{B{name}=NN} (C{str}).
89 @return: A L{LatLon2Tuple}C{(lat, lon)}.
91 @see: Functions L{antipode_} and L{normal} and U{Geosphere
92 <https://CRAN.R-Project.org/web/packages/geosphere/geosphere.pdf>}.
93 '''
94 return LatLon2Tuple(*_anti2(lat, lon, _90_0, _180_0, _360_0), **name)
97def antipode_(phi, lam, **name):
98 '''Return the antipode, the point diametrically opposite to a given
99 point in C{radians}.
101 @arg phi: Latitude (C{radians}).
102 @arg lam: Longitude (C{radians}).
103 @kwarg name: Optional C{B{name}=NN} (C{str}).
105 @return: A L{PhiLam2Tuple}C{(phi, lam)}.
107 @see: Functions L{antipode} and L{normal_} and U{Geosphere
108 <https://CRAN.R-Project.org/web/packages/geosphere/geosphere.pdf>}.
109 '''
110 return PhiLam2Tuple(*_anti2(phi, lam, PI_2, PI, PI2), **name)
113def bearing(lat1, lon1, lat2, lon2, **final_wrap):
114 '''Compute the initial or final bearing (forward or reverse azimuth) between two
115 (spherical) points.
117 @arg lat1: Start latitude (C{degrees}).
118 @arg lon1: Start longitude (C{degrees}).
119 @arg lat2: End latitude (C{degrees}).
120 @arg lon2: End longitude (C{degrees}).
121 @kwarg final_wrap: Optional keyword arguments for function L{pygeodesy.bearing_}.
123 @return: Initial or final bearing (compass C{degrees360}) or zero if both points
124 coincide.
125 '''
126 r = bearing_(Phid(lat1=lat1), Lamd(lon1=lon1),
127 Phid(lat2=lat2), Lamd(lon2=lon2), **final_wrap)
128 return degrees(r)
131def bearing_(phi1, lam1, phi2, lam2, final=False, wrap=False):
132 '''Compute the initial or final bearing (forward or reverse azimuth) between two
133 (spherical) points.
135 @arg phi1: Start latitude (C{radians}).
136 @arg lam1: Start longitude (C{radians}).
137 @arg phi2: End latitude (C{radians}).
138 @arg lam2: End longitude (C{radians}).
139 @kwarg final: If C{True}, return the final, otherwise the initial bearing (C{bool}).
140 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{phi2}} and B{C{lam2}}
141 (C{bool}).
143 @return: Initial or final bearing (compass C{radiansPI2}) or zero if both points
144 coincide.
146 @see: U{Bearing<https://www.Movable-Type.co.UK/scripts/latlong.html>}, U{Course
147 between two points<https://www.EdWilliams.org/avform147.htm#Crs>} and
148 U{Bearing Between Two Points<https://web.Archive.org/web/20020630205931/
149 https://MathForum.org/library/drmath/view/55417.html>}.
150 '''
151 db, phi2, lam2 = _Wrap.philam3(lam1, phi2, lam2, wrap)
152 if final: # swap plus PI
153 phi1, lam1, phi2, lam2, db = phi2, lam2, phi1, lam1, -db
154 r = PI3
155 else:
156 r = PI2
157 sa1, ca1, sa2, ca2, sdb, cdb = sincos2_(phi1, phi2, db)
159 x = ca1 * sa2 - sa1 * ca2 * cdb
160 y = sdb * ca2
161 return _umod_PI2(atan2(y, x) + r) # .utily.wrapPI2
164def _bearingTo2(p1, p2, wrap=False): # for points.ispolar, sphericalTrigonometry.areaOf
165 '''(INTERNAL) Compute initial and final bearing.
166 '''
167 try: # for LatLon_ and ellipsoidal LatLon
168 return p1.bearingTo2(p2, wrap=wrap)
169 except AttributeError:
170 pass
171 # XXX spherical version, OK for ellipsoidal ispolar?
172 t = p1.philam + p2.philam
173 i = bearing_(*t, final=False, wrap=wrap)
174 f = bearing_(*t, final=True, wrap=wrap)
175 return Bearing2Tuple(degrees(i), degrees(f),
176 name__=_bearingTo2)
179def chord2angle(chord, radius=R_M):
180 '''Get the (central) angle from a chord length or distance.
182 @arg chord: Length or distance (C{meter}, same units as B{C{radius}}).
183 @kwarg radius: Mean earth radius (C{meter}, conventionally), datum (L{Datum}) or
184 ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
186 @return: Angle (C{radians} with sign of B{C{chord}}) or C{0} if C{B{radius}=0}.
188 @note: The angle will exceed C{PI} if C{B{chord} > B{radius} * 2}.
190 @see: Function L{angle2chord}.
191 '''
192 m = _mean_radius(radius)
193 r = fabs(chord / (m * _2_0)) if m > 0 else _0_0
194 if r:
195 i = int(r)
196 if i > 0:
197 r -= i
198 i *= PI
199 r = (asin1(r) + i) * _2_0
200 return _copysign(r, chord)
203def compassAngle(lat1, lon1, lat2, lon2, adjust=True, wrap=False):
204 '''Return the angle from North for the direction vector M{(lon2 - lon1,
205 lat2 - lat1)} between two points.
207 Suitable only for short, not near-polar vectors up to a few hundred
208 Km or Miles. Use function L{pygeodesy.bearing} for longer vectors.
210 @arg lat1: From latitude (C{degrees}).
211 @arg lon1: From longitude (C{degrees}).
212 @arg lat2: To latitude (C{degrees}).
213 @arg lon2: To longitude (C{degrees}).
214 @kwarg adjust: Adjust the longitudinal delta by the cosine of the mean
215 latitude (C{bool}).
216 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
217 B{C{lon2}} (C{bool}).
219 @return: Compass angle from North (C{degrees360}).
221 @note: Courtesy of Martin Schultz.
223 @see: U{Local, flat earth approximation
224 <https://www.EdWilliams.org/avform.htm#flat>}.
225 '''
226 d_lon, lat2, lon2 = _Wrap.latlon3(lon1, lat2, lon2, wrap)
227 if adjust: # scale delta lon
228 d_lon *= _scale_deg(lat1, lat2)
229 return atan2b(d_lon, lat2 - lat1)
232def cosineLaw(lat1, lon1, lat2, lon2, corr=0, earth=None, wrap=False,
233 datum=_WGS84, radius=R_M):
234 '''Compute the distance between two points using the U{Law of Cosines
235 <https://www.Movable-Type.co.UK/scripts/latlong.html#cosine-law>}
236 formula, optionally corrected.
238 @arg lat1: Start latitude (C{degrees}).
239 @arg lon1: Start longitude (C{degrees}).
240 @arg lat2: End latitude (C{degrees}).
241 @arg lon2: End longitude (C{degrees}).
242 @kwarg corr: Use C{B{corr}=2} to apply the U{Forsythe-Andoyer-Lambert
243 <https://www2.UNB.CA/gge/Pubs/TR77.pdf>}, C{B{corr}=1} for the
244 U{Andoyer-Lambert<https://Books.Google.com/books?id=x2UiAQAAIAAJ>}
245 corrected (ellipsoidal) or keep C{B{corr}=0} for the uncorrected
246 (spherical) C{Law of Cosines} formula (C{int}).
247 @kwarg earth: Mean earth radius (C{meter}) or datum (L{Datum}) or ellipsoid
248 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
249 @kwarg wrap: If C{True}, wrap or I{normalize} and B{C{lat2}} and B{C{lon2}}
250 (C{bool}).
251 @kwarg datum: Default ellipsiodal B{C{earth}} (and for backward compatibility).
252 @kwarg radius: Default spherical B{C{earth}} (and for backward compatibility).
254 @return: Distance (C{meter}, same units as B{C{radius}} or the datum's or
255 ellipsoid axes).
257 @raise TypeError: Invalid B{C{earth}}, B{C{datum}} or B{C{radius}}.
259 @raise ValueError: Invalid B{C{corr}}.
261 @see: Functions L{cosineLaw_}, L{equirectangular}, L{euclidean}, L{flatLocal} /
262 L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys} and
263 method L{Ellipsoid.distance2}.
265 @note: See note at function L{vincentys_}.
266 '''
267 return _dE(cosineLaw_, earth or datum, wrap, lat1, lon1, lat2, lon2, corr=corr) if corr else \
268 _dS(cosineLaw_, earth or radius, wrap, lat1, lon1, lat2, lon2)
271def cosineLaw_(phi2, phi1, lam21, corr=0, earth=None, datum=_WGS84):
272 '''Compute the I{angular} distance between two points using the U{Law of Cosines
273 <https://www.Movable-Type.co.UK/scripts/latlong.html#cosine-law>} formula,
274 optionally corrected.
276 @arg phi2: End latitude (C{radians}).
277 @arg phi1: Start latitude (C{radians}).
278 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
279 @kwarg corr: Use C{B{corr}=2} to apply the U{Forsythe-Andoyer-Lambert
280 <https://www2.UNB.CA/gge/Pubs/TR77.pdf>}, C{B{corr}=1} for the
281 U{Andoyer-Lambert<https://Books.Google.com/books?id=x2UiAQAAIAAJ>}
282 corrected (ellipsoidal) or keep C{B{corr}=0} for the uncorrected
283 (spherical) C{Law of Cosines} formula (C{int}).
284 @kwarg earth: Mean earth radius (C{meter}) or datum (L{Datum}) or ellipsoid
285 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
286 @kwarg datum: Default ellipsoidal B{C{earth}} (and for backward compatibility).
288 @return: Angular distance (C{radians}).
290 @raise TypeError: Invalid B{C{earth}} or B{C{datum}}.
292 @raise ValueError: Invalid B{C{corr}}.
294 @see: Functions L{cosineLaw}, L{euclidean_}, L{flatLocal_} / L{hubeny_},
295 L{flatPolar_}, L{haversine_}, L{thomas_} and L{vincentys_} and U{Geodesy-PHP
296 <https://GitHub.com/jtejido/geodesy-php/blob/master/src/Geodesy/Distance/
297 AndoyerLambert.php>}.
298 '''
299 s2, c2, s1, c1, r, c21 = _sincosa6(phi2, phi1, lam21)
300 if corr and isnon0(c1) and isnon0(c2):
301 E = _ellipsoidal(earth or datum, cosineLaw_)
302 f = _0_25 * E.f
303 if f: # ellipsoidal
304 if corr == 1: # Andoyer-Lambert
305 r2 = atan2(E.b_a * s2, c2)
306 r1 = atan2(E.b_a * s1, c1)
307 s2, c2, s1, c1 = sincos2_(r2, r1)
308 r = acos1(s1 * s2 + c1 * c2 * c21)
309 if r:
310 sr, _, sr_2, cr_2 = sincos2_(r, r * _0_5)
311 if isnon0(sr_2) and isnon0(cr_2):
312 s = (sr + r) * ((s1 - s2) / sr_2)**2
313 c = (sr - r) * ((s1 + s2) / cr_2)**2
314 r += (c - s) * _0_5 * f
316 elif corr == 2: # Forsythe-Andoyer-Lambert
317 sr, cr, s2r, _ = sincos2_(r, r * 2)
318 if isnon0(sr) and fabs(cr) < EPS1:
319 s = (s1 + s2)**2 / (_1_0 + cr)
320 t = (s1 - s2)**2 / (_1_0 - cr)
321 x = s + t
322 y = s - t
324 s = 8 * r**2 / sr
325 a = 64 * r + s * cr * 2 # 16 * r**2 / tan(r)
326 d = 48 * sr + s # 8 * r**2 / tan(r)
327 b = -2 * d
328 e = 30 * s2r
330 c = fdot_(30, r, cr, s, e, _0_5) # 8 * r**2 / tan(r)
331 t = fdot_( a, x, b, y, e, y**2, -c, x**2, d, x * y) * _0_125
332 r += fdot_(-r, x, sr, y * 3, t, f) * f
333 else:
334 raise _ValueError(corr=corr)
335 return r
338def _d3(wrap, lat1, lon1, lat2, lon2):
339 '''(INTERNAL) Helper for _dE, _dS, ....
340 '''
341 if wrap:
342 d_lon, lat2, _ = _Wrap.latlon3(lon1, lat2, lon2, wrap)
343 return radians(lat2), Phid(lat1=lat1), radians(d_lon)
344 else: # for backward compaibility
345 return Phid(lat2=lat2), Phid(lat1=lat1), radians(lon2 - lon1)
348def _dE(fun_, earth, wrap, *lls, **corr):
349 '''(INTERNAL) Helper for ellipsoidal distances.
350 '''
351 E = _ellipsoidal(earth, fun_)
352 r = fun_(*_d3(wrap, *lls), datum=E, **corr)
353 return r * E.a
356def _dS(fun_, radius, wrap, *lls, **adjust):
357 '''(INTERNAL) Helper for spherical distances.
358 '''
359 r = fun_(*_d3(wrap, *lls), **adjust)
360 if radius is not R_M:
361 try: # datum?
362 radius = radius.ellipsoid.R1
363 except AttributeError:
364 pass # scalar?
365 lat1, _, lat2, _ = lls
366 radius = _mean_radius(radius, lat1, lat2)
367 return r * radius
370def _ellipsoidal(earth, where):
371 '''(INTERNAL) Helper for distances.
372 '''
373 return _EWGS84 if _isin(earth, _EWGS84, _WGS84) else (
374 earth if isinstance(earth, Ellipsoid) else
375 (earth if isinstance(earth, Datum) else # PYCHOK indent
376 _ellipsoidal_datum(earth, name__=where)).ellipsoid)
379def equirectangular(lat1, lon1, lat2, lon2, radius=R_M, **adjust_limit_wrap):
380 '''Approximate the distance between two points using the U{Equirectangular Approximation
381 / Projection<https://www.Movable-Type.co.UK/scripts/latlong.html#equirectangular>}.
383 @arg lat1: Start latitude (C{degrees}).
384 @arg lon1: Start longitude (C{degrees}).
385 @arg lat2: End latitude (C{degrees}).
386 @arg lon2: End longitude (C{degrees}).
387 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
388 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}).
389 @kwarg adjust_limit_wrap: Optionally, keyword arguments for function L{equirectangular4}.
391 @return: Distance (C{meter}, same units as B{C{radius}} or the datum's
392 ellipsoid axes).
394 @raise TypeError: Invalid B{C{radius}}.
396 @see: Function L{equirectangular4} for more details, the available B{C{options}},
397 errors, restrictions and other, approximate or accurate distance functions.
398 '''
399 r = _mean_radius(radius, lat1, lat2)
400 t = equirectangular4(Lat(lat1=lat1), Lon(lon1=lon1),
401 Lat(lat2=lat2), Lon(lon2=lon2),
402 **adjust_limit_wrap) # PYCHOK 4 vs 2-3
403 return degrees2m(sqrt(t.distance2), radius=r)
406def _equirectangular(lat1, lon1, lat2, lon2, **adjust_limit_wrap):
407 '''(INTERNAL) Helper for classes L{frechet._FrechetMeterRadians} and
408 L{hausdorff._HausdorffMeterRedians}.
409 '''
410 t = equirectangular4(lat1, lon1, lat2, lon2, **adjust_limit_wrap)
411 return t.distance2 * _RADIANS2
414def equirectangular4(lat1, lon1, lat2, lon2, adjust=True, limit=45, wrap=False):
415 '''Approximate the distance between two points using the U{Equirectangular Approximation
416 / Projection<https://www.Movable-Type.co.UK/scripts/latlong.html#equirectangular>}.
418 This approximation is valid for short distance of several hundred Km or Miles, see
419 the B{C{limit}} keyword argument and L{LimitError}.
421 @arg lat1: Start latitude (C{degrees}).
422 @arg lon1: Start longitude (C{degrees}).
423 @arg lat2: End latitude (C{degrees}).
424 @arg lon2: End longitude (C{degrees}).
425 @kwarg adjust: Adjust the wrapped, unrolled longitudinal delta by the cosine of the
426 mean latitude (C{bool}).
427 @kwarg limit: Optional limit for lat- and longitudinal deltas (C{degrees}) or C{None}
428 or C{0} for unlimited.
429 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and B{C{lon2}}
430 (C{bool}).
432 @return: A L{Distance4Tuple}C{(distance2, delta_lat, delta_lon, unroll_lon2)} with
433 C{distance2} in C{degrees squared}.
435 @raise LimitError: The lat- or longitudinal delta exceeds the B{C{-limit..limit}}
436 range and L{limiterrors<pygeodesy.limiterrors>} is C{True}.
438 @see: U{Local, flat earth approximation<https://www.EdWilliams.org/avform.htm#flat>},
439 functions L{equirectangular}, L{cosineLaw}, L{euclidean}, L{flatLocal} /
440 L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys} and methods
441 L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}.
442 '''
443 if wrap:
444 d_lon, lat2, ulon2 = _Wrap.latlon3(lon1, lat2, lon2, wrap)
445 else:
446 d_lon, ulon2 = (lon2 - lon1), lon2
447 d_lat = lat2 - lat1
449 if limit and limit > 0 and limiterrors():
450 d = max(fabs(d_lat), fabs(d_lon))
451 if d > limit:
452 t = _SPACE_(_delta_, Fmt.PAREN_g(d), Fmt.exceeds_limit(limit))
453 s = unstr(equirectangular4, lat1, lon1, lat2, lon2,
454 limit=limit, wrap=wrap)
455 raise LimitError(s, txt=t)
457 if adjust: # scale delta lon
458 d_lon *= _scale_deg(lat1, lat2)
460 d2 = hypot2(d_lat, d_lon) # degrees squared!
461 return Distance4Tuple(d2, d_lat, d_lon, ulon2 - lon2)
464def euclidean(lat1, lon1, lat2, lon2, radius=R_M, adjust=True, wrap=False):
465 '''Approximate the C{Euclidean} distance between two (spherical) points.
467 @arg lat1: Start latitude (C{degrees}).
468 @arg lon1: Start longitude (C{degrees}).
469 @arg lat2: End latitude (C{degrees}).
470 @arg lon2: End longitude (C{degrees}).
471 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
472 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
473 @kwarg adjust: Adjust the longitudinal delta by the cosine of the mean
474 latitude (C{bool}).
475 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
476 B{C{lon2}} (C{bool}).
478 @return: Distance (C{meter}, same units as B{C{radius}} or the ellipsoid
479 or datum axes).
481 @raise TypeError: Invalid B{C{radius}}.
483 @see: U{Distance between two (spherical) points
484 <https://www.EdWilliams.org/avform.htm#Dist>}, functions L{euclid},
485 L{euclidean_}, L{cosineLaw}, L{equirectangular}, L{flatLocal} /
486 L{hubeny}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys}
487 and methods L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and
488 C{LatLon.equirectangularTo}.
489 '''
490 return _dS(euclidean_, radius, wrap, lat1, lon1, lat2, lon2, adjust=adjust)
493def euclidean_(phi2, phi1, lam21, adjust=True):
494 '''Approximate the I{angular} C{Euclidean} distance between two (spherical) points.
496 @arg phi2: End latitude (C{radians}).
497 @arg phi1: Start latitude (C{radians}).
498 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
499 @kwarg adjust: Adjust the longitudinal delta by the cosine of the mean
500 latitude (C{bool}).
502 @return: Angular distance (C{radians}).
504 @see: Functions L{euclid}, L{euclidean}, L{cosineLaw_}, L{flatLocal_} /
505 L{hubeny_}, L{flatPolar_}, L{haversine_}, L{thomas_} and L{vincentys_}.
506 '''
507 if adjust:
508 lam21 *= _scale_rad(phi2, phi1)
509 return euclid(phi2 - phi1, lam21)
512def excessAbc_(A, b, c):
513 '''Compute the I{spherical excess} C{E} of a (spherical) triangle from two sides
514 and the included (small) angle.
516 @arg A: An interior triangle angle (C{radians}).
517 @arg b: Frist adjacent triangle side (C{radians}).
518 @arg c: Second adjacent triangle side (C{radians}).
520 @return: Spherical excess (C{radians}).
522 @raise UnitError: Invalid B{C{A}}, B{C{b}} or B{C{c}}.
524 @see: Functions L{excessGirard_}, L{excessLHuilier_} and U{Spherical
525 trigonometry<https://WikiPedia.org/wiki/Spherical_trigonometry>}.
526 '''
527 A = Radians_(A=A)
528 b = Radians_(b=b) * _0_5
529 c = Radians_(c=c) * _0_5
531 sA, cA, sb, cb, sc, cc = sincos2_(A, b, c)
532 s = sA * sb * sc
533 c = cA * sb * sc + cc * cb
534 return atan2(s, c) * _2_0
537def excessCagnoli_(a, b, c):
538 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using U{Cagnoli's
539 <https://Zenodo.org/record/35392>} (D.34) formula.
541 @arg a: First triangle side (C{radians}).
542 @arg b: Second triangle side (C{radians}).
543 @arg c: Third triangle side (C{radians}).
545 @return: Spherical excess (C{radians}).
547 @raise UnitError: Invalid B{C{a}}, B{C{b}} or B{C{c}}.
549 @see: Function L{excessLHuilier_} and U{Spherical trigonometry
550 <https://WikiPedia.org/wiki/Spherical_trigonometry>}.
551 '''
552 a = Radians_(a=a)
553 b = Radians_(b=b)
554 c = Radians_(c=c)
556 r = _maprod(cos, a * _0_5, b * _0_5, c * _0_5)
557 if r:
558 s = fsumf_(a, b, c) * _0_5
559 t = _maprod(sin, s, s - a, s - b, s - c)
560 r = asin1(sqrt(t) * _0_5 / r) if t > 0 else _0_0
561 return Radians(Cagnoli=r * _2_0)
564def excessGirard_(A, B, C):
565 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using U{Girard's
566 <https://MathWorld.Wolfram.com/GirardsSphericalExcessFormula.html>} formula.
568 @arg A: First interior triangle angle (C{radians}).
569 @arg B: Second interior triangle angle (C{radians}).
570 @arg C: Third interior triangle angle (C{radians}).
572 @return: Spherical excess (C{radians}).
574 @raise UnitError: Invalid B{C{A}}, B{C{B}} or B{C{C}}.
576 @see: Function L{excessLHuilier_} and U{Spherical trigonometry
577 <https://WikiPedia.org/wiki/Spherical_trigonometry>}.
578 '''
579 r = fsumf_(Radians_(A=A),
580 Radians_(B=B),
581 Radians_(C=C), -PI)
582 return Radians(Girard=r)
585def excessLHuilier_(a, b, c):
586 '''Compute the I{spherical excess} C{E} of a (spherical) triangle using U{L'Huilier's
587 <https://MathWorld.Wolfram.com/LHuiliersTheorem.html>}'s Theorem.
589 @arg a: First triangle side (C{radians}).
590 @arg b: Second triangle side (C{radians}).
591 @arg c: Third triangle side (C{radians}).
593 @return: Spherical excess (C{radians}).
595 @raise UnitError: Invalid B{C{a}}, B{C{b}} or B{C{c}}.
597 @see: Function L{excessCagnoli_}, L{excessGirard_} and U{Spherical
598 trigonometry<https://WikiPedia.org/wiki/Spherical_trigonometry>}.
599 '''
600 a = Radians_(a=a)
601 b = Radians_(b=b)
602 c = Radians_(c=c)
604 s = fsumf_(a, b, c) * _0_5
605 r = _maprod(tan_2, s, s - a, s - b, s - c)
606 r = atan(sqrt(r)) if r > 0 else _0_0
607 return Radians(LHuilier=r * _4_0)
610def excessKarney(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
611 '''Compute the surface area of a (spherical) quadrilateral bounded by a
612 segment of a great circle, two meridians and the equator using U{Karney's
613 <https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}
614 method.
616 @arg lat1: Start latitude (C{degrees}).
617 @arg lon1: Start longitude (C{degrees}).
618 @arg lat2: End latitude (C{degrees}).
619 @arg lon2: End longitude (C{degrees}).
620 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
621 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) or C{None}.
622 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
623 B{C{lon2}} (C{bool}).
625 @return: Surface area, I{signed} (I{square} C{meter} or the same units as
626 B{C{radius}} I{squared}) or the I{spherical excess} (C{radians})
627 if C{B{radius}=0} or C{None}.
629 @raise TypeError: Invalid B{C{radius}}.
631 @raise UnitError: Invalid B{C{lat2}} or B{C{lat1}}.
633 @raise ValueError: Semi-circular longitudinal delta.
635 @see: Functions L{excessKarney_} and L{excessQuad}.
636 '''
637 r = excessKarney_(*_d3(wrap, lat1, lon1, lat2, lon2))
638 if radius:
639 r *= _mean_radius(radius, lat1, lat2)**2
640 return r
643def excessKarney_(phi2, phi1, lam21):
644 '''Compute the I{spherical excess} C{E} of a (spherical) quadrilateral bounded by
645 a segment of a great circle, two meridians and the equator using U{Karney's
646 <https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}
647 method.
649 @arg phi2: End latitude (C{radians}).
650 @arg phi1: Start latitude (C{radians}).
651 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
653 @return: Spherical excess, I{signed} (C{radians}).
655 @raise ValueError: Semi-circular longitudinal delta B{C{lam21}}.
657 @see: Function L{excessKarney} and U{Area of a spherical polygon
658 <https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}.
659 '''
660 # from: Veness <https://www.Movable-Type.co.UK/scripts/latlong.html> Area
661 # method due to Karney: for each edge of the polygon,
662 #
663 # tan(Δλ / 2) · (tan(φ1 / 2) + tan(φ2 / 2))
664 # tan(E / 2) = -----------------------------------------
665 # 1 + tan(φ1 / 2) · tan(φ2 / 2)
666 #
667 # where E is the spherical excess of the trapezium obtained by extending
668 # the edge to the equator-circle vector for each edge (see also ***).
669 t2 = tan_2(phi2)
670 t1 = tan_2(phi1)
671 c = (t1 * t2) + _1_0
672 s = (t1 + t2) * tan_2(lam21, lam21=None)
673 return Radians(Karney=atan2(s, c) * _2_0)
676# ***) Original post no longer available, following is a copy of the main part
677# <http://OSGeo-org.1560.x6.Nabble.com/Area-of-a-spherical-polygon-td3841625.html>
678#
679# The area of a polygon on a (unit) sphere is given by the spherical excess
680#
681# A = 2 * pi - sum(exterior angles)
682#
683# However this is badly conditioned if the polygon is small. In this case, use
684#
685# A = sum(S12{i, i+1}) over the edges of the polygon
686#
687# where S12 is the area of the quadrilateral bounded by an edge of the polygon,
688# two meridians and the equator, i.e. with vertices (phi1, lambda1), (phi2,
689# lambda2), (0, lambda1) and (0, lambda2). S12 is given by
690#
691# tan(S12 / 2) = tan(lambda21 / 2) * (tan(phi1 / 2) + tan(phi2 / 2)) /
692# (tan(phi1 / 2) * tan(phi2 / 2) + 1)
693#
694# = tan(lambda21 / 2) * tanh((Lamb(phi1) + Lamb(phi2)) / 2)
695#
696# where lambda21 = lambda2 - lambda1 and Lamb(x) is the Lambertian (or the
697# inverse Gudermannian) function
698#
699# Lambertian(x) = asinh(tan(x)) = atanh(sin(x)) = 2 * atanh(tan(x / 2))
700#
701# Notes: The formula for S12 is exact, except that...
702# - it is indeterminate if an edge is a semi-circle
703# - the formula for A applies only if the polygon does not include a pole
704# (if it does, then add +/- 2 * pi to the result)
705# - in the limit of small phi and lambda, S12 reduces to the trapezoidal
706# formula, S12 = (lambda2 - lambda1) * (phi1 + phi2) / 2
707# - I derived this result from the equation for the area of a spherical
708# triangle in terms of two edges and the included angle given by, e.g.
709# U{Todhunter, I. - Spherical Trigonometry (1871), Sec. 103, Eq. (2)
710# <http://Books.Google.com/books?id=3uBHAAAAIAAJ&pg=PA71>}
711# - I would be interested to know if this formula for S12 is already known
712# - Charles Karney
715def excessQuad(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
716 '''Compute the surface area of a (spherical) quadrilateral bounded by a segment
717 of a great circle, two meridians and the equator.
719 @arg lat1: Start latitude (C{degrees}).
720 @arg lon1: Start longitude (C{degrees}).
721 @arg lat2: End latitude (C{degrees}).
722 @arg lon2: End longitude (C{degrees}).
723 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
724 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) or C{None}.
725 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
726 B{C{lon2}} (C{bool}).
728 @return: Surface area, I{signed} (I{square} C{meter} or the same units as
729 B{C{radius}} I{squared}) or the I{spherical excess} (C{radians})
730 if C{B{radius}=0} or C{None}.
732 @raise TypeError: Invalid B{C{radius}}.
734 @raise UnitError: Invalid B{C{lat2}} or B{C{lat1}}.
736 @see: Function L{excessQuad_} and L{excessKarney}.
737 '''
738 r = excessQuad_(*_d3(wrap, lat1, lon1, lat2, lon2))
739 if radius:
740 r *= _mean_radius(radius, lat1, lat2)**2
741 return r
744def excessQuad_(phi2, phi1, lam21):
745 '''Compute the I{spherical excess} C{E} of a (spherical) quadrilateral bounded
746 by a segment of a great circle, two meridians and the equator.
748 @arg phi2: End latitude (C{radians}).
749 @arg phi1: Start latitude (C{radians}).
750 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
752 @return: Spherical excess, I{signed} (C{radians}).
754 @see: Function L{excessQuad} and U{Spherical trigonometry
755 <https://WikiPedia.org/wiki/Spherical_trigonometry>}.
756 '''
757 c = cos((phi2 - phi1) * _0_5)
758 s = sin((phi2 + phi1) * _0_5) * tan_2(lam21)
759 return Radians(Quad=atan2(s, c) * _2_0)
762def flatLocal(lat1, lon1, lat2, lon2, datum=_WGS84, scaled=True, wrap=False):
763 '''Compute the distance between two (ellipsoidal) points using
764 the U{ellipsoidal Earth to plane projection<https://WikiPedia.org/
765 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}
766 aka U{Hubeny<https://www.OVG.AT/de/vgi/files/pdf/3781/>} formula.
768 @arg lat1: Start latitude (C{degrees}).
769 @arg lon1: Start longitude (C{degrees}).
770 @arg lat2: End latitude (C{degrees}).
771 @arg lon2: End longitude (C{degrees}).
772 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2}
773 or L{a_f2Tuple}) to use.
774 @kwarg scaled: Scale prime_vertical by C{cos(B{phi})} (C{bool}), see
775 method L{pygeodesy.Ellipsoid.roc2_}.
776 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
777 B{C{lon2}} (C{bool}).
779 @return: Distance (C{meter}, same units as the B{C{datum}}'s or ellipsoid axes).
781 @raise TypeError: Invalid B{C{datum}}.
783 @note: The meridional and prime_vertical radii of curvature are taken and
784 scaled at the mean of both latitude.
786 @see: Functions L{flatLocal_} or L{hubeny_}, L{cosineLaw}, L{equirectangular},
787 L{euclidean}, L{flatPolar}, L{haversine}, L{thomas} and L{vincentys}, method
788 L{Ellipsoid.distance2} and U{local, flat earth approximation
789 <https://www.EdWilliams.org/avform.htm#flat>}.
790 '''
791 t = _d3(wrap, lat1, lon1, lat2, lon2)
792 E = _ellipsoidal(datum, flatLocal)
793 return E._hubeny_2(*t, scaled=scaled, squared=False) * E.a
795hubeny = flatLocal # PYCHOK for Karl Hubeny
798def flatLocal_(phi2, phi1, lam21, datum=_WGS84, scaled=True):
799 '''Compute the I{angular} distance between two (ellipsoidal) points using
800 the U{ellipsoidal Earth to plane projection<https://WikiPedia.org/
801 wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}
802 aka U{Hubeny<https://www.OVG.AT/de/vgi/files/pdf/3781/>} formula.
804 @arg phi2: End latitude (C{radians}).
805 @arg phi1: Start latitude (C{radians}).
806 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
807 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2}
808 or L{a_f2Tuple}) to use.
809 @kwarg scaled: Scale prime_vertical by C{cos(B{phi})} (C{bool}), see
810 method L{pygeodesy.Ellipsoid.roc2_}.
812 @return: Angular distance (C{radians}).
814 @raise TypeError: Invalid B{C{datum}}.
816 @note: The meridional and prime_vertical radii of curvature are taken and
817 scaled I{at the mean of both latitude}.
819 @see: Functions L{flatLocal} or L{hubeny}, L{cosineLaw_}, L{flatPolar_},
820 L{euclidean_}, L{haversine_}, L{thomas_} and L{vincentys_} and U{local,
821 flat earth approximation<https://www.EdWilliams.org/avform.htm#flat>}.
822 '''
823 E = _ellipsoidal(datum, flatLocal_)
824 return E._hubeny_2(phi2, phi1, lam21, scaled=scaled, squared=False)
826hubeny_ = flatLocal_ # PYCHOK for Karl Hubeny
829def flatPolar(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
830 '''Compute the distance between two (spherical) points using the U{polar
831 coordinate flat-Earth<https://WikiPedia.org/wiki/Geographical_distance
832 #Polar_coordinate_flat-Earth_formula>} formula.
834 @arg lat1: Start latitude (C{degrees}).
835 @arg lon1: Start longitude (C{degrees}).
836 @arg lat2: End latitude (C{degrees}).
837 @arg lon2: End longitude (C{degrees}).
838 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
839 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
840 @kwarg wrap: If C{True}, wrap or I{normalize} and B{C{lat2}} and B{C{lon2}}
841 (C{bool}).
843 @return: Distance (C{meter}, same units as B{C{radius}} or the datum's or
844 ellipsoid axes).
846 @raise TypeError: Invalid B{C{radius}}.
848 @see: Functions L{flatPolar_}, L{cosineLaw}, L{flatLocal} / L{hubeny},
849 L{equirectangular}, L{euclidean}, L{haversine}, L{thomas} and L{vincentys}.
850 '''
851 return _dS(flatPolar_, radius, wrap, lat1, lon1, lat2, lon2)
854def flatPolar_(phi2, phi1, lam21):
855 '''Compute the I{angular} distance between two (spherical) points using the
856 U{polar coordinate flat-Earth<https://WikiPedia.org/wiki/Geographical_distance
857 #Polar_coordinate_flat-Earth_formula>} formula.
859 @arg phi2: End latitude (C{radians}).
860 @arg phi1: Start latitude (C{radians}).
861 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
863 @return: Angular distance (C{radians}).
865 @see: Functions L{flatPolar}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /
866 L{hubeny_}, L{haversine_}, L{thomas_} and L{vincentys_}.
867 '''
868 a = fabs(PI_2 - phi1) # co-latitude
869 b = fabs(PI_2 - phi2) # co-latitude
870 if a < b:
871 a, b = b, a
872 if a < EPS0:
873 a = _0_0
874 elif b > 0:
875 b = b / a # /= chokes PyChecker
876 c = b * cos(lam21) * _2_0
877 c = fsumf_(_1_0, b**2, -fabs(c))
878 a *= sqrt0(c)
879 return a
882def _hartzell(pov, los, earth, **kwds):
883 '''(INTERNAL) Helper for C{CartesianBase.hartzell} and C{LatLonBase.hartzell}.
884 '''
885 if earth is None:
886 earth = pov.datum
887 else:
888 earth = _spherical_datum(earth, name__=hartzell)
889 pov = pov.toDatum(earth)
890 h = pov.height
891 if h < 0: # EPS0
892 t = _SPACE_(Fmt.PARENSPACED(height=h), _inside_)
893 raise IntersectionError(pov=pov, earth=earth, txt=t)
894 return hartzell(pov, los=los, earth=earth, **kwds) if h > 0 else pov # EPS0
897def hartzell(pov, los=False, earth=_WGS84, **name_LatLon_and_kwds):
898 '''Compute the intersection of the earth's surface and a Line-Of-Sight from
899 a Point-Of-View in space.
901 @arg pov: Point-Of-View outside the earth (C{LatLon}, C{Cartesian},
902 L{Ecef9Tuple} or L{Vector3d}).
903 @kwarg los: Line-Of-Sight, I{direction} to earth (L{Los}, L{Vector3d}),
904 C{True} for the I{normal, plumb} onto the surface or C{False}
905 or C{None} to point to the center of the earth.
906 @kwarg earth: The earth model (L{Datum}, L{Ellipsoid}, L{Ellipsoid2},
907 L{a_f2Tuple} or a C{scalar} earth radius in C{meter}).
908 @kwarg name_LatLon_and_kwds: Optional C{B{name}="hartzell"} (C{str}), class
909 C{B{LatLon}=None} to return the intersection and optionally,
910 additional C{LatLon} keyword arguments, include the B{C{datum}}
911 if different from and to convert from B{C{earth}}.
913 @return: The intersection (L{Vector3d}, B{C{pov}}'s C{cartesian type} or
914 the given B{C{LatLon}} instance) with attribute C{height} set to
915 the distance to the B{C{pov}}.
917 @raise IntersectionError: Invalid B{C{pov}} or B{C{pov}} inside the earth or
918 invalid B{C{los}} or B{C{los}} points outside or
919 away from the earth.
921 @raise TypeError: Invalid B{C{earth}}, C{ellipsoid} or C{datum}.
923 @see: Class L{Los}, functions L{tyr3d} and L{hartzell4} and methods
924 L{Ellipsoid.hartzell4}, any C{Cartesian.hartzell} and C{LatLon.hartzell}.
925 '''
926 n, kwds = _name2__(name_LatLon_and_kwds, name__=hartzell)
927 try:
928 D = _spherical_datum(earth, name__=hartzell)
929 r, h, i = _MODS.triaxials._hartzell3(pov, los, D.ellipsoid._triaxial)
931 C = _MODS.cartesianBase.CartesianBase
932 if kwds:
933 c = C(r, datum=D)
934 r = c.toLatLon(**_xkwds(kwds, height=h))
935 elif isinstance(r, C):
936 r.height = h
937 if i:
938 r._iteration = i
939 except Exception as x:
940 raise IntersectionError(pov=pov, los=los, earth=earth, cause=x, **kwds)
941 return _xnamed(r, n) if n else r
944def haversine(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
945 '''Compute the distance between two (spherical) points using the U{Haversine
946 <https://www.Movable-Type.co.UK/scripts/latlong.html>} formula.
948 @arg lat1: Start latitude (C{degrees}).
949 @arg lon1: Start longitude (C{degrees}).
950 @arg lat2: End latitude (C{degrees}).
951 @arg lon2: End longitude (C{degrees}).
952 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or ellipsoid
953 (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}) to use.
954 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
955 B{C{lon2}} (C{bool}).
957 @return: Distance (C{meter}, same units as B{C{radius}}).
959 @raise TypeError: Invalid B{C{radius}}.
961 @see: U{Distance between two (spherical) points
962 <https://www.EdWilliams.org/avform.htm#Dist>}, functions L{cosineLaw},
963 L{equirectangular}, L{euclidean}, L{flatLocal} / L{hubeny}, L{flatPolar},
964 L{thomas} and L{vincentys} and methods L{Ellipsoid.distance2},
965 C{LatLon.distanceTo*} and C{LatLon.equirectangularTo}.
967 @note: See note at function L{vincentys_}.
968 '''
969 return _dS(haversine_, radius, wrap, lat1, lon1, lat2, lon2)
972def haversine_(phi2, phi1, lam21):
973 '''Compute the I{angular} distance between two (spherical) points using the
974 U{Haversine<https://www.Movable-Type.co.UK/scripts/latlong.html>} formula.
976 @arg phi2: End latitude (C{radians}).
977 @arg phi1: Start latitude (C{radians}).
978 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
980 @return: Angular distance (C{radians}).
982 @see: Functions L{haversine}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /
983 L{hubeny_}, L{flatPolar_}, L{thomas_} and L{vincentys_}.
985 @note: See note at function L{vincentys_}.
986 '''
987 h = hav(phi2 - phi1) + cos(phi1) * cos(phi2) * hav(lam21) # haversine
988 return atan2(sqrt0(h), sqrt0(_1_0 - h)) * _2_0 # == asin1(sqrt(h)) * 2
991def heightOf(angle, distance, radius=R_M):
992 '''Determine the height above the (spherical) earth' surface after
993 traveling along a straight line at a given tilt.
995 @arg angle: Tilt angle above horizontal (C{degrees}).
996 @arg distance: Distance along the line (C{meter} or same units as
997 B{C{radius}}).
998 @kwarg radius: Optional mean earth radius (C{meter}).
1000 @return: Height (C{meter}, same units as B{C{distance}} and B{C{radius}}).
1002 @raise ValueError: Invalid B{C{angle}}, B{C{distance}} or B{C{radius}}.
1004 @see: U{MultiDop geog_lib.GeogBeamHt<https://GitHub.com/NASA/MultiDop>}
1005 (U{Shapiro et al. 2009, JTECH
1006 <https://Journals.AMetSoc.org/doi/abs/10.1175/2009JTECHA1256.1>}
1007 and U{Potvin et al. 2012, JTECH
1008 <https://Journals.AMetSoc.org/doi/abs/10.1175/JTECH-D-11-00019.1>}).
1009 '''
1010 r = h = Radius(radius)
1011 d = fabs(Distance(distance))
1012 if d > h:
1013 d, h = h, d
1015 if d > EPS0: # and h > EPS0
1016 d = d / h # /= h chokes PyChecker
1017 s = sin(Phid(angle=angle, clip=_180_0))
1018 s = fsumf_(_1_0, s * d * _2_0, d**2)
1019 if s > 0:
1020 return h * sqrt(s) - r
1022 raise _ValueError(angle=angle, distance=distance, radius=radius)
1025def heightOrthometric(h_loc, N):
1026 '''Get the I{orthometric} height B{H}, the height above the geoid, earth surface.
1028 @arg h_loc: The height above the ellipsoid (C{meter}) or an I{ellipsoidal}
1029 location (C{LatLon} or C{Cartesian} with a C{height} or C{h}
1030 attribute), otherwise C{0 meter}.
1031 @arg N: The I{geoid} height (C{meter}), the height of the geoid above the
1032 ellipsoid at the same B{C{h_loc}} location.
1034 @return: I{Orthometric} height C{B{H} = B{h} - B{N}} (C{meter}, same units
1035 as B{C{h}} and B{C{N}}).
1037 @see: U{Ellipsoid, Geoid, and Orthometric Heights<https://www.NGS.NOAA.gov/
1038 GEOID/PRESENTATIONS/2007_02_24_CCPS/Roman_A_PLSC2007notes.pdf>}, page
1039 6 and module L{pygeodesy.geoids}.
1040 '''
1041 h = h_loc if _isHeight(h_loc) else _xattr(h_loc, height=_xattr(h_loc, h=0))
1042 return Height(H=Height(h=h) - Height(N=N))
1045def horizon(height, radius=R_M, refraction=False):
1046 '''Determine the distance to the horizon from a given altitude above the
1047 (spherical) earth.
1049 @arg height: Altitude (C{meter} or same units as B{C{radius}}).
1050 @kwarg radius: Optional mean earth radius (C{meter}).
1051 @kwarg refraction: Consider atmospheric refraction (C{bool}).
1053 @return: Distance (C{meter}, same units as B{C{height}} and B{C{radius}}).
1055 @raise ValueError: Invalid B{C{height}} or B{C{radius}}.
1057 @see: U{Distance to horizon<https://www.EdWilliams.org/avform.htm#Horizon>}.
1058 '''
1059 h, r = Height(height), Radius(radius)
1060 if min(h, r) < 0:
1061 raise _ValueError(height=height, radius=radius)
1063 if refraction:
1064 r *= 2.415750694528 # 2.0 / 0.8279
1065 else:
1066 r += r + h
1067 return sqrt0(r * h)
1070class _idllmn6(object): # see also .geodesicw._wargs, .latlonBase._toCartesian3, .vector2d._numpy
1071 '''(INTERNAL) Helper for C{intersection2} and C{intersections2}.
1072 '''
1073 @contextmanager # <https://www.Python.org/dev/peps/pep-0343/> Examples
1074 def __call__(self, datum, lat1, lon1, lat2, lon2, small, wrap, s, **kwds):
1075 try:
1076 if wrap:
1077 _, lat2, lon2 = _Wrap.latlon3(lon1, lat2, lon2, wrap)
1078 kwds = _xkwds(kwds, wrap=wrap) # for _xError
1079 m = small if small is _100km else Meter_(small=small)
1080 n = typename(intersections2 if s else intersection2)
1081 if datum is None or euclidean(lat1, lon1, lat2, lon2) < m:
1082 d, m = None, _MODS.vector3d
1083 _i = m._intersects2 if s else m._intersect3d3
1084 elif _isRadius(datum) and datum < 0 and not s:
1085 d = _spherical_datum(-datum, name=n)
1086 m = _MODS.sphericalNvector
1087 _i = m.intersection
1088 else:
1089 d = _spherical_datum(datum, name=n)
1090 if d.isSpherical:
1091 m = _MODS.sphericalTrigonometry
1092 _i = m._intersects2 if s else m._intersect
1093 elif d.isEllipsoidal:
1094 try:
1095 if d.ellipsoid.geodesic:
1096 pass
1097 m = _MODS.ellipsoidalKarney
1098 except ImportError:
1099 m = _MODS.ellipsoidalExact
1100 _i = m._intersections2 if s else m._intersection3 # ellipsoidalBaseDI
1101 else:
1102 raise _TypeError(datum=datum)
1103 yield _i, d, lat2, lon2, m, n
1105 except (TypeError, ValueError) as x:
1106 raise _xError(x, lat1=lat1, lon1=lon1, datum=datum,
1107 lat2=lat2, lon2=lon2, small=small, **kwds)
1109_idllmn6 = _idllmn6() # PYCHOK singleton
1112def intersection2(lat1, lon1, bearing1,
1113 lat2, lon2, bearing2, datum=None, wrap=False, small=_100km): # was=True
1114 '''I{Conveniently} compute the intersection of two lines each defined by
1115 a (geodetic) point and a bearing from North, using either ...
1117 1) L{vector3d.intersection3d3} for B{C{small}} distances (below 100 Km
1118 or about 0.88 degrees) or if I{no} B{C{datum}} is specified, or ...
1120 2) L{sphericalTrigonometry.intersection} for a spherical B{C{datum}}
1121 or a C{scalar B{datum}} representing the earth radius, conventionally
1122 in C{meter} or ...
1124 3) L{sphericalNvector.intersection} if B{C{datum}} is a I{negative}
1125 C{scalar}, (negative) earth radius, conventionally in C{meter} or ...
1127 4) L{ellipsoidalKarney.intersection3} for an ellipsoidal B{C{datum}}
1128 and if I{Karney}'s U{geographiclib<https://PyPI.org/project/geographiclib>}
1129 is installed, otherwise ...
1131 5) L{ellipsoidalExact.intersection3}, provided B{C{datum}} is ellipsoidal.
1133 @arg lat1: Latitude of the first point (C{degrees}).
1134 @arg lon1: Longitude of the first point (C{degrees}).
1135 @arg bearing1: Bearing at the first point (compass C{degrees360}).
1136 @arg lat2: Latitude of the second point (C{degrees}).
1137 @arg lon2: Longitude of the second point (C{degrees}).
1138 @arg bearing2: Bearing at the second point (compass C{degrees360}).
1139 @kwarg datum: Optional datum (L{Datum}) or ellipsoid (L{Ellipsoid},
1140 L{Ellipsoid2} or L{a_f2Tuple}) or C{scalar} earth radius
1141 (C{meter}, same units as B{C{radius1}} and B{C{radius2}})
1142 or C{None}.
1143 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
1144 B{C{lon2}} (C{bool}).
1145 @kwarg small: Upper limit for small distances (C{meter}).
1147 @return: Intersection point (L{LatLon2Tuple}C{(lat, lon)}).
1149 @raise IntersectionError: No or an ambiguous intersection or colinear,
1150 parallel or otherwise non-intersecting lines.
1152 @raise TypeError: Invalid B{C{datum}}.
1154 @raise UnitError: Invalid B{C{lat1}}, B{C{lon1}}, B{C{bearing1}}, B{C{lat2}},
1155 B{C{lon2}} or B{C{bearing2}}.
1157 @see: Method L{RhumbLine.intersection2}.
1158 '''
1159 b1 = Bearing(bearing1=bearing1)
1160 b2 = Bearing(bearing2=bearing2)
1161 with _idllmn6(datum, lat1, lon1, lat2, lon2,
1162 small, wrap, False, bearing1=b1, bearing2=b2) as t:
1163 _i, d, lat2, lon2, m, n = t
1164 if d is None:
1165 t, _, _ = _i(m.Vector3d(lon1, lat1, 0), b1,
1166 m.Vector3d(lon2, lat2, 0), b2, useZ=False)
1167 t = LatLon2Tuple(t.y, t.x, name=n)
1169 else:
1170 t = _i(m.LatLon(lat1, lon1, datum=d), b1,
1171 m.LatLon(lat2, lon2, datum=d), b2,
1172 LatLon=None, height=0, wrap=False)
1173 if isinstance(t, Intersection3Tuple): # ellipsoidal
1174 t, _, _ = t
1175 t = LatLon2Tuple(t.lat, t.lon, name=n)
1176 return t
1179def intersections2(lat1, lon1, radius1,
1180 lat2, lon2, radius2, datum=None, wrap=False, small=_100km): # was=True
1181 '''I{Conveniently} compute the intersections of two circles each defined
1182 by a (geodetic) center point and a radius, using either ...
1184 1) L{vector3d.intersections2} for B{C{small}} distances (below 100 Km
1185 or about 0.88 degrees) or if I{no} B{C{datum}} is specified, or ...
1187 2) L{sphericalTrigonometry.intersections2} for a spherical B{C{datum}}
1188 or a C{scalar B{datum}} representing the earth radius, conventionally
1189 in C{meter} or ...
1191 3) L{ellipsoidalKarney.intersections2} for an ellipsoidal B{C{datum}}
1192 and if I{Karney}'s U{geographiclib<https://PyPI.org/project/geographiclib>}
1193 is installed, otherwise ...
1195 4) L{ellipsoidalExact.intersections2}, provided B{C{datum}} is ellipsoidal.
1197 @arg lat1: Latitude of the first circle center (C{degrees}).
1198 @arg lon1: Longitude of the first circle center (C{degrees}).
1199 @arg radius1: Radius of the first circle (C{meter}, conventionally).
1200 @arg lat2: Latitude of the second circle center (C{degrees}).
1201 @arg lon2: Longitude of the second circle center (C{degrees}).
1202 @arg radius2: Radius of the second circle (C{meter}, same units as B{C{radius1}}).
1203 @kwarg datum: Optional datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2}
1204 or L{a_f2Tuple}) or C{scalar} earth radius (C{meter}, same units as
1205 B{C{radius1}} and B{C{radius2}}) or C{None}.
1206 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and B{C{lon2}}
1207 (C{bool}).
1208 @kwarg small: Upper limit for small distances (C{meter}).
1210 @return: 2-Tuple of the intersection points, each a L{LatLon2Tuple}C{(lat, lon)}.
1211 Both points are the same instance, aka the I{radical center} if the
1212 circles are abutting
1214 @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting
1215 circles or no convergence.
1217 @raise TypeError: Invalid B{C{datum}}.
1219 @raise UnitError: Invalid B{C{lat1}}, B{C{lon1}}, B{C{radius1}}, B{C{lat2}},
1220 B{C{lon2}} or B{C{radius2}}.
1221 '''
1222 r1 = Radius_(radius1=radius1)
1223 r2 = Radius_(radius2=radius2)
1224 with _idllmn6(datum, lat1, lon1, lat2, lon2,
1225 small, wrap, True, radius1=r1, radius2=r2) as t:
1226 _i, d, lat2, lon2, m, n = t
1227 if d is None:
1228 r1 = m2degrees(r1, radius=R_M, lat=lat1)
1229 r2 = m2degrees(r2, radius=R_M, lat=lat2)
1231 def _V2T(x, y, _, **unused): # _ == z unused
1232 return LatLon2Tuple(y, x, name=n)
1234 t = _i(m.Vector3d(lon1, lat1, 0), r1,
1235 m.Vector3d(lon2, lat2, 0), r2, sphere=False,
1236 Vector=_V2T)
1237 else:
1238 def _LL2T(lat, lon, **unused):
1239 return LatLon2Tuple(lat, lon, name=n)
1241 t = _i(m.LatLon(lat1, lon1, datum=d), r1,
1242 m.LatLon(lat2, lon2, datum=d), r2,
1243 LatLon=_LL2T, height=0, wrap=False)
1244 return t
1247def isantipode(lat1, lon1, lat2, lon2, eps=EPS):
1248 '''Check whether two points are I{antipodal}, on diametrically
1249 opposite sides of the earth.
1251 @arg lat1: Latitude of one point (C{degrees}).
1252 @arg lon1: Longitude of one point (C{degrees}).
1253 @arg lat2: Latitude of the other point (C{degrees}).
1254 @arg lon2: Longitude of the other point (C{degrees}).
1255 @kwarg eps: Tolerance for near-equality (C{degrees}).
1257 @return: C{True} if points are antipodal within the
1258 B{C{eps}} tolerance, C{False} otherwise.
1260 @see: Functions L{isantipode_} and L{antipode}.
1261 '''
1262 return (fabs(lat1 + lat2) <= eps and
1263 fabs(lon1 + lon2) <= eps) or _isequalTo(
1264 normal(lat1, lon1), antipode(lat2, lon2), eps)
1267def isantipode_(phi1, lam1, phi2, lam2, eps=EPS):
1268 '''Check whether two points are I{antipodal}, on diametrically
1269 opposite sides of the earth.
1271 @arg phi1: Latitude of one point (C{radians}).
1272 @arg lam1: Longitude of one point (C{radians}).
1273 @arg phi2: Latitude of the other point (C{radians}).
1274 @arg lam2: Longitude of the other point (C{radians}).
1275 @kwarg eps: Tolerance for near-equality (C{radians}).
1277 @return: C{True} if points are antipodal within the
1278 B{C{eps}} tolerance, C{False} otherwise.
1280 @see: Functions L{isantipode} and L{antipode_}.
1281 '''
1282 return (fabs(phi1 + phi2) <= eps and
1283 fabs(lam1 + lam2) <= eps) or _isequalTo_(
1284 normal_(phi1, lam1), antipode_(phi2, lam2), eps)
1287def _isequalTo(p1, p2, eps=EPS):
1288 '''Compare 2 point lat-/lons ignoring C{class}.
1289 '''
1290 return (fabs(p1.lat - p2.lat) <= eps and
1291 fabs(p1.lon - p2.lon) <= eps) if eps else (p1.latlon == p2.latlon)
1294def _isequalTo_(p1, p2, eps=EPS): # underscore_!
1295 '''(INTERNAL) Compare 2 point phi-/lams ignoring C{class}.
1296 '''
1297 return (fabs(p1.phi - p2.phi) <= eps and
1298 fabs(p1.lam - p2.lam) <= eps) if eps else (p1.philam == p2.philam)
1301def isnormal(lat, lon, eps=0):
1302 '''Check whether B{C{lat}} I{and} B{C{lon}} are within their
1303 respective I{normal} range in C{degrees}.
1305 @arg lat: Latitude (C{degrees}).
1306 @arg lon: Longitude (C{degrees}).
1307 @kwarg eps: Optional tolerance C{degrees}).
1309 @return: C{True} if C{(abs(B{lat}) + B{eps}) <= 90} and
1310 C{(abs(B{lon}) + B{eps}) <= 180}, C{False} otherwise.
1312 @see: Functions L{isnormal_} and L{normal}.
1313 '''
1314 return _loneg(fabs(lon)) >= eps and (_90_0 - fabs(lat)) >= eps # co-latitude
1317def isnormal_(phi, lam, eps=0):
1318 '''Check whether B{C{phi}} I{and} B{C{lam}} are within their
1319 respective I{normal} range in C{radians}.
1321 @arg phi: Latitude (C{radians}).
1322 @arg lam: Longitude (C{radians}).
1323 @kwarg eps: Optional tolerance C{radians}).
1325 @return: C{True} if C{(abs(B{phi}) + B{eps}) <= PI/2} and
1326 C{(abs(B{lam}) + B{eps}) <= PI}, C{False} othwerwise.
1328 @see: Functions L{isnormal} and L{normal_}.
1329 '''
1330 return (PI_2 - fabs(phi)) >= eps and (PI - fabs(lam)) >= eps
1333def _maprod(fun_, *ts):
1334 '''(INTERNAL) Helper for C{excessCagnoli_} and C{excessLHuilier_}.
1335 '''
1336 return fprod(map(fun_, ts))
1339def _normal2(a, b, n_2, n, n2):
1340 '''(INTERNAL) Helper for C{normal} and C{normal_}.
1341 '''
1342 if fabs(b) > n:
1343 b = remainder(b, n2)
1344 if fabs(a) > n_2:
1345 r = remainder(a, n)
1346 if r != a:
1347 a = -r
1348 b -= n if b > 0 else -n
1349 return float0_(a, b)
1352def normal(lat, lon, **name):
1353 '''Normalize a lat- I{and} longitude pair in C{degrees}.
1355 @arg lat: Latitude (C{degrees}).
1356 @arg lon: Longitude (C{degrees}).
1357 @kwarg name: Optional C{B{name}="normal"} (C{str}).
1359 @return: L{LatLon2Tuple}C{(lat, lon)} with C{-90 <= lat <= 90}
1360 and C{-180 <= lon <= 180}.
1362 @see: Functions L{normal_} and L{isnormal}.
1363 '''
1364 return LatLon2Tuple(*_normal2(lat, lon, _90_0, _180_0, _360_0),
1365 name=_name__(name, name__=normal))
1368def normal_(phi, lam, **name):
1369 '''Normalize a lat- I{and} longitude pair in C{radians}.
1371 @arg phi: Latitude (C{radians}).
1372 @arg lam: Longitude (C{radians}).
1373 @kwarg name: Optional C{B{name}="normal_"} (C{str}).
1375 @return: L{PhiLam2Tuple}C{(phi, lam)} with C{abs(phi) <= PI/2}
1376 and C{abs(lam) <= PI}.
1378 @see: Functions L{normal} and L{isnormal_}.
1379 '''
1380 return PhiLam2Tuple(*_normal2(phi, lam, PI_2, PI, PI2),
1381 name=_name__(name, name__=normal_))
1384def _opposes(d, m, n, n2):
1385 '''(INTERNAL) Helper for C{opposing} and C{opposing_}.
1386 '''
1387 d = d % n2 # -20 % 360 == 340, -1 % PI2 == PI2 - 1
1388 return False if d < m or d > (n2 - m) else (
1389 True if (n - m) < d < (n + m) else None)
1392def opposing(bearing1, bearing2, margin=_90_0):
1393 '''Compare the direction of two bearings given in C{degrees}.
1395 @arg bearing1: First bearing (compass C{degrees}).
1396 @arg bearing2: Second bearing (compass C{degrees}).
1397 @kwarg margin: Optional, interior angle bracket (C{degrees}).
1399 @return: C{True} if both bearings point in opposite, C{False} if
1400 in similar or C{None} if in I{perpendicular} directions.
1402 @see: Function L{opposing_}.
1403 '''
1404 m = Degrees_(margin=margin, low=EPS0, high=_90_0)
1405 return _opposes(bearing2 - bearing1, m, _180_0, _360_0)
1408def opposing_(radians1, radians2, margin=PI_2):
1409 '''Compare the direction of two bearings given in C{radians}.
1411 @arg radians1: First bearing (C{radians}).
1412 @arg radians2: Second bearing (C{radians}).
1413 @kwarg margin: Optional, interior angle bracket (C{radians}).
1415 @return: C{True} if both bearings point in opposite, C{False} if
1416 in similar or C{None} if in perpendicular directions.
1418 @see: Function L{opposing}.
1419 '''
1420 m = Radians_(margin=margin, low=EPS0, high=PI_2)
1421 return _opposes(radians2 - radians1, m, PI, PI2)
1424def _Propy(func, nargs, kwds):
1425 '''(INTERNAL) Helper for the C{frechet.[-]Frechet**} and
1426 C{hausdorff.[-]Hausdorff*} classes.
1427 '''
1428 try:
1429 _xcallable(distance=func)
1430 # assert _args_kwds_count2(func)[0] == nargs + int(ismethod(func))
1431 _ = func(*_0_0s(nargs), **kwds)
1432 except Exception as x:
1433 t = unstr(func, **kwds)
1434 raise _TypeError(t, cause=x)
1435 return func
1438def _radical2(d, r1, r2, **name): # in .ellipsoidalBaseDI, .sphericalTrigonometry, .vector3d
1439 # (INTERNAL) See C{radical2} below
1440 # assert d > EPS0
1441 r = fsumf_(_1_0, (r1 / d)**2, -(r2 / d)**2) * _0_5
1442 n = _name__(name, name__=radical2)
1443 return Radical2Tuple(max(_0_0, min(_1_0, r)), r * d, name=n)
1446def radical2(distance, radius1, radius2, **name):
1447 '''Compute the I{radical ratio} and I{radical line} of two U{intersecting
1448 circles<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>}.
1450 The I{radical line} is perpendicular to the axis thru the centers of
1451 the circles at C{(0, 0)} and C{(B{distance}, 0)}.
1453 @arg distance: Distance between the circle centers (C{scalar}).
1454 @arg radius1: Radius of the first circle (C{scalar}).
1455 @arg radius2: Radius of the second circle (C{scalar}).
1456 @kwarg name: Optional C{B{name}=NN} (C{str}).
1458 @return: A L{Radical2Tuple}C{(ratio, xline)} where C{0.0 <= ratio <=
1459 1.0} and C{xline} is along the B{C{distance}}.
1461 @raise IntersectionError: The B{C{distance}} exceeds the sum of
1462 B{C{radius1}} and B{C{radius2}}.
1464 @raise UnitError: Invalid B{C{distance}}, B{C{radius1}} or B{C{radius2}}.
1465 '''
1466 d = Distance_(distance, low=_0_0)
1467 r1 = Radius_(radius1=radius1)
1468 r2 = Radius_(radius2=radius2)
1469 if d > (r1 + r2):
1470 raise IntersectionError(distance=d, radius1=r1, radius2=r2,
1471 txt=_too_(_distant_))
1472 return _radical2(d, r1, r2, **name) if d > EPS0 else \
1473 Radical2Tuple(_0_5, _0_0, **name)
1476class Radical2Tuple(_NamedTuple):
1477 '''2-Tuple C{(ratio, xline)} of the I{radical} C{ratio} and
1478 I{radical} C{xline}, both C{scalar} and C{0.0 <= ratio <= 1.0}
1479 '''
1480 _Names_ = (_ratio_, _xline_)
1481 _Units_ = ( Scalar, Scalar)
1484def _radistance(inst):
1485 '''(INTERNAL) Helper for the L{frechet._FrechetMeterRadians}
1486 and L{hausdorff._HausdorffMeterRedians} classes.
1487 '''
1488 wrap_, kwds_ = _xkwds_pop2(inst._kwds, wrap=False)
1489 func_ = inst._func_
1490 try: # calling lower-overhead C{func_}
1491 func_(0, _0_25, _0_5, **kwds_)
1492 wrap_ = _Wrap._philamop(wrap_)
1493 except TypeError:
1494 return inst.distance
1496 def _philam(p):
1497 try:
1498 return p.phi, p.lam
1499 except AttributeError: # no .phi or .lam
1500 return radians(p.lat), radians(p.lon)
1502 def _func_wrap(point1, point2):
1503 phi1, lam1 = wrap_(*_philam(point1))
1504 phi2, lam2 = wrap_(*_philam(point2))
1505 return func_(phi2, phi1, lam2 - lam1, **kwds_)
1507 inst._units = inst._units_
1508 return _func_wrap
1511def _scale_deg(lat1, lat2): # degrees
1512 # scale factor cos(mean of lats) for delta lon
1513 m = fabs(lat1 + lat2) * _0_5
1514 return cos(radians(m)) if m < _90_0 else _0_0
1517def _scale_rad(phi1, phi2): # radians, by .frechet, .hausdorff, .heights
1518 # scale factor cos(mean of phis) for delta lam
1519 m = fabs(phi1 + phi2) * _0_5
1520 return cos(m) if m < PI_2 else _0_0
1523def _sincosa6(phi2, phi1, lam21): # [4] in cosineLaw
1524 '''(INTERNAL) C{sin}es, C{cos}ines and C{acos}ine.
1525 '''
1526 s2, c2, s1, c1, _, c21 = sincos2_(phi2, phi1, lam21)
1527 return s2, c2, s1, c1, acos1(s1 * s2 + c1 * c2 * c21), c21
1530def thomas(lat1, lon1, lat2, lon2, datum=_WGS84, wrap=False):
1531 '''Compute the distance between two (ellipsoidal) points using U{Thomas'
1532 <https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} formula.
1534 @arg lat1: Start latitude (C{degrees}).
1535 @arg lon1: Start longitude (C{degrees}).
1536 @arg lat2: End latitude (C{degrees}).
1537 @arg lon2: End longitude (C{degrees}).
1538 @kwarg datum: Datum (L{Datum}) or ellipsoid (L{Ellipsoid}, L{Ellipsoid2}
1539 or L{a_f2Tuple}) to use.
1540 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
1541 B{C{lon2}} (C{bool}).
1543 @return: Distance (C{meter}, same units as the B{C{datum}}'s or ellipsoid axes).
1545 @raise TypeError: Invalid B{C{datum}}.
1547 @see: Functions L{thomas_}, L{cosineLaw}, L{equirectangular}, L{euclidean},
1548 L{flatLocal} / L{hubeny}, L{flatPolar}, L{haversine}, L{vincentys} and
1549 method L{Ellipsoid.distance2}.
1550 '''
1551 return _dE(thomas_, datum, wrap, lat1, lon1, lat2, lon2)
1554def thomas_(phi2, phi1, lam21, datum=_WGS84):
1555 '''Compute the I{angular} distance between two (ellipsoidal) points using
1556 U{Thomas'<https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} formula.
1558 @arg phi2: End latitude (C{radians}).
1559 @arg phi1: Start latitude (C{radians}).
1560 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
1561 @kwarg datum: Datum (L{Datum}) ?or ellipsoid to use (L{Ellipsoid},
1562 L{Ellipsoid2} or L{a_f2Tuple}).
1564 @return: Angular distance (C{radians}).
1566 @raise TypeError: Invalid B{C{datum}}.
1568 @see: Functions L{thomas}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /
1569 L{hubeny_}, L{flatPolar_}, L{haversine_} and L{vincentys_} and
1570 U{Geodesy-PHP<https://GitHub.com/jtejido/geodesy-php/blob/master/
1571 src/Geodesy/Distance/ThomasFormula.php>}.
1572 '''
1573 s2, c2, s1, c1, r, _ = _sincosa6(phi2, phi1, lam21)
1574 if r and isnon0(c1) and isnon0(c2):
1575 E = _ellipsoidal(datum, thomas_)
1576 f = E.f * _0_25
1577 if f: # ellipsoidal
1578 r1 = atan2(E.b_a * s1, c1)
1579 r2 = atan2(E.b_a * s2, c2)
1581 j = (r2 + r1) * _0_5
1582 k = (r2 - r1) * _0_5
1583 sj, cj, sk, ck, h, _ = sincos2_(j, k, lam21 * _0_5)
1585 h = fsumf_(sk**2, (ck * h)**2, -(sj * h)**2)
1586 u = _1_0 - h
1587 if isnon0(u) and isnon0(h):
1588 r = atan(sqrt0(h / u)) * 2 # == acos(1 - 2 * h)
1589 sr, cr = sincos2(r)
1590 if isnon0(sr):
1591 u = (sj * ck)**2 * 2 / u
1592 h = (sk * cj)**2 * 2 / h
1593 x = u + h
1594 y = u - h
1596 b = r * 2
1597 s = r / sr
1598 e = 4 * s**2
1599 d = 2 * cr
1600 a = e * d
1601 c = s - (a - d) * _0_5
1603 t = fdot_(a, x, -b, y, -d, y**2, c, x**2, e, x * y) * _0_25
1604 r -= fdot_(s, x, -1, y, -t, f) * f * sr
1605 return r
1608def vincentys(lat1, lon1, lat2, lon2, radius=R_M, wrap=False):
1609 '''Compute the distance between two (spherical) points using
1610 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}
1611 spherical formula.
1613 @arg lat1: Start latitude (C{degrees}).
1614 @arg lon1: Start longitude (C{degrees}).
1615 @arg lat2: End latitude (C{degrees}).
1616 @arg lon2: End longitude (C{degrees}).
1617 @kwarg radius: Mean earth radius (C{meter}), datum (L{Datum}) or
1618 ellipsoid (L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple})
1619 to use.
1620 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{lat2}} and
1621 B{C{lon2}} (C{bool}).
1623 @return: Distance (C{meter}, same units as B{C{radius}}).
1625 @raise UnitError: Invalid B{C{radius}}.
1627 @see: Functions L{vincentys_}, L{cosineLaw}, L{equirectangular}, L{euclidean},
1628 L{flatLocal}/L{hubeny}, L{flatPolar}, L{haversine} and L{thomas} and
1629 methods L{Ellipsoid.distance2}, C{LatLon.distanceTo*} and
1630 C{LatLon.equirectangularTo}.
1632 @note: See note at function L{vincentys_}.
1633 '''
1634 return _dS(vincentys_, radius, wrap, lat1, lon1, lat2, lon2)
1637def vincentys_(phi2, phi1, lam21):
1638 '''Compute the I{angular} distance between two (spherical) points using
1639 U{Vincenty's<https://WikiPedia.org/wiki/Great-circle_distance>}
1640 spherical formula.
1642 @arg phi2: End latitude (C{radians}).
1643 @arg phi1: Start latitude (C{radians}).
1644 @arg lam21: Longitudinal delta, M{end-start} (C{radians}).
1646 @return: Angular distance (C{radians}).
1648 @see: Functions L{vincentys}, L{cosineLaw_}, L{euclidean_}, L{flatLocal_} /
1649 L{hubeny_}, L{flatPolar_}, L{haversine_} and L{thomas_}.
1651 @note: Functions L{vincentys_}, L{haversine_} and L{cosineLaw_} produce
1652 equivalent results, but L{vincentys_} is suitable for antipodal
1653 points and slightly more expensive (M{3 cos, 3 sin, 1 hypot, 1 atan2,
1654 6 mul, 2 add}) than L{haversine_} (M{2 cos, 2 sin, 2 sqrt, 1 atan2, 5
1655 mul, 1 add}) and L{cosineLaw_} (M{3 cos, 3 sin, 1 acos, 3 mul, 1 add}).
1656 '''
1657 s1, c1, s2, c2, s21, c21 = sincos2_(phi1, phi2, lam21)
1659 c = c2 * c21
1660 x = s1 * s2 + c1 * c
1661 y = c1 * s2 - s1 * c
1662 return atan2(hypot(c2 * s21, y), x)
1664# **) MIT License
1665#
1666# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.
1667#
1668# Permission is hereby granted, free of charge, to any person obtaining a
1669# copy of this software and associated documentation files (the "Software"),
1670# to deal in the Software without restriction, including without limitation
1671# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1672# and/or sell copies of the Software, and to permit persons to whom the
1673# Software is furnished to do so, subject to the following conditions:
1674#
1675# The above copyright notice and this permission notice shall be included
1676# in all copies or substantial portions of the Software.
1677#
1678# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1679# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1680# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1681# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1682# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1683# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1684# OTHER DEALINGS IN THE SOFTWARE.