Coverage for pygeodesy/karney.py: 92%

366 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-29 12:40 -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_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.05.28' 

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{NONFINITONAN} - set all C{GDict} items to C{NAN} iff any argument is C{non-finite}. 

244 

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

246 

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

248 

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

250 

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

252 

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

254 ''' 

255 EMPTY = 0 # formerly aka NONE 

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

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

258 _CAP_2 = 1 << 2 

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

260 _CAP_4 = 1 << 4 # for goedesicw 

261# = 1 << 5 # unused 

262# = 1 << 6 # unused 

263 _CAP_ALL = 0x1F 

264# _CAP_MASK = _CAP_ALL 

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

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

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

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

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

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

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

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

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 NONFINITONAN = 1 << 16 # see method GDict._toNAN 

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

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

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

283 

284 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE 

285 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA 

286 

287 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE 

288 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA 

289 

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

291 

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

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

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

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

296 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \ 

297 _DEBUG_LINE | _SALP_CALPs_ 

298 

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

300 _INVERSE3 = AZIMUTH | DISTANCE # for goedesicw only 

301 

302 _OUT_ALL = ALL # see geographiclib.geodesiccapabilities.py 

303 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL 

304 

305 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE 

306 _AZIMUTH_LATITUDE_LONG_UNROLL = AZIMUTH | LATITUDE | LONG_UNROLL 

307 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE 

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

309 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE 

310# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE 

311 

312 def items(self): 

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

314 ''' 

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

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

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

318 yield n, C 

319 

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

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

322 ''' 

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

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

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

326 

327if _FOR_DOCS: # PYCHOK force ... 

328 pass # ... CAPS.__doc__ into epydoc 

329else: 

330 Caps = Caps() # PYCHOK singleton 

331 

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

333 azi1=Caps.AZIMUTH, 

334 azi2=Caps.AZIMUTH, 

335 lat1=Caps.LATITUDE, 

336 lat2=Caps.LATITUDE, 

337 lon1=Caps.LONGITUDE, 

338 lon2=Caps.LONGITUDE, 

339 m12 =Caps.REDUCEDLENGTH, 

340 M12 =Caps.GEODESICSCALE, 

341 M21 =Caps.GEODESICSCALE, 

342 s12 =Caps.DISTANCE, 

343 S12 =Caps.AREA) 

344 

345 

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

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

348 ''' 

349 ALL = Caps.ALL 

350 AREA = Caps.AREA 

351 AZIMUTH = Caps.AZIMUTH 

352 DISTANCE = Caps.DISTANCE 

353 DISTANCE_IN = Caps.DISTANCE_IN 

354 EMPTY = Caps.EMPTY # aka NONE 

355 GEODESICSCALE = Caps.GEODESICSCALE 

356 LATITUDE = Caps.LATITUDE 

357 LINE_CAPS = Caps.LINE_CAPS 

358 LINE_OFF = Caps.LINE_OFF 

359 LONGITUDE = Caps.LONGITUDE 

360 LONG_UNROLL = Caps.LONG_UNROLL 

361 NONFINITONAN = Caps.NONFINITONAN 

362 REDUCEDLENGTH = Caps.REDUCEDLENGTH 

363 STANDARD = Caps.STANDARD 

364 STANDARD_LINE = Caps.STANDARD_LINE # for geodesici 

365 

366 _caps = 0 # None 

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

368 

369 @Property_RO 

370 def caps(self): 

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

372 ''' 

373 return self._caps 

374 

375 def caps_(self, caps): 

376 '''Check the available capabilities. 

377 

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

379 values for all capabilities to be checked. 

380 

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

382 otherwise (C{bool}). 

383 ''' 

384 caps &= Caps._OUT_ALL 

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

386 

387 @property 

388 def debug(self): 

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

390 ''' 

391 return bool(self._debug) 

392 

393 @debug.setter # PYCHOK setter! 

394 def debug(self, debug): 

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

396 more details in L{GDict} results. 

397 ''' 

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

399 

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

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

402 ''' 

403 if iter is not None: 

404 self._iteration = r._iteration = iter 

405 return r 

406 

407 

408class Direct9Tuple(_GTuple): 

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

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

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

412 C{meter} I{squared}. 

413 ''' 

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

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

416 

417 

418class GDict(ADict): # XXX _NamedDict 

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

420 

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

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

423 ''' 

424 def toDirect9Tuple(self, dflt=NAN): 

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

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

427 

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

429 

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

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

432 ''' 

433 return self._toTuple(Direct9Tuple, dflt) 

434 

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

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

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

438 result. 

439 

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

441 

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

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

444 ''' 

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

446 

447 def toInverse10Tuple(self, dflt=NAN): 

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

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

450 

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

452 

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

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

455 ''' 

456 return self._toTuple(Inverse10Tuple, dflt) 

457 

458 def _toNAN(self, outmask): # .GeodesicExact._GDistInverse, .GeodesicLineExact._GenPosition 

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

460 ''' 

461 if (outmask & Caps.NONFINITONAN): 

462 d = dict((k, NAN) for k, C in _key2Caps.items() 

463 if (outmask & C) == C) 

464 self.set_(**d) 

465 return self 

466 

467 @deprecated_method 

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

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

470 

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

472 ''' 

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

474 

475 def toRhumb8Tuple(self, dflt=NAN): 

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

477 

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

479 

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

481 azi12, s12, S12, a12)}. 

482 ''' 

483 return self._toTuple(Rhumb8Tuple, dflt) 

484 

485 def toRhumbSolve7Tuple(self, dflt=NAN): 

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

487 

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

489 

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

491 azi12, s12, S12)}. 

492 ''' 

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

494 

495 def _toTuple(self, nTuple, dflt): 

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

497 ''' 

498 _g = getattr 

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

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

501 

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

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

504 ''' 

505 X = GDict(self) 

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

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

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

509 v = getattr(gl, n, X) 

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

511 X[n] = v 

512 return X 

513 

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

515 '''(INTERNAL) Remove superfluous items. 

516 ''' 

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

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

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

520 return self 

521 

522 

523class GeodSolve12Tuple(_GTuple): 

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

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

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

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

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

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

530 ''' 

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

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

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

534 

535 

536class Inverse10Tuple(_GTuple): 

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

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

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

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

541 ''' 

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

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

544 

545 def toGDict(self, **updates): 

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

547 

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

549 ''' 

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

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

552 **updates) # PYCHOK indent 

553 

554 

555class _kWrapped(_CapsBase): # in .geodesicw 

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

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

558 ''' 

559 

560 @property_ROnce 

561 def geographiclib(self): 

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

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

564 otherwise raise a C{LazyImportError}. 

565 ''' 

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

567 from geographiclib.geodesic import Geodesic 

568 g.Geodesic = Geodesic 

569 from geographiclib.geodesicline import GeodesicLine 

570 g.GeodesicLine = GeodesicLine 

571 from geographiclib.geomath import Math 

572 g.Math = Math 

573# from geographiclib.polygonarea import PolygonArea 

574# g.PolygonArea = PolygonArea # see below 

575 return g 

576 

577 @property_ROnce 

578 def Math(self): 

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

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

581 otherwise C{None}. 

582 ''' 

583 try: 

584 g = self.geographiclib 

585 M = g.Math 

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

587 if _K_2_0: 

588 M = None 

589# elif not _K_2_0: # XXX set 2.0? 

590# _K_2_0 = True 

591 except (AttributeError, ImportError): 

592 M = None 

593 return M 

594 

595 @property_RO 

596 def Math_K_2(self): 

597 return (_2_4_ if _K_2_4 else 

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

599 

600 @property_ROnce 

601 def _PolygonArea(self): # lazy import 

602 from geographiclib.polygonarea import PolygonArea 

603 return PolygonArea 

604 

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

606 

607 

608class Rhumb8Tuple(_GTuple): 

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

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

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

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

613 ''' 

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

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

616 

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

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

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

620 

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

622 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments 

623 to specify or override L{Inverse10Tuple} items. 

624 

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

626 m12, M12, M21, S12)} 

627 ''' 

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

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

630 if a12_azi1_azi2_m12_M12_M21: 

631 d.update(a12_azi1_azi2_m12_M12_M21) 

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

633 

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

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

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

637 

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

639 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword 

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

641 

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

643 m12, M12, M21, S12)}. 

644 ''' 

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

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

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

648 if a12_m12_M12_M21_salp1_calp1_salp2_calp2: 

649 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2) 

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

651 

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

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

654 ''' 

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

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

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

658 

659 @deprecated_method 

660 def _to7Tuple(self): 

661 '''DEPRECATED, do not use!''' 

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

663 

664 

665class _Xables(object): 

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

667 ''' 

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

669 ENV = NN 

670 

671 def GeoConvert(self, *dir_): 

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

673 

674 def GeodSolve(self, *dir_): 

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

676 

677 def IntersectTool(self, *dir_): 

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

679 

680 def RhumbSolve(self, *dir_): 

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

682 

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

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

685 if path: 

686 try: 

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

688 if base: 

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

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

691 else: 

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

693 return r 

694 except (IndexError, IOError, OSError): 

695 pass 

696 return NN 

697 

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

699 n = typename(which, which) 

700 self.ENV = E = _PYGEODESY_ENV(n) 

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

702 

703 def X_not(self, path): 

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

705 

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

707 os = _MODS.os # import os 

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

709 

710_Xables = _Xables() # PYCHOK singleton 

711 

712 

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

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

715 

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

717 

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

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

720 ''' 

721 try: 

722 return _wrapped.Math.AngRound(x) 

723 except AttributeError: 

724 if x: 

725 y = _1_16th - fabs(x) 

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

727 x = _copysign(_1_16th - y, x) 

728 else: 

729 x = _0_0 # -0 to 0 

730 return x 

731 

732 

733def _atan2d(y, x): 

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

735 ''' 

736 try: 

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

738 except AttributeError: 

739 return atan2d(y, x) 

740 

741 

742def _cbrt(x): 

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

744 ''' 

745 try: 

746 return _wrapped.Math.cbrt(x) 

747 except AttributeError: 

748 return cbrt(x) 

749 

750 

751def _copyBit(x, y): 

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

753 ''' 

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

755 

756 

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

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

759 ''' 

760 r = cx - sx 

761 if r: 

762 r *= (cx + sx) * _2_0 

763 return r 

764 

765 

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

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

768 

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

770 ''' 

771 try: 

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

773 except AttributeError: 

774 if K_2_0 or _K_2_0: # geographiclib 2.0 

775 _r, _360 = fremainder, _360_0 

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

777 _r( deg, _360)) 

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

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

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

781 else: 

782 _n = _norm180 

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

784 d = _n(d) 

785 if t > 0 and d == _180_0: 

786 d = _N_180_0 

787 d, t = _sum2(d, t) 

788 return d, t 

789 

790 

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

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

793 

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

795 ''' 

796 try: 

797 return _wrapped.Math.LatFix(deg) 

798 except AttributeError: 

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

800 

801 

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

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

804 

805 @return: C{True} if finite. 

806 ''' 

807 try: 

808 return _wrapped.Math.isfinite(x) 

809 except AttributeError: 

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

811 

812 

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

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

815 ''' 

816 if llz2: 

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

818 v = llz2.get(n, None) 

819 if v is not None: 

820 setattr(gl, n, v) 

821 return gl 

822 

823 

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

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

826 

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

828 ''' 

829 try: 

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

831 except AttributeError: 

832 return norm2(x, y) 

833 

834 

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

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

837 

838 @return: Reduced angle C{degrees}. 

839 ''' 

840 try: 

841 return _wrapped.Math.AngNormalize(deg) 

842 except AttributeError: 

843 d = fremainder(deg, _360_0) 

844 if _isin(d, _180_0, _N_180_0): 

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

846 return d 

847 

848 

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

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

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

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

853 or C{geodesicw.Geodesic} instance. 

854 ''' 

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

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

857 

858 if _MODS.booleans.isBoolean(points): 

859 # recursive call for each boolean clip 

860 

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

862 return _polygon(geodesic, clip, *args) 

863 

864 if not closed: # closed only 

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

866 

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

868 

869 gP = geodesic.Polygon(line) 

870 _A = gP.AddPoint 

871 

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

873 p1 = p0 = Ps[0] 

874 

875 # note, lon deltas are unrolled, by default 

876 _A(p1.lat, p1.lon) 

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

878 if wrap and not Ps.looped: 

879 p2 = _unrollon(p1, p2) 

880 _A(p2.lat, p2.lon) 

881 p1 = p2 

882 if closed and line and p1 != p0: 

883 _A(p0.lat, p0.lon) 

884 

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

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

887 

888 

889try: 

890 from math import fma as _fma # since 3.13 

891 

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

893 for c in cs: 

894 s = _fma(s, x, c) 

895 return s 

896 

897except ImportError: # Python 3.12- 

898 

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

900 t = _0_0 

901 for c in cs: 

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

903 return s + t 

904 

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

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

907# S += s 

908# return float(S) 

909 

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

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

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

913 

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

915 ''' 

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

917 try: 

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

919 except AttributeError: # no .Math 

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

921 else: 

922 r = cs[i] 

923 return float(r) 

924 

925 

926def _remainder(x, y): 

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

928 

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

930 ''' 

931 try: 

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

933 except AttributeError: 

934 return fremainder(x, y) 

935 

936 

937if _K_2_0: 

938 from math import cos as _cos, sin as _sin 

939 

940 def _sincos2(rad): 

941 return _sin(rad), _cos(rad) 

942 

943 _signBit = _MODS.basics.signBit 

944else: 

945 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared 

946 

947 def _signBit(x): 

948 '''(INTERNAL) GeographicLib 1.52-. 

949 ''' 

950 return x < 0 

951 

952 

953def _sincos2d(deg): 

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

955 

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

957 ''' 

958 try: 

959 return _wrapped.Math.sincosd(deg) 

960 except AttributeError: 

961 return sincos2d(deg) 

962 

963 

964def _sincos2de(deg, t): 

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

966 

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

968 ''' 

969 try: 

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

971 except AttributeError: 

972 return sincos2d(deg, adeg=t) 

973 

974 

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

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

977 

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

979 

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

981 

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

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

984 ''' 

985 try: 

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

987 except AttributeError: 

988 # if Algorithm_3_1: 

989 s = a + b 

990 r = s - b 

991 t = s - r 

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

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

994 # else: 

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

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

997 return s, t 

998 

999 

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

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

1002 

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

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

1005 

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

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

1008 function C{_sum2} above. 

1009 

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

1011 ''' 

1012 z = 0 

1013 for x in xs: 

1014 if x: 

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

1016 if s: 

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

1018 if s: 

1019 t += r # accumulate r into t 

1020 else: 

1021 # assert t == 0 # s == 0 implies t == 0 

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

1023 else: 

1024 s, t = unsigned0(t), r 

1025 else: 

1026 z += 1 

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

1028 return s, t, z 

1029 

1030 

1031def _tand(x): 

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

1033 ''' 

1034 try: 

1035 return _wrapped.Math.tand(x) 

1036 except AttributeError: 

1037 return tand(x) # Error=None 

1038 

1039 

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

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

1042 

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

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

1045 ''' 

1046 if wrap: 

1047 d, t = _diff182(lon1, lon2) 

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

1049 lon2 += t 

1050 else: 

1051 lon2 = _norm180(lon2) 

1052 return (lon2 - lon1), lon2 

1053 

1054 

1055def _unsigned2(x): 

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

1057 ''' 

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

1059 

1060 

1061__all__ += _ALL_DOCS(Caps, _CapsBase) 

1062 

1063# **) MIT License 

1064# 

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

1066# 

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

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

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

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

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

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

1073# 

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

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

1076# 

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

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

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

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

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

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

1083# OTHER DEALINGS IN THE SOFTWARE.