1

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

3

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

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

6

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 All methods of the I{wrapped} L{Geodesic<geodesicw.Geodesic>} and L{GeodesicLine<geodesicw.GeodesicLine>}

10 classes return a L{GDict} instance offering access to the C{dict} items either by C{key} or by

11 C{attribute} name.

12

13 With env variable C{PYGEODESY_GEOGRAPHICLIB} left undefined or set to C{"2"}, modules L{geodesicx},

14 L{geodesicw} and this module will use U{GeographicLib 2.0<https://GeographicLib.SourceForge.io/C++/doc/>}

15 and newer transcoding, otherwise C{1.52} or older.

16

17 Karney-based functionality

18==========================

19

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

21

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

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

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

25

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

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

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

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

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

31

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

33 classGeographicLib_1_1CassiniSoldner.html>}

34

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

36

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

38 classGeographicLib_1_1EllipticFunction.html>}

39

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

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

42

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

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

45

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

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

48

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

50 classGeographicLib_1_1PolygonAreaT.html>}

51

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

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

54 classGeographicLib_1_1GeodesicLineExact.html>}

55

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

57

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

59

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

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

62

63 - L{JacobiConformal} -- U{JacobiConformal

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

65

66 - L{KTransverseMercator} - U{TransverseMercator

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

68

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

70 classGeographicLib_1_1LocalCartesian.html>}

71

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

73

74 - L{rhumb.aux_}, L{RhumbAux}, L{RhumbLineAux} -- U{Rhumb

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

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

77

78 - L{rhumb.ekx}, L{Rhumb}, L{RhumbLine} -- U{Rhumb

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

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

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

82 from I{GeographicLib 2.0}

83

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

85

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

87

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

89

90 - L{atan1d}, L{atan2d}, L{sincos2}, L{sincos2d}, L{tand} -- U{geomath.Math

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

92

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

94

95 2. These C{pygeodesy} modules and classes

96

97 - L{ellipsoidalGeodSolve}, L{ellipsoidalKarney}, L{geodsolve}, L{karney}, L{rhumb.solve}

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

99 L{GnomonicKarney}, L{HeightIDWkarney}

100

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

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

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

104

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

106

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

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

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

110

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

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

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

114

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

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

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

118

119 - L{RhumbLineAux.Intersection} and L{RhumbLine.Intersection}

120

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

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

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

124

125 4. The C{pygeodesy} methods to compute I{ellipsoidal} intersections and nearest points

126

127 - L{RhumbLineAux.Intersecant2}, L{RhumbLineAux.PlumbTo}, L{RhumbLine.Intersecant2} and L{RhumbLine.PlumbTo}

128

129 are I{transcoded} of I{Karney}'s iterative C++ function U{rhumb-intercept

130<https://SourceForge.net/p/geographiclib/discussion/1026620/thread/2ddc295e/>}.

131

132 5. Spherical functions

133

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

135

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

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

138'''

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

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

141

142 from pygeodesy.basics import _copysign, int1s, isint, itemsorted, neg, unsigned0, \

143 _xgeographiclib, _zip, _version_info

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

145 _1_16th, _1_0, _2_0, _180_0, _N_180_0, _360_0

146 from pygeodesy.errors import GeodesicError, _ValueError, _xkwds, _xkwds_get1

147 from pygeodesy.fmath import cbrt, fremainder, norm2

148# from pygeodesy.internals import _version_info # from .basics

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

150 _lat1_, _lat2_, _lon1_, _lon2_, _m12_, _M12_, \

151 _M21_, _number_, _s12_, _S12_, _UNDER_, \

152 _BAR_, NN # PYCHOK used!

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

154 from pygeodesy.named import ADict, _NamedBase, _NamedTuple, notImplemented, _Pass

155 from pygeodesy.props import deprecated_method, Property_RO

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

157 Meter as _M, Meter2 as _M2, Number_

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

159

160# from math import fabs # from .utily

161

162__all__ = _ALL_LAZY.karney

163__version__ = '24.06.27'

164

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

166_perimeter_ = 'perimeter'

167

168

169 class _GTuple(_NamedTuple): # in .testNamedTuples

170 '''(INTERNAL) Helper.

171 '''

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

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

174

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

176 '''

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

178 if updates:

179 r.update(updates)

180 if self._iteration is not None:

181 r._iteration = self._iteration

182 return r

183

184

185 class _Lat(Lat):

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

187 '''

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

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

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

191

192

193 class _Lon(Lon):

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

195 '''

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

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

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

199

200

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

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

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

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

205 '''

206 _Names_ = (_number_, _perimeter_, _area_)

207 _Units_ = ( Number_, _M, _M2)

208

209

210 class Caps(object): # PYCHOK

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

212 '''

213 EMPTY = 0 # formerly aka NONE

214 _CAP_1 = 1 << 0 # for goedesicw only

215 _CAP_1p = 1 << 1 # for goedesicw only

216 _CAP_2 = 1 << 2

217 _CAP_3 = 1 << 3 # for goedesicw only

218# _CAP_4 = 1 << 4

219# _CAP_ALL = 0x1F

220# _CAP_MASK = _CAP_ALL

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

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

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

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

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

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

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

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

229

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

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

232

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

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

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

236 _STD_LINE = _STD | _CAP_2 | _CAP_1p # for goedesici and -w

237

238 LINE_CAPS = _STD_LINE | REDUCEDLENGTH | GEODESICSCALE # .geodesici only

239

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

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

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

243

244 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE

245 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA

246

247 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE

248 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA

249

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

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

252

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

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

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

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

257 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \

258 _DEBUG_LINE | _ANGLE_ONLY | _SALPs_CALPs

259

260 _OUT_ALL = ALL

261 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL

262

263 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE

264 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE

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

266 _LINE = AZIMUTH | LATITUDE | LONG_UNROLL

267 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE

268# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE

269

270 def toStr(self, Csk, sep=_BAR_):

271 '''Return a C{Caps} or C{outmask} as C{str} or tuple of C{str}s.

272 '''

273 s = []

274 for c, C in itemsorted(self.__class__.__dict__):

275 if isint(C) and (Csk & C) and int1s(C) == 1 \

276 and (C in (Caps.REVERSE2, Caps._SALPs_CALPs)

277 or c.replace(_UNDER_, NN).isupper()):

278 s.append(c)

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

280

281 Caps = Caps() # PYCHOK singleton

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

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

284

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

286

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

288

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

290

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

292

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

294

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

296

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

298

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

300

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

302

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

304

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

306

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

308

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

310

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

312

313_KEY2Caps = dict(m12=Caps.REDUCEDLENGTH, # see GDict._unCaps

314 M12=Caps.GEODESICSCALE,

315 M21=Caps.GEODESICSCALE, S12=Caps.AREA)

316

317

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

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

320 '''

321 ALL = Caps.ALL

322 AREA = Caps.AREA

323 AZIMUTH = Caps.AZIMUTH

324 DISTANCE = Caps.DISTANCE

325 DISTANCE_IN = Caps.DISTANCE_IN

326 EMPTY = Caps.EMPTY # aka NONE

327 GEODESICSCALE = Caps.GEODESICSCALE

328 LATITUDE = Caps.LATITUDE

329 LINE_CAPS = Caps.LINE_CAPS

330 LINE_OFF = Caps.LINE_OFF

331 LONGITUDE = Caps.LONGITUDE

332 LONG_UNROLL = Caps.LONG_UNROLL

333 REDUCEDLENGTH = Caps.REDUCEDLENGTH

334 STANDARD = Caps.STANDARD

335 _STD_LINE = Caps._STD_LINE # for geodesici

336

337 _caps = 0 # None

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

339

340 @Property_RO

341 def caps(self):

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

343 '''

344 return self._caps

345

346 def caps_(self, caps):

347 '''Check the available capabilities.

348

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

350 for all capabilities to be checked.

351

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

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

354 '''

355 caps &= Caps._OUT_ALL

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

357

358 @property

359 def debug(self):

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

361 '''

362 return bool(self._debug)

363

364 @debug.setter # PYCHOK setter!

365 def debug(self, debug):

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

367 more details in L{GDict} results.

368 '''

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

370

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

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

373 '''

374 if iter is not None:

375 self._iteration = r._iteration = iter

376 return r

377

378

379 class Direct9Tuple(_GTuple):

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

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

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

383 C{meter} I{squared}.

384 '''

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

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

387

388

389 class GDict(ADict): # XXX _NamedDict

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

391

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

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

394 '''

395 def toDirect9Tuple(self, dflt=NAN):

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

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

398

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

400

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

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

403 '''

404 return self._toTuple(Direct9Tuple, dflt)

405

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

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

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

409 result.

410

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

412

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

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

415 '''

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

417

418 def toInverse10Tuple(self, dflt=NAN):

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

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

421

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

423

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

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

426 '''

427 return self._toTuple(Inverse10Tuple, dflt)

428

429 @deprecated_method

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

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

432

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

434 '''

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

436

437 def toRhumb8Tuple(self, dflt=NAN):

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

439

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

441

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

443 azi12, s12, S12, a12)}.

444 '''

445 return self._toTuple(Rhumb8Tuple, dflt)

446

447 def toRhumbSolve7Tuple(self, dflt=NAN):

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

449

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

451

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

453 azi12, s12, S12)}.

454 '''

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

456

457 def _toTuple(self, nTuple, dflt):

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

459 '''

460 _g = getattr

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

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

463

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

465 '''(INTERNAL) Remove superfluous items.

466 '''

467 for k, m in _KEY2Caps.items():

468 if k in self and not (outmask & m):

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

470 return self

471

472

473 class Inverse10Tuple(_GTuple):

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

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

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

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

478 '''

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

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

481

482 def toGDict(self, **updates):

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

484

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

486 '''

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

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

489 **updates) # PYCHOK indent

490

491

492 class _kWrapped(object): # in .geodesicw

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

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

495 '''

496

497 @Property_RO

498 def geographiclib(self):

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

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

501 otherwise raise a C{LazyImportError}.

502 '''

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

504 from geographiclib.geodesic import Geodesic

505 g.Geodesic = Geodesic

506 from geographiclib.geodesicline import GeodesicLine

507 g.GeodesicLine = GeodesicLine

508 from geographiclib.geomath import Math

509 g.Math = Math

510 return g

511

512 @Property_RO

513 def Math(self):

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

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

516 otherwise C{None}.

517 '''

518 try:

519 g = self.geographiclib

520 M = g.Math

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

522 if _K_2_0:

523 M = None

524# elif not _K_2_0: # XXX set 2.0?

525# _K_2_0 = True

526 except (AttributeError, ImportError):

527 M = None

528 return M

529

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

531

532

533 class Rhumb8Tuple(_GTuple):

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

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

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

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

538 '''

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

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

541

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

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

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

545

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

547 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments

548 to specify or override L{Inverse10Tuple} items.

549

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

551 m12, M12, M21, S12)}

552 '''

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

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

555 if a12_azi1_azi2_m12_M12_M21:

556 d.update(a12_azi1_azi2_m12_M12_M21)

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

558

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

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

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

562

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

564 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword

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

566

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

568 m12, M12, M21, S12)}.

569 '''

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

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

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

573 if a12_m12_M12_M21_salp1_calp1_salp2_calp2:

574 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2)

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

576

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

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

579 '''

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

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

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

583

584 @deprecated_method

585 def _to7Tuple(self):

586 '''DEPRECATED, do not use!'''

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

588

589

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

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

592

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

594

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

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

597 '''

598 try:

599 return _wrapped.Math.AngRound(x)

600 except AttributeError:

601 if x:

602 y = _1_16th - fabs(x)

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

604 x = _copysign(_1_16th - y, x)

605 else:

606 x = _0_0 # -0 to 0

607 return x

608

609

610 def _atan2d(y, x):

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

612 '''

613 try:

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

615 except AttributeError:

616 return atan2d(y, x)

617

618

619 def _cbrt(x):

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

621 '''

622 try:

623 return _wrapped.Math.cbrt(x)

624 except AttributeError:

625 return cbrt(x)

626

627

628 def _copyBit(x, y):

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

630 '''

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

632

633

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

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

636 '''

637 r = cx - sx

638 if r:

639 r *= (cx + sx) * _2_0

640 return r

641

642

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

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

645

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

647 '''

648 try:

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

650 except AttributeError:

651 if K_2_0 or _K_2_0: # geographiclib 2.0

652 _r, _360 = fremainder, _360_0

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

654 _r( deg, _360))

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

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

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

658 else:

659 _n = _norm180

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

661 d = _n(d)

662 if t > 0 and d == _180_0:

663 d = _N_180_0

664 d, t = _sum2(d, t)

665 return d, t

666

667

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

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

670

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

672 '''

673 try:

674 return _wrapped.Math.LatFix(deg)

675 except AttributeError:

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

677

678

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

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

681

682 @return: C{True} if finite.

683 '''

684 try:

685 return _wrapped.Math.isfinite(x)

686 except AttributeError:

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

688

689

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

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

692

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

694 '''

695 try:

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

697 except AttributeError:

698 return norm2(x, y)

699

700

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

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

703

704 @return: Reduced angle C{degrees}.

705 '''

706 try:

707 return _wrapped.Math.AngNormalize(deg)

708 except AttributeError:

709 d = fremainder(deg, _360_0)

710 if d in (_180_0, _N_180_0):

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

712 return d

713

714

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

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

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

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

719 or C{geodesicw.Geodesic} instance.

720 '''

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

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

723

724 if _MODS.booleans.isBoolean(points):

725 # recursive call for each boolean clip

726

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

728 return _polygon(geodesic, clip, *args)

729

730 if not closed: # closed only

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

732

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

734

735 gP = geodesic.Polygon(line)

736 _A = gP.AddPoint

737

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

739 p1 = p0 = Ps[0]

740

741 # note, lon deltas are unrolled, by default

742 _A(p1.lat, p1.lon)

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

744 if wrap and not Ps.looped:

745 p2 = _unrollon(p1, p2)

746 _A(p2.lat, p2.lon)

747 p1 = p2

748 if closed and line and p1 != p0:

749 _A(p0.lat, p0.lon)

750

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

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

753

754

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

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

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

758

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

760 '''

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

762# try:

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

764# except AttributeError:

765# s, t = cs[i], _0_0

766# for c in cs[i+1:j]:

767# s, t = _sum2_(s * x, t * x, c)

768# return s # + t

769 s = cs[i]

770 i += 1

771 if x and i < j:

772 s, _ = _sum2_(s, _0_0, x=x, *cs[i:j])

773 return s # + t

774

775

776 def _remainder(x, y):

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

778

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

780 '''

781 try:

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

783 except AttributeError:

784 return fremainder(x, y)

785

786

787 if _K_2_0:

788 from math import cos as _cos, sin as _sin

789

790 def _sincos2(rad):

791 return _sin(rad), _cos(rad)

792

793 _signBit = _MODS.basics.signBit

794 else:

795 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared

796

797 def _signBit(x):

798 '''(INTERNAL) GeographicLib 1.52-.

799 '''

800 return x < 0

801

802

803 def _sincos2d(deg):

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

805

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

807 '''

808 try:

809 return _wrapped.Math.sincosd(deg)

810 except AttributeError:

811 return sincos2d(deg)

812

813

814 def _sincos2de(deg, t):

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

816

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

818 '''

819 try:

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

821 except AttributeError:

822 return sincos2d(deg, adeg=t)

823

824

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

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

827

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

829

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

831

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

833 '''

834 try:

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

836 except AttributeError:

837 s = u + v

838 r = s - v

839 t = s - r

840 # if Algorithm_3_1:

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

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

843 # r -= u

844 # t -= v

845 # t += r

846 # t = -t

847 # else:

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

849 return s, t

850

851

852 def _sum2_(s, t, *vs, **x):

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

854

855 @kwarg x: Optional polynomial C{B{x}=1} (C{scalar}).

856

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

858

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

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

861 function C{_sum2} above.

862

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

864 '''

865 x = _xkwds_get1(x, x=_1_0)

866 p = x != _1_0

867

868 _s2, _u0 = _sum2, unsigned0

869 for v in vs:

870 if p:

871 s *= x

872 t *= x

873 if v:

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

875 if s:

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

877 if s:

878 t += u # accumulate u into t

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

880# raise _AssertionError(t=t, txt_not_=_0_)

881 else:

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

883 else:

884 s, t = _u0(t), u

885 return s, t

886

887

888 def _tand(x):

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

890 '''

891 try:

892 return _wrapped.Math.tand(x)

893 except AttributeError:

894 return tand(x)

895

896

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

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

899

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

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

902 '''

903 if wrap:

904 d, t = _diff182(lon1, lon2)

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

906 else:

907 lon2 = _norm180(lon2)

908 return (lon2 - lon1), lon2

909

910

911 def _unsigned2(x):

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

913 '''

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

915

916

917__all__ += _ALL_DOCS(_CapsBase)

918

919# **) MIT License

920#

921# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.

922#

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

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

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

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

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

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

929#

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

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

932#

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

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

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

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

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

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

939# OTHER DEALINGS IN THE SOFTWARE.