Coverage for pygeodesy/karney.py: 94%
287 statements
« prev ^ index » next coverage.py v7.2.2, created at 2024-05-25 12:04 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2024-05-25 12:04 -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.
7The I{wrapped} class methods return a L{GDict} instance offering access to the C{dict} items
8either by C{key} or by C{attribute} name.
9All methods of the I{wrapped} L{Geodesic<geodesicw.Geodesic>} and L{GeodesicLine<geodesicw.GeodesicLine>}
10classes return a L{GDict} instance offering access to the C{dict} items either by C{key} or by
11C{attribute} name.
13With env variable C{PYGEODESY_GEOGRAPHICLIB} left undefined or set to C{"2"}, modules L{geodesicx},
14L{geodesicw} and this module will use U{GeographicLib 2.0<https://GeographicLib.SourceForge.io/C++/doc/>}
15and newer transcoding, otherwise C{1.52} or older.
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{JacobiConformal} -- U{JacobiConformal
64 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1JacobiConformal.html#details>}
66 - L{KTransverseMercator} - U{TransverseMercator
67 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>}
69 - L{LocalCartesian}, L{Ltp} -- U{LocalCartesian<https://GeographicLib.SourceForge.io/C++/doc/
70 classGeographicLib_1_1LocalCartesian.html>}
72 - L{Osgr} -- U{OSGB<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1OSGB.html>}
74 - L{rhumb.aux_}, L{RhumbAux}, L{RhumbLineAux} -- U{Rhumb
75 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Rhumb.html>} and U{RhumbLine
76 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1RhumbLine.html>} from I{GeographicLib 2.2+}
78 - L{rhumb.ekx}, L{Rhumb}, L{RhumbLine} -- U{Rhumb
79 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Rhumb.html>},
80 U{RhumbLine<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1RhumbLine.html>},
81 U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>}
82 from I{GeographicLib 2.0}
84 - L{Ups} -- U{PolarStereographic<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1PolarStereographic.html>}
86 - L{Utm} -- U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>}
88 - L{UtmUps}, L{Epsg} -- U{UTMUPS<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1UTMUPS.html>}
90 - L{atan1d}, L{atan2d}, L{sincos2}, L{sincos2d}, L{tand} -- U{geomath.Math
91 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Math.html>}
93are I{transcoded} from C++ classes in I{Karney}'s U{GeographicLib<https://GeographicLib.SourceForge.io/C++/doc/annotated.html>}.
952. These C{pygeodesy} modules and classes
97 - L{ellipsoidalGeodSolve}, L{ellipsoidalKarney}, L{geodsolve}, L{karney}, L{rhumb.solve}
98 - L{EquidistantKarney}, L{FrechetKarney}, L{GeodesicSolve}, L{GeodesicLineSolve}, L{GnomonicGeodSolve},
99 L{GnomonicKarney}, L{HeightIDWkarney}
101are or use I{wrappers} around I{Karney}'s Python U{geographiclib<https://PyPI.org/project/geographiclib>}
102C{geodesic}, C++ utility U{GeodSolve<https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>} or
103C++ utility U{RhumbSolve<https://GeographicLib.SourceForge.io/C++/doc/RhumbSolve.1.html>}.
1053. All C{pygeodesy} functions and methods to compute I{ellipsoidal} intersections, nearest points and trilaterations
107 - L{ellipsoidalExact.intersection3}, L{ellipsoidalExact.intersections2}, L{ellipsoidalExact.nearestOn},
108 L{ellipsoidalExact.LatLon.intersection3}, L{ellipsoidalExact.LatLon.intersections2},
109 L{ellipsoidalExact.LatLon.nearestOn}, L{ellipsoidalExact.LatLon.trilaterate5}
111 - L{ellipsoidalKarney.intersection3}, L{ellipsoidalKarney.intersections2}, L{ellipsoidalKarney.nearestOn},
112 L{ellipsoidalKarney.LatLon.intersection3}, L{ellipsoidalKarney.LatLon.intersections2},
113 L{ellipsoidalKarney.LatLon.nearestOn}, L{ellipsoidalKarney.LatLon.trilaterate5}
115 - L{ellipsoidalVincenty.intersection3}, L{ellipsoidalVincenty.intersections2}, L{ellipsoidalVincenty.nearestOn},
116 L{ellipsoidalVincenty.LatLon.intersection3}, L{ellipsoidalVincenty.LatLon.intersections2},
117 L{ellipsoidalVincenty.LatLon.nearestOn}, L{ellipsoidalVincenty.LatLon.trilaterate5}
119 - L{RhumbLineAux.Intersection} and L{RhumbLine.Intersection}
121are implementations of I{Karney}'s iterative solution posted under U{The B{ellipsoidal} case
122<https://GIS.StackExchange.com/questions/48937/calculating-intersection-of-two-circles>} and in paper U{Geodesics
123on an ellipsoid of revolution<https://ArXiv.org/pdf/1102.1215.pdf>} (pp 20-21, section B{14. MARITIME BOUNDARIES}).
1254. The C{pygeodesy} methods to compute I{ellipsoidal} intersections and nearest points
127 - L{RhumbLineAux.Intersecant2}, L{RhumbLineAux.PlumbTo}, L{RhumbLine.Intersecant2} and L{RhumbLine.PlumbTo}
129are I{transcoded} of I{Karney}'s iterative C++ function U{rhumb-intercept
130<https://SourceForge.net/p/geographiclib/discussion/1026620/thread/2ddc295e/>}.
1325. Spherical functions
134 - L{pygeodesy.excessKarney_}, L{sphericalTrigonometry.areaOf}
136in C{pygeodesy} are based on I{Karney}'s post U{Area of a spherical polygon
137<https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}, 3rd Answer.
138'''
139# make sure int/int division yields float quotient, see .basics
140from __future__ import division as _; del _ # PYCHOK semicolon
142from pygeodesy.basics import _copysign, int1s, isint, itemsorted, neg, unsigned0, \
143 _xgeographiclib, _zip, _version_info
144from pygeodesy.constants import NAN, _isfinite as _math_isfinite, _0_0, \
145 _1_16th, _1_0, _2_0, _180_0, _N_180_0, _360_0
146from pygeodesy.errors import GeodesicError, _ValueError, _xkwds, _xkwds_get
147from pygeodesy.fmath import cbrt, fremainder, norm2
148# from pygeodesy.internals import _version_info # from .basics
149from pygeodesy.interns import NN, _2_, _a12_, _area_, _azi2_, _azi12_, _composite_, \
150 _lat1_, _lat2_, _lon1_, _lon2_, _m12_, _M12_, _M21_, \
151 _number_, _s12_, _S12_, _UNDER_, _BAR_ # PYCHOK used!
152from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _getenv
153from pygeodesy.named import ADict, _NamedBase, _NamedTuple, notImplemented, _Pass
154from pygeodesy.props import deprecated_method, Property_RO
155from pygeodesy.units import Bearing as _Azi, Degrees as _Deg, Lat, Lon, \
156 Meter as _M, Meter2 as _M2, Number_
157from pygeodesy.utily import atan2d, sincos2d, tand, _unrollon, fabs
159# from math import fabs # from .utily
161__all__ = _ALL_LAZY.karney
162__version__ = '24.05.20'
164_K_2_0 = _getenv('PYGEODESY_GEOGRAPHICLIB', _2_) == _2_
165_perimeter_ = 'perimeter'
168class _GTuple(_NamedTuple): # in .testNamedTuples
169 '''(INTERNAL) Helper.
170 '''
171 def toGDict(self, **updates): # NO name=NN
172 '''Convert this C{*Tuple} to a L{GDict}.
174 @kwarg updates: Optional items to apply (C{nam=value} pairs)
175 '''
176 r = GDict(_zip(self._Names_, self)) # strict=True
177 if updates:
178 r.update(updates)
179 if self._iteration is not None:
180 r._iteration = self._iteration
181 return r
184class _Lat(Lat):
185 '''(INTERNAL) Latitude B{C{lat}}.
186 '''
187 def __init__(self, *lat, **Error_name):
188 kwds = _xkwds(Error_name, clip=0, Error=GeodesicError)
189 Lat.__new__(_Lat, *lat, **kwds)
192class _Lon(Lon):
193 '''(INTERNAL) Longitude B{C{lon}}.
194 '''
195 def __init__(self, *lon, **Error_name):
196 kwds = _xkwds(Error_name, clip=0, Error=GeodesicError)
197 Lon.__new__(_Lon, *lon, **kwds)
200class Area3Tuple(_NamedTuple): # in .geodesicx.gxarea
201 '''3-Tuple C{(number, perimeter, area)} with the C{number}
202 of points of the polygon or polyline, the C{perimeter} in
203 C{meter} and the C{area} in C{meter} I{squared}.
204 '''
205 _Names_ = (_number_, _perimeter_, _area_)
206 _Units_ = ( Number_, _M, _M2)
209class Caps(object): # PYCHOK
210 '''(INTERNAL) Overriden by C{Caps} below.
211 '''
212 EMPTY = 0 # formerly aka NONE
213 _CAP_1 = 1 << 0 # for goedesicw only
214 _CAP_1p = 1 << 1 # for goedesicw only
215# _CAP_2 = 1 << 2
216 _CAP_3 = 1 << 3 # for goedesicw only
217# _CAP_4 = 1 << 4
218# _CAP_ALL = 0x1F
219# _CAP_MASK = _CAP_ALL
220 LATITUDE = 1 << 7 # compute latitude C{lat2}
221 LONGITUDE = 1 << 8 # compute longitude C{lon2} _CAP_3
222 AZIMUTH = 1 << 9 # azimuths C{azi1} and C{azi2}
223 DISTANCE = 1 << 10 # compute distance C{s12} _CAP_1
224 DISTANCE_IN = 1 << 11 # allow distance C{s12} in Direct _CAP_1 | _CAP_1p
225 REDUCEDLENGTH = 1 << 12 # compute reduced length C{m12} _CAP_1 | _CAP_2
226 GEODESICSCALE = 1 << 13 # compute geodesic scales C{M12} and C{M21} _CAP_1 | _CAP_2
227 AREA = 1 << 14 # compute area C{S12} _CAP_4
229 STANDARD = AZIMUTH | DISTANCE | DISTANCE_IN | LATITUDE | LONGITUDE
230 ALL = 0x7F80 # without LONG_UNROLL, LINE_OFF, REVERSE2 and _DEBUG_*
232 _DIRECT3 = AZIMUTH | LATITUDE | LONGITUDE | _CAP_3 # for goedesicw only
233 _INVERSE3 = AZIMUTH | DISTANCE | _CAP_1 # for goedesicw only
234 _STD = STANDARD | _CAP_3 | _CAP_1 # for goedesicw only
235 _STD_LINE = _STD | _CAP_1p # for goedesicw only
237 LINE_OFF = 1 << 15 # Line without updates from parent geodesic or rhumb
238 LONG_UNROLL = 1 << 16 # unroll C{lon2} in .Direct and .Position
239 REVERSE2 = 1 << 17 # reverse C{azi2}
241 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE
242 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA
244 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE
245 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA
247 _ANGLE_ONLY = 1 << 18 # angular distance C{a12} only
248 _SALPs_CALPs = 1 << 19 # (INTERNAL) GeodesicExact._GenInverse
250 _DEBUG_AREA = 1 << 20 # (INTERNAL) include Line details
251 _DEBUG_DIRECT = 1 << 21 # (INTERNAL) include Direct details
252 _DEBUG_INVERSE = 1 << 22 # (INTERNAL) include Inverse details
253 _DEBUG_LINE = 1 << 23 # (INTERNAL) include Line details
254 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \
255 _DEBUG_LINE | _ANGLE_ONLY | _SALPs_CALPs
257 _OUT_ALL = ALL
258 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL
260 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE
261 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE
262# _DISTANCE_IN_OUT = DISTANCE_IN & _OUT_MASK # == DISTANCE_IN in .gx, .gxline
263 _LINE = AZIMUTH | LATITUDE | LONG_UNROLL
264 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE
265# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE
267 def toStr(self, Csk, sep=_BAR_):
268 '''Return a C{Caps} or C{outmask} as C{str} or tuple of C{str}s.
269 '''
270 s = []
271 for c, C in itemsorted(self.__class__.__dict__):
272 if isint(C) and (Csk & C) and int1s(C) == 1 \
273 and (C in (Caps.REVERSE2, Caps._SALPs_CALPs)
274 or c.replace(_UNDER_, NN).isupper()):
275 s.append(c)
276 return sep.join(s) if sep else tuple(s)
278Caps = Caps() # PYCHOK singleton
279'''I{Enum}-style masks to be bit-C{or}'ed to specify geodesic or
280rhumb capabilities (C{caps}) and expected results (C{outmask}).
282C{AREA} - compute area C{S12},
284C{AZIMUTH} - include azimuths C{azi1} and C{azi2},
286C{DISTANCE} - compute distance C{s12},
288C{DISTANCE_IN} - allow distance C{s12} in C{.Direct},
290C{EMPTY} - nothing, formerly aka C{NONE},
292C{GEODESICSCALE} - compute geodesic scales C{M12} and C{M21},
294C{LATITUDE} - compute latitude C{lat2},
296C{LINE_OFF} - Line without updates from parent geodesic or rhumb.
298C{LONGITUDE} - compute longitude C{lon2},
300C{LONG_UNROLL} - unroll C{lon2} in C{.Direct},
302C{REDUCEDLENGTH} - compute reduced length C{m12},
304C{REVERSE2} - reverse C{azi2},
306and C{ALL} - all of the above.
308C{STANDARD} = C{AZIMUTH | DISTANCE | DISTANCE_IN | LATITUDE | LONGITUDE}'''
311class _CapsBase(_NamedBase): # in .auxilats, .geodesicx.gxbases
312 '''(INTERNAL) Base class for C{[_]Geodesic*Exact}.
313 '''
314 ALL = Caps.ALL
315 AREA = Caps.AREA
316 AZIMUTH = Caps.AZIMUTH
317 DISTANCE = Caps.DISTANCE
318 DISTANCE_IN = Caps.DISTANCE_IN
319 EMPTY = Caps.EMPTY # aka NONE
320 GEODESICSCALE = Caps.GEODESICSCALE
321 LATITUDE = Caps.LATITUDE
322 LINE_OFF = Caps.LINE_OFF
323 LONGITUDE = Caps.LONGITUDE
324 LONG_UNROLL = Caps.LONG_UNROLL
325 REDUCEDLENGTH = Caps.REDUCEDLENGTH
326 STANDARD = Caps.STANDARD
328 _caps = 0 # None
329 _debug = 0 # or Caps._DEBUG_...
331 @Property_RO
332 def caps(self):
333 '''Get the capabilities (bit-or'ed C{Caps}).
334 '''
335 return self._caps
337 def caps_(self, caps):
338 '''Check the available capabilities.
340 @arg caps: Bit-or'ed combination of L{Caps} values
341 for all capabilities to be checked.
343 @return: C{True} if I{all} B{C{caps}} are available,
344 C{False} otherwise (C{bool}).
345 '''
346 caps &= Caps._OUT_ALL
347 return (self.caps & caps) == caps
349 @property
350 def debug(self):
351 '''Get the C{debug} option (C{bool}).
352 '''
353 return bool(self._debug)
355 @debug.setter # PYCHOK setter!
356 def debug(self, debug):
357 '''Set the C{debug} option (C{bool}) to include
358 more details in L{GDict} results.
359 '''
360 self._debug = Caps._DEBUG_ALL if debug else 0
362 def _iter2tion(self, r, iter=None, **unused):
363 '''(INTERNAL) Copy C{C{s}.iter} into C{B{r}._iteration}.
364 '''
365 if iter is not None:
366 self._iteration = r._iteration = iter
367 return r
370class Direct9Tuple(_GTuple):
371 '''9-Tuple C{(a12, lat2, lon2, azi2, s12, m12, M12, M21, S12)} with arc
372 length C{a12}, angles C{lat2}, C{lon2} and azimuth C{azi2} in C{degrees},
373 distance C{s12} and reduced length C{m12} in C{meter} and area C{S12} in
374 C{meter} I{squared}.
375 '''
376 _Names_ = (_a12_, _lat2_, _lon2_, _azi2_, _s12_, _m12_, _M12_, _M21_, _S12_)
377 _Units_ = (_Azi, _Lat, _Lon, _Azi, _M, _Pass, _Pass, _Pass, _M2)
380class GDict(ADict): # XXX _NamedDict
381 '''A C{dict} with both C{key} I{and} C{attribute} access to the C{dict} items.
383 Results of all C{geodesic} and C{rhumb} methods (with capitalized named) are
384 returned as L{GDict} instances, see for example L{GeodesicExact} and L{RhumbAux}.
385 '''
386 def toDirect9Tuple(self, dflt=NAN):
387 '''Convert this L{GDict} result to a 9-tuple, like I{Karney}'s method
388 C{geographiclib.geodesic.Geodesic._GenDirect}.
390 @kwarg dflt: Default value for missing items (C{any}).
392 @return: L{Direct9Tuple}C{(a12, lat2, lon2, azi2,
393 s12, m12, M12, M21, S12)}
394 '''
395 return self._toTuple(Direct9Tuple, dflt)
397 def toGeodSolve12Tuple(self, dflt=NAN): # PYCHOK 12 args
398 '''Convert this L{GDict} result to a 12-Tuple, compatible with I{Karney}'s
399 U{GeodSolve<https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>}
400 result.
402 @kwarg dflt: Default value for missing items (C{any}).
404 @return: L{GeodSolve12Tuple}C{(lat1, lon1, azi1, lat2, lon2, azi2,
405 s12, a12, m12, M12, M21, S12)}.
406 '''
407 return self._toTuple(_MODS.geodsolve.GeodSolve12Tuple, dflt)
409 def toInverse10Tuple(self, dflt=NAN):
410 '''Convert this L{GDict} result to a 10-tuple, like I{Karney}'s
411 method C{geographiclib.geodesic.Geodesic._GenInverse}.
413 @kwarg dflt: Default value for missing items (C{any}).
415 @return: L{Inverse10Tuple}C{(a12, s12, salp1, calp1,
416 salp2, calp2, m12, M12, M21, S12)}.
417 '''
418 return self._toTuple(Inverse10Tuple, dflt)
420 @deprecated_method
421 def toRhumb7Tuple(self, dflt=NAN): # PYCHOK no cover
422 '''DEPRECATED on 23.12.07, use method C{toRhumb8Tuple}.
424 @return: A I{DEPRECATED} L{Rhumb7Tuple}.
425 '''
426 return self._toTuple(_MODS.deprecated.classes.Rhumb7Tuple, dflt)
428 def toRhumb8Tuple(self, dflt=NAN):
429 '''Convert this L{GDict} result to a 8-tuple.
431 @kwarg dflt: Default value for missing items (C{any}).
433 @return: L{Rhumb8Tuple}C{(lat1, lon1, lat2, lon2,
434 azi12, s12, S12, a12)}.
435 '''
436 return self._toTuple(Rhumb8Tuple, dflt)
438 def toRhumbSolve7Tuple(self, dflt=NAN):
439 '''Convert this L{GDict} result to a 8-tuple.
441 @kwarg dflt: Default value for missing items (C{any}).
443 @return: L{RhumbSolve7Tuple}C{(lat1, lon1, lat2, lon2,
444 azi12, s12, S12)}.
445 '''
446 return self._toTuple(_MODS.rhumb.solve.RhumbSolve7Tuple, dflt)
448 def _toTuple(self, nTuple, dflt):
449 '''(INTERNAL) Convert this C{GDict} to an B{C{nTuple}}.
450 '''
451 _g = getattr
452 t = tuple(_g(self, n, dflt) for n in nTuple._Names_)
453 return nTuple(t, iteration=self._iteration)
456class Inverse10Tuple(_GTuple):
457 '''10-Tuple C{(a12, s12, salp1, calp1, salp2, calp2, m12, M12, M21, S12)} with arc length
458 C{a12} in C{degrees}, distance C{s12} and reduced length C{m12} in C{meter}, area
459 C{S12} in C{meter} I{squared} and the sines C{salp1}, C{salp2} and cosines C{calp1},
460 C{calp2} of the initial C{1} and final C{2} (forward) azimuths.
461 '''
462 _Names_ = (_a12_, _s12_, 'salp1', 'calp1', 'salp2', 'calp2', _m12_, _M12_, _M21_, _S12_)
463 _Units_ = (_Azi, _M, _Pass, _Pass, _Pass, _Pass, _Pass, _Pass, _Pass, _M2)
465 def toGDict(self, **updates):
466 '''Convert this C{Inverse10Tuple} to a L{GDict}.
468 @kwarg updates: Optional items to apply (C{nam=value} pairs)
469 '''
470 return _GTuple.toGDict(self, azi1=atan2d(self.salp1, self.calp1), # PYCHOK namedTuple
471 azi2=atan2d(self.salp2, self.calp2), # PYCHOK namedTuple
472 **updates) # PYCHOK indent
475class _kWrapped(object): # in .geodesicw
476 ''''(INTERNAL) Wrapper for some of I{Karney}'s U{geographiclib
477 <https://PyPI.org/project/geographiclib>} classes.
478 '''
480 @Property_RO
481 def geographiclib(self):
482 '''Lazily import C{geographiclib}, provided the U{geographiclib
483 <https://PyPI.org/project/geographiclib>} package is installed,
484 otherwise raise a C{LazyImportError}.
485 '''
486 g = _xgeographiclib(self.__class__, 1, 49)
487 from geographiclib.geodesic import Geodesic
488 g.Geodesic = Geodesic
489 from geographiclib.geodesicline import GeodesicLine
490 g.GeodesicLine = GeodesicLine
491 from geographiclib.geomath import Math
492 g.Math = Math
493 return g
495 @Property_RO
496 def Math(self):
497 '''Wrap the C{geomath.Math} class, provided the U{geographiclib
498 <https://PyPI.org/project/geographiclib>} package is installed,
499 otherwise C{None}.
500 '''
501 try:
502 g = self.geographiclib
503 M = g.Math
504 if _version_info(g) < (2,):
505 if _K_2_0:
506 M = None
507# elif not _K_2_0: # XXX set 2.0?
508# _K_2_0 = True
509 except (AttributeError, ImportError):
510 M = None
511 return M
513_wrapped = _kWrapped() # PYCHOK singleton, .datum, .test/base.py
516class Rhumb8Tuple(_GTuple):
517 '''8-Tuple C{(lat1, lon1, lat2, lon2, azi12, s12, S12, a12)} with lat- C{lat1},
518 C{lat2} and longitudes C{lon1}, C{lon2} of both points, the azimuth of the
519 rhumb line C{azi12}, the distance C{s12}, the area C{S12} under the rhumb
520 line and the angular distance C{a12} between both points.
521 '''
522 _Names_ = (_lat1_, _lon1_, _lat2_, _lon2_, _azi12_, _s12_, _S12_, _a12_)
523 _Units_ = ( Lat, Lon, Lat, Lon, _Azi, _M, _M2, _Deg)
525 def toDirect9Tuple(self, dflt=NAN, **a12_azi1_azi2_m12_M12_M21):
526 '''Convert this L{Rhumb8Tuple} result to a 9-tuple, like I{Karney}'s
527 method C{geographiclib.geodesic.Geodesic._GenDirect}.
529 @kwarg dflt: Default value for missing items (C{any}).
530 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments
531 to specify or override L{Inverse10Tuple} items.
533 @return: L{Direct9Tuple}C{(a12, lat2, lon2, azi2, s12,
534 m12, M12, M21, S12)}
535 '''
536 d = dict(azi1=self.azi12, M12=_1_0, m12=self.s12, # PYCHOK attr
537 azi2=self.azi12, M21=_1_0) # PYCHOK attr
538 if a12_azi1_azi2_m12_M12_M21:
539 d.update(a12_azi1_azi2_m12_M12_M21)
540 return self._toTuple(Direct9Tuple, dflt, d)
542 def toInverse10Tuple(self, dflt=NAN, **a12_m12_M12_M21_salp1_calp1_salp2_calp2):
543 '''Convert this L{Rhumb8Tuple} to a 10-tuple, like I{Karney}'s
544 method C{geographiclib.geodesic.Geodesic._GenInverse}.
546 @kwarg dflt: Default value for missing items (C{any}).
547 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword
548 arguments to specify or override L{Inverse10Tuple} items.
550 @return: L{Inverse10Tuple}C{(a12, s12, salp1, calp1, salp2, calp2,
551 m12, M12, M21, S12)}.
552 '''
553 s, c = sincos2d(self.azi12) # PYCHOK attr
554 d = dict(salp1=s, calp1=c, M12=_1_0, m12=self.s12, # PYCHOK attr
555 salp2=s, calp2=c, M21=_1_0)
556 if a12_m12_M12_M21_salp1_calp1_salp2_calp2:
557 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2)
558 return self._toTuple(Inverse10Tuple, dflt, d)
560 def _toTuple(self, nTuple, dflt, updates={}):
561 '''(INTERNAL) Convert this C{Rhumb8Tuple} to an B{C{nTuple}}.
562 '''
563 _g = self.toGDict(**updates).get
564 t = tuple(_g(n, dflt) for n in nTuple._Names_)
565 return nTuple(t, name=self.name)
567 @deprecated_method
568 def _to7Tuple(self):
569 '''DEPRECATED, do not use!'''
570 return _MODS.deprecated.classes.Rhumb7Tuple(self[:-1])
573def _around(x): # in .utily.sincos2d
574 '''I{Coarsen} a scalar by rounding small values to underflow to C{0.0}.
576 @return: Coarsened value (C{float}).
578 @see: I{Karney}'s U{geomath.Math.AngRound<https://SourceForge.net/p/
579 geographiclib/code/ci/release/tree/python/geographiclib/geomath.py>}
580 '''
581 try:
582 return _wrapped.Math.AngRound(x)
583 except AttributeError:
584 if x:
585 y = _1_16th - fabs(x)
586 if y > 0: # fabs(x) < _1_16th
587 x = _copysign(_1_16th - y, x)
588 else:
589 x = _0_0 # -0 to 0
590 return x
593def _atan2d(y, x):
594 '''Return C{atan2(B{y}, B{x})} in C{degrees}.
595 '''
596 try:
597 return _wrapped.Math.atan2d(y, x)
598 except AttributeError:
599 return atan2d(y, x)
602def _cbrt(x):
603 '''Return C{cubic root(B{x})}.
604 '''
605 try:
606 return _wrapped.Math.cbrt(x)
607 except AttributeError:
608 return cbrt(x)
611def _copyBit(x, y):
612 '''Like C{copysign0(B{x}, B{y})}, with C{B{x} > 0}.
613 '''
614 return (-x) if _signBit(y) else x
617def _2cos2x(cx, sx): # in .auxDST, .auxLat, .gxbases
618 '''Return M{2 * cos(2 * x)} from cos(x) and sin(x).
619 '''
620 r = cx - sx
621 if r:
622 r *= (cx + sx) * _2_0
623 return r
626def _diff182(deg0, deg, K_2_0=False):
627 '''Compute C{deg - deg0}, reduced to C{[-180,180]} accurately.
629 @return: 2-Tuple C{(delta_angle, residual)} in C{degrees}.
630 '''
631 try:
632 return _wrapped.Math.AngDiff(deg0, deg)
633 except AttributeError:
634 if K_2_0 or _K_2_0: # geographiclib 2.0
635 _r, _360 = fremainder, _360_0
636 d, t = _sum2(_r(-deg0, _360),
637 _r( deg, _360))
638 d, t = _sum2(_r( d, _360), t)
639 if d in (_0_0, _180_0, _N_180_0):
640 d = _copysign(d, -t if t else (deg - deg0))
641 else:
642 _n = _norm180
643 d, t = _sum2(_n(-deg0), _n(deg))
644 d = _n(d)
645 if t > 0 and d == _180_0:
646 d = _N_180_0
647 d, t = _sum2(d, t)
648 return d, t
651def _fix90(deg): # mimick Math.LatFix
652 '''Replace angle in C{degrees} outside [-90,90] by NAN.
654 @return: Angle C{degrees} or NAN.
655 '''
656 try:
657 return _wrapped.Math.LatFix(deg)
658 except AttributeError:
659 return NAN if fabs(deg) > 90 else deg
662def _isfinite(x): # mimick geomath.Math.isfinite
663 '''Check finiteness of C{x}.
665 @return: C{True} if finite.
666 '''
667 try:
668 return _wrapped.Math.isfinite(x)
669 except AttributeError:
670 return _math_isfinite(x) # and fabs(x) <= _MAX
673def _norm2(x, y): # mimick geomath.Math.norm
674 '''Normalize C{B{x}} and C{B{y}}.
676 @return: 2-Tuple of C{(B{x}, B{y})}, normalized.
677 '''
678 try:
679 return _wrapped.Math.norm(x, y)
680 except AttributeError:
681 return norm2(x, y)
684def _norm180(deg): # mimick geomath.Math.AngNormalize
685 '''Reduce angle in C{degrees} to (-180,180].
687 @return: Reduced angle C{degrees}.
688 '''
689 try:
690 return _wrapped.Math.AngNormalize(deg)
691 except AttributeError:
692 d = fremainder(deg, _360_0)
693 if d in (_180_0, _N_180_0):
694 d = _copysign(_180_0, deg) if _K_2_0 else _180_0
695 return d
698def _polygon(geodesic, points, closed, line, wrap):
699 '''(INTERNAL) Compute the area or perimeter of a polygon,
700 using a L{GeodesicExact}, L{GeodesicSolve} or (if the
701 C{geographiclib} package is installed) a C{Geodesic}
702 or C{geodesicw.Geodesic} instance.
703 '''
704 if not wrap: # capability LONG_UNROLL can't be off
705 notImplemented(None, wrap=wrap, up=3)
707 if _MODS.booleans.isBoolean(points):
708 # recursive call for each boolean clip
710 def _a_p(clip, *args, **unused):
711 return _polygon(geodesic, clip, *args)
713 if not closed: # closed only
714 raise _ValueError(closed=closed, points=_composite_)
716 return points._sum1(_a_p, closed, line, wrap)
718 gP = geodesic.Polygon(line)
719 _A = gP.AddPoint
721 Ps = _MODS.iters.PointsIter(points, loop=1, wrap=wrap) # base=LatLonEllipsoidalBase(0, 0)
722 p1 = p0 = Ps[0]
724 # note, lon deltas are unrolled, by default
725 _A(p1.lat, p1.lon)
726 for p2 in Ps.iterate(closed=closed):
727 if wrap and not Ps.looped:
728 p2 = _unrollon(p1, p2)
729 _A(p2.lat, p2.lon)
730 p1 = p2
731 if closed and line and p1 != p0:
732 _A(p0.lat, p0.lon)
734 # gP.Compute returns (number_of_points, perimeter, signed area)
735 return gP.Compute(False, True)[1 if line else 2]
738def _polynomial(x, cs, i, j): # PYCHOK shared
739 '''(INTERNAL) Like C++ C{GeographicLib.Math.hpp.polyval} but with a
740 different signature and cascaded summation as C{karney._sum2_}.
742 @return: M{sum(cs[k] * x**(j - k - 1) for k in range(i, j)}
743 '''
744 # assert 0 <= i <= j <= len(cs)
745# try:
746# return _wrapped.Math.polyval(j - i - 1, cs, i, x)
747# except AttributeError:
748# s, t = cs[i], _0_0
749# for c in cs[i+1:j]:
750# s, t = _sum2_(s * x, t * x, c)
751# return s # + t
752 s = cs[i]
753 i += 1
754 if x and i < j:
755 s, _ = _sum2_(s, _0_0, x=x, *cs[i:j])
756 return s # + t
759def _remainder(x, y):
760 '''Remainder of C{x / y}.
762 @return: Remainder in the range M{[-y / 2, y / 2]}, preserving signed 0.0.
763 '''
764 try:
765 return _wrapped.Math.remainder(x, y)
766 except AttributeError:
767 return fremainder(x, y)
770if _K_2_0:
771 from math import cos as _cos, sin as _sin
773 def _sincos2(rad):
774 return _sin(rad), _cos(rad)
776 _signBit = _MODS.basics.signBit
777else:
778 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared
780 def _signBit(x):
781 '''(INTERNAL) GeographicLib 1.52-.
782 '''
783 return x < 0
786def _sincos2d(deg):
787 '''Return sine and cosine of an angle in C{degrees}.
789 @return: 2-Tuple C{(sin(B{deg}), cos(B{deg}))}.
790 '''
791 try:
792 return _wrapped.Math.sincosd(deg)
793 except AttributeError:
794 return sincos2d(deg)
797def _sincos2de(deg, t):
798 '''Return sine and cosine of a corrected angle in C{degrees}.
800 @return: 2-Tuple C{(sin(B{deg}), cos(B{deg}))}.
801 '''
802 try:
803 return _wrapped.Math.sincosde(deg, t)
804 except AttributeError:
805 return sincos2d(deg, adeg=t)
808def _sum2(u, v): # mimick geomath.Math.sum, actually sum2
809 '''Error-free summation like C{geomath.Math.sum}.
811 @return: 2-Tuple C{(B{u} + B{v}, residual)}.
813 @note: The C{residual} can be the same as B{C{u}} or B{C{v}}.
815 @see: U{Algorithm 3.1<https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>}.
816 '''
817 try:
818 return _wrapped.Math.sum(u, v)
819 except AttributeError:
820 s = u + v
821 r = s - v
822 t = s - r
823 # if Algorithm_3_1:
824 # t = (u - t) + (v + r)
825 # elif C_CPP: # Math::sum C/C++
826 # r -= u
827 # t -= v
828 # t += r
829 # t = -t
830 # else:
831 t = (u - r) + (v - t)
832 return s, t
835def _sum2_(s, t, *vs, **x):
836 '''Accumulate any B{C{vs}} into a previous C{_sum2(s, t)}.
838 @kwarg x: Optional polynomial C{B{x}=1} (C{scalar}).
840 @return: 2-Tuple C{(B{s} + B{t} + sum(B{vs}), residual)}.
842 @see: I{Karney's} C++ U{Accumulator<https://GeographicLib.SourceForge.io/
843 C++/doc/Accumulator_8hpp_source.html>} comments for more details and
844 function C{_sum2} above.
846 @note: NOT "error-free", see C{pygeodesy.test/testKarney.py}.
847 '''
848 x = _xkwds_get(x, x=_1_0)
849 p = x != _1_0
851 _s2, _u0 = _sum2, unsigned0
852 for v in vs:
853 if p:
854 s *= x
855 t *= x
856 if v:
857 t, u = _s2(t, v) # start at the least-
858 if s:
859 s, t = _s2(s, t) # significant end
860 if s:
861 t += u # accumulate u into t
862# elif t: # s == 0 implies t == 0
863# raise _AssertionError(t=t, txt_not_=_0_)
864 else:
865 s = _u0(u) # result is u, t = 0
866 else:
867 s, t = _u0(t), u
868 return s, t
871def _tand(x):
872 '''Return C{tan(B{x})} in C{degrees}.
873 '''
874 try:
875 return _wrapped.Math.tand(x)
876 except AttributeError:
877 return tand(x)
880def _unroll2(lon1, lon2, wrap=False): # see .ellipsoidalBaseDI._intersects2
881 '''Unroll B{C{lon2 - lon1}} like C{geodesic.Geodesic.Inverse}.
883 @return: 2-Tuple C{(B{lon2} - B{lon1}, B{lon2})} with B{C{lon2}}
884 unrolled if B{C{wrap}} is C{True}, normalized otherwise.
885 '''
886 if wrap:
887 d, t = _diff182(lon1, lon2)
888 lon2, _ = _sum2_(d, t, lon1) # (lon1 + d) + t
889 else:
890 lon2 = _norm180(lon2)
891 return (lon2 - lon1), lon2
894def _unsigned2(x):
895 '''(INTERNAL) Unsign B{C{x}}.
896 '''
897 return (neg(x), True) if _signBit(x) else (x, False)
900__all__ += _ALL_DOCS(_CapsBase)
902# **) MIT License
903#
904# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
905#
906# Permission is hereby granted, free of charge, to any person obtaining a
907# copy of this software and associated documentation files (the "Software"),
908# to deal in the Software without restriction, including without limitation
909# the rights to use, copy, modify, merge, publish, distribute, sublicense,
910# and/or sell copies of the Software, and to permit persons to whom the
911# Software is furnished to do so, subject to the following conditions:
912#
913# The above copyright notice and this permission notice shall be included
914# in all copies or substantial portions of the Software.
915#
916# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
917# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
918# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
919# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
920# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
921# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
922# OTHER DEALINGS IN THE SOFTWARE.