Coverage for pygeodesy/karney.py: 93%

358 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-25 13:15 -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}, 

23 L{AlbersEqualAreaCylindrical}, L{AlbersEqualAreaNorth}, L{AlbersEqualAreaSouth} -- 

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

25 

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

31 

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

33 classGeographicLib_1_1CassiniSoldner.html>} 

34 

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

36 

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

38 classGeographicLib_1_1EllipticFunction.html>} 

39 

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

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

42 

43 - L{Etm}, L{ExactTransverseMercator} -- U{TransverseMercatorExact 

44 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercatorExact.html>} 

45 

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

48 

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

50 classGeographicLib_1_1PolygonAreaT.html>} 

51 

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

55 

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

57 

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

59 

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

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

62 

63 - L{Intersector} -- U{Intersect 

64 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Intersect.html>} from I{GeographicLib 2.3+} 

65 

66 - L{JacobiConformal} -- U{JacobiConformal 

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

68 

69 - L{KTransverseMercator} - U{TransverseMercator 

70 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>} 

71 

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

73 classGeographicLib_1_1LocalCartesian.html>} 

74 

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

76 

77 - L{rhumb.aux_}, L{RhumbAux}, L{RhumbLineAux} -- U{Rhumb 

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

79 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1RhumbLine.html>} from I{GeographicLib 2.2+} 

80 

81 - L{rhumb.ekx}, L{Rhumb}, L{RhumbLine} -- U{Rhumb 

82 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Rhumb.html>}, 

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

84 U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1TransverseMercator.html>} 

85 from I{GeographicLib 2.0} 

86 

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

88 

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

90 

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

92 

93 - L{atan1d}, L{atan2d}, L{sincos2}, L{sincos2d}, L{tand} -- U{geomath.Math 

94 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Math.html>} 

95 

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

97 

982. These C{pygeodesy} modules and classes 

99 

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

101 - L{EquidistantKarney}, L{FrechetKarney}, L{GeodesicSolve}, L{GeodesicLineSolve}, L{GnomonicGeodSolve}, 

102 L{GnomonicKarney}, L{HeightIDWkarney}, L{Intersectool} 

103 

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

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

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

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

108 

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

110 

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

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

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

114 

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

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

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

118 

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

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

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

122 

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

124 

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

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

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

128 

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

130 

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

132 

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

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

135 

1365. Spherical functions 

137 

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

139 

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

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

142''' 

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

144from __future__ import division as _; del _ # PYCHOK semicolon 

145 

146from pygeodesy.basics import _copysign, _isin, isint, neg, unsigned0, \ 

147 _xgeographiclib, _zip 

148from pygeodesy.constants import NAN, _isfinite as _math_isfinite, _0_0, \ 

149 _1_16th, _1_0, _2_0, _180_0, _N_180_0, _360_0 

150from pygeodesy.errors import GeodesicError, _ValueError, _xkwds 

151from pygeodesy.fmath import cbrt, fremainder, norm2 # Fhorner, Fsum 

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

153 _version_info 

154from pygeodesy.interns import NN, _a12_, _area_, _azi1_, _azi2_, _azi12_, \ 

155 _composite_, _lat1_, _lat2_, _lon1_, _lon2_, \ 

156 _m12_, _M12_, _M21_, _number_, _s12_, _S12_, \ 

157 _SPACE_, _UNDER_, _X_, _1_, _2_, _BAR_ # PYCHOK used! 

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

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

160from pygeodesy.props import deprecated_method, Property_RO, property_RO, \ 

161 property_ROnce 

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

163 Meter as _M, Meter2 as _M2, Number_ 

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

165 

166# from math import fabs # from .utily 

167 

168__all__ = _ALL_LAZY.karney 

169__version__ = '25.04.14' 

170 

171_2_4_ = '2.4' 

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

173_K_2_4 = _K_2_0 == _2_4_ 

174_K_2_0 = _K_2_4 or (_K_2_0 == _2_) 

175_perimeter_ = 'perimeter' 

176 

177 

178class _GTuple(_NamedTuple): # in .testNamedTuples 

179 '''(INTERNAL) Helper. 

180 ''' 

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

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

183 

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

185 ''' 

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

187 if updates: 

188 r.update(updates) 

189 if self._iteration is not None: 

190 r._iteration = self._iteration 

191 return r 

192 

193 

194class _Lat(Lat): 

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

196 ''' 

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

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

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

200 

201 

202class _Lon(Lon): 

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

204 ''' 

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

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

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

208 

209 

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

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

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

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

214 ''' 

215 _Names_ = (_number_, _perimeter_, _area_) 

216 _Units_ = ( Number_, _M, _M2) 

217 

218 

219class Caps(object): 

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

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

222 

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

224 

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

226 

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

228 

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

230 

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

232 

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

234 

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

236 

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

238 

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

240 

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

242 

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

244 

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

246 

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

248 

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

250 

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

252 ''' 

253 EMPTY = 0 # formerly aka NONE 

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

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

256 _CAP_2 = 1 << 2 

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

258 _CAP_4 = 1 << 4 # for goedesicw 

259# = 1 << 5 # unused 

260# = 1 << 6 # unused 

261 _CAP_ALL = 0x1F 

262# _CAP_MASK = _CAP_ALL 

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

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

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

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

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

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

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

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

271 

272 STANDARD = AZIMUTH | DISTANCE | LATITUDE | LONGITUDE 

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

274 

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

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

277# = 1 << 16 # unused 

278 LINE_OFF = 1 << 17 # Line without updates from parent geodesic or rhumb 

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

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

281 

282 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE 

283 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA 

284 

285 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE 

286 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA 

287 

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

289 

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

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

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

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

294 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \ 

295 _DEBUG_LINE | _SALP_CALPs_ 

296 

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

298 _INVERSE3 = AZIMUTH | DISTANCE # for goedesicw only 

299 

300 _OUT_ALL = ALL # see geographiclib.geodesiccapabilities.py 

301 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL 

302 

303 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE 

304 _AZIMUTH_LATITUDE_LONG_UNROLL = AZIMUTH | LATITUDE | LONG_UNROLL 

305 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE 

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

307 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE 

308# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE 

309 

310 def items(self): 

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

312 ''' 

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

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

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

316 yield n, C 

317 

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

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

320 ''' 

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

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

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

324 

325if _FOR_DOCS: # PYCHOK force ... 

326 pass # ... CAPS.__doc__ into epydoc 

327else: 

328 Caps = Caps() # PYCHOK singleton 

329 

330_key2Caps = dict(a12 =Caps.DISTANCE, # in GDict._unCaps 

331 azi2=Caps.AZIMUTH, 

332 lat2=Caps.LATITUDE, 

333 lon2=Caps.LONGITUDE, 

334 m12 =Caps.REDUCEDLENGTH, 

335 M12 =Caps.GEODESICSCALE, 

336 M21 =Caps.GEODESICSCALE, 

337 s12 =Caps.DISTANCE, 

338 S12 =Caps.AREA) 

339 

340 

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

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

343 ''' 

344 ALL = Caps.ALL 

345 AREA = Caps.AREA 

346 AZIMUTH = Caps.AZIMUTH 

347 DISTANCE = Caps.DISTANCE 

348 DISTANCE_IN = Caps.DISTANCE_IN 

349 EMPTY = Caps.EMPTY # aka NONE 

350 GEODESICSCALE = Caps.GEODESICSCALE 

351 LATITUDE = Caps.LATITUDE 

352 LINE_CAPS = Caps.LINE_CAPS 

353 LINE_OFF = Caps.LINE_OFF 

354 LONGITUDE = Caps.LONGITUDE 

355 LONG_UNROLL = Caps.LONG_UNROLL 

356 REDUCEDLENGTH = Caps.REDUCEDLENGTH 

357 STANDARD = Caps.STANDARD 

358 STANDARD_LINE = Caps.STANDARD_LINE # for geodesici 

359 

360 _caps = 0 # None 

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

362 

363 @Property_RO 

364 def caps(self): 

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

366 ''' 

367 return self._caps 

368 

369 def caps_(self, caps): 

370 '''Check the available capabilities. 

371 

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

373 values for all capabilities to be checked. 

374 

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

376 otherwise (C{bool}). 

377 ''' 

378 caps &= Caps._OUT_ALL 

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

380 

381 @property 

382 def debug(self): 

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

384 ''' 

385 return bool(self._debug) 

386 

387 @debug.setter # PYCHOK setter! 

388 def debug(self, debug): 

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

390 more details in L{GDict} results. 

391 ''' 

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

393 

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

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

396 ''' 

397 if iter is not None: 

398 self._iteration = r._iteration = iter 

399 return r 

400 

401 

402class Direct9Tuple(_GTuple): 

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

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

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

406 C{meter} I{squared}. 

407 ''' 

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

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

410 

411 

412class GDict(ADict): # XXX _NamedDict 

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

414 

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

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

417 ''' 

418 def toDirect9Tuple(self, dflt=NAN): 

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

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

421 

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

423 

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

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

426 ''' 

427 return self._toTuple(Direct9Tuple, dflt) 

428 

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

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

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

432 result. 

433 

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

435 

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

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

438 ''' 

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

440 

441 def toInverse10Tuple(self, dflt=NAN): 

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

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

444 

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

446 

447 @return: L{Inverse10Tuple}C{(a12, s12, salp1, calp1, 

448 salp2, calp2, m12, M12, M21, S12)}. 

449 ''' 

450 return self._toTuple(Inverse10Tuple, dflt) 

451 

452 def _toNAN(self, outmask): # .GeodesicLineExact._GenPosition 

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

454 ''' 

455 d = dict((n, NAN) for n in GeodSolve12Tuple._Names_) 

456 return self.set_(**d)._unCaps(outmask) 

457 

458 @deprecated_method 

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

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

461 

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

463 ''' 

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

465 

466 def toRhumb8Tuple(self, dflt=NAN): 

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

468 

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

470 

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

472 azi12, s12, S12, a12)}. 

473 ''' 

474 return self._toTuple(Rhumb8Tuple, dflt) 

475 

476 def toRhumbSolve7Tuple(self, dflt=NAN): 

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

478 

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

480 

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

482 azi12, s12, S12)}. 

483 ''' 

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

485 

486 def _toTuple(self, nTuple, dflt): 

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

488 ''' 

489 _g = getattr 

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

491 return nTuple(t, iteration=self._iteration) 

492 

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

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

495 ''' 

496 X = GDict(self) 

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

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

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

500 v = getattr(gl, n, X) 

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

502 X[n] = v 

503 return X 

504 

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

506 '''(INTERNAL) Remove superfluous items. 

507 ''' 

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

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

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

511 return self 

512 

513 

514class GeodSolve12Tuple(_GTuple): 

515 '''12-Tuple C{(lat1, lon1, azi1, lat2, lon2, azi2, s12, a12, m12, M12, M21, S12)} with 

516 angles C{lat1}, C{lon1}, C{azi1}, C{lat2}, C{lon2} and C{azi2} and arc C{a12} all in 

517 C{degrees}, initial C{azi1} and final C{azi2} forward azimuths, distance C{s12} and 

518 reduced length C{m12} in C{meter}, area C{S12} in C{meter} I{squared} and geodesic 

519 scale factors C{M12} and C{M21}, both C{scalar}, see U{GeodSolve 

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

521 ''' 

522 # from GeodSolve --help option -f ... lat1 lon1 azi1 lat2 lon2 azi2 s12 a12 m12 M12 M21 S12 

523 _Names_ = (_lat1_, _lon1_, _azi1_, _lat2_, _lon2_, _azi2_, _s12_, _a12_, _m12_, _M12_, _M21_, _S12_) 

524 _Units_ = (_Lat, _Lon, _Azi, _Lat, _Lon, _Azi, _M, _Deg, _Pass, _Pass, _Pass, _M2) 

525 

526 

527class Inverse10Tuple(_GTuple): 

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

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

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

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

532 ''' 

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

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

535 

536 def toGDict(self, **updates): 

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

538 

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

540 ''' 

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

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

543 **updates) # PYCHOK indent 

544 

545 

546class _kWrapped(object): # in .geodesicw 

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

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

549 ''' 

550 

551 @property_ROnce 

552 def geographiclib(self): 

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

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

555 otherwise raise a C{LazyImportError}. 

556 ''' 

557 g = _xgeographiclib(type(self).__module__, 1, 49) 

558 from geographiclib.geodesic import Geodesic 

559 g.Geodesic = Geodesic 

560 from geographiclib.geodesicline import GeodesicLine 

561 g.GeodesicLine = GeodesicLine 

562 from geographiclib.geomath import Math 

563 g.Math = Math 

564 return g 

565 

566 @property_ROnce 

567 def Math(self): 

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

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

570 otherwise C{None}. 

571 ''' 

572 try: 

573 g = self.geographiclib 

574 M = g.Math 

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

576 if _K_2_0: 

577 M = None 

578# elif not _K_2_0: # XXX set 2.0? 

579# _K_2_0 = True 

580 except (AttributeError, ImportError): 

581 M = None 

582 return M 

583 

584 @property_RO 

585 def Math_K_2(self): 

586 return (_2_4_ if _K_2_4 else 

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

588 

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

590 

591 

592class Rhumb8Tuple(_GTuple): 

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

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

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

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

597 ''' 

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

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

600 

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

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

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

604 

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

606 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments 

607 to specify or override L{Inverse10Tuple} items. 

608 

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

610 m12, M12, M21, S12)} 

611 ''' 

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

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

614 if a12_azi1_azi2_m12_M12_M21: 

615 d.update(a12_azi1_azi2_m12_M12_M21) 

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

617 

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

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

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

621 

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

623 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword 

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

625 

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

627 m12, M12, M21, S12)}. 

628 ''' 

629 s, c = sincos2d(self.azi12) # PYCHOK attr 

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

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

632 if a12_m12_M12_M21_salp1_calp1_salp2_calp2: 

633 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2) 

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

635 

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

637 '''(INTERNAL) Convert this C{Rhumb8Tuple} to an B{C{nTuple}}. 

638 ''' 

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

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

641 return nTuple(t, name=self.name) 

642 

643 @deprecated_method 

644 def _to7Tuple(self): 

645 '''DEPRECATED, do not use!''' 

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

647 

648 

649class _Xables(object): 

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

651 ''' 

652 bin_ = '/opt/local/bin/' # '/opt/local/Cellar/geographiclib/2.3/bin/' # HomeBrew on macOS 

653 ENV = NN 

654 

655 def GeoConvert(self, *dir_): 

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

657 

658 def GeodSolve(self, *dir_): 

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

660 

661 def IntersectTool(self, *dir_): 

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

663 

664 def RhumbSolve(self, *dir_): 

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

666 

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

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

669 if path: 

670 try: 

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

672 if base: 

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

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

675 else: 

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

677 return r 

678 except (IndexError, IOError, OSError): 

679 pass 

680 return NN 

681 

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

683 n = typename(which, which) 

684 self.ENV = E = _PYGEODESY_ENV(n) 

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

686 

687 def X_not(self, path): 

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

689 

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

691 os = _MODS.os # import os 

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

693 

694_Xables = _Xables() # PYCHOK singleton 

695 

696 

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

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

699 

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

701 

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

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

704 ''' 

705 try: 

706 return _wrapped.Math.AngRound(x) 

707 except AttributeError: 

708 if x: 

709 y = _1_16th - fabs(x) 

710 if y > 0: # fabs(x) < _1_16th 

711 x = _copysign(_1_16th - y, x) 

712 else: 

713 x = _0_0 # -0 to 0 

714 return x 

715 

716 

717def _atan2d(y, x): 

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

719 ''' 

720 try: 

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

722 except AttributeError: 

723 return atan2d(y, x) 

724 

725 

726def _cbrt(x): 

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

728 ''' 

729 try: 

730 return _wrapped.Math.cbrt(x) 

731 except AttributeError: 

732 return cbrt(x) 

733 

734 

735def _copyBit(x, y): 

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

737 ''' 

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

739 

740 

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

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

743 ''' 

744 r = cx - sx 

745 if r: 

746 r *= (cx + sx) * _2_0 

747 return r 

748 

749 

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

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

752 

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

754 ''' 

755 try: 

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

757 except AttributeError: 

758 if K_2_0 or _K_2_0: # geographiclib 2.0 

759 _r, _360 = fremainder, _360_0 

760 d, t = _sum2(_r(-deg0, _360), 

761 _r( deg, _360)) 

762 d, t = _sum2(_r( d, _360), t) 

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

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

765 else: 

766 _n = _norm180 

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

768 d = _n(d) 

769 if t > 0 and d == _180_0: 

770 d = _N_180_0 

771 d, t = _sum2(d, t) 

772 return d, t 

773 

774 

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

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

777 

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

779 ''' 

780 try: 

781 return _wrapped.Math.LatFix(deg) 

782 except AttributeError: 

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

784 

785 

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

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

788 

789 @return: C{True} if finite. 

790 ''' 

791 try: 

792 return _wrapped.Math.isfinite(x) 

793 except AttributeError: 

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

795 

796 

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

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

799 ''' 

800 if llz2: 

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

802 v = llz2.get(n, None) 

803 if v is not None: 

804 setattr(gl, n, v) 

805 return gl 

806 

807 

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

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

810 

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

812 ''' 

813 try: 

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

815 except AttributeError: 

816 return norm2(x, y) 

817 

818 

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

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

821 

822 @return: Reduced angle C{degrees}. 

823 ''' 

824 try: 

825 return _wrapped.Math.AngNormalize(deg) 

826 except AttributeError: 

827 d = fremainder(deg, _360_0) 

828 if _isin(d, _180_0, _N_180_0): 

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

830 return d 

831 

832 

833def _polygon(geodesic, points, closed, line, wrap): 

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

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

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

837 or C{geodesicw.Geodesic} instance. 

838 ''' 

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

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

841 

842 if _MODS.booleans.isBoolean(points): 

843 # recursive call for each boolean clip 

844 

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

846 return _polygon(geodesic, clip, *args) 

847 

848 if not closed: # closed only 

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

850 

851 return points._sum1(_a_p, closed, line, wrap) 

852 

853 gP = geodesic.Polygon(line) 

854 _A = gP.AddPoint 

855 

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

857 p1 = p0 = Ps[0] 

858 

859 # note, lon deltas are unrolled, by default 

860 _A(p1.lat, p1.lon) 

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

862 if wrap and not Ps.looped: 

863 p2 = _unrollon(p1, p2) 

864 _A(p2.lat, p2.lon) 

865 p1 = p2 

866 if closed and line and p1 != p0: 

867 _A(p0.lat, p0.lon) 

868 

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

870 return gP.Compute(False, True)[1 if line else 2] 

871 

872 

873try: 

874 from math import fma as _fma # since 3.13 

875 

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

877 for c in cs: 

878 s = _fma(s, x, c) 

879 return s 

880 

881except ImportError: # Python 3.12- 

882 

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

884 t = _0_0 

885 for c in cs: 

886 s, t, _ = _sum3(s * x, t * x, c) 

887 return s + t 

888 

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

890# S = Fhorner(x, *cs, incx=False) 

891# S += s 

892# return float(S) 

893 

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

895 '''(INTERNAL) Like C++ C{GeographicLib.Math.hpp.polyval} but with a 

896 signature and cascaded summation different from C{karney._sum3}. 

897 

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

899 ''' 

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

901 try: 

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

903 except AttributeError: # no .Math 

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

905 else: 

906 r = cs[i] 

907 return float(r) 

908 

909 

910def _remainder(x, y): 

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

912 

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

914 ''' 

915 try: 

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

917 except AttributeError: 

918 return fremainder(x, y) 

919 

920 

921if _K_2_0: 

922 from math import cos as _cos, sin as _sin 

923 

924 def _sincos2(rad): 

925 return _sin(rad), _cos(rad) 

926 

927 _signBit = _MODS.basics.signBit 

928else: 

929 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared 

930 

931 def _signBit(x): 

932 '''(INTERNAL) GeographicLib 1.52-. 

933 ''' 

934 return x < 0 

935 

936 

937def _sincos2d(deg): 

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

939 

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

941 ''' 

942 try: 

943 return _wrapped.Math.sincosd(deg) 

944 except AttributeError: 

945 return sincos2d(deg) 

946 

947 

948def _sincos2de(deg, t): 

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

950 

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

952 ''' 

953 try: 

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

955 except AttributeError: 

956 return sincos2d(deg, adeg=t) 

957 

958 

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

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

961 

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

963 

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

965 

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

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

968 ''' 

969 try: 

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

971 except AttributeError: 

972 # if Algorithm_3_1: 

973 s = a + b 

974 r = s - b 

975 t = s - r 

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

977 # r -= a; t -= b; t += r; t = -t 

978 # else: 

979 t = (a - r) + (b - t) 

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

981 return s, t 

982 

983 

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

985 '''Accumulate any B{C{xs}} into a previous C{_sum2(s, t)}. 

986 

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

988 B{xs}, C{t} the residual and C{n} the number of zero C{xs}. 

989 

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

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

992 function C{_sum2} above. 

993 

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

995 ''' 

996 z = 0 

997 for x in xs: 

998 if x: 

999 t, r = _sum2(t, x) # start at the least- 

1000 if s: 

1001 s, t = _sum2(s, t) # -significant end 

1002 if s: 

1003 t += r # accumulate r into t 

1004 else: 

1005 # assert t == 0 # s == 0 implies t == 0 

1006 s = unsigned0(r) # result is r, t = 0 

1007 else: 

1008 s, t = unsigned0(t), r 

1009 else: 

1010 z += 1 

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

1012 return s, t, z 

1013 

1014 

1015def _tand(x): 

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

1017 ''' 

1018 try: 

1019 return _wrapped.Math.tand(x) 

1020 except AttributeError: 

1021 return tand(x) # Error=None 

1022 

1023 

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

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

1026 

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

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

1029 ''' 

1030 if wrap: 

1031 d, t = _diff182(lon1, lon2) 

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

1033 lon2 += t 

1034 else: 

1035 lon2 = _norm180(lon2) 

1036 return (lon2 - lon1), lon2 

1037 

1038 

1039def _unsigned2(x): 

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

1041 ''' 

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

1043 

1044 

1045__all__ += _ALL_DOCS(Caps, _CapsBase) 

1046 

1047# **) MIT License 

1048# 

1049# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved. 

1050# 

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

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

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

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

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

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

1057# 

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

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

1060# 

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

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

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

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

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

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

1067# OTHER DEALINGS IN THE SOFTWARE.