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} C{RhumbAux}, C{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>} in I{GeographicLib 2.2+}

75

76 - L{rhumbx} C{Rhumb}, C{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 in 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 iterative implementations of I{Karney}'s solution posted here 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_0, \

138 _1_16th, _180_0, _N_180_0, _360_0

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

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

141 from pygeodesy.fmath import cbrt, fremainder, norm2, hypot as _hypot, unstr # PYCHOK shared

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

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

144 _number_, _s12_, _S12_

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

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

147 from pygeodesy.props import deprecated_method, Property_RO, property_RO

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

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

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

151 Precision_, _1mm as _TOL_M # PYCHOK shared

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

153

154# from math import fabs # from .utily

155

156__all__ = _ALL_LAZY.karney

157__version__ = '23.08.09'

158

159_EWGS84 = _WGS84.ellipsoid # PYCHOK in .auxilats, .geodesicx.gx, .ktm, .solveBase

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

161_perimeter_ = 'perimeter'

162

163

164 class _GTuple(_NamedTuple): # in .testNamedTuples

165 '''(INTERNAL) Helper.

166 '''

167 def toGDict(self, **updates):

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

169

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

171 '''

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

173 if updates:

174 r.update(updates)

175 if self._iteration is not None:

176 r._iteration = self._iteration

177 return r

178

179

180 class _Lat(Lat):

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

182 '''

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

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

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

186

187

188 class _Lon(Lon):

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

190 '''

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

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

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

194

195

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

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

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

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

200 '''

201 _Names_ = (_number_, _perimeter_, _area_)

202 _Units_ = ( Number_, _M, _M2)

203

204

205 class Caps(object): # PYCHOK

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

207 '''

208 EMPTY = 0 # formerly aka NONE

209 _CAP_1 = 1 << 0 # for goedesicw only

210 _CAP_1p = 1 << 1 # for goedesicw only

211# _CAP_2 = 1 << 2

212 _CAP_3 = 1 << 3 # for goedesicw only

213# _CAP_4 = 1 << 4

214# _CAP_ALL = 0x1F

215# _CAP_MASK = _CAP_ALL

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

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

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

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

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

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

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

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

224

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

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

227

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

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

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

231 _STD_LINE = _STD | _CAP_1p # for goedesicw only

232

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

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

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

236

237 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE

238 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA

239

240 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE

241 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA

242

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

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

245

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

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

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

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

250 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \

251 _DEBUG_LINE | _ANGLE_ONLY | _SALPs_CALPs

252 _OUT_ALL = ALL

253 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL

254

255 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE

256 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE

257 _DISTANCE_IN_OUT = DISTANCE_IN & _OUT_MASK

258 _LINE = AZIMUTH | LATITUDE | LONG_UNROLL

259 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE

260# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE

261

262 Caps = Caps() # PYCHOK singleton

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

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

265

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

267

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

269

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

271

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

273

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

275

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

277

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

279

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

281

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

283

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

285

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

287

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

289

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

291

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

293

294

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

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

297 '''

298 ALL = Caps.ALL

299 AREA = Caps.AREA

300 AZIMUTH = Caps.AZIMUTH

301 DISTANCE = Caps.DISTANCE

302 DISTANCE_IN = Caps.DISTANCE_IN

303 EMPTY = Caps.EMPTY # aka NONE

304 GEODESICSCALE = Caps.GEODESICSCALE

305 LATITUDE = Caps.LATITUDE

306 LINE_OFF = Caps.LINE_OFF

307 LONGITUDE = Caps.LONGITUDE

308 LONG_UNROLL = Caps.LONG_UNROLL

309 REDUCEDLENGTH = Caps.REDUCEDLENGTH

310 STANDARD = Caps.STANDARD

311

312 _caps = 0 # None

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

314

315 @Property_RO

316 def caps(self):

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

318 '''

319 return self._caps

320

321 def caps_(self, caps):

322 '''Check the available capabilities.

323

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

325 for all capabilities to be checked.

326

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

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

329 '''

330 caps &= Caps._OUT_ALL

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

332

333 @property

334 def debug(self):

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

336 '''

337 return bool(self._debug)

338

339 @debug.setter # PYCHOK setter!

340 def debug(self, debug):

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

342 more details in L{GDict} results.

343 '''

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

345

346 def _iter2tion(self, r, s):

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

348 '''

349 i = _xkwds_get(s, iter=None)

350 if i is not None:

351 self._iteration = r._iteration = i

352 return r

353

354

355 class Direct9Tuple(_GTuple):

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

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

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

359 C{meter} I{squared}.

360 '''

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

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

363

364

365 class GDict(_Dict): # XXX _NamedDict

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

367 to the C{dict} items.

368

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

370 L{GDict} instance.

371 '''

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

373

374 @property_RO

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

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

377 C{None} if not available/applicable.

378 '''

379 return self._iteration

380

381 def toDirect9Tuple(self, dflt=NAN):

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

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

384

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

386

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

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

389 '''

390 return self._toTuple(Direct9Tuple, dflt)

391

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

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

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

395 result.

396

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

398

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

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

401 '''

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

403

404 def toInverse10Tuple(self, dflt=NAN):

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

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

407

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

409

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

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

412 '''

413 return self._toTuple(Inverse10Tuple, dflt)

414

415 @deprecated_method

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

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

418

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

420 '''

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

422

423 def toRhumb8Tuple(self, dflt=NAN):

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

425

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

427

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

429 azi12, s12, S12, a12)}.

430 '''

431 return self._toTuple(Rhumb8Tuple, dflt)

432

433 def toRhumbSolve7Tuple(self, dflt=NAN):

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

435

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

437

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

439 azi12, s12, S12)}.

440 '''

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

442

443 def _toTuple(self, nTuple, dflt):

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

445 '''

446 _g = getattr

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

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

449

450

451 class Inverse10Tuple(_GTuple):

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

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

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

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

456 '''

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

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

459

460 def toGDict(self, **updates):

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

462

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

464 '''

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

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

467 **updates) # PYCHOK indent

468

469

470 class _kWrapped(object):

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

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

473 '''

474

475 @Property_RO

476 def geographiclib(self):

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

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

479 otherwise an C{ImportError}.

480 '''

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

482 from geographiclib.geodesic import Geodesic

483 g.Geodesic = Geodesic

484 from geographiclib.geodesicline import GeodesicLine

485 g.GeodesicLine = GeodesicLine

486 from geographiclib.geomath import Math

487 g.Math = Math

488 return g

489

490 @Property_RO # MCCABE 13

491 def Math(self):

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

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

494 otherwise C{None}.

495 '''

496 try:

497 g = self.geographiclib

498 M = g.Math

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

500 if _K_2_0:

501 M = None

502# elif not _K_2_0: # XXX set 2.0?

503# _K_2_0 = True

504 except (AttributeError, ImportError):

505 M = None

506 return M

507

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

509

510

511 class Rhumb8Tuple(_GTuple):

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

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

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

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

516 '''

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

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

519

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

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

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

523

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

525 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments

526 to specify or override L{Inverse10Tuple} items.

527

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

529 m12, M12, M21, S12)}

530 '''

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

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

533 if a12_azi1_azi2_m12_M12_M21:

534 d.update(a12_azi1_azi2_m12_M12_M21)

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

536

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

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

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

540

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

542 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword

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

544

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

546 m12, M12, M21, S12)}.

547 '''

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

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

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

551 if a12_m12_M12_M21_salp1_calp1_salp2_calp2:

552 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2)

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

554

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

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

557 '''

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

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

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

561

562 @deprecated_method

563 def _to7Tuple(self):

564 '''DEPRECATED, do not use!'''

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

566

567

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

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

570

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

572

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

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

575 '''

576 try:

577 return _wrapped.Math.AngRound(x)

578 except AttributeError:

579 if x:

580 y = _1_16th - fabs(x)

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

582 x = _copysign(_1_16th - y, x)

583 else:

584 x = _0_0 # -0 to 0

585 return x

586

587

588 def _atan2d(y, x):

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

590 '''

591 try:

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

593 except AttributeError:

594 return atan2d(y, x)

595

596

597 def _cbrt(x):

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

599 '''

600 try:

601 return _wrapped.Math.cbrt(x)

602 except AttributeError:

603 return cbrt(x)

604

605

606 def _copyBit(x, y):

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

608 '''

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

610

611

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

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

614

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

616 '''

617 try:

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

619 except AttributeError:

620 if K_2_0 or _K_2_0: # geographiclib 2.0

621 _r, _360 = fremainder, _360_0

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

623 _r( deg, _360))

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

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

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

627 else:

628 _n = _norm180

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

630 d = _n(d)

631 if t > 0 and d == _180_0:

632 d = _N_180_0

633 d, t = _sum2(d, t)

634 return d, t

635

636

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

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

639

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

641 '''

642 try:

643 return _wrapped.Math.LatFix(deg)

644 except AttributeError:

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

646

647

648 def _isfinite(x): # mimick geomath.Math.AngNormalize

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

650

651 @return: C{True} if finite.

652 '''

653 try:

654 return _wrapped.Math.isfinite(x)

655 except AttributeError:

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

657

658

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

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

661

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

663 '''

664 try:

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

666 except AttributeError:

667 return norm2(x, y)

668

669

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

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

672

673 @return: Reduced angle C{degrees}.

674 '''

675 try:

676 return _wrapped.Math.AngNormalize(deg)

677 except AttributeError:

678 d = fremainder(deg, _360_0)

679 if d in (_180_0, _N_180_0):

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

681 return d

682

683

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

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

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

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

688 or C{geodesicw.Geodesic} instance.

689 '''

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

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

692

693 if _MODS.booleans.isBoolean(points):

694 # recursive call for each boolean clip

695

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

697 return _polygon(geodesic, clip, *args)

698

699 if not closed: # closed only

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

701

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

703

704 gP = geodesic.Polygon(line)

705 _A = gP.AddPoint

706

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

708 p1 = p0 = Ps[0]

709

710 # note, lon deltas are unrolled, by default

711 _A(p1.lat, p1.lon)

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

713 if wrap and not Ps.looped:

714 p2 = _unrollon(p1, p2)

715 _A(p2.lat, p2.lon)

716 p1 = p2

717 if closed and line and p1 != p0:

718 _A(p0.lat, p0.lon)

719

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

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

722

723

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

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

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

727

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

729 '''

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

731# try:

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

733# except AttributeError:

734# pass

735 s, t = cs[i], _0_0

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

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

738 return s # + t

739

740

741 def _remainder(x, y):

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

743

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

745 '''

746 try:

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

748 except AttributeError:

749 return fremainder(x, y)

750

751

752 if _K_2_0:

753 from pygeodesy.basics import signBit as _signBit

754 from math import cos as _cos, sin as _sin

755

756 def _sincos2(rad):

757 return _sin(rad), _cos(rad)

758

759 else:

760 from pygeodesy.utily import sincos2 as _sincos2 # PYCHOK shared

761

762 def _signBit(x):

763 '''(INTERNAL) GeographicLin 1.52-.

764 '''

765 return x < 0

766

767

768 def _sincos2d(deg):

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

770

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

772 '''

773 try:

774 return _wrapped.Math.sincosd(deg)

775 except AttributeError:

776 return sincos2d(deg)

777

778

779 def _sincos2de(deg, t):

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

781

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

783 '''

784 try:

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

786 except AttributeError:

787 return sincos2d(deg, adeg=t)

788

789

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

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

792

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

794

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

796

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

798 '''

799 try:

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

801 except AttributeError:

802 s = u + v

803 r = s - v

804 t = s - r

805 # if Algorithm_3_1:

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

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

808 # r -= u

809 # t -= v

810 # t += r

811 # t = -t

812 # else:

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

814 return s, t

815

816

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

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

819

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

821

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

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

824 function C{_sum2} above.

825

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

827 '''

828 _s2, _u0 = _sum2, unsigned0

829 for v in vs:

830 if v:

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

832 if s:

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

834 if s:

835 t += u # accumulate u into t

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

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

838 else:

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

840 else:

841 s, t = _u0(t), u

842 return s, t

843

844

845 def _tand(x):

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

847 '''

848 try:

849 return _wrapped.Math.tand(x)

850 except AttributeError:

851 return tand(x)

852

853

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

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

856

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

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

859 '''

860 if wrap:

861 d, t = _diff182(lon1, lon2)

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

863 else:

864 lon2 = _norm180(lon2)

865 return (lon2 - lon1), lon2

866

867

868 def _unsigned2(x):

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

870 '''

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

872

873

874__all__ += _ALL_DOCS(_CapsBase)

875

876# **) MIT License

877#

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

879#

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

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

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

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

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

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

886#

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

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

889#

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

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

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

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

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

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

896# OTHER DEALINGS IN THE SOFTWARE.