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_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, norm2, unstr, \

143 Fsum, hypot as _hypot # PYCHOK shared

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

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

146 _number_, _s12_, _S12_

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

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

149 from pygeodesy.props import deprecated_method, Property_RO, property_RO

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

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

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

153 Precision_, _1mm as _TOL_M # PYCHOK shared

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

155

156# from math import fabs # from .utily

157

158__all__ = _ALL_LAZY.karney

159__version__ = '23.08.20'

160

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

162_perimeter_ = 'perimeter'

163

164

165 class _GTuple(_NamedTuple): # in .testNamedTuples

166 '''(INTERNAL) Helper.

167 '''

168 def toGDict(self, **updates):

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

170

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

172 '''

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

174 if updates:

175 r.update(updates)

176 if self._iteration is not None:

177 r._iteration = self._iteration

178 return r

179

180

181 class _Lat(Lat):

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

183 '''

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

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

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

187

188

189 class _Lon(Lon):

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

191 '''

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

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

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

195

196

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

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

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

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

201 '''

202 _Names_ = (_number_, _perimeter_, _area_)

203 _Units_ = ( Number_, _M, _M2)

204

205

206 class Caps(object): # PYCHOK

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

208 '''

209 EMPTY = 0 # formerly aka NONE

210 _CAP_1 = 1 << 0 # for goedesicw only

211 _CAP_1p = 1 << 1 # for goedesicw only

212# _CAP_2 = 1 << 2

213 _CAP_3 = 1 << 3 # for goedesicw only

214# _CAP_4 = 1 << 4

215# _CAP_ALL = 0x1F

216# _CAP_MASK = _CAP_ALL

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

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

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

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

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

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

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

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

225

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

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

228

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

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

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

232 _STD_LINE = _STD | _CAP_1p # for goedesicw only

233

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

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

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

237

238 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE

239 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA

240

241 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE

242 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA

243

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

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

246

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

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

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

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

251 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \

252 _DEBUG_LINE | _ANGLE_ONLY | _SALPs_CALPs

253 _OUT_ALL = ALL

254 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL

255

256 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE

257 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE

258 _DISTANCE_IN_OUT = DISTANCE_IN & _OUT_MASK

259 _LINE = AZIMUTH | LATITUDE | LONG_UNROLL

260 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE

261# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE

262

263 Caps = Caps() # PYCHOK singleton

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

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

266

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

268

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

270

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

272

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

274

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

276

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

278

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

280

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

282

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

284

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

286

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

288

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

290

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

292

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

294

295

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

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

298 '''

299 ALL = Caps.ALL

300 AREA = Caps.AREA

301 AZIMUTH = Caps.AZIMUTH

302 DISTANCE = Caps.DISTANCE

303 DISTANCE_IN = Caps.DISTANCE_IN

304 EMPTY = Caps.EMPTY # aka NONE

305 GEODESICSCALE = Caps.GEODESICSCALE

306 LATITUDE = Caps.LATITUDE

307 LINE_OFF = Caps.LINE_OFF

308 LONGITUDE = Caps.LONGITUDE

309 LONG_UNROLL = Caps.LONG_UNROLL

310 REDUCEDLENGTH = Caps.REDUCEDLENGTH

311 STANDARD = Caps.STANDARD

312

313 _caps = 0 # None

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

315

316 @Property_RO

317 def caps(self):

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

319 '''

320 return self._caps

321

322 def caps_(self, caps):

323 '''Check the available capabilities.

324

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

326 for all capabilities to be checked.

327

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

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

330 '''

331 caps &= Caps._OUT_ALL

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

333

334 @property

335 def debug(self):

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

337 '''

338 return bool(self._debug)

339

340 @debug.setter # PYCHOK setter!

341 def debug(self, debug):

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

343 more details in L{GDict} results.

344 '''

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

346

347 def _iter2tion(self, r, s):

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

349 '''

350 i = _xkwds_get(s, iter=None)

351 if i is not None:

352 self._iteration = r._iteration = i

353 return r

354

355

356 class Direct9Tuple(_GTuple):

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

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

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

360 C{meter} I{squared}.

361 '''

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

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

364

365

366 class GDict(_Dict): # XXX _NamedDict

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

368 to the C{dict} items.

369

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

371 L{GDict} instance.

372 '''

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

374

375 @property_RO

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

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

378 C{None} if not available/applicable.

379 '''

380 return self._iteration

381

382 def toDirect9Tuple(self, dflt=NAN):

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

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

385

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

387

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

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

390 '''

391 return self._toTuple(Direct9Tuple, dflt)

392

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

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

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

396 result.

397

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

399

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

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

402 '''

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

404

405 def toInverse10Tuple(self, dflt=NAN):

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

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

408

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

410

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

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

413 '''

414 return self._toTuple(Inverse10Tuple, dflt)

415

416 @deprecated_method

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

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

419

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

421 '''

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

423

424 def toRhumb8Tuple(self, dflt=NAN):

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

426

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

428

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

430 azi12, s12, S12, a12)}.

431 '''

432 return self._toTuple(Rhumb8Tuple, dflt)

433

434 def toRhumbSolve7Tuple(self, dflt=NAN):

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

436

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

438

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

440 azi12, s12, S12)}.

441 '''

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

443

444 def _toTuple(self, nTuple, dflt):

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

446 '''

447 _g = getattr

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

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

450

451

452 class Inverse10Tuple(_GTuple):

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

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

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

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

457 '''

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

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

460

461 def toGDict(self, **updates):

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

463

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

465 '''

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

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

468 **updates) # PYCHOK indent

469

470

471 class _kWrapped(object): # in .geodesicw

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

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

474 '''

475

476 @Property_RO

477 def geographiclib(self):

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

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

480 otherwise an C{ImportError}.

481 '''

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

483 from geographiclib.geodesic import Geodesic

484 g.Geodesic = Geodesic

485 from geographiclib.geodesicline import GeodesicLine

486 g.GeodesicLine = GeodesicLine

487 from geographiclib.geomath import Math

488 g.Math = Math

489 return g

490

491 @Property_RO # MCCABE 13

492 def Math(self):

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

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

495 otherwise C{None}.

496 '''

497 try:

498 g = self.geographiclib

499 M = g.Math

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

501 if _K_2_0:

502 M = None

503# elif not _K_2_0: # XXX set 2.0?

504# _K_2_0 = True

505 except (AttributeError, ImportError):

506 M = None

507 return M

508

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

510

511

512 class Rhumb8Tuple(_GTuple):

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

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

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

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

517 '''

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

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

520

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

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

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

524

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

526 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments

527 to specify or override L{Inverse10Tuple} items.

528

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

530 m12, M12, M21, S12)}

531 '''

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

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

534 if a12_azi1_azi2_m12_M12_M21:

535 d.update(a12_azi1_azi2_m12_M12_M21)

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

537

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

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

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

541

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

543 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword

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

545

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

547 m12, M12, M21, S12)}.

548 '''

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

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

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

552 if a12_m12_M12_M21_salp1_calp1_salp2_calp2:

553 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2)

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

555

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

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

558 '''

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

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

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

562

563 @deprecated_method

564 def _to7Tuple(self):

565 '''DEPRECATED, do not use!'''

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

567

568

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

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

571

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

573

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

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

576 '''

577 try:

578 return _wrapped.Math.AngRound(x)

579 except AttributeError:

580 if x:

581 y = _1_16th - fabs(x)

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

583 x = _copysign(_1_16th - y, x)

584 else:

585 x = _0_0 # -0 to 0

586 return x

587

588

589 def _atan2d(y, x):

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

591 '''

592 try:

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

594 except AttributeError:

595 return atan2d(y, x)

596

597

598 def _cbrt(x):

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

600 '''

601 try:

602 return _wrapped.Math.cbrt(x)

603 except AttributeError:

604 return cbrt(x)

605

606

607 def _copyBit(x, y):

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

609 '''

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

611

612

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

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

615 '''

616 r = cx - sx

617 if r:

618 r *= (cx + sx) * _2_0

619 return r

620

621

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

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

624

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

626 '''

627 try:

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

629 except AttributeError:

630 if K_2_0 or _K_2_0: # geographiclib 2.0

631 _r, _360 = fremainder, _360_0

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

633 _r( deg, _360))

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

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

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

637 else:

638 _n = _norm180

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

640 d = _n(d)

641 if t > 0 and d == _180_0:

642 d = _N_180_0

643 d, t = _sum2(d, t)

644 return d, t

645

646

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

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

649

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

651 '''

652 try:

653 return _wrapped.Math.LatFix(deg)

654 except AttributeError:

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

656

657

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

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

660

661 @return: C{True} if finite.

662 '''

663 try:

664 return _wrapped.Math.isfinite(x)

665 except AttributeError:

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

667

668

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

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

671

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

673 '''

674 try:

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

676 except AttributeError:

677 return norm2(x, y)

678

679

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

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

682

683 @return: Reduced angle C{degrees}.

684 '''

685 try:

686 return _wrapped.Math.AngNormalize(deg)

687 except AttributeError:

688 d = fremainder(deg, _360_0)

689 if d in (_180_0, _N_180_0):

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

691 return d

692

693

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

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

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

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

698 or C{geodesicw.Geodesic} instance.

699 '''

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

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

702

703 if _MODS.booleans.isBoolean(points):

704 # recursive call for each boolean clip

705

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

707 return _polygon(geodesic, clip, *args)

708

709 if not closed: # closed only

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

711

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

713

714 gP = geodesic.Polygon(line)

715 _A = gP.AddPoint

716

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

718 p1 = p0 = Ps[0]

719

720 # note, lon deltas are unrolled, by default

721 _A(p1.lat, p1.lon)

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

723 if wrap and not Ps.looped:

724 p2 = _unrollon(p1, p2)

725 _A(p2.lat, p2.lon)

726 p1 = p2

727 if closed and line and p1 != p0:

728 _A(p0.lat, p0.lon)

729

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

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

732

733

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

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

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

737

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

739 '''

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

741# try:

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

743# except AttributeError:

744# pass

745 s, t = cs[i], _0_0

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

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

748 return s # + t

749

750

751 def _remainder(x, y):

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

753

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

755 '''

756 try:

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

758 except AttributeError:

759 return fremainder(x, y)

760

761

762 if _K_2_0:

763 from math import cos as _cos, sin as _sin

764

765 def _sincos2(rad):

766 return _sin(rad), _cos(rad)

767

768 _signBit = _MODS.basics.signBit

769 else:

770 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared

771

772 def _signBit(x):

773 '''(INTERNAL) GeographicLib 1.52-.

774 '''

775 return x < 0

776

777

778 def _sincos2d(deg):

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

780

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

782 '''

783 try:

784 return _wrapped.Math.sincosd(deg)

785 except AttributeError:

786 return sincos2d(deg)

787

788

789 def _sincos2de(deg, t):

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

791

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

793 '''

794 try:

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

796 except AttributeError:

797 return sincos2d(deg, adeg=t)

798

799

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

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

802

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

804

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

806

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

808 '''

809 try:

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

811 except AttributeError:

812 s = u + v

813 r = s - v

814 t = s - r

815 # if Algorithm_3_1:

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

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

818 # r -= u

819 # t -= v

820 # t += r

821 # t = -t

822 # else:

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

824 return s, t

825

826

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

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

829

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

831

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

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

834 function C{_sum2} above.

835

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

837 '''

838 _s2, _u0 = _sum2, unsigned0

839 for v in vs:

840 if v:

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

842 if s:

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

844 if s:

845 t += u # accumulate u into t

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

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

848 else:

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

850 else:

851 s, t = _u0(t), u

852 return s, t

853

854

855 def _tand(x):

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

857 '''

858 try:

859 return _wrapped.Math.tand(x)

860 except AttributeError:

861 return tand(x)

862

863

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

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

866

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

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

869 '''

870 if wrap:

871 d, t = _diff182(lon1, lon2)

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

873 else:

874 lon2 = _norm180(lon2)

875 return (lon2 - lon1), lon2

876

877

878 def _unsigned2(x):

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

880 '''

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

882

883

884__all__ += _ALL_DOCS(_CapsBase)

885

886# **) MIT License

887#

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

889#

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

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

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

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

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

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

896#

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

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

899#

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

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

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

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

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

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

906# OTHER DEALINGS IN THE SOFTWARE.