Coverage for pygeodesy/karney.py: 93%
333 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-09-25 17:40 -0400
« prev ^ index » next coverage.py v7.6.0, created at 2024-09-25 17:40 -0400
2# -*- coding: utf-8 -*-
4u'''Wrapper around several C{geomath.Math} functions from I{Karney}'s Python package U{geographiclib
5<https://PyPI.org/project/geographiclib>}, provided that package is installed.
7Methods of the I{wrapped} L{Geodesic<geodesicw.Geodesic>} and L{GeodesicLine<geodesicw.GeodesicLine>}
8classes return a L{GDict} instance offering access to the C{dict} items either by C{key} or by
9C{attribute} name.
11With env variable C{PYGEODESY_GEOGRAPHICLIB} left undefined or set to C{"2"}, modules L{geodesicw},
12L{geodesicx} and this module will use U{GeographicLib 2.0+<https://GeographicLib.SourceForge.io/C++/doc/>}
13and newer transcoding, otherwise C{1.52} or older. Set C{PYGEODESY_GEOGRAPHICLIB=2.4} to default to the
14C{Jacobi amplitude} instead of C{Bulirsch}' function in methods L{ExactTransverseMercator.forward
15<pygeodesy.ExactTransverseMercator.forward>} and L{reverse <pygeodesy.ExactTransverseMercator.reverse>}.
17Karney-based functionality
18==========================
201. The following classes and functions in C{pygeodesy}
22 - L{AlbersEqualArea}, L{AlbersEqualArea2}, L{AlbersEqualArea4},
23 L{AlbersEqualAreaCylindrical}, L{AlbersEqualAreaNorth}, L{AlbersEqualAreaSouth} --
24 U{AlbersEqualArea<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AlbersEqualArea.html>}
26 - L{AuxAngle}, L{AuxDST}, L{AuxDLat}, L{AuxLat} -- U{AuxAngle
27 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AuxAngle.html>},
28 U{DST<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1DST.html>},
29 U{DAuxLatitude<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1DAuxLatitude.html>},
30 U{AuxLatitude<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AuxLatitude.html>} in I{GeographicLib 2.2+}
32 - L{CassiniSoldner} -- U{CassiniSoldner<https://GeographicLib.SourceForge.io/C++/doc/
33 classGeographicLib_1_1CassiniSoldner.html>}
35 - L{EcefKarney} -- U{Geocentric<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Geocentric.html>}
37 - L{Elliptic} -- U{EllipticFunction<https://GeographicLib.SourceForge.io/C++/doc/
38 classGeographicLib_1_1EllipticFunction.html>}
40 - L{EquidistantExact}, L{EquidistantGeodSolve}, L{EquidistantKarney} -- U{AzimuthalEquidistant
41 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AzimuthalEquidistant.html>}
43 - L{Etm}, L{ExactTransverseMercator} -- U{TransverseMercatorExact
44 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercatorExact.html>}
46 - L{Geodesic}, L{GeodesicLine} -- I{wrapped} U{geodesic.Geodesic<https://PyPI.org/project/geographiclib>},
47 I{wrapped} U{geodesicline.GeodesicLine<https://PyPI.org/project/geographiclib>}
49 - L{GeodesicAreaExact}, L{PolygonArea} -- U{PolygonArea<https://GeographicLib.SourceForge.io/C++/doc/
50 classGeographicLib_1_1PolygonAreaT.html>}
52 - L{GeodesicExact}, L{GeodesicLineExact} -- U{GeodesicExact<https://GeographicLib.SourceForge.io/C++/doc/
53 classGeographicLib_1_1GeodesicExact.html>}, U{GeodesicLineExact<https://GeographicLib.SourceForge.io/C++/doc/
54 classGeographicLib_1_1GeodesicLineExact.html>}
56 - L{GeoidKarney} -- U{Geoid<https://GeographicLib.SourceForge.io/C++/doc/geoid.html>}
58 - L{Georef} -- U{Georef<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Georef.html>}
60 - L{GnomonicExact}, L{GnomonicGeodSolve}, L{GnomonicKarney} -- U{Gnomonic
61 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Gnomonic.html>}
63 - L{Intersector} -- U{Intersect
64 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Intersect.html>} from I{GeographicLib 2.3+}
66 - L{JacobiConformal} -- U{JacobiConformal
67 <https://geographiclib.sourceforge.io/C++/doc/classGeographicLib_1_1experimental_1_1JacobiConformal.html>}
69 - L{KTransverseMercator} - U{TransverseMercator
70 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>}
72 - L{LocalCartesian}, L{Ltp} -- U{LocalCartesian<https://GeographicLib.SourceForge.io/C++/doc/
73 classGeographicLib_1_1LocalCartesian.html>}
75 - L{Osgr} -- U{OSGB<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1OSGB.html>}
77 - L{rhumb.aux_}, L{RhumbAux}, L{RhumbLineAux} -- U{Rhumb
78 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Rhumb.html>} and U{RhumbLine
79 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1RhumbLine.html>} from I{GeographicLib 2.2+}
81 - L{rhumb.ekx}, L{Rhumb}, L{RhumbLine} -- U{Rhumb
82 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Rhumb.html>},
83 U{RhumbLine<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1RhumbLine.html>},
84 U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>}
85 from I{GeographicLib 2.0}
87 - L{Ups} -- U{PolarStereographic<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1PolarStereographic.html>}
89 - L{Utm} -- U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>}
91 - L{UtmUps}, L{Epsg} -- U{UTMUPS<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1UTMUPS.html>}
93 - L{atan1d}, L{atan2d}, L{sincos2}, L{sincos2d}, L{tand} -- U{geomath.Math
94 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Math.html>}
96are I{transcoded} from C++ classes in I{Karney}'s U{GeographicLib<https://GeographicLib.SourceForge.io/C++/doc/annotated.html>}.
982. These C{pygeodesy} modules and classes
100 - L{ellipsoidalGeodSolve}, L{ellipsoidalKarney}, L{geodesici}, L{geodsolve}, L{karney}, L{rhumb.solve}
101 - L{EquidistantKarney}, L{FrechetKarney}, L{GeodesicSolve}, L{GeodesicLineSolve}, L{GnomonicGeodSolve},
102 L{GnomonicKarney}, L{HeightIDWkarney}, L{Intersectool}
104are or use I{wrappers} around I{Karney}'s Python U{geographiclib<https://PyPI.org/project/geographiclib>} or
105C++ utility U{GeodSolve<https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>},
106U{IntersectTool<https://GeographicLib.SourceForge.io/C++/doc/IntersectTool.1.html>} or
107U{RhumbSolve<https://GeographicLib.SourceForge.io/C++/doc/RhumbSolve.1.html>}.
1093. All C{pygeodesy} functions and methods to compute I{ellipsoidal} intersections, nearest points and trilaterations
111 - L{ellipsoidalExact.intersection3}, L{ellipsoidalExact.intersections2}, L{ellipsoidalExact.nearestOn},
112 L{ellipsoidalExact.LatLon.intersection3}, L{ellipsoidalExact.LatLon.intersections2},
113 L{ellipsoidalExact.LatLon.nearestOn}, L{ellipsoidalExact.LatLon.trilaterate5}
115 - L{ellipsoidalKarney.intersection3}, L{ellipsoidalKarney.intersections2}, L{ellipsoidalKarney.nearestOn},
116 L{ellipsoidalKarney.LatLon.intersection3}, L{ellipsoidalKarney.LatLon.intersections2},
117 L{ellipsoidalKarney.LatLon.nearestOn}, L{ellipsoidalKarney.LatLon.trilaterate5}
119 - L{ellipsoidalVincenty.intersection3}, L{ellipsoidalVincenty.intersections2}, L{ellipsoidalVincenty.nearestOn},
120 L{ellipsoidalVincenty.LatLon.intersection3}, L{ellipsoidalVincenty.LatLon.intersections2},
121 L{ellipsoidalVincenty.LatLon.nearestOn}, L{ellipsoidalVincenty.LatLon.trilaterate5}
123 - L{RhumbLineAux.Intersection} and L{RhumbLine.Intersection}
125are implementations of I{Karney}'s iterative solution posted under U{The B{ellipsoidal} case
126<https://GIS.StackExchange.com/questions/48937/calculating-intersection-of-two-circles>} and in paper U{Geodesics
127on an ellipsoid of revolution<https://ArXiv.org/pdf/1102.1215.pdf>} (pp 20-21, section B{14. MARITIME BOUNDARIES}).
1294. The C{pygeodesy} methods to compute I{ellipsoidal} intersections and nearest points
131 - L{RhumbLineAux.Intersecant2}, L{RhumbLineAux.PlumbTo}, L{RhumbLine.Intersecant2} and L{RhumbLine.PlumbTo}
133are I{transcoded} of I{Karney}'s iterative C++ function U{rhumb-intercept
134<https://SourceForge.net/p/geographiclib/discussion/1026620/thread/2ddc295e/>}.
1365. Spherical functions
138 - L{pygeodesy.excessKarney_}, L{sphericalTrigonometry.areaOf}
140in C{pygeodesy} are based on I{Karney}'s post U{Area of a spherical polygon
141<https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}, 3rd Answer.
142'''
143# make sure int/int division yields float quotient, see .basics
144from __future__ import division as _; del _ # PYCHOK semicolon
146from pygeodesy.basics import _copysign, isint, neg, unsigned0, _xgeographiclib, \
147 _zip, _version_info
148from pygeodesy.constants import NAN, _isfinite as _math_isfinite, _0_0, \
149 _1_16th, _1_0, _2_0, _180_0, _N_180_0, _360_0
150from pygeodesy.errors import GeodesicError, _ValueError, _xkwds
151from pygeodesy.fmath import cbrt, fremainder, norm2 # Fhorner, Fsum
152# from pygeodesy.internals import _version_info # from .basics
153from pygeodesy.interns import NN, _2_, _a12_, _area_, _azi1_, _azi2_, _azi12_, \
154 _composite_, _lat1_, _lat2_, _lon1_, _lon2_, \
155 _m12_, _M12_, _M21_, _number_, _s12_, _S12_, \
156 _UNDER_, _X_, _BAR_ # PYCHOK used!
157from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _getenv
158from pygeodesy.named import ADict, _NamedBase, _NamedTuple, notImplemented, _Pass
159from pygeodesy.props import deprecated_method, Property_RO, property_RO, \
160 property_ROnce
161from pygeodesy.units import Azimuth as _Azi, Degrees as _Deg, Lat, Lon, \
162 Meter as _M, Meter2 as _M2, Number_
163from pygeodesy.utily import atan2d, sincos2d, tand, _unrollon, fabs
165# from math import fabs # from .utily
167__all__ = _ALL_LAZY.karney
168__version__ = '24.09.13'
170_K_2_0 = _getenv('PYGEODESY_GEOGRAPHICLIB', _2_)
171_K_2_4 = _K_2_0 == '2.4'
172_K_2_0 = _K_2_0 == _2_ or _K_2_4
173_perimeter_ = 'perimeter'
176class _GTuple(_NamedTuple): # in .testNamedTuples
177 '''(INTERNAL) Helper.
178 '''
179 def toGDict(self, **updates): # NO name=NN
180 '''Convert this C{*Tuple} to a L{GDict}.
182 @kwarg updates: Optional items to apply (C{name=value} pairs)
183 '''
184 r = GDict(_zip(self._Names_, self)) # strict=True
185 if updates:
186 r.update(updates)
187 if self._iteration is not None:
188 r._iteration = self._iteration
189 return r
192class _Lat(Lat):
193 '''(INTERNAL) Latitude B{C{lat}}.
194 '''
195 def __init__(self, *lat, **Error_name):
196 kwds = _xkwds(Error_name, clip=0, Error=GeodesicError)
197 Lat.__new__(_Lat, *lat, **kwds)
200class _Lon(Lon):
201 '''(INTERNAL) Longitude B{C{lon}}.
202 '''
203 def __init__(self, *lon, **Error_name):
204 kwds = _xkwds(Error_name, clip=0, Error=GeodesicError)
205 Lon.__new__(_Lon, *lon, **kwds)
208class Area3Tuple(_NamedTuple): # in .geodesicx.gxarea
209 '''3-Tuple C{(number, perimeter, area)} with the C{number}
210 of points of the polygon or polyline, the C{perimeter} in
211 C{meter} and the C{area} in C{meter} I{squared}.
212 '''
213 _Names_ = (_number_, _perimeter_, _area_)
214 _Units_ = ( Number_, _M, _M2)
217class Caps(object):
218 '''(INTERNAL) Overriden by C{Caps} below.
219 '''
220 EMPTY = 0 # formerly aka NONE
221 _CAP_1 = 1 << 0 # for goedesici/-w
222 _CAP_1p = 1 << 1 # for goedesici/-w
223 _CAP_2 = 1 << 2
224 _CAP_3 = 1 << 3 # for goedesici/-w
225 _CAP_4 = 1 << 4 # for goedesicw
226 _CAP_ALL = 0x1F
227# _CAP_MASK = _CAP_ALL
228 LATITUDE = 1 << 7 # compute latitude C{lat2}
229 LONGITUDE = 1 << 8 | _CAP_3 # compute longitude C{lon2}
230 AZIMUTH = 1 << 9 # azimuths C{azi1} and C{azi2}
231 DISTANCE = 1 << 10 | _CAP_1 # compute distance C{s12}
232 DISTANCE_IN = 1 << 11 | _CAP_1 | _CAP_1p # allow distance C{s12} in Direct
233 REDUCEDLENGTH = 1 << 12 | _CAP_1 | _CAP_2 # compute reduced length C{m12}
234 GEODESICSCALE = 1 << 13 | _CAP_1 | _CAP_2 # compute geodesic scales C{M12} and C{M21}
235 AREA = 1 << 14 | _CAP_4 # compute area C{S12}
237 STANDARD = AZIMUTH | DISTANCE | LATITUDE | LONGITUDE
239 _DIRECT3 = AZIMUTH | LATITUDE | LONGITUDE # for goedesicw only
240 _INVERSE3 = AZIMUTH | DISTANCE # for goedesicw only
241 _STD = STANDARD # for goedesicw only
242 _STD_LINE = _STD | DISTANCE_IN # for goedesici/-w
244 LINE_CAPS = _STD_LINE | REDUCEDLENGTH | GEODESICSCALE # .geodesici only
245 LONG_UNROLL = 1 << 15 # unroll C{lon2} in .Direct and .Position
246# = 1 << 16 # unused
247 LINE_OFF = 1 << 17 # Line without updates from parent geodesic or rhumb
248 REVERSE2 = 1 << 18 # reverse C{azi2}
249 ALL = 0x7F80 | _CAP_ALL # without LONG_UNROLL, LINE_OFF, REVERSE2 and _DEBUG_*
251 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE
252 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA
254 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE
255 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA
257 _SALP_CALPs_ = 1 << 19 # (INTERNAL) GeodesicExact._GenInverse
259 _DEBUG_AREA = 1 << 20 # (INTERNAL) include Line details
260 _DEBUG_DIRECT = 1 << 21 # (INTERNAL) include Direct details
261 _DEBUG_INVERSE = 1 << 22 # (INTERNAL) include Inverse details
262 _DEBUG_LINE = 1 << 23 # (INTERNAL) include Line details
263 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \
264 _DEBUG_LINE | _SALP_CALPs_
266 _OUT_ALL = ALL # see geographiclib.geodesiccapabilities.py
267 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL
269 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE
270 _AZIMUTH_LATITUDE_LONG_UNROLL = AZIMUTH | LATITUDE | LONG_UNROLL
271 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE
272# _DISTANCE_IN_OUT = DISTANCE_IN & _OUT_MASK # == DISTANCE_IN in .gx, .gxline
273 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE
274# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE
276 def items(self):
277 '''Yield all I{public} C{Caps} as 2-tuple C{(NAME, mask)}.
278 '''
279 for n, C in Caps.__class__.__dict__.items():
280 if isint(C) and not n.startswith(_UNDER_) \
281 and n.replace(_UNDER_, NN).isupper():
282 yield n, C
284 def toStr(self, Cask, sep=_BAR_):
285 '''Return C{Caps} or an C{outmask} as C{str} or tuple of C{str}s.
286 '''
287 s = (n for n, C in sorted(Caps.items())
288 if C and (Cask & C) == C) # and int1s(C) <= 3
289 return sep.join(s) if sep else tuple(s)
291Caps = Caps() # PYCHOK singleton
292'''I{Enum}-style masks to be bit-C{or}'ed to specify geodesic or
293rhumb capabilities (C{caps}) and results (C{outmask}).
295C{AREA} - compute area C{S12},
297C{AZIMUTH} - include azimuths C{azi1} and C{azi2},
299C{DISTANCE} - compute distance C{s12} and angular distance C{a12},
301C{DISTANCE_IN} - allow distance C{s12} in C{.Direct},
303C{EMPTY} - nothing, formerly aka C{NONE},
305C{GEODESICSCALE} - compute geodesic scales C{M12} and C{M21},
307C{LATITUDE} - compute latitude C{lat2},
309C{LINE_OFF} - Line without updates from parent geodesic or rhumb.
311C{LONGITUDE} - compute longitude C{lon2},
313C{LONG_UNROLL} - unroll C{lon2} in C{.Direct},
315C{REDUCEDLENGTH} - compute reduced length C{m12},
317C{REVERSE2} - reverse C{azi2},
319and C{ALL} - all of the above.
321C{STANDARD} = C{AZIMUTH | DISTANCE | DISTANCE_IN | LATITUDE | LONGITUDE}'''
323_key2Caps = dict(a12 =Caps.DISTANCE, # in GDict._unCaps
324 azi2=Caps.AZIMUTH,
325 lat2=Caps.LATITUDE,
326 lon2=Caps.LONGITUDE,
327 m12 =Caps.REDUCEDLENGTH,
328 M12 =Caps.GEODESICSCALE,
329 M21 =Caps.GEODESICSCALE,
330 s12 =Caps.DISTANCE,
331 S12 =Caps.AREA)
334class _CapsBase(_NamedBase): # in .auxilats, .geodesicx.gxbases
335 '''(INTERNAL) Base class for C{Geodesic*}, C{Geodesic*Exact}, C{Intersectool} and C{Rhumb*Base}.
336 '''
337 ALL = Caps.ALL
338 AREA = Caps.AREA
339 AZIMUTH = Caps.AZIMUTH
340 DISTANCE = Caps.DISTANCE
341 DISTANCE_IN = Caps.DISTANCE_IN
342 EMPTY = Caps.EMPTY # aka NONE
343 GEODESICSCALE = Caps.GEODESICSCALE
344 LATITUDE = Caps.LATITUDE
345 LINE_CAPS = Caps.LINE_CAPS
346 LINE_OFF = Caps.LINE_OFF
347 LONGITUDE = Caps.LONGITUDE
348 LONG_UNROLL = Caps.LONG_UNROLL
349 REDUCEDLENGTH = Caps.REDUCEDLENGTH
350 STANDARD = Caps.STANDARD
351 _STD_LINE = Caps._STD_LINE # for geodesici
353 _caps = 0 # None
354 _debug = 0 # or Caps._DEBUG_...
356 @Property_RO
357 def caps(self):
358 '''Get the capabilities (bit-or'ed C{Caps}).
359 '''
360 return self._caps
362 def caps_(self, caps):
363 '''Check the available capabilities.
365 @arg caps: Bit-or'ed combination of L{Caps} values
366 for all capabilities to be checked.
368 @return: C{True} if I{all} B{C{caps}} are available,
369 C{False} otherwise (C{bool}).
370 '''
371 caps &= Caps._OUT_ALL
372 return (self.caps & caps) == caps
374 @property
375 def debug(self):
376 '''Get the C{debug} option (C{bool}).
377 '''
378 return bool(self._debug)
380 @debug.setter # PYCHOK setter!
381 def debug(self, debug):
382 '''Set the C{debug} option (C{bool}) to include
383 more details in L{GDict} results.
384 '''
385 self._debug = Caps._DEBUG_ALL if debug else 0
387 def _iter2tion(self, r, iter=None, **unused):
388 '''(INTERNAL) Copy C{C{s}.iter} into C{B{r}._iteration}.
389 '''
390 if iter is not None:
391 self._iteration = r._iteration = iter
392 return r
395class Direct9Tuple(_GTuple):
396 '''9-Tuple C{(a12, lat2, lon2, azi2, s12, m12, M12, M21, S12)} with arc
397 length C{a12}, angles C{lat2}, C{lon2} and azimuth C{azi2} in C{degrees},
398 distance C{s12} and reduced length C{m12} in C{meter} and area C{S12} in
399 C{meter} I{squared}.
400 '''
401 _Names_ = (_a12_, _lat2_, _lon2_, _azi2_, _s12_, _m12_, _M12_, _M21_, _S12_)
402 _Units_ = (_Azi, _Lat, _Lon, _Azi, _M, _Pass, _Pass, _Pass, _M2)
405class GDict(ADict): # XXX _NamedDict
406 '''A C{dict} with both C{key} I{and} C{attribute} access to the C{dict} items.
408 Results of all C{geodesic} and C{rhumb} methods (with capitalized named) are
409 returned as L{GDict} instances, see for example L{GeodesicExact} and L{RhumbAux}.
410 '''
411 def toDirect9Tuple(self, dflt=NAN):
412 '''Convert this L{GDict} result to a 9-tuple, like I{Karney}'s method
413 C{geographiclib.geodesic.Geodesic._GenDirect}.
415 @kwarg dflt: Default value for missing items (C{any}).
417 @return: L{Direct9Tuple}C{(a12, lat2, lon2, azi2,
418 s12, m12, M12, M21, S12)}
419 '''
420 return self._toTuple(Direct9Tuple, dflt)
422 def toGeodSolve12Tuple(self, dflt=NAN): # PYCHOK 12 args
423 '''Convert this L{GDict} result to a 12-Tuple, compatible with I{Karney}'s
424 U{GeodSolve<https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>}
425 result.
427 @kwarg dflt: Default value for missing items (C{any}).
429 @return: L{GeodSolve12Tuple}C{(lat1, lon1, azi1, lat2, lon2, azi2,
430 s12, a12, m12, M12, M21, S12)}.
431 '''
432 return self._toTuple(_MODS.geodsolve.GeodSolve12Tuple, dflt)
434 def toInverse10Tuple(self, dflt=NAN):
435 '''Convert this L{GDict} result to a 10-tuple, like I{Karney}'s
436 method C{geographiclib.geodesic.Geodesic._GenInverse}.
438 @kwarg dflt: Default value for missing items (C{any}).
440 @return: L{Inverse10Tuple}C{(a12, s12, salp1, calp1,
441 salp2, calp2, m12, M12, M21, S12)}.
442 '''
443 return self._toTuple(Inverse10Tuple, dflt)
445 def _toNAN(self, outmask): # .GeodesicLineExact._GenPosition
446 '''(INTERNAL) Convert this C{GDict} to all C{NAN}s.
447 '''
448 d = dict((n, NAN) for n in GeodSolve12Tuple._Names_)
449 return self.set_(**d)._unCaps(outmask)
451 @deprecated_method
452 def toRhumb7Tuple(self, dflt=NAN): # PYCHOK no cover
453 '''DEPRECATED on 23.12.07, use method C{toRhumb8Tuple}.
455 @return: A I{DEPRECATED} L{Rhumb7Tuple}.
456 '''
457 return self._toTuple(_MODS.deprecated.classes.Rhumb7Tuple, dflt)
459 def toRhumb8Tuple(self, dflt=NAN):
460 '''Convert this L{GDict} result to a 8-tuple.
462 @kwarg dflt: Default value for missing items (C{any}).
464 @return: L{Rhumb8Tuple}C{(lat1, lon1, lat2, lon2,
465 azi12, s12, S12, a12)}.
466 '''
467 return self._toTuple(Rhumb8Tuple, dflt)
469 def toRhumbSolve7Tuple(self, dflt=NAN):
470 '''Convert this L{GDict} result to a 8-tuple.
472 @kwarg dflt: Default value for missing items (C{any}).
474 @return: L{RhumbSolve7Tuple}C{(lat1, lon1, lat2, lon2,
475 azi12, s12, S12)}.
476 '''
477 return self._toTuple(_MODS.rhumb.solve.RhumbSolve7Tuple, dflt)
479 def _toTuple(self, nTuple, dflt):
480 '''(INTERNAL) Convert this C{GDict} to an B{C{nTuple}}.
481 '''
482 _g = getattr
483 t = tuple(_g(self, n, dflt) for n in nTuple._Names_)
484 return nTuple(t, iteration=self._iteration)
486 def _2X(self, gl, _2X=_X_): # .Intersectool, .Intersector
487 '''(INTERNAL) Rename C{-2} attr to C{-X} or C{-M}.
488 '''
489 X = GDict(self)
490 for n in (_lat2_, _lon2_, _azi2_, _s12_, _a12_):
491 if n in X: # X._X = X._2
492 X[n[:-1] + _2X] = X.pop(n)
493 v = getattr(gl, n, X)
494 if v is not X: # X._2 = gl._2
495 X[n] = v
496 return X
498 def _unCaps(self, outmask): # in .geodsolve
499 '''(INTERNAL) Remove superfluous items.
500 '''
501 for k, C in _key2Caps.items():
502 if k in self and (outmask & C) != C:
503 self.pop(k) # delattr(self, k)
504 return self
507class GeodSolve12Tuple(_GTuple):
508 '''12-Tuple C{(lat1, lon1, azi1, lat2, lon2, azi2, s12, a12, m12, M12, M21, S12)} with
509 angles C{lat1}, C{lon1}, C{azi1}, C{lat2}, C{lon2} and C{azi2} and arc C{a12} all in
510 C{degrees}, initial C{azi1} and final C{azi2} forward azimuths, distance C{s12} and
511 reduced length C{m12} in C{meter}, area C{S12} in C{meter} I{squared} and geodesic
512 scale factors C{M12} and C{M21}, both C{scalar}, see U{GeodSolve
513 <https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>}.
514 '''
515 # from GeodSolve --help option -f ... lat1 lon1 azi1 lat2 lon2 azi2 s12 a12 m12 M12 M21 S12
516 _Names_ = (_lat1_, _lon1_, _azi1_, _lat2_, _lon2_, _azi2_, _s12_, _a12_, _m12_, _M12_, _M21_, _S12_)
517 _Units_ = (_Lat, _Lon, _Azi, _Lat, _Lon, _Azi, _M, _Deg, _Pass, _Pass, _Pass, _M2)
520class Inverse10Tuple(_GTuple):
521 '''10-Tuple C{(a12, s12, salp1, calp1, salp2, calp2, m12, M12, M21, S12)} with arc length
522 C{a12} in C{degrees}, distance C{s12} and reduced length C{m12} in C{meter}, area
523 C{S12} in C{meter} I{squared} and the sines C{salp1}, C{salp2} and cosines C{calp1},
524 C{calp2} of the initial C{1} and final C{2} (forward) azimuths.
525 '''
526 _Names_ = (_a12_, _s12_, 'salp1', 'calp1', 'salp2', 'calp2', _m12_, _M12_, _M21_, _S12_)
527 _Units_ = (_Azi, _M, _Pass, _Pass, _Pass, _Pass, _Pass, _Pass, _Pass, _M2)
529 def toGDict(self, **updates):
530 '''Convert this C{Inverse10Tuple} to a L{GDict}.
532 @kwarg updates: Optional items to apply (C{nam=value} pairs)
533 '''
534 return _GTuple.toGDict(self, azi1=atan2d(self.salp1, self.calp1), # PYCHOK namedTuple
535 azi2=atan2d(self.salp2, self.calp2), # PYCHOK namedTuple
536 **updates) # PYCHOK indent
539class _kWrapped(object): # in .geodesicw
540 ''''(INTERNAL) Wrapper for some of I{Karney}'s U{geographiclib
541 <https://PyPI.org/project/geographiclib>} classes.
542 '''
544 @property_ROnce
545 def geographiclib(self):
546 '''Lazily import C{geographiclib}, provided the U{geographiclib
547 <https://PyPI.org/project/geographiclib>} package is installed,
548 otherwise raise a C{LazyImportError}.
549 '''
550 g = _xgeographiclib(self.__class__.__module__, 1, 49)
551 from geographiclib.geodesic import Geodesic
552 g.Geodesic = Geodesic
553 from geographiclib.geodesicline import GeodesicLine
554 g.GeodesicLine = GeodesicLine
555 from geographiclib.geomath import Math
556 g.Math = Math
557 return g
559 @property_ROnce
560 def Math(self):
561 '''Wrap the C{geomath.Math} class, provided the U{geographiclib
562 <https://PyPI.org/project/geographiclib>} package is installed,
563 otherwise C{None}.
564 '''
565 try:
566 g = self.geographiclib
567 M = g.Math
568 if _version_info(g) < (2,):
569 if _K_2_0:
570 M = None
571# elif not _K_2_0: # XXX set 2.0?
572# _K_2_0 = True
573 except (AttributeError, ImportError):
574 M = None
575 return M
577 @property_RO
578 def Math_K_2(self):
579 return ('_K_2_4' if _K_2_4 else
580 ('_K_2_0' if _K_2_0 else '_K_1_0')) if self.Math else NN
582_wrapped = _kWrapped() # PYCHOK singleton, .datum, .test/base.py
585class Rhumb8Tuple(_GTuple):
586 '''8-Tuple C{(lat1, lon1, lat2, lon2, azi12, s12, S12, a12)} with lat- C{lat1},
587 C{lat2} and longitudes C{lon1}, C{lon2} of both points, the azimuth of the
588 rhumb line C{azi12}, the distance C{s12}, the area C{S12} under the rhumb
589 line and the angular distance C{a12} between both points.
590 '''
591 _Names_ = (_lat1_, _lon1_, _lat2_, _lon2_, _azi12_, _s12_, _S12_, _a12_)
592 _Units_ = ( Lat, Lon, Lat, Lon, _Azi, _M, _M2, _Deg)
594 def toDirect9Tuple(self, dflt=NAN, **a12_azi1_azi2_m12_M12_M21):
595 '''Convert this L{Rhumb8Tuple} result to a 9-tuple, like I{Karney}'s
596 method C{geographiclib.geodesic.Geodesic._GenDirect}.
598 @kwarg dflt: Default value for missing items (C{any}).
599 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments
600 to specify or override L{Inverse10Tuple} items.
602 @return: L{Direct9Tuple}C{(a12, lat2, lon2, azi2, s12,
603 m12, M12, M21, S12)}
604 '''
605 d = dict(azi1=self.azi12, M12=_1_0, m12=self.s12, # PYCHOK attr
606 azi2=self.azi12, M21=_1_0) # PYCHOK attr
607 if a12_azi1_azi2_m12_M12_M21:
608 d.update(a12_azi1_azi2_m12_M12_M21)
609 return self._toTuple(Direct9Tuple, dflt, d)
611 def toInverse10Tuple(self, dflt=NAN, **a12_m12_M12_M21_salp1_calp1_salp2_calp2):
612 '''Convert this L{Rhumb8Tuple} to a 10-tuple, like I{Karney}'s
613 method C{geographiclib.geodesic.Geodesic._GenInverse}.
615 @kwarg dflt: Default value for missing items (C{any}).
616 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword
617 arguments to specify or override L{Inverse10Tuple} items.
619 @return: L{Inverse10Tuple}C{(a12, s12, salp1, calp1, salp2, calp2,
620 m12, M12, M21, S12)}.
621 '''
622 s, c = sincos2d(self.azi12) # PYCHOK attr
623 d = dict(salp1=s, calp1=c, M12=_1_0, m12=self.s12, # PYCHOK attr
624 salp2=s, calp2=c, M21=_1_0)
625 if a12_m12_M12_M21_salp1_calp1_salp2_calp2:
626 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2)
627 return self._toTuple(Inverse10Tuple, dflt, d)
629 def _toTuple(self, nTuple, dflt, updates={}):
630 '''(INTERNAL) Convert this C{Rhumb8Tuple} to an B{C{nTuple}}.
631 '''
632 _g = self.toGDict(**updates).get
633 t = tuple(_g(n, dflt) for n in nTuple._Names_)
634 return nTuple(t, name=self.name)
636 @deprecated_method
637 def _to7Tuple(self):
638 '''DEPRECATED, do not use!'''
639 return _MODS.deprecated.classes.Rhumb7Tuple(self[:-1])
642def _around(x): # in .utily.sincos2d
643 '''I{Coarsen} a scalar by rounding small values to underflow to C{0.0}.
645 @return: Coarsened value (C{float}).
647 @see: I{Karney}'s U{geomath.Math.AngRound<https://SourceForge.net/p/
648 geographiclib/code/ci/release/tree/python/geographiclib/geomath.py>}
649 '''
650 try:
651 return _wrapped.Math.AngRound(x)
652 except AttributeError:
653 if x:
654 y = _1_16th - fabs(x)
655 if y > 0: # fabs(x) < _1_16th
656 x = _copysign(_1_16th - y, x)
657 else:
658 x = _0_0 # -0 to 0
659 return x
662def _atan2d(y, x):
663 '''Return C{atan2(B{y}, B{x})} in C{degrees}.
664 '''
665 try:
666 return _wrapped.Math.atan2d(y, x)
667 except AttributeError:
668 return atan2d(y, x)
671def _cbrt(x):
672 '''Return C{cubic root(B{x})}.
673 '''
674 try:
675 return _wrapped.Math.cbrt(x)
676 except AttributeError:
677 return cbrt(x)
680def _copyBit(x, y):
681 '''Like C{copysign0(B{x}, B{y})}, with C{B{x} > 0}.
682 '''
683 return (-x) if _signBit(y) else x
686def _2cos2x(cx, sx): # in .auxDST, .auxLat, .gxbases
687 '''Return M{2 * cos(2 * x)} from cos(x) and sin(x).
688 '''
689 r = cx - sx
690 if r:
691 r *= (cx + sx) * _2_0
692 return r
695def _diff182(deg0, deg, K_2_0=False):
696 '''Compute C{deg - deg0}, reduced to C{[-180,180]} accurately.
698 @return: 2-Tuple C{(delta_angle, residual)} in C{degrees}.
699 '''
700 try:
701 return _wrapped.Math.AngDiff(deg0, deg)
702 except AttributeError:
703 if K_2_0 or _K_2_0: # geographiclib 2.0
704 _r, _360 = fremainder, _360_0
705 d, t = _sum2(_r(-deg0, _360),
706 _r( deg, _360))
707 d, t = _sum2(_r( d, _360), t)
708 if d in (_0_0, _180_0, _N_180_0):
709 d = _copysign(d, -t if t else (deg - deg0))
710 else:
711 _n = _norm180
712 d, t = _sum2(_n(-deg0), _n(deg))
713 d = _n(d)
714 if t > 0 and d == _180_0:
715 d = _N_180_0
716 d, t = _sum2(d, t)
717 return d, t
720def _fix90(deg): # mimick Math.LatFix
721 '''Replace angle in C{degrees} outside [-90,90] by NAN.
723 @return: Angle C{degrees} or NAN.
724 '''
725 try:
726 return _wrapped.Math.LatFix(deg)
727 except AttributeError:
728 return NAN if fabs(deg) > 90 else deg
731def _isfinite(x): # mimick geomath.Math.isfinite
732 '''Check finiteness of C{x}.
734 @return: C{True} if finite.
735 '''
736 try:
737 return _wrapped.Math.isfinite(x)
738 except AttributeError:
739 return _math_isfinite(x) # and fabs(x) <= _MAX
742def _llz2gl(gl, **llz2): # see .geodesici._llz2G
743 '''(INTERNAL) Set C{gl.lat2, .lon2, .azi2} from C{llz2}.
744 '''
745 if llz2:
746 for n in (_lat2_, _lon2_, _azi2_): # _lat1_, _lon1_, _azi1_
747 v = llz2.get(n, None)
748 if v is not None:
749 setattr(gl, n, v)
750 return gl
753def _norm2(x, y): # mimick geomath.Math.norm
754 '''Normalize C{B{x}} and C{B{y}}.
756 @return: 2-Tuple of C{(B{x}, B{y})}, normalized.
757 '''
758 try:
759 return _wrapped.Math.norm(x, y)
760 except AttributeError:
761 return norm2(x, y)
764def _norm180(deg): # mimick geomath.Math.AngNormalize
765 '''Reduce angle in C{degrees} to (-180,180].
767 @return: Reduced angle C{degrees}.
768 '''
769 try:
770 return _wrapped.Math.AngNormalize(deg)
771 except AttributeError:
772 d = fremainder(deg, _360_0)
773 if d in (_180_0, _N_180_0):
774 d = _copysign(_180_0, deg) if _K_2_0 else _180_0
775 return d
778def _polygon(geodesic, points, closed, line, wrap):
779 '''(INTERNAL) Compute the area or perimeter of a polygon,
780 using a L{GeodesicExact}, L{GeodesicSolve} or (if the
781 C{geographiclib} package is installed) a C{Geodesic}
782 or C{geodesicw.Geodesic} instance.
783 '''
784 if not wrap: # capability LONG_UNROLL can't be off
785 notImplemented(None, wrap=wrap, up=3)
787 if _MODS.booleans.isBoolean(points):
788 # recursive call for each boolean clip
790 def _a_p(clip, *args, **unused):
791 return _polygon(geodesic, clip, *args)
793 if not closed: # closed only
794 raise _ValueError(closed=closed, points=_composite_)
796 return points._sum1(_a_p, closed, line, wrap)
798 gP = geodesic.Polygon(line)
799 _A = gP.AddPoint
801 Ps = _MODS.iters.PointsIter(points, loop=1, wrap=wrap) # base=LatLonEllipsoidalBase(0, 0)
802 p1 = p0 = Ps[0]
804 # note, lon deltas are unrolled, by default
805 _A(p1.lat, p1.lon)
806 for p2 in Ps.iterate(closed=closed):
807 if wrap and not Ps.looped:
808 p2 = _unrollon(p1, p2)
809 _A(p2.lat, p2.lon)
810 p1 = p2
811 if closed and line and p1 != p0:
812 _A(p0.lat, p0.lon)
814 # gP.Compute returns (number_of_points, perimeter, signed area)
815 return gP.Compute(False, True)[1 if line else 2]
818try:
819 from math import fma as _fma # since 3.13
821 def _poly_fma(x, s, *cs):
822 for c in cs:
823 s = _fma(s, x, c)
824 return s
826except ImportError: # Python 3.12-
828 def _poly_fma(x, s, *cs): # PYCHOK redef
829 t = _0_0
830 for c in cs:
831 s, t, _ = _sum3(s * x, t * x, c)
832 return s + t
834# def _poly_fma(x, *cs):
835# S = Fhorner(x, *cs, incx=False)
836# return float(S)
838def _polynomial(x, cs, i, j): # PYCHOK shared
839 '''(INTERNAL) Like C++ C{GeographicLib.Math.hpp.polyval} but with a
840 signature and cascaded summation different from C{karney._sum3}.
842 @return: M{sum(x**(j - k - 1) * cs[k] for k in range(i, j)}
843 '''
844 if (i + 1) < j <= len(cs): # load _Rtuple._tuple
845 try:
846 r = _wrapped.Math.polyval(j - i - 1, cs, i, x)
847 except AttributeError:
848 r = _poly_fma(x, *cs[i:j])
849 else:
850 r = cs[i]
851 return float(r)
854def _remainder(x, y):
855 '''Remainder of C{x / y}.
857 @return: Remainder in the range M{[-y / 2, y / 2]}, preserving signed 0.0.
858 '''
859 try:
860 return _wrapped.Math.remainder(x, y)
861 except AttributeError:
862 return fremainder(x, y)
865if _K_2_0:
866 from math import cos as _cos, sin as _sin
868 def _sincos2(rad):
869 return _sin(rad), _cos(rad)
871 _signBit = _MODS.basics.signBit
872else:
873 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared
875 def _signBit(x):
876 '''(INTERNAL) GeographicLib 1.52-.
877 '''
878 return x < 0
881def _sincos2d(deg):
882 '''Return sine and cosine of an angle in C{degrees}.
884 @return: 2-Tuple C{(sin(B{deg}), cos(B{deg}))}.
885 '''
886 try:
887 return _wrapped.Math.sincosd(deg)
888 except AttributeError:
889 return sincos2d(deg)
892def _sincos2de(deg, t):
893 '''Return sine and cosine of a corrected angle in C{degrees}.
895 @return: 2-Tuple C{(sin(B{deg}), cos(B{deg}))}.
896 '''
897 try:
898 return _wrapped.Math.sincosde(deg, t)
899 except AttributeError:
900 return sincos2d(deg, adeg=t)
903def _sum2(a, b): # mimick geomath.Math.sum, actually sum2
904 '''Error-free summation like C{geomath.Math.sum}.
906 @return: 2-Tuple C{(B{a} + B{b}, residual)}.
908 @note: The C{residual} can be the same as B{C{a}} or B{C{b}}.
910 @see: U{TwoSum<https://accurate-algorithms.readthedocs.io/en/latest/ch04summation.html>}
911 and I{Knuth}'s U{Algorithm 3.1<https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>}.
912 '''
913 try:
914 return _wrapped.Math.sum(a, b)
915 except AttributeError:
916 # if Algorithm_3_1:
917 s = a + b
918 r = s - b
919 t = s - r
920 # elif C_CPP: # Math::sum C/C++
921 # r -= a; t -= b; t += r; t = -t
922 # else:
923 t = (a - r) + (b - t)
924 # assert fabs(s) >= fabs(t)
925 return s, t
928def _sum3(s, t, *xs):
929 '''Accumulate any B{C{xs}} into a previous C{_sum2(s, t)}.
931 @return: 3-Tuple C{(s, t, n)} where C{s} is the sum of B{s}, B{t} and all
932 B{xs}, C{t} the residual and C{n} the number of zero C{xs}.
934 @see: I{Karney's} C++ U{Accumulator<https://GeographicLib.SourceForge.io/
935 C++/doc/Accumulator_8hpp_source.html>} comments for more details and
936 function C{_sum2} above.
938 @note: Not "error-free", see C{pygeodesy.test/testKarney.py}.
939 '''
940 z = 0
941 for x in xs:
942 if x:
943 t, r = _sum2(t, x) # start at the least-
944 if s:
945 s, t = _sum2(s, t) # -significant end
946 if s:
947 t += r # accumulate r into t
948 else:
949 # assert t == 0 # s == 0 implies t == 0
950 s = unsigned0(r) # result is r, t = 0
951 else:
952 s, t = unsigned0(t), r
953 else:
954 z += 1
955 # assert fabs(s) >= fabs(t)
956 return s, t, z
959def _tand(x):
960 '''Return C{tan(B{x})} in C{degrees}.
961 '''
962 try:
963 return _wrapped.Math.tand(x)
964 except AttributeError:
965 return tand(x)
968def _unroll2(lon1, lon2, wrap=False): # see .ellipsoidalBaseDI._intersects2
969 '''Unroll B{C{lon2 - lon1}} like C{geodesic.Geodesic.Inverse}.
971 @return: 2-Tuple C{(B{lon2} - B{lon1}, B{lon2})} with B{C{lon2}}
972 unrolled if C{B{wrap} is True}, normalized otherwise.
973 '''
974 if wrap:
975 d, t = _diff182(lon1, lon2)
976 lon2, t, _ = _sum3(d, t, lon1) # (lon1 + d) + t
977 lon2 += t
978 else:
979 lon2 = _norm180(lon2)
980 return (lon2 - lon1), lon2
983def _unsigned2(x):
984 '''(INTERNAL) Unsign B{C{x}}.
985 '''
986 return (neg(x), True) if _signBit(x) else (x, False)
989__all__ += _ALL_DOCS(_CapsBase)
991# **) MIT License
992#
993# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
994#
995# Permission is hereby granted, free of charge, to any person obtaining a
996# copy of this software and associated documentation files (the "Software"),
997# to deal in the Software without restriction, including without limitation
998# the rights to use, copy, modify, merge, publish, distribute, sublicense,
999# and/or sell copies of the Software, and to permit persons to whom the
1000# Software is furnished to do so, subject to the following conditions:
1001#
1002# The above copyright notice and this permission notice shall be included
1003# in all copies or substantial portions of the Software.
1004#
1005# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1006# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1007# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1008# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1009# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1010# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1011# OTHER DEALINGS IN THE SOFTWARE.