Coverage for pygeodesy/karney.py: 86%
277 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-04-11 14:35 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2023-04-11 14:35 -0400
2# -*- coding: utf-8 -*-
4u'''Wrapper around several C{geomath.Math} functions from I{Karney}'s Python package
5U{geographiclib<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.
10With env variable C{PYGEODESY_GEOGRAPHICLIB} left undefined or set to C{"2"}, this module,
11L{pygeodesy.geodesicw} and L{pygeodesy.geodesicx} will use U{GeographicLib 2.0
12<https://GeographicLib.SourceForge.io/C++/doc/>} transcoding, otherwise C{1.52} or older.
14Karney-based functionality
15==========================
171. The following classes and functions in C{pygeodesy}
19 - L{AlbersEqualArea}, L{AlbersEqualArea2}, L{AlbersEqualArea4},
20 L{AlbersEqualAreaCylindrical}, L{AlbersEqualAreaNorth}, L{AlbersEqualAreaSouth} --
21 U{AlbersEqualArea<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AlbersEqualArea.html>}
23 - L{CassiniSoldner} -- U{CassiniSoldner<https://GeographicLib.SourceForge.io/C++/doc/
24 classGeographicLib_1_1CassiniSoldner.html>}
26 - L{EcefKarney} -- U{Geocentric<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Geocentric.html>}
28 - L{Elliptic} -- U{EllipticFunction<https://GeographicLib.SourceForge.io/C++/doc/
29 classGeographicLib_1_1EllipticFunction.html>}
31 - L{EquidistantExact}, L{EquidistantGeodSolve}, L{EquidistantKarney} -- U{AzimuthalEquidistant
32 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AzimuthalEquidistant.html>}
34 - L{Etm}, L{ExactTransverseMercator} -- U{TransverseMercatorExact
35 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercatorExact.html>}
37 - L{Geodesic}, L{GeodesicLine} -- I{wrapped} U{geodesic.Geodesic<https://PyPI.org/project/geographiclib>},
38 I{wrapped} U{geodesicline.GeodesicLine<https://PyPI.org/project/geographiclib>}
40 - L{GeodesicAreaExact}, L{PolygonArea} -- U{PolygonArea<https://GeographicLib.SourceForge.io/html/
41 classGeographicLib_1_1PolygonAreaT.html>}
43 - L{GeodesicExact}, L{GeodesicLineExact} -- U{GeodesicExact<https://GeographicLib.SourceForge.io/C++/doc/
44 classGeographicLib_1_1GeodesicExact.html>}, U{GeodesicLineExact<https://GeographicLib.SourceForge.io/C++/doc/
45 classGeographicLib_1_1GeodesicLineExact.html>}
47 - L{GeoidKarney} -- U{Geoid<https://GeographicLib.SourceForge.io/C++/doc/geoid.html>}
49 - L{Georef} -- U{Georef<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Georef.html>}
51 - L{GnomonicExact}, L{GnomonicGeodSolve}, L{GnomonicKarney} -- U{Gnomonic
52 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Gnomonic.html>}
54 - L{JacobiConformal} -- U{JacobiConformal
55 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1JacobiConformal.html#details>}
57 - L{KTransverseMercator} - U{TransverseMercator
58 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>}
60 - L{LocalCartesian}, L{Ltp} -- U{LocalCartesian<https://GeographicLib.SourceForge.io/C++/doc/
61 classGeographicLib_1_1LocalCartesian.html>}
63 - L{Osgr} -- U{OSGB<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1OSGB.html>}.
65 - L{Rhumb}, L{RhumbLine} -- U{Rhumb<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Rhumb.html>},
66 U{RhumbLine<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1RhumbLine.html>},
67 U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>}
69 - L{Ups} -- U{PolarStereographic<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1PolarStereographic.html>}
71 - L{Utm} -- U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>}
73 - L{UtmUps}, L{Epsg} -- U{UTMUPS<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1UTMUPS.html>}
75 - L{pygeodesy.atand}, L{pygeodesy.atan2d}, L{pygeodesy.sincos2}, L{pygeodesy.sincos2d}, L{pygeodesy.tand} -- U{geomath.Math
76 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Math.html>}
78are I{transcoded} from C++ classes in I{Karney}'s U{GeographicLib<https://GeographicLib.SourceForge.io/C++/doc/annotated.html>}.
802. These C{pygeodesy} modules and classes
82 - L{ellipsoidalGeodSolve}, L{ellipsoidalKarney}, L{geodsolve}, L{karney}, L{rhumbsolve}
83 - L{EquidistantKarney}, L{FrechetKarney}, L{GeodesicSolve}, L{GeodesicLineSolve}, L{GnomonicGeodSolve},
84 L{GnomonicKarney}, L{HeightIDWkarney}
86are or use I{wrappers} around I{Karney}'s Python U{geographiclib<https://PyPI.org/project/geographiclib>}
87C{geodesic}, C++ utility U{GeodSolve<https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>} or
88C++ utility U{RhumbSolve<https://GeographicLib.SourceForge.io/C++/doc/RhumbSolve.1.html>}.
903. All C{pygeodesy} functions and methods to compute I{ellipsoidal} intersections, nearest points and trilaterations
92 - L{ellipsoidalExact.intersection3}, L{ellipsoidalExact.intersections2}, L{ellipsoidalExact.nearestOn},
93 L{ellipsoidalExact.LatLon.intersection3}, L{ellipsoidalExact.LatLon.intersections2},
94 L{ellipsoidalExact.LatLon.nearestOn}, L{ellipsoidalExact.LatLon.trilaterate5}
96 - L{ellipsoidalKarney.intersection3}, L{ellipsoidalKarney.intersections2}, L{ellipsoidalKarney.nearestOn},
97 L{ellipsoidalKarney.LatLon.intersection3}, L{ellipsoidalKarney.LatLon.intersections2},
98 L{ellipsoidalKarney.LatLon.nearestOn}, L{ellipsoidalKarney.LatLon.trilaterate5}
100 - L{ellipsoidalVincenty.intersection3}, L{ellipsoidalVincenty.intersections2}, L{ellipsoidalVincenty.nearestOn},
101 L{ellipsoidalVincenty.LatLon.intersection3}, L{ellipsoidalVincenty.LatLon.intersections2},
102 L{ellipsoidalVincenty.LatLon.nearestOn}, L{ellipsoidalVincenty.LatLon.trilaterate5}
104 - L{RhumbLine.intersection2}, L{RhumbLine.nearestOn4}
106are iterative implementations of I{Karney}'s solution posted here under U{The B{ellipsoidal} case
107<https://GIS.StackExchange.com/questions/48937/calculating-intersection-of-two-circles>} and in paper U{Geodesics
108on an ellipsoid of revolution<https://ArXiv.org/pdf/1102.1215.pdf>} (pp 20-21, section B{14. MARITIME BOUNDARIES}).
1104. Spherical functions
112 - L{pygeodesy.excessKarney_}, L{sphericalTrigonometry.areaOf}
114in C{pygeodesy} are based on I{Karney}'s post U{Area of a spherical polygon
115<https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}, 3rd Answer.
116'''
117# make sure int/int division yields float quotient, see .basics
118from __future__ import division as _; del _ # PYCHOK semicolon
120from pygeodesy.basics import _copysign, neg, unsigned0, _xgeographiclib, \
121 _xImportError, _xversion_info, _xinstanceof, \
122 _zip, isodd # PYCHOK shared
123from pygeodesy.constants import NAN, _isfinite as _math_isfinite, _0_0, _1_16th, \
124 _180_0, _N_180_0, _360_0
125from pygeodesy.datums import _WGS84, _a_ellipsoid # PYCHOK shared
126from pygeodesy.errors import _ValueError, _xkwds, _xkwds_get
127from pygeodesy.fmath import cbrt, fremainder, norm2, hypot as _hypot, unstr # PYCHOK shared
128from pygeodesy.interns import _2_, _a12_, _area_, _azi2_, _composite_, _lat2_, \
129 _lon2_, _m12_, _M12_, _M21_, _number_, _s12_, _S12_
130from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _getenv
131from pygeodesy.named import _Dict, _NamedBase, _NamedTuple, _Pass
132from pygeodesy.props import deprecated_method, Property_RO, property_RO
133# from pygeodesy.streps import unstr # from .fmath
134from pygeodesy.units import Bearing as _Azi, Degrees as _Deg, Lat, Lon, \
135 Meter as _M, Meter2 as _M2, Number_, \
136 Precision_, _1mm as _TOL_M # PYCHOK shared
137from pygeodesy.utily import atan2d, fabs, sincos2d, tand, unroll180, wrap360 # PYCHOK shared
139# from math import fabs # from .utily
141__all__ = _ALL_LAZY.karney
142__version__ = '23.04.07'
144_EWGS84 = _WGS84.ellipsoid # PYCHOK in .geodesicx.gx, .ktm, .rhumbx, .solveBase
145_K_2_0 = _getenv('PYGEODESY_GEOGRAPHICLIB', _2_) == _2_
146_perimeter_ = 'perimeter'
149class _GTuple(_NamedTuple): # in .testNamedTuples
150 '''(INTERNAL) Helper.
151 '''
152 def toGDict(self, **updates):
153 '''Convert this C{*Tuple} to a L{GDict}.
155 @kwarg updates: Optional items to apply (C{nam=value} pairs)
156 '''
157 r = GDict(_zip(self._Names_, self)) # strict=True
158 if updates:
159 r.update(updates)
160 if self._iteration is not None:
161 r._iteration = self._iteration
162 return r
165class _Lat(Lat):
166 '''(INTERNAL) Latitude B{C{lat}}.
167 '''
168 def __init__(self, *lat, **Error_name):
169 kwds = _xkwds(Error_name, clip=0, Error=GeodesicError)
170 Lat.__new__(_Lat, *lat, **kwds)
173class _Lon(Lon):
174 '''(INTERNAL) Longitude B{C{lon}}.
175 '''
176 def __init__(self, *lon, **Error_name):
177 kwds = _xkwds(Error_name, clip=0, Error=GeodesicError)
178 Lon.__new__(_Lon, *lon, **kwds)
181class Area3Tuple(_NamedTuple): # in .geodesicx.gxarea
182 '''3-Tuple C{(number, perimeter, area)} with the C{number}
183 of points on the polygon or polyline, the C{perimeter} in
184 C{meter} and the C{area} in C{meter} I{squared}.
185 '''
186 _Names_ = (_number_, _perimeter_, _area_)
187 _Units_ = ( Number_, _M, _M2)
190class Caps(object): # PYCHOK
191 '''(INTERNAL) Overriden by C{Caps} below.
192 '''
193 EMPTY = 0 # formerly aka NONE
194 LATITUDE = 1 << 7 # compute latitude C{lat2}
195 LONGITUDE = 1 << 8 # compute longitude C{lon2}
196 AZIMUTH = 1 << 9 # azimuths C{azi1} and C{azi2}
197 DISTANCE = 1 << 10 # compute distance C{s12}
198 DISTANCE_IN = 1 << 11 # allow distance C{s12} in Direct
199 REDUCEDLENGTH = 1 << 12 # compute reduced length C{m12}
200 GEODESICSCALE = 1 << 13 # compute geodesic scales C{M12} and C{M21}
201 AREA = 1 << 14 # compute area C{S12}
203 STANDARD = AZIMUTH | DISTANCE | DISTANCE_IN | LATITUDE | LONGITUDE
204 ALL = 0x7F80 # without LONG_UNROLL, LINE_OFF, REVERSE2 and _DEBUG_*
206 LINE_OFF = 1 << 15 # Line without updates from parent geodesic or rhumb
207 LONG_UNROLL = 1 << 16 # unroll C{lon2} in .Direct and .Position
208 REVERSE2 = 1 << 17 # reverse C{azi2}
210 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE
211 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA
213 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE
214 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA
216 _ANGLE_ONLY = 1 << 18 # angular distance C{a12} only
217 _SALPs_CALPs = 1 << 19 # (INTERNAL) GeodesicExact._GenInverse
219 _DEBUG_AREA = 1 << 20 # (INTERNAL) include Line details
220 _DEBUG_DIRECT = 1 << 21 # (INTERNAL) include Direct details
221 _DEBUG_INVERSE = 1 << 22 # (INTERNAL) include Inverse details
222 _DEBUG_LINE = 1 << 23 # (INTERNAL) include Line details
223 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \
224 _DEBUG_LINE | _ANGLE_ONLY | _SALPs_CALPs
225 _OUT_ALL = ALL
226 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL
228 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE
229 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE
230 _DISTANCE_IN_OUT = DISTANCE_IN & _OUT_MASK
231 _LINE = AZIMUTH | LATITUDE | LONG_UNROLL
232 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE
233# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE
235Caps = Caps() # PYCHOK singleton
236'''I{Enum}-style masks to be bit-C{or}'ed to specify geodesic or
237rhumb capabilities (C{caps}) and expected results (C{outmask}).
239C{AREA} - compute area C{S12},
241C{AZIMUTH} - include azimuths C{azi1} and C{azi2},
243C{DISTANCE} - compute distance C{s12},
245C{DISTANCE_IN} - allow distance C{s12} in C{.Direct},
247C{EMPTY} - nothing, formerly aka C{NONE},
249C{GEODESICSCALE} - compute geodesic scales C{M12} and C{M21},
251C{LINE_OFF} - Line without updates from parent geodesic or rhumb.
253C{LATITUDE} - compute latitude C{lat2},
255C{LONGITUDE} - compute longitude C{lon2},
257C{LONG_UNROLL} - unroll C{lon2} in C{.Direct},
259C{REDUCEDLENGTH} - compute reduced length C{m12},
261C{REVERSE2} - reverse C{azi2},
263and C{ALL} - all of the above.
265C{STANDARD} = C{AZIMUTH | DISTANCE | DISTANCE_IN | LATITUDE | LONGITUDE}'''
268class _CapsBase(_NamedBase): # in .geodesicx.gxbases, .rhumbx
269 '''(INTERNAL) Base class for C{[_]Geodesic*Exact}.
270 '''
271 ALL = Caps.ALL
272 AREA = Caps.AREA
273 AZIMUTH = Caps.AZIMUTH
274 DISTANCE = Caps.DISTANCE
275 DISTANCE_IN = Caps.DISTANCE_IN
276 EMPTY = Caps.EMPTY # aka NONE
277 GEODESICSCALE = Caps.GEODESICSCALE
278 LATITUDE = Caps.LATITUDE
279 LINE_OFF = Caps.LINE_OFF
280 LONGITUDE = Caps.LONGITUDE
281 LONG_UNROLL = Caps.LONG_UNROLL
282 REDUCEDLENGTH = Caps.REDUCEDLENGTH
283 STANDARD = Caps.STANDARD
285 _caps = 0 # None
286 _debug = 0 # or Caps._DEBUG_...
288 @Property_RO
289 def caps(self):
290 '''Get the capabilities (bit-or'ed C{Caps}).
291 '''
292 return self._caps
294 def caps_(self, caps):
295 '''Check the available capabilities.
297 @arg caps: Bit-or'ed combination of L{Caps} values
298 for all capabilities to be checked.
300 @return: C{True} if I{all} B{C{caps}} are available,
301 C{False} otherwise (C{bool}).
302 '''
303 caps &= Caps._OUT_ALL
304 return (self.caps & caps) == caps
306 @property
307 def debug(self):
308 '''Get the C{debug} option (C{bool}).
309 '''
310 return bool(self._debug)
312 @debug.setter # PYCHOK setter!
313 def debug(self, debug):
314 '''Set the C{debug} option (C{bool}) to include
315 more details in L{GDict} results.
316 '''
317 self._debug = Caps._DEBUG_ALL if debug else 0
319 def _iter2tion(self, r, s):
320 '''(INTERNAL) Copy C{C{s}.iter} into C{B{r}._iteration}.
321 '''
322 i = _xkwds_get(s, iter=None)
323 if i is not None:
324 self._iteration = r._iteration = i
325 return r
328class Direct9Tuple(_GTuple):
329 '''9-Tuple C{(a12, lat2, lon2, azi2, s12, m12, M12, M21, S12)} with arc
330 length C{a12}, angles C{lat2}, C{lon2} and azimuth C{azi2} in C{degrees},
331 distance C{s12} and reduced length C{m12} in C{meter} and area C{S12} in
332 C{meter} I{squared}.
333 '''
334 _Names_ = (_a12_, _lat2_, _lon2_, _azi2_, _s12_, _m12_, _M12_, _M21_, _S12_)
335 _Units_ = (_Azi, _Lat, _Lon, _Azi, _M, _Pass, _Pass, _Pass, _M2)
338class GDict(_Dict): # XXX _NamedDict
339 '''Basic C{dict} with both key I{and} attribute access
340 to the C{dict} items.
342 Results of all C{geodesic} methods are returned as a
343 L{GDict} instance.
344 '''
345 _iteration = None # Iteration number (C{int}) or C{None}
347 @property_RO
348 def iteration(self): # see .named._NamedBase
349 '''Get the iteration number (C{int}) or
350 C{None} if not available/applicable.
351 '''
352 return self._iteration
354 def toDirect9Tuple(self, dflt=NAN):
355 '''Convert this L{GDict} result to a 9-tuple, like I{Karney}'s
356 method C{geographiclib.geodesic.Geodesic._GenDirect}.
358 @kwarg dflt: Default value for missing items (C{any}).
360 @return: L{Direct9Tuple}C{(a12, lat2, lon2, azi2,
361 s12, m12, M12, M21, S12)}
362 '''
363 return self._toTuple(Direct9Tuple, dflt)
365 def toGeodSolve12Tuple(self, dflt=NAN): # PYCHOK 12 args
366 '''Convert this L{GDict} result to a 12-Tuple, compatible with I{Karney}'s
367 U{GeodSolve<https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>}
368 result.
370 @kwarg dflt: Default value for missing items (C{any}).
372 @return: L{GeodSolve12Tuple}C{(lat1, lon1, azi1, lat2, lon2, azi2,
373 s12, a12, m12, M12, M21, S12)}.
374 '''
375 return self._toTuple(_MODS.geodsolve.GeodSolve12Tuple, dflt)
377 def toInverse10Tuple(self, dflt=NAN):
378 '''Convert this L{GDict} result to a 10-tuple, like I{Karney}'s
379 method C{geographiclib.geodesic.Geodesic._GenInverse}.
381 @kwarg dflt: Default value for missing items (C{any}).
383 @return: L{Inverse10Tuple}C{(a12, s12, salp1, calp1,
384 salp2, calp2, m12, M12, M21, S12)}.
385 '''
386 return self._toTuple(Inverse10Tuple, dflt)
388 @deprecated_method
389 def toRhumb7Tuple(self, dflt=NAN):
390 '''DEPRECATED, used method C{toRhumb8Tuple}.
392 @return: A I{DEPRECATED} L{Rhumb7Tuple}.
393 '''
394 return self._toTuple(_MODS.deprecated.Rhumb7Tuple, dflt)
396 def toRhumb8Tuple(self, dflt=NAN):
397 '''Convert this L{GDict} result to a 8-tuple.
399 @kwarg dflt: Default value for missing items (C{any}).
401 @return: L{Rhumb8Tuple}C{(lat1, lon1, lat2, lon2,
402 azi12, s12, S12, a12)}.
403 '''
404 return self._toTuple(_MODS.rhumbx.Rhumb8Tuple, dflt)
406 def toRhumbSolve7Tuple(self, dflt=NAN):
407 '''Convert this L{GDict} result to a 8-tuple.
409 @kwarg dflt: Default value for missing items (C{any}).
411 @return: L{RhumbSolve7Tuple}C{(lat1, lon1, lat2, lon2,
412 azi12, s12, S12)}.
413 '''
414 return self._toTuple(_MODS.rhumbsolve.RhumbSolve7Tuple, dflt)
416 def _toTuple(self, nTuple, dflt):
417 '''(INTERNAL) Convert this C{GDict} to an B{C{nTuple}}.
418 '''
419 _g = getattr
420 t = tuple(_g(self, n, dflt) for n in nTuple._Names_)
421 return nTuple(t, iteration=self._iteration)
424class GeodesicError(_ValueError):
425 '''Error raised for L{pygeodesy.geodesicx} lack of convergence
426 or other L{pygeodesy.geodesicx} or L{pygeodesy.karney} issues.
427 '''
428 pass
431class Inverse10Tuple(_GTuple):
432 '''10-Tuple C{(a12, s12, salp1, calp1, salp2, calp2, m12, M12, M21, S12)} with arc length
433 C{a12} in C{degrees}, distance C{s12} and reduced length C{m12} in C{meter}, area
434 C{S12} in C{meter} I{squared} and the sines C{salp1}, C{salp2} and cosines C{calp1},
435 C{calp2} of the initial C{1} and final C{2} foward azimuths.
436 '''
437 _Names_ = (_a12_, _s12_, 'salp1', 'calp1', 'salp2', 'calp2', _m12_, _M12_, _M21_, _S12_)
438 _Units_ = (_Azi, _M, _Pass, _Pass, _Pass, _Pass, _Pass, _Pass, _Pass, _M2)
440 def toGDict(self, **updates):
441 '''Convert this C{Inverse10Tuple} to a L{GDict}.
443 @kwarg updates: Optional items to apply (C{nam=value} pairs)
444 '''
445 return _GTuple.toGDict(self, azi1=atan2d(self.salp1, self.calp1), # PYCHOK indent, namedTuple
446 azi2=atan2d(self.salp2, self.calp2), # PYCHOK namedTuple
447 **updates) # PYCHOK indent
450class _kWrapped(object):
451 ''''(INTERNAL) Wrapper for some of I{Karney}'s U{geographiclib
452 <https://PyPI.org/project/geographiclib>} classes.
453 '''
455 @Property_RO
456 def geographiclib(self):
457 '''Get the imported C{geographiclib}, provided the U{geographiclib
458 <https://PyPI.org/project/geographiclib>} package is installed,
459 otherwise an C{ImportError}.
460 '''
461 g = _xgeographiclib(self.__class__, 1, 49)
462 from geographiclib.geodesic import Geodesic
463 g.Geodesic = Geodesic
464 from geographiclib.geodesicline import GeodesicLine
465 g.GeodesicLine = GeodesicLine
466 from geographiclib.geomath import Math
467 g.Math = Math
468 return g
470 @Property_RO # MCCABE 13
471 def Math(self):
472 '''Wrap the C{geomath.Math} class, provided the U{geographiclib
473 <https://PyPI.org/project/geographiclib>} package is installed,
474 otherwise C{None}.
475 '''
476 try:
477 g = self.geographiclib
478 M = g.Math
479 if _xversion_info(g) < (2,):
480 if _K_2_0:
481 M = None
482# elif not _K_2_0: # XXX set 2.0?
483# _K_2_0 = True
484 except (AttributeError, ImportError):
485 M = None
486 return M
488_wrapped = _kWrapped() # PYCHOK singleton, .datum, .test/base.py
491def _around(x): # in .utily.sincos2d
492 '''I{Coarsen} a scalar by rounding small values to underflow to C{0.0}.
494 @return: Coarsened value (C{float}).
496 @see: I{Karney}'s U{geomath.Math.AngRound<https://SourceForge.net/p/
497 geographiclib/code/ci/release/tree/python/geographiclib/geomath.py>}
498 '''
499 try:
500 return _wrapped.Math.AngRound(x)
501 except AttributeError:
502 pass
503 if x:
504 y = _1_16th - fabs(x)
505 if y > 0: # fabs(x) < _1_16th
506 x = _copysign(_1_16th - y, x)
507 else:
508 x = _0_0 # -0 to 0
509 return x
512def _atan2d(y, x):
513 '''Return C{atan2(B{y}, B{x})} in C{degrees}.
514 '''
515 try:
516 return _wrapped.Math.atan2d(y, x)
517 except AttributeError:
518 return atan2d(y, x)
521def _cbrt(x):
522 '''Return C{cubic root(B{x})}.
523 '''
524 try:
525 return _wrapped.Math.cbrt(x)
526 except AttributeError:
527 return cbrt(x)
530def _copyBit(x, y):
531 '''Like C{copysign0(B{x}, B{y})}, with C{B{x} > 0}.
532 '''
533 return (-x) if _signBit(y) else x
536def _diff182(deg0, deg):
537 '''Compute C{deg - deg0}, reduced to C{[-180,180]} accurately.
539 @return: 2-Tuple C{(delta_angle, residual)} in C{degrees}.
540 '''
541 try:
542 return _wrapped.Math.AngDiff(deg0, deg)
543 except AttributeError:
544 pass
545 if _K_2_0: # geographiclib 2.0
546 d, t = _sum2(fremainder(-deg0, _360_0),
547 fremainder( deg, _360_0))
548 d, t = _sum2(fremainder( d, _360_0), t)
549 if d in (_0_0, _180_0, _N_180_0):
550 d = _copysign(d, -t if t else (deg - deg0))
551 else:
552 d, t = _sum2(_norm180(-deg0), _norm180(deg))
553 d = _norm180(d)
554 if t > 0 and d == _180_0:
555 d = _N_180_0
556 d, t = _sum2(d, t)
557 return d, t
560def _fix90(deg): # mimick Math.LatFix
561 '''Replace angle in C{degrees} outside [-90,90] by NAN.
563 @return: Angle C{degrees} or NAN.
564 '''
565 try:
566 return _wrapped.Math.LatFix(deg)
567 except AttributeError:
568 return NAN if fabs(deg) > 90 else deg
571def _isfinite(x): # mimick geomath.Math.AngNormalize
572 '''Check finiteness of C{x}.
574 @return: C{True} if finite.
575 '''
576 try:
577 return _wrapped.Math.isfinite(x)
578 except AttributeError:
579 return _math_isfinite(x) # and fabs(x) <= _MAX
582def _norm2(x, y): # mimick geomath.Math.norm
583 '''Normalize C{B{x}} and C{B{y}}.
585 @return: 2-Tuple of C{(B{x}, B{y})}, normalized.
586 '''
587 try:
588 return _wrapped.Math.norm(x, y)
589 except AttributeError:
590 return norm2(x, y)
593def _norm180(deg): # mimick geomath.Math.AngNormalize
594 '''Reduce angle in C{degrees} to (-180,180].
596 @return: Reduced angle C{degrees}.
597 '''
598 try:
599 return _wrapped.Math.AngNormalize(deg)
600 except AttributeError:
601 pass
602 d = fremainder(deg, _360_0)
603 if d in (_180_0, -_180_0):
604 d = _copysign(_180_0, deg) if _K_2_0 else _180_0
605 return d
608def _polygon(geodesic, points, closed, line, wrap):
609 '''(INTERNAL) Compute the area or perimeter of a polygon,
610 using a L{GeodesicExact}, L{GeodesicSolve} or (if the
611 C{geographiclib} package is installed) a C{Geodesic}
612 or C{geodesicw.Geodesic} instance.
613 '''
614 if not wrap: # capability LONG_UNROLL can't be off
615 raise _ValueError(wrap=wrap)
617 if _MODS.booleans.isBoolean(points):
618 # recursive call for each boolean clip
620 def _a_p(clip, *args, **unused):
621 return _polygon(geodesic, clip, *args)
623 if not closed: # closed only
624 raise _ValueError(closed=closed, points=_composite_)
626 return points._sum1(_a_p, closed, line, wrap)
628 gP = geodesic.Polygon(line)
629 _A = gP.AddPoint
631 Ps = _MODS.iters.PointsIter(points, loop=1) # base=LatLonEllipsoidalBase(0, 0)
632 p0 = Ps[0]
634 # note, lon deltas are unrolled, by default
635 _A(p0.lat, p0.lon)
636 for p in Ps.iterate(closed=closed):
637 _A(p.lat, p.lon)
638 if closed and line and p != p0:
639 _A(p0.lat, p0.lon)
641 # gP.Compute returns (number_of_points, perimeter, signed area)
642 return gP.Compute(False, True)[1 if line else 2]
645def _polynomial(x, cs, i, j): # PYCHOK shared
646 '''(INTERNAL) Like C++ C{GeographicLib.Math.hpp.polyval} but with a
647 different signature and cascaded summation as C{karney._sum2_}.
649 @return: M{sum(cs[k] * x**(j - k - 1) for k in range(i, j)}
650 '''
651 s, t = cs[i], _0_0
652 _s2_ = _sum2_
653 for c in cs[i+1:j]:
654 s, t = _s2_(s * x, t * x, c)
655 return s # + t
658def _remainder(x, y):
659 '''Remainder of C{x / y}.
661 @return: Remainder in the range M{[-y / 2, y / 2]}, preserving signed 0.0.
662 '''
663 try:
664 return _wrapped.Math.remainder(x, y)
665 except AttributeError:
666 return fremainder(x, y)
669if _K_2_0:
670 from pygeodesy.basics import signBit as _signBit
671 from math import cos as _cos, sin as _sin
673 def _sincos2(rad):
674 return _sin(rad), _cos(rad)
676else:
677 from pygeodesy.utily import sincos2 as _sincos2 # PYCHOK shared
679 def _signBit(x):
680 '''(INTERNAL) GeographicLin 1.52-.
681 '''
682 return x < 0
685def _sincos2d(deg):
686 '''Return sine and cosine of an angle in C{degrees}.
688 @return: 2-Tuple C{(sin(B{deg}), cos(B{deg}))}.
689 '''
690 try:
691 return _wrapped.Math.sincosd(deg)
692 except AttributeError:
693 return sincos2d(deg)
696def _sincos2de(deg, t):
697 '''Return sine and cosine of a corrected angle in C{degrees}.
699 @return: 2-Tuple C{(sin(B{deg}), cos(B{deg}))}.
700 '''
701 try:
702 return _wrapped.Math.sincosde(deg, t)
703 except AttributeError:
704 return sincos2d(deg, adeg=t)
707def _sum2(u, v): # mimick geomath.Math.sum, actually sum2
708 '''Error-free summation like C{geomath.Math.sum}.
710 @return: 2-Tuple C{(B{u} + B{v}, residual)}.
712 @note: The C{residual} can be the same as B{C{u}} or B{C{v}}.
714 @see: U{Algorithm 3.1<https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>}.
715 '''
716 try:
717 return _wrapped.Math.sum(u, v)
718 except AttributeError:
719 pass
720 s = u + v
721 r = s - v
722 t = s - r
723 # if Algorithm_3_1:
724 # t = (u - t) + (v + r)
725 # elif C_CPP: # Math::sum C/C++
726 # r -= u
727 # t -= v
728 # t += r
729 # t = -t
730 # else:
731 t = (u - r) + (v - t)
732 return s, t
735def _sum2_(s, t, *vs):
736 '''Accumulate any B{C{vs}} into a previous C{_sum2(s, t)}.
738 @return: 2-Tuple C{(B{s} + B{t} + sum(B{vs}), residual)}.
740 @see: I{Karney's} C++ U{Accumulator<https://GeographicLib.SourceForge.io/
741 html/Accumulator_8hpp_source.html>} comments for more details and
742 function C{_sum2} above.
744 @note: NOT "error-free", see C{pygeodesy.test/testKarney.py}.
745 '''
746 _s2, _u0 = _sum2, unsigned0
747 for v in vs:
748 if v:
749 t, u = _s2(t, v) # start at the least-
750 if s:
751 s, t = _s2(s, t) # significant end
752 if s:
753 t += u # accumulate u into t
754# elif t: # s == 0 implies t == 0
755# raise _AssertionError(t=t, txt=_not_(_0_))
756 else:
757 s = _u0(u) # result is u, t = 0
758 else:
759 s, t = _u0(t), u
760 return s, t
763def _tand(x):
764 '''Return C{tan(B{x})} in C{degrees}.
765 '''
766 try:
767 return _wrapped.Math.tand(x)
768 except AttributeError:
769 return tand(x)
772def _unroll2(lon1, lon2, wrap=False): # see .ellipsoidalBaseDI._intersects2
773 '''Unroll B{C{lon2 - lon1}} like C{geodesic.Geodesic.Inverse}.
775 @return: 2-Tuple C{(B{lon2} - B{lon1}, B{lon2})} with B{C{lon2}}
776 unrolled if B{C{wrap}} is C{True}, normalized otherwise.
777 '''
778 if wrap:
779 d, t = _diff182(lon1, lon2)
780 lon2, _ = _sum2_(d, t, lon1) # (lon1 + d) + t
781 else:
782 lon2 = _norm180(lon2)
783 return (lon2 - lon1), lon2
786def _unsigned2(x):
787 '''(INTERNAL) Unsign B{C{x}}.
788 '''
789 return (neg(x), True) if _signBit(x) else (x, False)
792__all__ += _ALL_DOCS(_CapsBase)
794# **) MIT License
795#
796# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.
797#
798# Permission is hereby granted, free of charge, to any person obtaining a
799# copy of this software and associated documentation files (the "Software"),
800# to deal in the Software without restriction, including without limitation
801# the rights to use, copy, modify, merge, publish, distribute, sublicense,
802# and/or sell copies of the Software, and to permit persons to whom the
803# Software is furnished to do so, subject to the following conditions:
804#
805# The above copyright notice and this permission notice shall be included
806# in all copies or substantial portions of the Software.
807#
808# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
809# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
810# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
811# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
812# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
813# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
814# OTHER DEALINGS IN THE SOFTWARE.