1

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

3

4 u'''Wrapper around several C{geomath.Math} functions from I{Karney}'s Python package

5 U{geographiclib<https://PyPI.org/project/geographiclib>}, provided that package is installed.

6

7 The I{wrapped} class methods return a L{GDict} instance offering access to the C{dict} items

8 either by C{key} or by C{attribute} name.

9

10 With env variable C{PYGEODESY_GEOGRAPHICLIB} left undefined or set to C{"2"}, this module,

11 L{pygeodesy.geodesicw} and L{pygeodesy.geodesicx} will use U{GeographicLib 2.0

12<https://GeographicLib.SourceForge.io/C++/doc/>} and newer transcoding, otherwise C{1.52}

13 or older.

14

15 Karney-based functionality

16==========================

17

18 1. The following classes and functions in C{pygeodesy}

19

20 - L{AlbersEqualArea}, L{AlbersEqualArea2}, L{AlbersEqualArea4},

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

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

23

24 - L{AuxAngle}, L{AuxDST}, L{AuxDLat}, L{AuxLat} -- U{AuxAngle

25 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AuxAngle.html>},

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

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

28 U{AuxLatitude<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AuxLatitude.html>} in I{GeographicLib 2.2+}

29

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

31 classGeographicLib_1_1CassiniSoldner.html>}

32

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

34

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

36 classGeographicLib_1_1EllipticFunction.html>}

37

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

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

40

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

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

43

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

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

46

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

48 classGeographicLib_1_1PolygonAreaT.html>}

49

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

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

52 classGeographicLib_1_1GeodesicLineExact.html>}

53

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

55

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

57

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

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

60

61 - L{JacobiConformal} -- U{JacobiConformal

62 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1JacobiConformal.html#details>}

63

64 - L{KTransverseMercator} - U{TransverseMercator

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

66

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

68 classGeographicLib_1_1LocalCartesian.html>}

69

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

71

72 - L{rhumbaux}, L{RhumbAux}, L{RhumbLineAux} -- U{Rhumb

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

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

75

76 - L{rhumbx}, L{Rhumb}, L{RhumbLine} -- U{Rhumb

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

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

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

80 from I{GeographicLib 2.0}

81

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

83

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

85

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

87

88 - L{pygeodesy.atand}, L{pygeodesy.atan2d}, L{pygeodesy.sincos2}, L{pygeodesy.sincos2d}, L{pygeodesy.tand} -- U{geomath.Math

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

90

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

92

93 2. These C{pygeodesy} modules and classes

94

95 - L{ellipsoidalGeodSolve}, L{ellipsoidalKarney}, L{geodsolve}, L{karney}, L{rhumbsolve}

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

97 L{GnomonicKarney}, L{HeightIDWkarney}

98

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

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

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

102

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

104

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

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

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

108

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

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

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

112

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

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

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

116

117 - L{RhumbLineAux.intersection2} and L{RhumbLineAux.nearestOn4} C{(exact=None)} in L{rhumbaux} and

118 L{RhumbLine.intersection2} and L{RhumbLine.nearestOn4} C{(exact=None)} in L{rhumbx}

119

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

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

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

123

124 4. Spherical functions

125

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

127

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

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

130'''

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

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

133

134 from pygeodesy.basics import _copysign, neg, unsigned0, _xgeographiclib, \

135 _xImportError, _xversion_info, _xinstanceof, \

136 _zip, isodd # PYCHOK shared

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

138 _1_0, _2_0, _180_0, _N_180_0, _360_0

139 from pygeodesy.datums import _a_ellipsoid, _EWGS84, _WGS84 # PYCHOK shared

140# from pygeodesy.ellipsoids import _EWGS84 # from .datums

141 from pygeodesy.errors import GeodesicError, _ValueError, _xkwds, _xkwds_get

142 from pygeodesy.fmath import cbrt, fremainder, hypot as _hypot, norm2, \

143 Fsum, unstr # PYCHOK shared

144# from pygeodesy.fsums import Fsum # from .fmath

145 from pygeodesy.interns import _2_, _a12_, _area_, _azi2_, _azi12_, _composite_, \

146 _lat1_, _lat2_, _lon1_, _lon2_, _m12_, _M12_, _M21_, \

147 _number_, _s12_, _S12_

148 from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _getenv

149 from pygeodesy.named import _Dict, _NamedBase, _NamedTuple, notImplemented, _Pass

150 from pygeodesy.props import deprecated_method, Property_RO, property_RO

151# from pygeodesy.streps import unstr # from .fmath

152 from pygeodesy.units import Bearing as _Azi, Degrees as _Deg, Lat, Lon, \

153 Meter as _M, Meter2 as _M2, Number_, \

154 Precision_, _1mm as _TOL_M # PYCHOK shared

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

156

157# from math import fabs # from .utily

158

159__all__ = _ALL_LAZY.karney

160__version__ = '23.09.15'

161

162_K_2_0 = _getenv('PYGEODESY_GEOGRAPHICLIB', _2_) == _2_

163_perimeter_ = 'perimeter'

164

165

166 class _GTuple(_NamedTuple): # in .testNamedTuples

167 '''(INTERNAL) Helper.

168 '''

169 def toGDict(self, **updates):

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

171

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

173 '''

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

175 if updates:

176 r.update(updates)

177 if self._iteration is not None:

178 r._iteration = self._iteration

179 return r

180

181

182 class _Lat(Lat):

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

184 '''

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

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

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

188

189

190 class _Lon(Lon):

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

192 '''

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

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

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

196

197

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

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

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

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

202 '''

203 _Names_ = (_number_, _perimeter_, _area_)

204 _Units_ = ( Number_, _M, _M2)

205

206

207 class Caps(object): # PYCHOK

208 '''(INTERNAL) Overriden by C{Caps} below.

209 '''

210 EMPTY = 0 # formerly aka NONE

211 _CAP_1 = 1 << 0 # for goedesicw only

212 _CAP_1p = 1 << 1 # for goedesicw only

213# _CAP_2 = 1 << 2

214 _CAP_3 = 1 << 3 # for goedesicw only

215# _CAP_4 = 1 << 4

216# _CAP_ALL = 0x1F

217# _CAP_MASK = _CAP_ALL

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

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

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

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

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

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

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

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

226

227 STANDARD = AZIMUTH | DISTANCE | DISTANCE_IN | LATITUDE | LONGITUDE

228 ALL = 0x7F80 # without LONG_UNROLL, LINE_OFF, REVERSE2 and _DEBUG_*

229

230 _DIRECT3 = AZIMUTH | LATITUDE | LONGITUDE | _CAP_3 # for goedesicw only

231 _INVERSE3 = AZIMUTH | DISTANCE | _CAP_1 # for goedesicw only

232 _STD = STANDARD | _CAP_3 | _CAP_1 # for goedesicw only

233 _STD_LINE = _STD | _CAP_1p # for goedesicw only

234

235 LINE_OFF = 1 << 15 # Line without updates from parent geodesic or rhumb

236 LONG_UNROLL = 1 << 16 # unroll C{lon2} in .Direct and .Position

237 REVERSE2 = 1 << 17 # reverse C{azi2}

238

239 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE

240 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA

241

242 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE

243 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA

244

245 _ANGLE_ONLY = 1 << 18 # angular distance C{a12} only

246 _SALPs_CALPs = 1 << 19 # (INTERNAL) GeodesicExact._GenInverse

247

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

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

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

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

252 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \

253 _DEBUG_LINE | _ANGLE_ONLY | _SALPs_CALPs

254 _OUT_ALL = ALL

255 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL

256

257 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE

258 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE

259 _DISTANCE_IN_OUT = DISTANCE_IN & _OUT_MASK

260 _LINE = AZIMUTH | LATITUDE | LONG_UNROLL

261 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE

262# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE

263

264 Caps = Caps() # PYCHOK singleton

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

266 rhumb capabilities (C{caps}) and expected results (C{outmask}).

267

268 C{AREA} - compute area C{S12},

269

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

271

272 C{DISTANCE} - compute distance C{s12},

273

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

275

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

277

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

279

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

281

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

283

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

285

286 C{LONG_UNROLL} - unroll C{lon2} in C{.Direct},

287

288 C{REDUCEDLENGTH} - compute reduced length C{m12},

289

290 C{REVERSE2} - reverse C{azi2},

291

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

293

294 C{STANDARD} = C{AZIMUTH | DISTANCE | DISTANCE_IN | LATITUDE | LONGITUDE}'''

295

296

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

298 '''(INTERNAL) Base class for C{[_]Geodesic*Exact}.

299 '''

300 ALL = Caps.ALL

301 AREA = Caps.AREA

302 AZIMUTH = Caps.AZIMUTH

303 DISTANCE = Caps.DISTANCE

304 DISTANCE_IN = Caps.DISTANCE_IN

305 EMPTY = Caps.EMPTY # aka NONE

306 GEODESICSCALE = Caps.GEODESICSCALE

307 LATITUDE = Caps.LATITUDE

308 LINE_OFF = Caps.LINE_OFF

309 LONGITUDE = Caps.LONGITUDE

310 LONG_UNROLL = Caps.LONG_UNROLL

311 REDUCEDLENGTH = Caps.REDUCEDLENGTH

312 STANDARD = Caps.STANDARD

313

314 _caps = 0 # None

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

316

317 @Property_RO

318 def caps(self):

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

320 '''

321 return self._caps

322

323 def caps_(self, caps):

324 '''Check the available capabilities.

325

326 @arg caps: Bit-or'ed combination of L{Caps} values

327 for all capabilities to be checked.

328

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

330 C{False} otherwise (C{bool}).

331 '''

332 caps &= Caps._OUT_ALL

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

334

335 @property

336 def debug(self):

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

338 '''

339 return bool(self._debug)

340

341 @debug.setter # PYCHOK setter!

342 def debug(self, debug):

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

344 more details in L{GDict} results.

345 '''

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

347

348 def _iter2tion(self, r, s):

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

350 '''

351 i = _xkwds_get(s, iter=None)

352 if i is not None:

353 self._iteration = r._iteration = i

354 return r

355

356

357 class Direct9Tuple(_GTuple):

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

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

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

361 C{meter} I{squared}.

362 '''

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

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

365

366

367 class GDict(_Dict): # XXX _NamedDict

368 '''Basic C{dict} with both key I{and} attribute access

369 to the C{dict} items.

370

371 Results of all C{geodesic} methods are returned as a

372 L{GDict} instance.

373 '''

374 _iteration = None # Iteration number (C{int}) or C{None}

375

376 @property_RO

377 def iteration(self): # see .named._NamedBase

378 '''Get the iteration number (C{int}) or

379 C{None} if not available/applicable.

380 '''

381 return self._iteration

382

383 def toDirect9Tuple(self, dflt=NAN):

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

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

386

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

388

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

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

391 '''

392 return self._toTuple(Direct9Tuple, dflt)

393

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

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

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

397 result.

398

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

400

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

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

403 '''

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

405

406 def toInverse10Tuple(self, dflt=NAN):

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

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

409

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

411

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

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

414 '''

415 return self._toTuple(Inverse10Tuple, dflt)

416

417 @deprecated_method

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

419 '''DEPRECATED, used method C{toRhumb8Tuple}.

420

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

422 '''

423 return self._toTuple(_MODS.deprecated.Rhumb7Tuple, dflt)

424

425 def toRhumb8Tuple(self, dflt=NAN):

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

427

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

429

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

431 azi12, s12, S12, a12)}.

432 '''

433 return self._toTuple(Rhumb8Tuple, dflt)

434

435 def toRhumbSolve7Tuple(self, dflt=NAN):

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

437

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

439

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

441 azi12, s12, S12)}.

442 '''

443 return self._toTuple(_MODS.rhumbsolve.RhumbSolve7Tuple, dflt)

444

445 def _toTuple(self, nTuple, dflt):

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

447 '''

448 _g = getattr

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

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

451

452

453 class Inverse10Tuple(_GTuple):

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

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

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

457 C{calp2} of the initial C{1} and final C{2} foward azimuths.

458 '''

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

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

461

462 def toGDict(self, **updates):

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

464

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

466 '''

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

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

469 **updates) # PYCHOK indent

470

471

472 class _kWrapped(object): # in .geodesicw

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

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

475 '''

476

477 @Property_RO

478 def geographiclib(self):

479 '''Get the imported C{geographiclib}, provided the U{geographiclib

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

481 otherwise an C{ImportError}.

482 '''

483 g = _xgeographiclib(self.__class__, 1, 49)

484 from geographiclib.geodesic import Geodesic

485 g.Geodesic = Geodesic

486 from geographiclib.geodesicline import GeodesicLine

487 g.GeodesicLine = GeodesicLine

488 from geographiclib.geomath import Math

489 g.Math = Math

490 return g

491

492 @Property_RO # MCCABE 13

493 def Math(self):

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

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

496 otherwise C{None}.

497 '''

498 try:

499 g = self.geographiclib

500 M = g.Math

501 if _xversion_info(g) < (2,):

502 if _K_2_0:

503 M = None

504# elif not _K_2_0: # XXX set 2.0?

505# _K_2_0 = True

506 except (AttributeError, ImportError):

507 M = None

508 return M

509

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

511

512

513 class Rhumb8Tuple(_GTuple):

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

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

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

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

518 '''

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

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

521

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

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

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

525

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

527 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments

528 to specify or override L{Inverse10Tuple} items.

529

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

531 m12, M12, M21, S12)}

532 '''

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

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

535 if a12_azi1_azi2_m12_M12_M21:

536 d.update(a12_azi1_azi2_m12_M12_M21)

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

538

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

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

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

542

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

544 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword

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

546

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

548 m12, M12, M21, S12)}.

549 '''

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

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

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

553 if a12_m12_M12_M21_salp1_calp1_salp2_calp2:

554 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2)

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

556

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

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

559 '''

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

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

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

563

564 @deprecated_method

565 def _to7Tuple(self):

566 '''DEPRECATED, do not use!'''

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

568

569

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

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

572

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

574

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

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

577 '''

578 try:

579 return _wrapped.Math.AngRound(x)

580 except AttributeError:

581 if x:

582 y = _1_16th - fabs(x)

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

584 x = _copysign(_1_16th - y, x)

585 else:

586 x = _0_0 # -0 to 0

587 return x

588

589

590 def _atan2d(y, x):

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

592 '''

593 try:

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

595 except AttributeError:

596 return atan2d(y, x)

597

598

599 def _cbrt(x):

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

601 '''

602 try:

603 return _wrapped.Math.cbrt(x)

604 except AttributeError:

605 return cbrt(x)

606

607

608 def _copyBit(x, y):

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

610 '''

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

612

613

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

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

616 '''

617 r = cx - sx

618 if r:

619 r *= (cx + sx) * _2_0

620 return r

621

622

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

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

625

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

627 '''

628 try:

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

630 except AttributeError:

631 if K_2_0 or _K_2_0: # geographiclib 2.0

632 _r, _360 = fremainder, _360_0

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

634 _r( deg, _360))

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

636 if d in (_0_0, _180_0, _N_180_0):

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

638 else:

639 _n = _norm180

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

641 d = _n(d)

642 if t > 0 and d == _180_0:

643 d = _N_180_0

644 d, t = _sum2(d, t)

645 return d, t

646

647

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

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

650

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

652 '''

653 try:

654 return _wrapped.Math.LatFix(deg)

655 except AttributeError:

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

657

658

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

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

661

662 @return: C{True} if finite.

663 '''

664 try:

665 return _wrapped.Math.isfinite(x)

666 except AttributeError:

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

668

669

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

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

672

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

674 '''

675 try:

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

677 except AttributeError:

678 return norm2(x, y)

679

680

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

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

683

684 @return: Reduced angle C{degrees}.

685 '''

686 try:

687 return _wrapped.Math.AngNormalize(deg)

688 except AttributeError:

689 d = fremainder(deg, _360_0)

690 if d in (_180_0, _N_180_0):

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

692 return d

693

694

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

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

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

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

699 or C{geodesicw.Geodesic} instance.

700 '''

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

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

703

704 if _MODS.booleans.isBoolean(points):

705 # recursive call for each boolean clip

706

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

708 return _polygon(geodesic, clip, *args)

709

710 if not closed: # closed only

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

712

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

714

715 gP = geodesic.Polygon(line)

716 _A = gP.AddPoint

717

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

719 p1 = p0 = Ps[0]

720

721 # note, lon deltas are unrolled, by default

722 _A(p1.lat, p1.lon)

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

724 if wrap and not Ps.looped:

725 p2 = _unrollon(p1, p2)

726 _A(p2.lat, p2.lon)

727 p1 = p2

728 if closed and line and p1 != p0:

729 _A(p0.lat, p0.lon)

730

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

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

733

734

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

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

737 different signature and cascaded summation as C{karney._sum2_}.

738

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

740 '''

741 # assert 0 <= i <= j <= len(cs)

742# try:

743# return _wrapped.Math.polyval(j - i - 1, cs, i, x)

744# except AttributeError:

745# pass

746 s, t = cs[i], _0_0

747 for c in cs[i+1: j]:

748 s, t = _sum2_(s * x, t * x, c)

749 return s # + t

750

751

752 def _remainder(x, y):

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

754

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

756 '''

757 try:

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

759 except AttributeError:

760 return fremainder(x, y)

761

762

763 if _K_2_0:

764 from math import cos as _cos, sin as _sin

765

766 def _sincos2(rad):

767 return _sin(rad), _cos(rad)

768

769 _signBit = _MODS.basics.signBit

770 else:

771 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared

772

773 def _signBit(x):

774 '''(INTERNAL) GeographicLib 1.52-.

775 '''

776 return x < 0

777

778

779 def _sincos2d(deg):

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

781

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

783 '''

784 try:

785 return _wrapped.Math.sincosd(deg)

786 except AttributeError:

787 return sincos2d(deg)

788

789

790 def _sincos2de(deg, t):

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

792

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

794 '''

795 try:

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

797 except AttributeError:

798 return sincos2d(deg, adeg=t)

799

800

801 def _sum2(u, v): # mimick geomath.Math.sum, actually sum2

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

803

804 @return: 2-Tuple C{(B{u} + B{v}, residual)}.

805

806 @note: The C{residual} can be the same as B{C{u}} or B{C{v}}.

807

808 @see: U{Algorithm 3.1<https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>}.

809 '''

810 try:

811 return _wrapped.Math.sum(u, v)

812 except AttributeError:

813 s = u + v

814 r = s - v

815 t = s - r

816 # if Algorithm_3_1:

817 # t = (u - t) + (v + r)

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

819 # r -= u

820 # t -= v

821 # t += r

822 # t = -t

823 # else:

824 t = (u - r) + (v - t)

825 return s, t

826

827

828 def _sum2_(s, t, *vs):

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

830

831 @return: 2-Tuple C{(B{s} + B{t} + sum(B{vs}), residual)}.

832

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

834 html/Accumulator_8hpp_source.html>} comments for more details and

835 function C{_sum2} above.

836

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

838 '''

839 _s2, _u0 = _sum2, unsigned0

840 for v in vs:

841 if v:

842 t, u = _s2(t, v) # start at the least-

843 if s:

844 s, t = _s2(s, t) # significant end

845 if s:

846 t += u # accumulate u into t

847# elif t: # s == 0 implies t == 0

848# raise _AssertionError(t=t, txt=_not_(_0_))

849 else:

850 s = _u0(u) # result is u, t = 0

851 else:

852 s, t = _u0(t), u

853 return s, t

854

855

856 def _tand(x):

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

858 '''

859 try:

860 return _wrapped.Math.tand(x)

861 except AttributeError:

862 return tand(x)

863

864

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

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

867

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

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

870 '''

871 if wrap:

872 d, t = _diff182(lon1, lon2)

873 lon2, _ = _sum2_(d, t, lon1) # (lon1 + d) + t

874 else:

875 lon2 = _norm180(lon2)

876 return (lon2 - lon1), lon2

877

878

879 def _unsigned2(x):

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

881 '''

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

883

884

885__all__ += _ALL_DOCS(_CapsBase)

886

887# **) MIT License

888#

889# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.

890#

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

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

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

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

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

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

897#

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

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

900#

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

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

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

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

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

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

907# OTHER DEALINGS IN THE SOFTWARE.