Coverage for pygeodesy/karney.py: 92%

362 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-03-25 15:01 -0400

1 

2# -*- coding: utf-8 -*- 

3 

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. 

6 

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. 

10 

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>}. 

16 

17Karney-based functionality 

18========================== 

19 

201. The following classes and functions in C{pygeodesy} 

21 

22 - L{AlbersEqualArea}, L{AlbersEqualArea2}, L{AlbersEqualArea4}, L{AlbersEqualAreaCylindrical}, 

23 L{AlbersEqualAreaNorth}, L{AlbersEqualAreaSouth} -- U{AlbersEqualArea 

24 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AlbersEqualArea.html>} 

25 

26 - L{Ang}, L{Deg}, L{Rad} -- U{AngleT<https://GeographicLib.SourceForge.io/C++/doc/ 

27 classGeographicLib_1_1AngleT.html>} in I{GeographicLib 2.7}. 

28 

29 - L{AuxAngle}, L{AuxDST}, L{AuxDLat}, L{AuxLat} -- U{AuxAngle<https://GeographicLib.SourceForge.io/C++/doc/ 

30 classGeographicLib_1_1AuxAngle.html>}, U{DST<https://GeographicLib.SourceForge.io/C++/doc/ 

31 classGeographicLib_1_1DST.html>}, U{DAuxLatitude<https://GeographicLib.SourceForge.io/C++/doc/ 

32 classGeographicLib_1_1DAuxLatitude.html>}, U{AuxLatitude<https://GeographicLib.SourceForge.io/C++/doc/ 

33 classGeographicLib_1_1AuxLatitude.html>} in I{GeographicLib 2.2+} 

34 

35 - L{CassiniSoldner} -- U{CassiniSoldner<https://GeographicLib.SourceForge.io/C++/doc/ 

36 classGeographicLib_1_1CassiniSoldner.html>} 

37 

38 - L{Conformal3}, L{Conformal3B}, L{Conformal3Sphere} -- U{Conformal3<https://GeographicLib.SourceForge.io/C++/doc/ 

39 classGeographicLib_1_1Triaxial_1_1Conformal3.html>} in I{GeographicLib 2.7} 

40 

41 - L{EcefKarney} -- U{Geocentric<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Geocentric.html>} 

42 

43 - L{Elliptic} -- U{EllipticFunction<https://GeographicLib.SourceForge.io/C++/doc/ 

44 classGeographicLib_1_1EllipticFunction.html>} 

45 

46 - L{EquidistantExact}, L{EquidistantGeodSolve}, L{EquidistantKarney} -- U{AzimuthalEquidistant 

47 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AzimuthalEquidistant.html>} 

48 

49 - L{Etm}, L{ExactTransverseMercator} -- U{TransverseMercatorExact<https://GeographicLib.SourceForge.io/C++/doc/ 

50 classGeographicLib_1_1TransverseMercatorExact.html>} 

51 

52 - L{Geodesic}, L{GeodesicLine} -- I{wrapped} U{geodesic.Geodesic<https://PyPI.org/project/geographiclib>}, 

53 I{wrapped} U{geodesicline.GeodesicLine<https://PyPI.org/project/geographiclib>} 

54 

55 - L{GeodesicAreaExact}, L{PolygonArea} -- U{PolygonArea<https://GeographicLib.SourceForge.io/C++/doc/ 

56 classGeographicLib_1_1PolygonAreaT.html>} 

57 

58 - L{GeodesicExact}, L{GeodesicLineExact} -- U{GeodesicExact<https://GeographicLib.SourceForge.io/C++/doc/ 

59 classGeographicLib_1_1GeodesicExact.html>}, U{GeodesicLineExact<https://GeographicLib.SourceForge.io/C++/doc/ 

60 classGeographicLib_1_1GeodesicLineExact.html>} 

61 

62 - L{GeoidKarney} -- U{Geoid<https://GeographicLib.SourceForge.io/C++/doc/geoid.html>} 

63 

64 - L{Georef} -- U{Georef<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Georef.html>} 

65 

66 - L{GnomonicExact}, L{GnomonicGeodSolve}, L{GnomonicKarney} -- U{Gnomonic 

67 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Gnomonic.html>} 

68 

69 - L{Intersector} -- U{Intersect<https://GeographicLib.SourceForge.io/C++/doc/ 

70 classGeographicLib_1_1Intersect.html>} from I{GeographicLib 2.3+} 

71 

72 - L{JacobiConformal} -- U{JacobiConformal 

73 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1experimental_1_1JacobiConformal.html>} 

74 

75 - L{KTransverseMercator} - U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/ 

76 classGeographicLib_1_1TransverseMercator.html>} 

77 

78 - L{LocalCartesian}, L{Ltp} -- U{LocalCartesian<https://GeographicLib.SourceForge.io/C++/doc/ 

79 classGeographicLib_1_1LocalCartesian.html>} 

80 

81 - L{Osgr} -- U{OSGB<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1OSGB.html>} 

82 

83 - L{rhumb.aux_}, L{RhumbAux}, L{RhumbLineAux} -- U{Rhumb<https://GeographicLib.SourceForge.io/C++/doc/ 

84 classGeographicLib_1_1Rhumb.html>} and U{RhumbLine<https://GeographicLib.SourceForge.io/C++/doc/ 

85 classGeographicLib_1_1RhumbLine.html>} from I{GeographicLib 2.2+} 

86 

87 - L{rhumb.ekx}, L{Rhumb}, L{RhumbLine} -- U{Rhumb<https://GeographicLib.SourceForge.io/C++/doc/ 

88 classGeographicLib_1_1Rhumb.html>}, U{RhumbLine<https://GeographicLib.SourceForge.io/C++/doc/ 

89 classGeographicLib_1_1RhumbLine.html>}, U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/ 

90 classGeographicLib_1_1TransverseMercator.html>} from I{GeographicLib 2.0} 

91 

92 - L{Triaxial3}, L{Triaxial3B} -- U{Cartesian3<https://GeographicLib.SourceForge.io/C++/doc/ 

93 classGeographicLib_1_1Triaxial_1_1Cartesian3.html>}, U{Ellipsoidal3<https://GeographicLib.SourceForge.io/C++/doc/ 

94 classGeographicLib_1_1Triaxial_1_1Ellipsoidal3.html>} in I{GeographicLib 2.7} 

95 

96 - L{Ups} -- U{PolarStereographic<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1PolarStereographic.html>} 

97 

98 - L{Utm} -- U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>} 

99 

100 - L{UtmUps}, L{Epsg} -- U{UTMUPS<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1UTMUPS.html>} 

101 

102 - L{atan1d}, L{atan2d}, L{sincos2}, L{sincos2d}, L{tand} -- U{geomath.Math<https://GeographicLib.SourceForge.io/C++/doc/ 

103 classGeographicLib_1_1Math.html>} 

104 

105are I{transcoded} from C++ classes in I{Karney}'s U{GeographicLib<https://GeographicLib.SourceForge.io/C++/doc/annotated.html>}. 

106 

1072. These C{pygeodesy} modules and classes 

108 

109 - L{ellipsoidalGeodSolve}, L{ellipsoidalKarney}, L{geodesici}, L{geodsolve}, L{karney}, L{rhumb.solve} 

110 - L{EquidistantKarney}, L{FrechetKarney}, L{GeodesicSolve}, L{GeodesicLineSolve}, L{Geodesic3Solve}, 

111 L{GeodesicLine3Solve}, L{GnomonicGeodSolve}, L{GnomonicKarney}, L{HeightIDWkarney}, L{Intersectool} 

112 

113are or use I{wrappers} around I{Karney}'s Python U{geographiclib<https://PyPI.org/project/geographiclib>} or 

114C++ utility U{GeodSolve<https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>}, U{Geod3Solve 

115<https://GeographicLib.SourceForge.io/C++/doc/Geod3Solve.1.html>}, U{IntersectTool 

116<https://GeographicLib.SourceForge.io/C++/doc/IntersectTool.1.html>} or U{RhumbSolve 

117<https://GeographicLib.SourceForge.io/C++/doc/RhumbSolve.1.html>}. 

118 

1193. All C{pygeodesy} functions and methods to compute I{ellipsoidal} intersections, nearest points and trilaterations 

120 

121 - L{ellipsoidalExact.intersection3}, L{ellipsoidalExact.intersections2}, L{ellipsoidalExact.nearestOn}, 

122 L{ellipsoidalExact.LatLon.intersection3}, L{ellipsoidalExact.LatLon.intersections2}, 

123 L{ellipsoidalExact.LatLon.nearestOn}, L{ellipsoidalExact.LatLon.trilaterate5} 

124 

125 - L{ellipsoidalKarney.intersection3}, L{ellipsoidalKarney.intersections2}, L{ellipsoidalKarney.nearestOn}, 

126 L{ellipsoidalKarney.LatLon.intersection3}, L{ellipsoidalKarney.LatLon.intersections2}, 

127 L{ellipsoidalKarney.LatLon.nearestOn}, L{ellipsoidalKarney.LatLon.trilaterate5} 

128 

129 - L{ellipsoidalVincenty.intersection3}, L{ellipsoidalVincenty.intersections2}, L{ellipsoidalVincenty.nearestOn}, 

130 L{ellipsoidalVincenty.LatLon.intersection3}, L{ellipsoidalVincenty.LatLon.intersections2}, 

131 L{ellipsoidalVincenty.LatLon.nearestOn}, L{ellipsoidalVincenty.LatLon.trilaterate5} 

132 

133 - L{RhumbLineAux.Intersection} and L{RhumbLine.Intersection} 

134 

135are implementations of I{Karney}'s iterative solution posted under U{The B{ellipsoidal} case 

136<https://GIS.StackExchange.com/questions/48937/calculating-intersection-of-two-circles>} and in paper U{Geodesics 

137on an ellipsoid of revolution<https://ArXiv.org/pdf/1102.1215.pdf>} (pp 20-21, section B{14. MARITIME BOUNDARIES}). 

138 

1394. The C{pygeodesy} methods to compute I{ellipsoidal} intersections and nearest points 

140 

141 - L{RhumbLineAux.Intersecant2}, L{RhumbLineAux.PlumbTo}, L{RhumbLine.Intersecant2} and L{RhumbLine.PlumbTo} 

142 

143are I{transcoded} of I{Karney}'s iterative C++ function U{rhumb-intercept 

144<https://SourceForge.net/p/geographiclib/discussion/1026620/thread/2ddc295e/>}. 

145 

1465. Spherical functions 

147 

148 - L{pygeodesy.excessKarney_}, L{sphericalTrigonometry.areaOf} 

149 

150in C{pygeodesy} are based on I{Karney}'s post U{Area of a spherical polygon 

151<https://MathOverflow.net/questions/97711/the-area-of-spherical-polygons>}, 3rd Answer. 

152''' 

153# make sure int/int division yields float quotient, see .basics 

154from __future__ import division as _; del _ # noqa: E702 ; 

155 

156from pygeodesy.basics import _copysign, _isin, isint, neg, _xgeographiclib, _zip 

157from pygeodesy.constants import NAN, _isfinite as _math_isfinite, \ 

158 _0_0, _1_0, _2_0, _180_0, _N_180_0, _360_0 

159# from pygeodesy.deprecated.classes import Rhumb7Tuple # _MODS 

160from pygeodesy.errors import GeodesicError, _ValueError, _xkwds 

161# from pygeodesy.geod3Solve import Geod3Solve8Tuple # _MODS 

162from pygeodesy.fmath import cbrt, fhorner, fremainder, norm2 

163from pygeodesy.fsums import _Ksum 

164from pygeodesy.internals import _getenv, _popen2, _PYGEODESY_ENV, typename, \ 

165 _version_info 

166from pygeodesy.interns import NN, _a12_, _area_, _azi2_, _azi12_, _composite_, \ 

167 _lat1_, _lat2_, _lon1_, _lon2_, _m12_, _M12_, \ 

168 _M21_, _number_, _s12_, _S12_, _SPACE_, \ 

169 _UNDER_, _X_, _1_, _2_, _BAR_ # PYCHOK used! 

170from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _FOR_DOCS 

171from pygeodesy.named import ADict, _NamedBase, _NamedTuple, notImplemented, _Pass 

172from pygeodesy.props import deprecated_method, property_RO, property_ROnce 

173from pygeodesy.units import Azimuth as _Azi, Degrees as _Deg, Lat, Lon, \ 

174 Meter as _M, Meter2 as _M2, Number_ 

175from pygeodesy.utily import atan2d, sincos2d, tand, _unrollon, fabs 

176 

177# from math import fabs # from .utily 

178 

179__all__ = _ALL_LAZY.karney 

180__version__ = '25.12.23' 

181 

182_1_16th = _1_0 / 16 

183_2_4_ = '2.4' 

184_K_2_0 = _getenv(_PYGEODESY_ENV(typename(_xgeographiclib)[2:]), _2_) 

185_K_2_4 = _K_2_0 == _2_4_ 

186_K_2_0 = _K_2_4 or (_K_2_0 == _2_) 

187_perimeter_ = 'perimeter' 

188 

189 

190class _GTuple(_NamedTuple): # in .testNamedTuples 

191 '''(INTERNAL) Helper. 

192 ''' 

193 def _dup_iteration(self, r): 

194 if self._iteration is not None: 

195 r._iteration = self._iteration 

196 return r 

197 

198 def toGDict(self, **updates): # NO name=NN 

199 '''Convert this C{*Tuple} to a L{GDict}. 

200 

201 @kwarg updates: Optional items to apply (C{name=value} pairs) 

202 ''' 

203 r = GDict(_zip(self._Names_, self)) # strict=True 

204 if updates: 

205 r.update(updates) 

206 return self._dup_iteration(r) 

207 

208 def _toTuple(self, nTuple, dflt, updates={}): 

209 '''(INTERNAL) Convert this C{_GTuple} to an B{C{nTuple}}. 

210 ''' 

211 _g = self.toGDict(**updates).get 

212 t = tuple(_g(n, dflt) for n in nTuple._Names_) 

213 t = nTuple(t, name=self.name) 

214 return self._dup_iteration(t) 

215 

216 

217class _Lat(Lat): 

218 '''(INTERNAL) Latitude B{C{lat}}. 

219 ''' 

220 def __init__(self, *lat, **Error_name): 

221 kwds = _xkwds(Error_name, clip=0, Error=GeodesicError) 

222 Lat.__new__(_Lat, *lat, **kwds) 

223 

224 

225class _Lon(Lon): 

226 '''(INTERNAL) Longitude B{C{lon}}. 

227 ''' 

228 def __init__(self, *lon, **Error_name): 

229 kwds = _xkwds(Error_name, clip=0, Error=GeodesicError) 

230 Lon.__new__(_Lon, *lon, **kwds) 

231 

232 

233class Area3Tuple(_NamedTuple): # in .geodesicx.gxarea 

234 '''3-Tuple C{(number, perimeter, area)} with the C{number} 

235 of points of the polygon or polyline, the C{perimeter} in 

236 C{meter} and the C{area} in C{meter} I{squared}. 

237 ''' 

238 _Names_ = (_number_, _perimeter_, _area_) 

239 _Units_ = ( Number_, _M, _M2) 

240 

241 

242class Caps(object): 

243 '''I{Enum}-style masks to be bit-C{or}'ed to specify C{geodesic}, C{rhumb} 

244 or C{-lines} capabilities (C{caps}) and method results (C{outmask}). 

245 

246 C{AREA} - compute the area C{S12}, 

247 

248 C{AZIMUTH} - include azimuths C{azi1} and C{azi2}, 

249 

250 C{DISTANCE} - compute distance C{s12} and angular distance C{a12}, 

251 

252 C{DISTANCE_IN} - allow distance C{s12} in method C{.Direct}, 

253 

254 C{EMPTY} - nothing, formerly aka C{NONE}, 

255 

256 C{GEODESICSCALE} - compute geodesic scales C{M12} and C{M21}, 

257 

258 C{LATITUDE} - compute latitude C{lat2}, 

259 

260 C{LINE_OFF} - Line without updates from parent C{geodesic} or C{rhumb}. 

261 

262 C{LONGITUDE} - compute longitude C{lon2}, 

263 

264 C{LONG_UNROLL} - unroll C{lon2} in method C{.Direct}, 

265 

266 C{NONFINITONAN} - set all C{GDict} items to C{NAN} iff any argument is C{non-finite}. 

267 

268 C{REDUCEDLENGTH} - compute the reduced length C{m12}, 

269 

270 C{REVERSE2} - reverse azimuth C{azi2} by 180 degrees, 

271 

272 and C{ALL} - all of the above. 

273 

274 C{STANDARD} = C{AZIMUTH | DISTANCE | LATITUDE | LONGITUDE} 

275 

276 C{LINE_CAPS} = C{STANDARD | DISTANCE_IN | REDUCEDLENGTH | GEODESICSCALE} 

277 ''' 

278 EMPTY = 0 # formerly aka NONE 

279 _CAP_1 = 1 << 0 # for goedesici/-w 

280 _CAP_1p = 1 << 1 # for goedesici/-w 

281 _CAP_2 = 1 << 2 

282 _CAP_3 = 1 << 3 # for goedesici/-w 

283 _CAP_4 = 1 << 4 # for goedesicw 

284# = 1 << 5 # unused 

285# = 1 << 6 # unused 

286 _CAP_ALL = 0x1F 

287# _CAP_MASK = _CAP_ALL 

288 LATITUDE = 1 << 7 # compute latitude C{lat2} 

289 LONGITUDE = 1 << 8 | _CAP_3 # compute longitude C{lon2} 

290 AZIMUTH = 1 << 9 # azimuths C{azi1} and C{azi2} 

291 DISTANCE = 1 << 10 | _CAP_1 # compute distance C{s12} 

292 DISTANCE_IN = 1 << 11 | _CAP_1 | _CAP_1p # allow distance C{s12} in Direct 

293 REDUCEDLENGTH = 1 << 12 | _CAP_1 | _CAP_2 # compute reduced length C{m12} 

294 GEODESICSCALE = 1 << 13 | _CAP_1 | _CAP_2 # compute geodesic scales C{M12} and C{M21} 

295 AREA = 1 << 14 | _CAP_4 # compute area C{S12} 

296 ALL = 0x7F80 | _CAP_ALL # without LONG_UNROLL, LINE_OFF, NONFINITONAN, REVERSE2 and _DEBUG_* 

297 

298 STANDARD = AZIMUTH | DISTANCE | LATITUDE | LONGITUDE 

299 STANDARD_LINE = STANDARD | DISTANCE_IN # for goedesici/-w 

300 

301 LINE_CAPS = STANDARD_LINE | REDUCEDLENGTH | GEODESICSCALE # .geodesici only 

302 LONG_UNROLL = 1 << 15 # unroll C{lon2} in .Direct and .Position 

303 LINE_OFF = 1 << 16 # Line without updates from parent geodesic or rhumb 

304 NONFINITONAN = 1 << 17 # see method GDict._toNAN 

305 REVERSE2 = 1 << 18 # reverse C{azi2} 

306 

307 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE 

308 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA 

309 

310 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE 

311 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA 

312 

313 _SALP_CALPs_ = 1 << 19 # (INTERNAL) GeodesicExact._GenInverse 

314 

315 _DEBUG_AREA = 1 << 20 # (INTERNAL) include Line details 

316 _DEBUG_DIRECT = 1 << 21 # (INTERNAL) include Direct details 

317 _DEBUG_INVERSE = 1 << 22 # (INTERNAL) include Inverse details 

318 _DEBUG_LINE = 1 << 23 # (INTERNAL) include Line details 

319 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \ 

320 _DEBUG_LINE | _SALP_CALPs_ 

321 

322 _DIRECT3 = AZIMUTH | LATITUDE | LONGITUDE # for goedesicw only 

323 _INVERSE3 = AZIMUTH | DISTANCE # for goedesicw only 

324 

325 _OUT_ALL = ALL # see geographiclib.geodesiccapabilities.py 

326 _OUT_MASK = ALL | LONG_UNROLL | NONFINITONAN | REVERSE2 | _DEBUG_ALL 

327 

328 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE 

329 _AZIMUTH_LATITUDE_LONG_UNROLL = AZIMUTH | LATITUDE | LONG_UNROLL 

330 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE 

331# _DISTANCE_IN_OUT = DISTANCE_IN & _OUT_MASK # == DISTANCE_IN in .gx, .gxline 

332 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE 

333# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE 

334 

335 def items(self): 

336 '''Yield all I{public} C{Caps} as 2-tuple C{(NAME, mask)}. 

337 ''' 

338 for n, C in type(self).__dict__.items(): 

339 if isint(C) and not n.startswith(_UNDER_) \ 

340 and n.replace(_UNDER_, NN).isupper(): 

341 yield n, C 

342 

343 def toStr(self, Cask, sep=_BAR_): 

344 '''Return C{Caps} or an C{outmask} as C{str} or tuple of C{str}s. 

345 ''' 

346 s = (n for n, C in sorted(Caps.items()) 

347 if C and (Cask & C) == C) # and int1s(C) <= 3 

348 return sep.join(s) if sep else tuple(s) 

349 

350if _FOR_DOCS: # PYCHOK force ... 

351 pass # ... CAPS.__doc__ into epydoc 

352else: 

353 Caps = Caps() # PYCHOK singleton 

354 

355_key2Caps = dict(a12 =Caps.DISTANCE, # in GDict._toNAN, -._unCaps 

356 azi1=Caps.AZIMUTH, 

357 azi2=Caps.AZIMUTH, 

358 lat1=Caps.LATITUDE, 

359 lat2=Caps.LATITUDE, 

360 lon1=Caps.LONGITUDE, 

361 lon2=Caps.LONGITUDE, 

362 m12 =Caps.REDUCEDLENGTH, 

363 M12 =Caps.GEODESICSCALE, 

364 M21 =Caps.GEODESICSCALE, 

365 s12 =Caps.DISTANCE, 

366 S12 =Caps.AREA) 

367 

368 

369class _CapsBase(_NamedBase): # in .auxilats, .geodesicx.gxbases 

370 '''(INTERNAL) Base class for C{Geodesic*}, C{Geodesic*Exact}, C{Intersectool} and C{Rhumb*Base}. 

371 ''' 

372 ALL = Caps.ALL 

373 AREA = Caps.AREA 

374 AZIMUTH = Caps.AZIMUTH 

375 DISTANCE = Caps.DISTANCE 

376 DISTANCE_IN = Caps.DISTANCE_IN 

377 EMPTY = Caps.EMPTY # aka NONE 

378 GEODESICSCALE = Caps.GEODESICSCALE 

379 LATITUDE = Caps.LATITUDE 

380 LINE_CAPS = Caps.LINE_CAPS 

381 LINE_OFF = Caps.LINE_OFF 

382 LONGITUDE = Caps.LONGITUDE 

383 LONG_UNROLL = Caps.LONG_UNROLL 

384 NONFINITONAN = Caps.NONFINITONAN 

385 REDUCEDLENGTH = Caps.REDUCEDLENGTH 

386 STANDARD = Caps.STANDARD 

387 STANDARD_LINE = Caps.STANDARD_LINE # for geodesici 

388 

389 _caps = 0 # None 

390 _debug = 0 # or Caps._DEBUG_... 

391 

392 @property_RO 

393 def caps(self): 

394 '''Get the capabilities (bit-or'ed C{Caps}). 

395 ''' 

396 return self._caps 

397 

398 def caps_(self, caps): 

399 '''Check the available capabilities. 

400 

401 @arg caps: Bit-or'ed combination of L{Caps<pygeodesy.karney.Caps>} 

402 values for all capabilities to be checked. 

403 

404 @return: C{True} if I{all} B{C{caps}} are available, C{False} 

405 otherwise (C{bool}). 

406 ''' 

407 caps &= Caps._OUT_ALL 

408 return (self.caps & caps) == caps 

409 

410 @property 

411 def debug(self): 

412 '''Get the C{debug} option (C{bool}). 

413 ''' 

414 return bool(self._debug) 

415 

416 @debug.setter # PYCHOK setter! 

417 def debug(self, debug): 

418 '''Set the C{debug} option (C{bool}) to include 

419 more details in L{GDict} results. 

420 ''' 

421 self._debug = Caps._DEBUG_ALL if debug else 0 

422 

423 def _iter2tion(self, r, iter=None, **unused): 

424 '''(INTERNAL) Copy C{C{s}.iter} into C{B{r}._iteration}. 

425 ''' 

426 if iter is not None: 

427 self._iteration = r._iteration = iter 

428 return r 

429 

430 

431class Direct9Tuple(_GTuple): 

432 '''9-Tuple C{(a12, lat2, lon2, azi2, s12, m12, M12, M21, S12)} with arc 

433 length C{a12}, angles C{lat2}, C{lon2} and azimuth C{azi2} in C{degrees}, 

434 distance C{s12} and reduced length C{m12} in C{meter} and area C{S12} in 

435 C{meter} I{squared}. 

436 ''' 

437 _Names_ = (_a12_, _lat2_, _lon2_, _azi2_, _s12_, _m12_, _M12_, _M21_, _S12_) 

438 _Units_ = (_Azi, _Lat, _Lon, _Azi, _M, _Pass, _Pass, _Pass, _M2) 

439 

440 

441class GDict(ADict): # XXX _NamedDict 

442 '''A C{dict} with both C{key} I{and} C{attribute} access to the C{dict} items. 

443 

444 Results of all C{geodesic} and C{rhumb} methods (with capitalized named) are 

445 returned as L{GDict} instances, see for example L{GeodesicExact} and L{RhumbAux}. 

446 ''' 

447 def toDirect9Tuple(self, dflt=NAN): 

448 '''Convert this L{GDict} result to a 9-tuple, like I{Karney}'s method 

449 C{geographiclib.geodesic.Geodesic._GenDirect}. 

450 

451 @kwarg dflt: Default value for missing items (C{any}). 

452 

453 @return: L{Direct9Tuple}C{(a12, lat2, lon2, azi2, 

454 s12, m12, M12, M21, S12)} 

455 ''' 

456 return self._toTuple(Direct9Tuple, dflt) 

457 

458 def toGeodSolve12Tuple(self, dflt=NAN): # PYCHOK 12 args 

459 '''Convert this L{GDict} result to a 12-Tuple, compatible with I{Karney}'s 

460 U{GeodSolve<https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>} 

461 result. 

462 

463 @kwarg dflt: Default value for missing items (C{any}). 

464 

465 @return: L{GeodSolve12Tuple}C{(lat1, lon1, azi1, lat2, lon2, azi2, 

466 s12, a12, m12, M12, M21, S12)}. 

467 ''' 

468 return self._toTuple(_MODS.geodsolve.GeodSolve12Tuple, dflt) 

469 

470 def toGeod3Solve8Tuple(self, dflt=NAN): # PYCHOK 12 args 

471 '''Convert this L{GDict} result to an 8-Tuple, compatible with I{Karney}'s 

472 U{Geod3Solve<https://GeographicLib.SourceForge.io/C++/doc/Geod3Solve.1.html>} 

473 result. 

474 

475 @kwarg dflt: Default value for missing items (C{any}). 

476 

477 @return: L{Geod3Solve8Tuple}C{(bet1, omg1, alp1, bet2, omg2, alp2, s12, a12)}. 

478 ''' 

479 return self._toTuple(_MODS.geod3solve.Geod3Solve8Tuple, dflt) 

480 

481 def toInverse10Tuple(self, dflt=NAN): 

482 '''Convert this L{GDict} result to a 10-tuple, like I{Karney}'s 

483 method C{geographiclib.geodesic.Geodesic._GenInverse}. 

484 

485 @kwarg dflt: Default value for missing items (C{any}). 

486 

487 @return: L{Inverse10Tuple}C{(a12, s12, salp1, calp1, salp2, calp2, 

488 m12, M12, M21, S12)}. 

489 ''' 

490 return self._toTuple(Inverse10Tuple, dflt) 

491 

492 def _toNAN(self, outmask, **specs): # .GeodesicExact._GDistInverse, .GeodesicLineExact._GenPosition 

493 '''(INTERNAL) Convert this C{GDict} to all C{NAN}s. 

494 ''' 

495 if (outmask & Caps.NONFINITONAN): 

496 def _t2(k): 

497 return k, specs.get(k, NAN) 

498 

499 d = dict(_t2(k) for k, C in _key2Caps.items() 

500 if (outmask & C) == C) 

501 self.set_(**d) # self.update(d) 

502 return self 

503 

504 @deprecated_method 

505 def toRhumb7Tuple(self, dflt=NAN): # PYCHOK no cover 

506 '''DEPRECATED on 23.12.07, use method C{toRhumb8Tuple}. 

507 

508 @return: A I{DEPRECATED} L{Rhumb7Tuple}. 

509 ''' 

510 return self._toTuple(_MODS.deprecated.classes.Rhumb7Tuple, dflt) 

511 

512 def toRhumb8Tuple(self, dflt=NAN): 

513 '''Convert this L{GDict} result to a 8-tuple. 

514 

515 @kwarg dflt: Default value for missing items (C{any}). 

516 

517 @return: L{Rhumb8Tuple}C{(lat1, lon1, lat2, lon2, azi12, 

518 s12, S12, a12)}. 

519 ''' 

520 return self._toTuple(Rhumb8Tuple, dflt) 

521 

522 def toRhumbSolve7Tuple(self, dflt=NAN): 

523 '''Convert this L{GDict} result to a 8-tuple. 

524 

525 @kwarg dflt: Default value for missing items (C{any}). 

526 

527 @return: L{RhumbSolve7Tuple}C{(lat1, lon1, lat2, lon2, 

528 azi12, s12, S12)}. 

529 ''' 

530 return self._toTuple(_MODS.rhumb.solve.RhumbSolve7Tuple, dflt) 

531 

532 def _toTuple(self, nTuple, dflt): 

533 '''(INTERNAL) Convert this C{GDict} to an B{C{nTuple}}. 

534 ''' 

535 t = tuple(self.get(n, dflt) for n in nTuple._Names_) 

536 return nTuple(t, iteration=self._iteration, name=self.name) # PYCHOK name 

537 

538 def _2X(self, gl, _2X=_X_): # .Intersectool, .Intersector 

539 '''(INTERNAL) Rename C{-2} attr to C{-X} or C{-M}. 

540 ''' 

541 X = GDict(self) 

542 for n in (_lat2_, _lon2_, _azi2_, _s12_, _a12_): 

543 if n in X: # X._X = X._2 

544 X[n[:-1] + _2X] = X.pop(n) 

545 v = getattr(gl, n, X) 

546 if v is not X: # X._2 = gl._2 

547 X[n] = v 

548 return X 

549 

550 def _unCaps(self, outmask): # in .geodsolve 

551 '''(INTERNAL) Remove superfluous items. 

552 ''' 

553 for k, C in _key2Caps.items(): 

554 if k in self and (outmask & C) != C: 

555 self.pop(k) # delattr(self, k) 

556 return self 

557 

558 

559class Inverse10Tuple(_GTuple): 

560 '''10-Tuple C{(a12, s12, salp1, calp1, salp2, calp2, m12, M12, M21, S12)} with arc length 

561 C{a12} in C{degrees}, distance C{s12} and reduced length C{m12} in C{meter}, area 

562 C{S12} in C{meter} I{squared} and the sines C{salp1}, C{salp2} and cosines C{calp1}, 

563 C{calp2} of the initial C{1} and final C{2} (forward) azimuths. 

564 ''' 

565 _Names_ = (_a12_, _s12_, 'salp1', 'calp1', 'salp2', 'calp2', _m12_, _M12_, _M21_, _S12_) 

566 _Units_ = (_Azi, _M, _Pass, _Pass, _Pass, _Pass, _Pass, _Pass, _Pass, _M2) 

567 

568 def toGDict(self, **updates): 

569 '''Convert this C{Inverse10Tuple} to a L{GDict}. 

570 

571 @kwarg updates: Optional items to apply (C{nam=value} pairs) 

572 ''' 

573 return _GTuple.toGDict(self, azi1=atan2d(self.salp1, self.calp1), # PYCHOK namedTuple 

574 azi2=atan2d(self.salp2, self.calp2), # PYCHOK namedTuple 

575 **updates) # PYCHOK indent 

576 

577 

578class Rhumb8Tuple(_GTuple): 

579 '''8-Tuple C{(lat1, lon1, lat2, lon2, azi12, s12, S12, a12)} with lat- C{lat1}, 

580 C{lat2} and longitudes C{lon1}, C{lon2} of both points, the azimuth of the 

581 rhumb line C{azi12}, the distance C{s12}, the area C{S12} under the rhumb 

582 line and the angular distance C{a12} between both points. 

583 ''' 

584 _Names_ = (_lat1_, _lon1_, _lat2_, _lon2_, _azi12_, _s12_, _S12_, _a12_) 

585 _Units_ = ( Lat, Lon, Lat, Lon, _Azi, _M, _M2, _Deg) 

586 

587 def toDirect9Tuple(self, dflt=NAN, **a12_azi1_azi2_m12_M12_M21): 

588 '''Convert this L{Rhumb8Tuple} result to a 9-tuple, like I{Karney}'s 

589 method C{geographiclib.geodesic.Geodesic._GenDirect}. 

590 

591 @kwarg dflt: Default value for missing items (C{any}). 

592 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments 

593 to specify or override L{Inverse10Tuple} items. 

594 

595 @return: L{Direct9Tuple}C{(a12, lat2, lon2, azi2, s12, 

596 m12, M12, M21, S12)} 

597 ''' 

598 d = dict(azi1=self.azi12, M12=_1_0, m12=self.s12, # PYCHOK attr 

599 azi2=self.azi12, M21=_1_0) # PYCHOK attr 

600 if a12_azi1_azi2_m12_M12_M21: 

601 d.update(a12_azi1_azi2_m12_M12_M21) 

602 return self._toTuple(Direct9Tuple, dflt, d) 

603 

604 def toInverse10Tuple(self, dflt=NAN, **a12_m12_M12_M21_salp1_calp1_salp2_calp2): 

605 '''Convert this L{Rhumb8Tuple} to a 10-tuple, like I{Karney}'s 

606 method C{geographiclib.geodesic.Geodesic._GenInverse}. 

607 

608 @kwarg dflt: Default value for missing items (C{any}). 

609 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword 

610 arguments to specify or override L{Inverse10Tuple} items. 

611 

612 @return: L{Inverse10Tuple}C{(a12, s12, salp1, calp1, salp2, calp2, 

613 m12, M12, M21, S12)}. 

614 ''' 

615 s, c = _sincos2d(self.azi12) # PYCHOK attr 

616 d = dict(salp1=s, calp1=c, M12=_1_0, m12=self.s12, # PYCHOK attr 

617 salp2=s, calp2=c, M21=_1_0) 

618 if a12_m12_M12_M21_salp1_calp1_salp2_calp2: 

619 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2) 

620 return self._toTuple(Inverse10Tuple, dflt, d) 

621 

622 @deprecated_method 

623 def _to7Tuple(self): # in DEPRECATED Rhumb7Tuple 

624 '''DEPRECATED, I{do not use!}''' 

625 return _MODS.deprecated.classes.Rhumb7Tuple(self[:-1]) 

626 

627 

628class _kWrapped(_CapsBase): # in .geodesicw 

629 '''(INTERNAL) Wrapper for some of I{Karney}'s U{geographiclib 

630 <https://PyPI.org/project/geographiclib>} classes. 

631 ''' 

632 

633 @property_ROnce 

634 def geographiclib(self): 

635 '''Lazily import C{geographiclib}, provided the U{geographiclib 

636 <https://PyPI.org/project/geographiclib>} package is installed, 

637 otherwise raise a C{LazyImportError}. 

638 ''' 

639 g = _xgeographiclib(type(self), 1, 49) 

640 from geographiclib.geodesic import Geodesic 

641 g.Geodesic = Geodesic 

642 from geographiclib.geodesicline import GeodesicLine 

643 g.GeodesicLine = GeodesicLine 

644 from geographiclib.geomath import Math 

645 g.Math = Math 

646# from geographiclib.polygonarea import PolygonArea 

647# g.PolygonArea = PolygonArea # see below 

648 return g 

649 

650 @property_ROnce 

651 def Math(self): 

652 '''Wrap the C{geomath.Math} class, provided the U{geographiclib 

653 <https://PyPI.org/project/geographiclib>} package is installed, 

654 otherwise C{None}. 

655 ''' 

656 try: 

657 g = self.geographiclib 

658 M = g.Math 

659 if _version_info(g) < (2,): 

660 if _K_2_0: 

661 M = None 

662# elif not _K_2_0: # XXX set 2.0? 

663# _K_2_0 = True 

664 except (AttributeError, ImportError): 

665 M = None 

666 return M 

667 

668 @property_RO 

669 def Math_K_2(self): 

670 return (_2_4_ if _K_2_4 else 

671 (_2_ if _K_2_0 else _1_)) if self.Math else NN 

672 

673 @property_ROnce 

674 def _PolygonArea(self): # lazy import 

675 from geographiclib.polygonarea import PolygonArea 

676 return PolygonArea 

677 

678_wrapped = _kWrapped() # PYCHOK singleton, .datum, .test/base.py 

679 

680 

681class _Xables(object): 

682 '''(INTERNAL) Get I{Karney}'s executable paths from/and env vars. 

683 ''' 

684 bin_ = '/opt/local/bin/' # '/opt/local/Cellar/geographiclib/2.X/bin/' # HomeBrew on macOS 

685 ENV = NN 

686 

687 def GeoConvert(self, *dir_): 

688 return self._path(self.GeoConvert, *dir_) 

689 

690 def GeodSolve(self, *dir_): 

691 return self._path(self.GeodSolve, *dir_) 

692 

693 def Geod3Solve(self, *dir_): 

694 return self._path(self.Geod3Solve, *dir_) 

695 

696 def IntersectTool(self, *dir_): 

697 return self._path(self.IntersectTool, *dir_) 

698 

699 def RhumbSolve(self, *dir_): 

700 return self._path(self.RhumbSolve, *dir_) 

701 

702 def name_version(self, path, base=True): 

703 # return C{(path + ' ' + version)} of an executable 

704 if path: 

705 try: 

706 r, s = _popen2((path, '--version')) 

707 if base: 

708 path = _MODS.os.path.basename(path) 

709 r = _SPACE_(path, r.split()[-1]) 

710 else: 

711 r = _MODS.streprs.Fmt.PARENSPACED(r, s) 

712 return r 

713 except (IndexError, IOError, OSError): 

714 pass 

715 return NN 

716 

717 def _path(self, which, *dir_): 

718 n = typename(which, which) 

719 self.ENV = E = _PYGEODESY_ENV(n) 

720 return _getenv(E, NN) or (NN(dir_[0], n) if dir_ else E) 

721 

722 def X_not(self, path): 

723 return 'env %s=%r not executable' % (self.ENV, path) 

724 

725 def X_OK(self, path): # is C{path} an executable? 

726 os = _MODS.os # import os 

727 return os.access(path, os.X_OK) if path else False 

728 

729_Xables = _Xables() # PYCHOK singleton 

730 

731 

732def _around(x): # in .utily.sincos2d 

733 '''I{Coarsen} a scalar by rounding small values to underflow to C{0.0}. 

734 

735 @return: Coarsened value (C{float}). 

736 

737 @see: I{Karney}'s U{geomath.Math.AngRound<https://SourceForge.net/p/ 

738 geographiclib/code/ci/release/tree/python/geographiclib/geomath.py>} 

739 ''' 

740 try: 

741 return _wrapped.Math.AngRound(x) 

742 except AttributeError: 

743 z = _1_16th 

744 w = z - fabs(x) 

745 if w > 0: # don't "simplify" z - (z - x) to x 

746 x = _copysign(z - w, x) 

747 return x 

748 

749 

750def _atan2d(y, x): 

751 '''Return C{atan2(B{y}, B{x})} in C{degrees}. 

752 ''' 

753 try: 

754 return _wrapped.Math.atan2d(y, x) 

755 except AttributeError: 

756 return atan2d(y, x) 

757 

758 

759def _cbrt(x): 

760 '''Return C{cubic root(B{x})}. 

761 ''' 

762 try: 

763 return _wrapped.Math.cbrt(x) 

764 except AttributeError: 

765 return cbrt(x) 

766 

767 

768def _copyBit(x, y): 

769 '''Like C{copysign0(B{x}, B{y})}, with C{B{x} > 0}. 

770 ''' 

771 return (-x) if _signBit(y) else x 

772 

773 

774def _2cos2x(cx, sx): # in .auxDST, .auxLat, .gxbases 

775 '''Return M{2 * cos(2 * x)} from cos(x) and sin(x). 

776 ''' 

777 r = cx - sx 

778 if r: 

779 r *= (cx + sx) * _2_0 

780 return r 

781 

782 

783def _diff182(deg0, deg, K_2_0=False): 

784 '''Compute C{deg - deg0}, reduced to C{[-180,180]} accurately. 

785 

786 @return: 2-Tuple C{(delta_angle, residual)} in C{degrees}. 

787 ''' 

788 try: 

789 return _wrapped.Math.AngDiff(deg0, deg) 

790 except AttributeError: 

791 if K_2_0 or _K_2_0: # geographiclib 2.0+ 

792 _r = fremainder 

793 d, t = _sum2(_r(-deg0, _360_0), 

794 _r( deg, _360_0)) 

795 d, t = _sum2(_r( d, _360_0), t) 

796 if _isin(d, _0_0, _180_0, _N_180_0): 

797 d = _copysign(d, (-t) if t else (deg - deg0)) 

798 else: 

799 _n = _norm180 

800 d, t = _sum2(_n(-deg0), _n(deg)) 

801 d = _n(d) 

802 if t > 0 and d == _180_0: 

803 d = _N_180_0 

804 d, t = _sum2(d, t) 

805 return d, t 

806 

807 

808def _fix90(deg): # mimick Math.LatFix 

809 '''Replace angle in C{degrees} outside [-90,90] by NAN. 

810 

811 @return: Angle C{degrees} or NAN. 

812 ''' 

813 try: 

814 return _wrapped.Math.LatFix(deg) 

815 except AttributeError: 

816 return NAN if fabs(deg) > 90 else deg 

817 

818 

819def _isfinite(x): # mimick geomath.Math.isfinite 

820 '''Check finiteness of C{x}. 

821 

822 @return: C{True} if finite. 

823 ''' 

824 try: 

825 return _wrapped.Math.isfinite(x) 

826 except AttributeError: 

827 return _math_isfinite(x) # and fabs(x) <= _MAX 

828 

829 

830def _llz2gl(gl, **llz2): # see .geodesici._llz2G 

831 '''(INTERNAL) Set C{gl.lat2, .lon2, .azi2} from C{llz2}. 

832 ''' 

833 if llz2: 

834 for n in (_lat2_, _lon2_, _azi2_): # _lat1_, _lon1_, _azi1_ 

835 v = llz2.get(n, None) 

836 if v is not None: 

837 setattr(gl, n, v) 

838 return gl 

839 

840 

841def _norm2(x, y): # mimick geomath.Math.norm 

842 '''Normalize C{B{x}} and C{B{y}}. 

843 

844 @return: 2-Tuple of C{(B{x}, B{y})}, normalized. 

845 ''' 

846 try: 

847 return _wrapped.Math.norm(x, y) 

848 except AttributeError: 

849 return norm2(x, y) 

850 

851 

852def _norm180(deg): # mimick geomath.Math.AngNormalize 

853 '''Reduce angle in C{degrees} to (-180,180]. 

854 

855 @return: Reduced angle C{degrees}. 

856 ''' 

857 try: 

858 return _wrapped.Math.AngNormalize(deg) 

859 except AttributeError: 

860 d = fremainder(deg, _360_0) 

861 if _isin(d, _180_0, _N_180_0): 

862 d = _copysign(_180_0, deg) if _K_2_0 else _180_0 

863 return d 

864 

865 

866def _polygon(geodesic, points, closed, line, wrap, polar): 

867 '''(INTERNAL) Compute the area or perimeter of a polygon, 

868 using a L{GeodesicExact}, L{GeodesicSolve} or (if the 

869 C{geographiclib} package is installed) a C{Geodesic} 

870 or C{geodesicw.Geodesic} instance. 

871 ''' 

872 if not wrap: # capability LONG_UNROLL can't be off 

873 notImplemented(None, wrap=wrap, up=3) 

874 

875 if _MODS.booleans.isBoolean(points): 

876 # recursive call for each boolean clip 

877 

878 def _a_p(clip, *args, **unused): 

879 return _polygon(geodesic, clip, *args) 

880 

881 if not closed: # closed only 

882 raise _ValueError(closed=closed, points=_composite_) 

883 

884 return points._sum1(_a_p, closed, line, wrap, polar) 

885 

886 gP = geodesic.Polygon(line) 

887 _A = gP.AddPoint 

888 

889 Ps = _MODS.iters.PointsIter(points, loop=1, wrap=wrap) # base=LatLonEllipsoidalBase(0, 0) 

890 p1 = p0 = Ps[0] 

891 

892 # note, lon deltas are unrolled, by default 

893 _A(p1.lat, p1.lon) 

894 for p2 in Ps.iterate(closed=closed): 

895 if wrap and not Ps.looped: 

896 p2 = _unrollon(p1, p2) 

897 _A(p2.lat, p2.lon) 

898 p1 = p2 

899 if closed and line and p1 != p0: 

900 _A(p0.lat, p0.lon) 

901 

902 # gP.Compute returns (number_of_points, perimeter, signed area) 

903 return gP.Compute(reverse=False, sign=True, polar=polar)[1 if line else 2] 

904 

905 

906try: 

907 from math import fma as _fma # since 3.13 

908 

909 def _poly_fma(x, s, *cs): 

910 for c in cs: 

911 s = _fma(s, x, c) 

912 return s 

913 

914except ImportError: # Python 3.12- 

915 

916 def _poly_fma(x, *cs): # PYCHOK redef 

917 return fhorner(x, *cs, incx=False) 

918 

919# def _poly_fma(x, s, *cs): 

920# t = _0_0 

921# for c in cs: 

922# s, t, _ = _sum3(s * x, t * x, c) 

923# return s + t 

924 

925def _polynomial(x, cs, i, j): # PYCHOK shared 

926 '''(INTERNAL) Like C++ C{GeographicLib.Math.hpp.polyval} but 

927 with a different signature and cascaded summation. 

928 

929 @return: M{sum(x**(j - k - 1) * cs[k] for k in range(i, j)} 

930 ''' 

931 if (i + 1) < j <= len(cs): # load _Rtuple._tuple 

932 try: 

933 r = _wrapped.Math.polyval(j - i - 1, cs, i, x) 

934 except AttributeError: # no .Math 

935 r = _poly_fma(x, _0_0, *cs[i:j]) 

936 else: 

937 r = cs[i] 

938 return float(r) 

939 

940 

941def _remainder(x, y): 

942 '''Remainder of C{x / y}. 

943 

944 @return: Remainder in the range M{[-y / 2, y / 2]}, preserving signed 0.0. 

945 ''' 

946 try: 

947 return _wrapped.Math.remainder(x, y) 

948 except AttributeError: 

949 return fremainder(x, y) 

950 

951 

952if _K_2_0: 

953 from math import cos as _cos, sin as _sin 

954 

955 def _sincos2(rad): 

956 return _sin(rad), _cos(rad) 

957 

958 _signBit = _MODS.basics.signBit 

959else: 

960 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared 

961 

962 def _signBit(x): 

963 '''(INTERNAL) GeographicLib 1.52-. 

964 ''' 

965 return x < 0 

966 

967 

968def _sincos2d(deg): 

969 '''Return sine and cosine of an angle in C{degrees}. 

970 

971 @return: 2-Tuple C{(sin(B{deg}), cos(B{deg}))}. 

972 ''' 

973 try: 

974 return _wrapped.Math.sincosd(deg) 

975 except AttributeError: 

976 return sincos2d(deg) 

977 

978 

979def _sincos2de(deg, t): 

980 '''Return sine and cosine of a corrected angle in C{degrees}. 

981 

982 @return: 2-Tuple C{(sin(B{deg}), cos(B{deg}))}. 

983 ''' 

984 try: 

985 return _wrapped.Math.sincosde(deg, t) 

986 except AttributeError: 

987 return sincos2d(deg, adeg=t) 

988 

989 

990def _sum2(a, b): # mimick geomath.Math.sum, actually sum2 

991 '''Error-free summation like C{geomath.Math.sum}. 

992 

993 @return: 2-Tuple C{(B{a} + B{b}, residual)}. 

994 

995 @note: The C{residual} can be the same as B{C{a}} or B{C{b}}. 

996 

997 @see: U{TwoSum<https://accurate-algorithms.readthedocs.io/en/latest/ch04summation.html>} 

998 and I{Knuth}'s U{Algorithm 3.1<https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>}. 

999 ''' 

1000 try: 

1001 return _wrapped.Math.sum(a, b) 

1002 except AttributeError: 

1003 # if Algorithm_3_1: 

1004 s = a + b 

1005 r = s - b 

1006 t = s - r 

1007 # elif C_CPP: # Math::sum C/C++ 

1008 # r -= a; t -= b; t += r; t = (-t) if s else s 

1009 # else: # if s == 0: t = _copysign_0_0(s) 

1010 t = ((a - r) + (b - t)) if s else s 

1011 # assert fabs(s) >= fabs(t) 

1012 return s, t 

1013 

1014 

1015def _sum3(s, t, *xs): 

1016 '''Accumulate all B{C{xs}} scalars into a previous C{_sum2(s, t)}. 

1017 

1018 @return: 3-Tuple C{(s, t, n)} where C{s} is the sum of B{s}, B{t} and all 

1019 B{xs}, C{t} the residual and C{n} the number of non-zero C{xs}. 

1020 

1021 @see: I{Karney's} C++ U{Accumulator<https://GeographicLib.SourceForge.io/ 

1022 C++/doc/Accumulator_8hpp_source.html>} comments for more details and 

1023 function C{_sum2} above. 

1024 

1025 @note: Not "error-free", see C{pygeodesy.test/testKarney.py}. 

1026 ''' 

1027 return _Ksum(s, t, *xs)._s_t_n3 if xs else (s, t, 0) 

1028 # previous _sum3 in .geodesicx.gxarea._Accumulator.Add 

1029 # which fails .fmath.frandoms tests, but does pass 

1030 # _sum3(1e20, 1, 2, 100, 5000, -1e20) ... 5103.0, 0.0, 4 

1031 

1032 

1033def _tand(x): 

1034 '''Return C{tan(B{x})} in C{degrees}. 

1035 ''' 

1036 try: 

1037 return _wrapped.Math.tand(x) 

1038 except AttributeError: 

1039 return tand(x) # Error=None 

1040 

1041 

1042def _unroll2(lon1, lon2, wrap=False): # see .ellipsoidalBaseDI._intersects2 

1043 '''Unroll B{C{lon2 - lon1}} like C{geodesic.Geodesic.Inverse}. 

1044 

1045 @return: 2-Tuple C{(B{lon2} - B{lon1}, B{lon2})} with B{C{lon2}} 

1046 unrolled if C{B{wrap} is True}, normalized otherwise. 

1047 ''' 

1048 if wrap: 

1049 d, t = _diff182(lon1, lon2) 

1050 lon2, t, _ = _sum3(d, t, lon1) # (lon1 + d) + t 

1051 lon2 += t 

1052 else: 

1053 lon2 = _norm180(lon2) 

1054 return (lon2 - lon1), lon2 

1055 

1056 

1057def _unsigned2(x): 

1058 '''(INTERNAL) Unsign B{C{x}}. 

1059 ''' 

1060 return (neg(x), True) if _signBit(x) else (x, False) 

1061 

1062 

1063__all__ += _ALL_DOCS(Caps, _CapsBase) 

1064 

1065# **) MIT License 

1066# 

1067# Copyright (C) 2016-2026 -- mrJean1 at Gmail -- All Rights Reserved. 

1068# 

1069# Permission is hereby granted, free of charge, to any person obtaining a 

1070# copy of this software and associated documentation files (the "Software"), 

1071# to deal in the Software without restriction, including without limitation 

1072# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

1073# and/or sell copies of the Software, and to permit persons to whom the 

1074# Software is furnished to do so, subject to the following conditions: 

1075# 

1076# The above copyright notice and this permission notice shall be included 

1077# in all copies or substantial portions of the Software. 

1078# 

1079# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

1080# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

1081# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

1082# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

1083# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

1084# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

1085# OTHER DEALINGS IN THE SOFTWARE.