Coverage for pygeodesy/karney.py: 92%

368 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-09-09 13:03 -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 _ # noqa: E702 ; 

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_ROnce 

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

162 Meter as _M, Meter2 as _M2, Number_ 

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

164 

165# from math import fabs # from .utily 

166 

167__all__ = _ALL_LAZY.karney 

168__version__ = '25.08.31' 

169 

170_2_4_ = '2.4' 

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

172_K_2_4 = _K_2_0 == _2_4_ 

173_K_2_0 = _K_2_4 or (_K_2_0 == _2_) 

174_perimeter_ = 'perimeter' 

175 

176 

177class _GTuple(_NamedTuple): # in .testNamedTuples 

178 '''(INTERNAL) Helper. 

179 ''' 

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

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

182 

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

184 ''' 

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

186 if updates: 

187 r.update(updates) 

188 if self._iteration is not None: 

189 r._iteration = self._iteration 

190 return r 

191 

192 

193class _Lat(Lat): 

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

195 ''' 

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

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

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

199 

200 

201class _Lon(Lon): 

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

203 ''' 

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

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

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

207 

208 

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

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

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

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

213 ''' 

214 _Names_ = (_number_, _perimeter_, _area_) 

215 _Units_ = ( Number_, _M, _M2) 

216 

217 

218class Caps(object): 

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

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

221 

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

223 

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

225 

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

227 

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

229 

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

231 

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

233 

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

235 

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

237 

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

239 

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

241 

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

243 

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

245 

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

247 

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

249 

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

251 

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

253 ''' 

254 EMPTY = 0 # formerly aka NONE 

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

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

257 _CAP_2 = 1 << 2 

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

259 _CAP_4 = 1 << 4 # for goedesicw 

260# = 1 << 5 # unused 

261# = 1 << 6 # unused 

262 _CAP_ALL = 0x1F 

263# _CAP_MASK = _CAP_ALL 

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

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

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

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

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

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

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

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

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

273 

274 STANDARD = AZIMUTH | DISTANCE | LATITUDE | LONGITUDE 

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

276 

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

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

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

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

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

282 

283 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE 

284 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA 

285 

286 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE 

287 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA 

288 

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

290 

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

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

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

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

295 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \ 

296 _DEBUG_LINE | _SALP_CALPs_ 

297 

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

299 _INVERSE3 = AZIMUTH | DISTANCE # for goedesicw only 

300 

301 _OUT_ALL = ALL # see geographiclib.geodesiccapabilities.py 

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

303 

304 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE 

305 _AZIMUTH_LATITUDE_LONG_UNROLL = AZIMUTH | LATITUDE | LONG_UNROLL 

306 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE 

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

308 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE 

309# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE 

310 

311 def items(self): 

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

313 ''' 

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

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

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

317 yield n, C 

318 

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

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

321 ''' 

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

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

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

325 

326if _FOR_DOCS: # PYCHOK force ... 

327 pass # ... CAPS.__doc__ into epydoc 

328else: 

329 Caps = Caps() # PYCHOK singleton 

330 

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

332 azi1=Caps.AZIMUTH, 

333 azi2=Caps.AZIMUTH, 

334 lat1=Caps.LATITUDE, 

335 lat2=Caps.LATITUDE, 

336 lon1=Caps.LONGITUDE, 

337 lon2=Caps.LONGITUDE, 

338 m12 =Caps.REDUCEDLENGTH, 

339 M12 =Caps.GEODESICSCALE, 

340 M21 =Caps.GEODESICSCALE, 

341 s12 =Caps.DISTANCE, 

342 S12 =Caps.AREA) 

343 

344 

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

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

347 ''' 

348 ALL = Caps.ALL 

349 AREA = Caps.AREA 

350 AZIMUTH = Caps.AZIMUTH 

351 DISTANCE = Caps.DISTANCE 

352 DISTANCE_IN = Caps.DISTANCE_IN 

353 EMPTY = Caps.EMPTY # aka NONE 

354 GEODESICSCALE = Caps.GEODESICSCALE 

355 LATITUDE = Caps.LATITUDE 

356 LINE_CAPS = Caps.LINE_CAPS 

357 LINE_OFF = Caps.LINE_OFF 

358 LONGITUDE = Caps.LONGITUDE 

359 LONG_UNROLL = Caps.LONG_UNROLL 

360 NONFINITONAN = Caps.NONFINITONAN 

361 REDUCEDLENGTH = Caps.REDUCEDLENGTH 

362 STANDARD = Caps.STANDARD 

363 STANDARD_LINE = Caps.STANDARD_LINE # for geodesici 

364 

365 _caps = 0 # None 

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

367 

368 @property_RO 

369 def caps(self): 

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

371 ''' 

372 return self._caps 

373 

374 def caps_(self, caps): 

375 '''Check the available capabilities. 

376 

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

378 values for all capabilities to be checked. 

379 

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

381 otherwise (C{bool}). 

382 ''' 

383 caps &= Caps._OUT_ALL 

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

385 

386 @property 

387 def debug(self): 

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

389 ''' 

390 return bool(self._debug) 

391 

392 @debug.setter # PYCHOK setter! 

393 def debug(self, debug): 

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

395 more details in L{GDict} results. 

396 ''' 

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

398 

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

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

401 ''' 

402 if iter is not None: 

403 self._iteration = r._iteration = iter 

404 return r 

405 

406 

407class Direct9Tuple(_GTuple): 

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

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

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

411 C{meter} I{squared}. 

412 ''' 

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

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

415 

416 

417class GDict(ADict): # XXX _NamedDict 

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

419 

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

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

422 ''' 

423 def toDirect9Tuple(self, dflt=NAN): 

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

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

426 

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

428 

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

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

431 ''' 

432 return self._toTuple(Direct9Tuple, dflt) 

433 

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

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

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

437 result. 

438 

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

440 

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

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

443 ''' 

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

445 

446 def toInverse10Tuple(self, dflt=NAN): 

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

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

449 

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

451 

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

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

454 ''' 

455 return self._toTuple(Inverse10Tuple, dflt) 

456 

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

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

459 ''' 

460 if (outmask & Caps.NONFINITONAN): 

461 def _t2(k): 

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

463 

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

465 if (outmask & C) == C) 

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

467 return self 

468 

469 @deprecated_method 

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

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

472 

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

474 ''' 

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

476 

477 def toRhumb8Tuple(self, dflt=NAN): 

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

479 

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

481 

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

483 azi12, s12, S12, a12)}. 

484 ''' 

485 return self._toTuple(Rhumb8Tuple, dflt) 

486 

487 def toRhumbSolve7Tuple(self, dflt=NAN): 

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

489 

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

491 

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

493 azi12, s12, S12)}. 

494 ''' 

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

496 

497 def _toTuple(self, nTuple, dflt): 

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

499 ''' 

500 _g = getattr 

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

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

503 

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

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

506 ''' 

507 X = GDict(self) 

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

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

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

511 v = getattr(gl, n, X) 

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

513 X[n] = v 

514 return X 

515 

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

517 '''(INTERNAL) Remove superfluous items. 

518 ''' 

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

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

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

522 return self 

523 

524 

525class GeodSolve12Tuple(_GTuple): 

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

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

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

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

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

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

532 ''' 

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

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

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

536 

537 

538class Inverse10Tuple(_GTuple): 

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

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

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

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

543 ''' 

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

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

546 

547 def toGDict(self, **updates): 

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

549 

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

551 ''' 

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

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

554 **updates) # PYCHOK indent 

555 

556 

557class _kWrapped(_CapsBase): # in .geodesicw 

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

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

560 ''' 

561 

562 @property_ROnce 

563 def geographiclib(self): 

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

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

566 otherwise raise a C{LazyImportError}. 

567 ''' 

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

569 from geographiclib.geodesic import Geodesic 

570 g.Geodesic = Geodesic 

571 from geographiclib.geodesicline import GeodesicLine 

572 g.GeodesicLine = GeodesicLine 

573 from geographiclib.geomath import Math 

574 g.Math = Math 

575# from geographiclib.polygonarea import PolygonArea 

576# g.PolygonArea = PolygonArea # see below 

577 return g 

578 

579 @property_ROnce 

580 def Math(self): 

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

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

583 otherwise C{None}. 

584 ''' 

585 try: 

586 g = self.geographiclib 

587 M = g.Math 

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

589 if _K_2_0: 

590 M = None 

591# elif not _K_2_0: # XXX set 2.0? 

592# _K_2_0 = True 

593 except (AttributeError, ImportError): 

594 M = None 

595 return M 

596 

597 @property_RO 

598 def Math_K_2(self): 

599 return (_2_4_ if _K_2_4 else 

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

601 

602 @property_ROnce 

603 def _PolygonArea(self): # lazy import 

604 from geographiclib.polygonarea import PolygonArea 

605 return PolygonArea 

606 

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

608 

609 

610class Rhumb8Tuple(_GTuple): 

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

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

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

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

615 ''' 

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

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

618 

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

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

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

622 

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

624 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments 

625 to specify or override L{Inverse10Tuple} items. 

626 

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

628 m12, M12, M21, S12)} 

629 ''' 

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

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

632 if a12_azi1_azi2_m12_M12_M21: 

633 d.update(a12_azi1_azi2_m12_M12_M21) 

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

635 

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

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

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

639 

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

641 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword 

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

643 

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

645 m12, M12, M21, S12)}. 

646 ''' 

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

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

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

650 if a12_m12_M12_M21_salp1_calp1_salp2_calp2: 

651 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2) 

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

653 

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

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

656 ''' 

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

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

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

660 

661 @deprecated_method 

662 def _to7Tuple(self): 

663 '''DEPRECATED, do not use!''' 

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

665 

666 

667class _Xables(object): 

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

669 ''' 

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

671 ENV = NN 

672 

673 def GeoConvert(self, *dir_): 

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

675 

676 def GeodSolve(self, *dir_): 

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

678 

679 def IntersectTool(self, *dir_): 

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

681 

682 def RhumbSolve(self, *dir_): 

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

684 

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

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

687 if path: 

688 try: 

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

690 if base: 

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

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

693 else: 

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

695 return r 

696 except (IndexError, IOError, OSError): 

697 pass 

698 return NN 

699 

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

701 n = typename(which, which) 

702 self.ENV = E = _PYGEODESY_ENV(n) 

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

704 

705 def X_not(self, path): 

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

707 

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

709 os = _MODS.os # import os 

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

711 

712_Xables = _Xables() # PYCHOK singleton 

713 

714 

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

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

717 

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

719 

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

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

722 ''' 

723 try: 

724 return _wrapped.Math.AngRound(x) 

725 except AttributeError: 

726 b, a = _1_16th, fabs(x) 

727 if a < b: 

728 a -= b 

729 a += b 

730 x = _copysign(a, x) 

731 return x 

732 

733 

734def _atan2d(y, x): 

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

736 ''' 

737 try: 

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

739 except AttributeError: 

740 return atan2d(y, x) 

741 

742 

743def _cbrt(x): 

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

745 ''' 

746 try: 

747 return _wrapped.Math.cbrt(x) 

748 except AttributeError: 

749 return cbrt(x) 

750 

751 

752def _copyBit(x, y): 

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

754 ''' 

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

756 

757 

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

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

760 ''' 

761 r = cx - sx 

762 if r: 

763 r *= (cx + sx) * _2_0 

764 return r 

765 

766 

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

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

769 

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

771 ''' 

772 try: 

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

774 except AttributeError: 

775 if K_2_0 or _K_2_0: # geographiclib 2.0 

776 _r, _360 = fremainder, _360_0 

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

778 _r( deg, _360)) 

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

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

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

782 else: 

783 _n = _norm180 

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

785 d = _n(d) 

786 if t > 0 and d == _180_0: 

787 d = _N_180_0 

788 d, t = _sum2(d, t) 

789 return d, t 

790 

791 

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

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

794 

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

796 ''' 

797 try: 

798 return _wrapped.Math.LatFix(deg) 

799 except AttributeError: 

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

801 

802 

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

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

805 

806 @return: C{True} if finite. 

807 ''' 

808 try: 

809 return _wrapped.Math.isfinite(x) 

810 except AttributeError: 

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

812 

813 

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

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

816 ''' 

817 if llz2: 

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

819 v = llz2.get(n, None) 

820 if v is not None: 

821 setattr(gl, n, v) 

822 return gl 

823 

824 

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

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

827 

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

829 ''' 

830 try: 

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

832 except AttributeError: 

833 return norm2(x, y) 

834 

835 

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

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

838 

839 @return: Reduced angle C{degrees}. 

840 ''' 

841 try: 

842 return _wrapped.Math.AngNormalize(deg) 

843 except AttributeError: 

844 d = fremainder(deg, _360_0) 

845 if _isin(d, _180_0, _N_180_0): 

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

847 return d 

848 

849 

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

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

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

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

854 or C{geodesicw.Geodesic} instance. 

855 ''' 

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

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

858 

859 if _MODS.booleans.isBoolean(points): 

860 # recursive call for each boolean clip 

861 

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

863 return _polygon(geodesic, clip, *args) 

864 

865 if not closed: # closed only 

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

867 

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

869 

870 gP = geodesic.Polygon(line) 

871 _A = gP.AddPoint 

872 

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

874 p1 = p0 = Ps[0] 

875 

876 # note, lon deltas are unrolled, by default 

877 _A(p1.lat, p1.lon) 

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

879 if wrap and not Ps.looped: 

880 p2 = _unrollon(p1, p2) 

881 _A(p2.lat, p2.lon) 

882 p1 = p2 

883 if closed and line and p1 != p0: 

884 _A(p0.lat, p0.lon) 

885 

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

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

888 

889 

890try: 

891 from math import fma as _fma # since 3.13 

892 

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

894 for c in cs: 

895 s = _fma(s, x, c) 

896 return s 

897 

898except ImportError: # Python 3.12- 

899 

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

901 t = _0_0 

902 for c in cs: 

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

904 return s + t 

905 

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

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

908# S += s 

909# return float(S) 

910 

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

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

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

914 

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

916 ''' 

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

918 try: 

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

920 except AttributeError: # no .Math 

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

922 else: 

923 r = cs[i] 

924 return float(r) 

925 

926 

927def _remainder(x, y): 

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

929 

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

931 ''' 

932 try: 

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

934 except AttributeError: 

935 return fremainder(x, y) 

936 

937 

938if _K_2_0: 

939 from math import cos as _cos, sin as _sin 

940 

941 def _sincos2(rad): 

942 return _sin(rad), _cos(rad) 

943 

944 _signBit = _MODS.basics.signBit 

945else: 

946 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared 

947 

948 def _signBit(x): 

949 '''(INTERNAL) GeographicLib 1.52-. 

950 ''' 

951 return x < 0 

952 

953 

954def _sincos2d(deg): 

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

956 

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

958 ''' 

959 try: 

960 return _wrapped.Math.sincosd(deg) 

961 except AttributeError: 

962 return sincos2d(deg) 

963 

964 

965def _sincos2de(deg, t): 

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

967 

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

969 ''' 

970 try: 

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

972 except AttributeError: 

973 return sincos2d(deg, adeg=t) 

974 

975 

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

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

978 

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

980 

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

982 

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

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

985 ''' 

986 try: 

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

988 except AttributeError: 

989 # if Algorithm_3_1: 

990 s = a + b 

991 r = s - b 

992 t = s - r 

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

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

995 # else: 

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

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

998 return s, t 

999 

1000 

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

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

1003 

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

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

1006 

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

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

1009 function C{_sum2} above. 

1010 

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

1012 ''' 

1013 z = 0 

1014 for x in xs: 

1015 if x: 

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

1017 if s: 

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

1019 if s: 

1020 t += r # accumulate r into t 

1021 else: 

1022 # assert t == 0 # s == 0 implies t == 0 

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

1024 else: 

1025 s, t = unsigned0(t), r 

1026 else: 

1027 z += 1 

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

1029 return s, t, z 

1030 

1031 

1032def _tand(x): 

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

1034 ''' 

1035 try: 

1036 return _wrapped.Math.tand(x) 

1037 except AttributeError: 

1038 return tand(x) # Error=None 

1039 

1040 

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

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

1043 

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

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

1046 ''' 

1047 if wrap: 

1048 d, t = _diff182(lon1, lon2) 

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

1050 lon2 += t 

1051 else: 

1052 lon2 = _norm180(lon2) 

1053 return (lon2 - lon1), lon2 

1054 

1055 

1056def _unsigned2(x): 

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

1058 ''' 

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

1060 

1061 

1062__all__ += _ALL_DOCS(Caps, _CapsBase) 

1063 

1064# **) MIT License 

1065# 

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

1067# 

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

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

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

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

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

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

1074# 

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

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

1077# 

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

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

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

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

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

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

1084# OTHER DEALINGS IN THE SOFTWARE.