1

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

3

4 u'''I{Charles F.F. Karney}'s U{geographiclib<https://PyPI.org/project/geographiclib>} C{geodesic}, wrapped.

5

6 Wrapper around Python classes C{Geodesic} and C{GeodesicLine} and several C{Math} functions from

7 I{Karney}'s Python package U{geographiclib<https://PyPI.org/project/geographiclib>}, provided

8 that package is installed.

9

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

11 by C{key} or by C{attribute} name.

12

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

14 L{pygeodesy.geodesicx} will use U{GeographicLib 2.0<https://GeographicLib.SourceForge.io/C++/doc/>}

15 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/

25 classGeographicLib_1_1AlbersEqualArea.html>}

26

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

28 classGeographicLib_1_1CassiniSoldner.html>}

29

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

31 classGeographicLib_1_1Geocentric.html>}

32

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

34 classGeographicLib_1_1EllipticFunction.html>}

35

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

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

38

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

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

41

42 - L{GeodesicAreaExact}, L{PolygonArea} -- U{PolygonArea<https://GeographicLib.SourceForge.io/

43 html/classGeographicLib_1_1PolygonAreaT.html>}

44

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

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

47 classGeographicLib_1_1GeodesicLineExact.html>}

48

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

50

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

52 classGeographicLib_1_1Georef.html>}

53

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

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

56

57 - L{JacobiConformal} -- U{JacobiConformal

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

59

60 - L{KTransverseMercator} - U{TransverseMercator

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

62

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

64 classGeographicLib_1_1LocalCartesian.html>}

65

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

67

68 - L{Rhumb}, L{RhumbLine} -- U{Rhumb<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Rhumb.html>},

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

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

71

72 - L{Ups} -- U{PolarStereographic<https://GeographicLib.SourceForge.io/C++/doc/

73 classGeographicLib_1_1PolarStereographic.html>}

74

75 - L{Utm} -- U{TransverseMercator<https://GeographicLib.SourceForge.io/C++/doc/

76 classGeographicLib_1_1TransverseMercator.html>}

77

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

79 classGeographicLib_1_1UTMUPS.html>}

80

81 - L{pygeodesy.atand}, L{pygeodesy.atan2d}, L{pygeodesy.sincos2}, L{pygeodesy.sincos2d}, L{pygeodesy.tand} -- U{

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

83

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

85

86 2. These C{pygeodesy} modules and classes

87

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

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

90 L{GnomonicKarney}, L{HeightIDWkarney}

91

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

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

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

95

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

97

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

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

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

101

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

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

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

105

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

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

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

109

110 - L{RhumbLine.intersection2}, L{RhumbLine.nearestOn4}

111

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

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

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

115

116 4. Spherical functions

117

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

119

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

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

122'''

123

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

125 _xImportError, _xversion_info, _xinstanceof, \

126 _zip, isodd # PYCHOK shared

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

128 _180_0, _N_180_0, _360_0

129 from pygeodesy.datums import _a_ellipsoid, _WGS84

130 from pygeodesy.errors import _ValueError, _xkwds, _xkwds_get

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

132 from pygeodesy.interns import NN, _2_, _a12_, _area_, _azi2_, _composite_, _DOT_, \

133 _lat2_, _lon2_, _m12_, _M12_, _M21_, _number_, _s12_, \

134 _S12_, _UNDER

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

136 from pygeodesy.named import callername, classname, _Dict, _NamedBase, \

137 _NamedTuple, _Pass

138 from pygeodesy.namedTuples import Destination3Tuple, Distance3Tuple

139 from pygeodesy.props import deprecated_method, Property, Property_RO, property_RO

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

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

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

143 Precision_, _1mm as _TOL_M # PYCHOK shared

144 from pygeodesy.utily import atan2d, fabs, sincos2d, tand, unroll180, wrap360

145

146# from math import fabs # from .utily

147

148__all__ = _ALL_LAZY.karney

149__version__ = '23.03.30'

150

151_EWGS84 = _WGS84.ellipsoid # PYCHOK in .geodesicx.gx, .ktm, .rhumbx, .solveBase

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

153_perimeter_ = 'perimeter'

154

155

156 class _GTuple(_NamedTuple): # in .testNamedTuples

157 '''(INTERNAL) Helper.

158 '''

159 def toGDict(self, **updates):

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

161

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

163 '''

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

165 if updates:

166 r.update(updates)

167 if self._iteration is not None:

168 r._iteration = self._iteration

169 return r

170

171

172 class _Lat(Lat):

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

174 '''

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

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

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

178

179

180 class _Lon(Lon):

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

182 '''

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

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

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

186

187

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

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

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

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

192 '''

193 _Names_ = (_number_, _perimeter_, _area_)

194 _Units_ = ( Number_, _M, _M2)

195

196

197 class Caps(object): # PYCHOK

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

199 '''

200 EMPTY = 0 # formerly aka NONE

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

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

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

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

205 DISTANCE_IN = 1 << 11 # allow distance C{s12} in Direct

206 REDUCEDLENGTH = 1 << 12 # compute reduced length C{m12}

207 GEODESICSCALE = 1 << 13 # compute geodesic scales C{M12} and C{M21}

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

209

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

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

212

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

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

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

216

217 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE

218 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA

219

220 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE

221 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA

222

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

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

225

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

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

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

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

230 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \

231 _DEBUG_LINE | _ANGLE_ONLY | _SALPs_CALPs

232 _OUT_ALL = ALL

233 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL

234

235 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE

236 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE

237 _DISTANCE_IN_OUT = DISTANCE_IN & _OUT_MASK

238 _LINE = AZIMUTH | LATITUDE | LONG_UNROLL

239 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE

240# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE

241

242 Caps = Caps() # PYCHOK singleton

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

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

245

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

247

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

249

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

251

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

253

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

255

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

257

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

259

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

261

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

263

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

265

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

267

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

269

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

271

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

273

274

275 class _CapsBase(_NamedBase): # in .geodesicx.gxbases, .rhumbx

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

277 '''

278 ALL = Caps.ALL

279 AREA = Caps.AREA

280 AZIMUTH = Caps.AZIMUTH

281 DISTANCE = Caps.DISTANCE

282 DISTANCE_IN = Caps.DISTANCE_IN

283 EMPTY = Caps.EMPTY # aka NONE

284 GEODESICSCALE = Caps.GEODESICSCALE

285 LATITUDE = Caps.LATITUDE

286 LINE_OFF = Caps.LINE_OFF

287 LONGITUDE = Caps.LONGITUDE

288 LONG_UNROLL = Caps.LONG_UNROLL

289 REDUCEDLENGTH = Caps.REDUCEDLENGTH

290 STANDARD = Caps.STANDARD

291

292 _caps = 0 # None

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

294

295 @Property_RO

296 def caps(self):

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

298 '''

299 return self._caps

300

301 def caps_(self, caps):

302 '''Check the available capabilities.

303

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

305 for all capabilities to be checked.

306

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

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

309 '''

310 caps &= Caps._OUT_ALL

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

312

313 @property

314 def debug(self):

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

316 '''

317 return bool(self._debug)

318

319 @debug.setter # PYCHOK setter!

320 def debug(self, debug):

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

322 more details in L{GDict} results.

323 '''

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

325

326 def _iter2tion(self, r, s):

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

328 '''

329 i = _xkwds_get(s, iter=None)

330 if i is not None:

331 self._iteration = r._iteration = i

332 return r

333

334

335 class Direct9Tuple(_GTuple):

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

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

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

339 C{meter} I{squared}.

340 '''

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

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

343

344

345 class GDict(_Dict): # XXX _NamedDict

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

347 to the C{dict} items.

348

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

350 L{GDict} instance.

351 '''

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

353

354 @property_RO

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

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

357 C{None} if not available/applicable.

358 '''

359 return self._iteration

360

361 def toDirect9Tuple(self, dflt=NAN):

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

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

364

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

366

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

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

369 '''

370 return self._toTuple(Direct9Tuple, dflt)

371

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

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

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

375 result.

376

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

378

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

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

381 '''

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

383

384 def toInverse10Tuple(self, dflt=NAN):

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

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

387

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

389

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

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

392 '''

393 return self._toTuple(Inverse10Tuple, dflt)

394

395 @deprecated_method

396 def toRhumb7Tuple(self, dflt=NAN):

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

398

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

400 '''

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

402

403 def toRhumb8Tuple(self, dflt=NAN):

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

405

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

407

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

409 azi12, s12, S12, a12)}.

410 '''

411 return self._toTuple(_MODS.rhumbx.Rhumb8Tuple, dflt)

412

413 def toRhumbSolve7Tuple(self, dflt=NAN):

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

415

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

417

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

419 azi12, s12, S12)}.

420 '''

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

422

423 def _toTuple(self, nTuple, dflt):

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

425 '''

426 _g = getattr

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

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

429

430

431 class GeodesicError(_ValueError):

432 '''Error raised for L{pygeodesy.geodesicx} lack of convergence

433 or other L{pygeodesy.geodesicx} or L{pygeodesy.karney} issues.

434 '''

435 pass

436

437

438 class Inverse10Tuple(_GTuple):

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

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

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

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

443 '''

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

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

446

447 def toGDict(self, **updates):

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

449

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

451 '''

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

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

454 **updates) # PYCHOK indent

455

456

457 class _Wrapped(object):

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

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

460 '''

461

462 @Property_RO # MCCABE 24

463 def Geodesic(self):

464 '''Get the I{wrapped} C{Geodesic} class, provided the U{geographiclib

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

466 otherwise an C{ImportError}.

467 '''

468 _Geodesic = self.geographiclib.Geodesic

469 _DIRECT3 = _Geodesic.AZIMUTH | _Geodesic.LATITUDE | _Geodesic.LONGITUDE

470 _INVERSE3 = _Geodesic.AZIMUTH | _Geodesic.DISTANCE

471

472 class Geodesic(_Geodesic):

473 '''I{Karney}'s U{Geodesic<https://GeographicLib.SourceForge.io/C++/doc/

474 python/code.html#geographiclib.geodesic.Geodesic>} wrapper.

475 '''

476 _debug = 0 # like .geodesicx.bases._GeodesicBase

477 _E = _EWGS84

478 LINE_OFF = 0 # in .azimuthal._GnomonicBase and .css.CassiniSoldner

479

480 def __init__(self, a_ellipsoid=_EWGS84, f=None, name=NN): # PYCHOK signature

481 '''New C{Geodesic} instance.

482

483 @arg a_ellipsoid: An ellipsoid (L{Ellipsoid}) or datum

484 (L{Datum}) or the equatorial radius

485 of the ellipsoid (C{meter}).

486 @arg f: The flattening of the ellipsoid (C{scalar}) if

487 B{C{a_ellipsoid}) is specified as C{meter}.

488 @kwarg name: Optional name (C{str}).

489 '''

490 if a_ellipsoid not in (Geodesic._E, None): # spherical OK

491 self._E = _a_ellipsoid(a_ellipsoid, f, name=name) # raiser=NN

492 try:

493 _Geodesic.__init__(self, *self.ellipsoid.a_f)

494 except (TypeError, ValueError) as x:

495 _raiseX(self, x, *self.ellipsoid.a_f)

496

497 Area = _Geodesic.Polygon # like GeodesicExact.Area

498

499 @Property

500 def debug(self):

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

502 '''

503 return bool(self._debug)

504

505 @debug.setter # PYCHOK setter!

506 def debug(self, debug):

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

508 more details in L{GDict} results.

509 '''

510 self._debug = _MODS.geodesicx.Caps._DEBUG_ALL if debug else 0

511

512 def Direct(self, lat1, lon1, azi1, s12, *outmask):

513 '''Return the C{Direct} result.

514 '''

515 try:

516 d = _Geodesic.Direct(self, lat1, lon1, azi1, s12, *outmask)

517 except (TypeError, ValueError) as x:

518 _raiseX(self, x, lat1, lon1, azi1, s12, *outmask)

519 return GDict(d)

520

521 def Direct3(self, lat1, lon1, azi1, s12): # PYCHOK outmask

522 '''Return the destination lat, lon and reverse azimuth

523 (final bearing) in C{degrees}.

524

525 @return: L{Destination3Tuple}C{(lat, lon, final)}.

526 '''

527 d = self.Direct(lat1, lon1, azi1, s12, _DIRECT3)

528 return Destination3Tuple(d.lat2, d.lon2, d.azi2)

529

530 @Property_RO

531 def ellipsoid(self):

532 '''Get this geodesic's ellipsoid (C{Ellipsoid[2]}).

533 '''

534 return self._E

535

536 @Property_RO

537 def f1(self): # in .css.CassiniSoldner.reset

538 '''Get the geodesic's ellipsoid I{1 - flattening} (C{float}).

539 '''

540 return getattr(self, _UNDER(Geodesic.f1.name), self.ellipsoid.f1)

541

542 def _GDictDirect(self, lat, lon, azi, arcmode, s12_a12,

543 outmask=_Geodesic.STANDARD):

544 '''(INTERNAL) Get C{._GenDirect} result as C{GDict}.

545 '''

546 try:

547 t = _Geodesic._GenDirect(self, lat, lon, azi, arcmode, s12_a12, outmask)

548 except (TypeError, ValueError) as x:

549 _raiseX(self, x, lat, lon, azi, arcmode, s12_a12, outmask)

550 return Direct9Tuple(t).toGDict() # *t

551

552 def _GDictInverse(self, lat1, lon1, lat2, lon2, outmask=_Geodesic.STANDARD):

553 '''(INTERNAL) Get C{._GenInverse} result as C{GDict}.

554 '''

555 try:

556 t = _Geodesic._GenInverse(self, lat1, lon1, lat2, lon2, outmask)

557 except (TypeError, ValueError) as x:

558 _raiseX(self, x, lat1, lon1, lat2, lon2, outmask)

559 return Inverse10Tuple(t).toGDict(lon1=lon1, lon2=lon2) # *t

560

561 def Inverse(self, lat1, lon1, lat2, lon2, *outmask):

562 '''Return the C{Inverse} result.

563 '''

564 try:

565 d = _Geodesic.Inverse(self, lat1, lon1, lat2, lon2, *outmask)

566 except (TypeError, ValueError) as x:

567 _raiseX(self, x, lat1, lon1, lat2, lon2, *outmask)

568 return GDict(d)

569

570 def Inverse1(self, lat1, lon1, lat2, lon2, wrap=False):

571 '''Return the non-negative, I{angular} distance in C{degrees}.

572 '''

573 # see .FrechetKarney.distance, .HausdorffKarney._distance

574 # and .HeightIDWkarney._distances

575 _, lon2 = unroll180(lon1, lon2, wrap=wrap) # self.LONG_UNROLL

576 r = self.Inverse(lat1, lon1, lat2, lon2)

577 # XXX self.DISTANCE needed for 'a12'?

578 return fabs(r.a12)

579

580 def Inverse3(self, lat1, lon1, lat2, lon2): # PYCHOK outmask

581 '''Return the distance in C{meter} and the forward and

582 reverse azimuths (initial and final bearing) in C{degrees}.

583

584 @return: L{Distance3Tuple}C{(distance, initial, final)}.

585 '''

586 r = self.Inverse(lat1, lon1, lat2, lon2, _INVERSE3)

587 return Distance3Tuple(r.s12, wrap360(r.azi1), wrap360(r.azi2))

588

589 def Line(self, lat1, lon1, azi1, *caps):

590 '''Set up a L{GeodesicLine} to compute several points on a

591 single geodesic.

592 '''

593 return _wrapped.GeodesicLine(self, lat1, lon1, azi1, *caps)

594

595 # Geodesic.Direct.__doc__ = _Geodesic.Direct.__doc__

596 # Geodesic.Inverse.__doc__ = _Geodesic.Inverse.__doc__

597 # Geodesic.Line.__doc__ = _Geodesic.Line.__doc__

598 return Geodesic

599

600 @Property_RO # MCCABE 16

601 def GeodesicLine(self):

602 '''Get the I{wrapped} C{GeodesicLine} class, provided the U{geographiclib

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

604 otherwise an C{ImportError}.

605 '''

606 _GeodesicLine = self.geographiclib.GeodesicLine

607

608 class GeodesicLine(_GeodesicLine):

609 '''I{Karney}'s U{GeodesicLine <https://GeographicLib.SourceForge.io/C++/doc/

610 python/code.html#geographiclib.geodesicline.GeodesicLine>} wrapper.

611 '''

612 def __init__(self, lat1, lon1, azi1, *caps):

613 try:

614 _GeodesicLine.__init__(self, lat1, lon1, azi1, *caps)

615 except (TypeError, ValueError) as x:

616 _raiseX(self, x, lat1, lon1, azi1, *caps)

617

618 @Property_RO

619 def a1(self):

620 '''Get the I{equatorial arc} (C{degrees}), the arc length between

621 the northward equatorial crossing and point C{(lat1, lon1)}.

622

623 @see: U{EquatorialArc<https://GeographicLib.SourceForge.io/

624 C++/doc/classGeographicLib_1_1GeodesicLine.html>}

625 '''

626 try:

627 return _atan2d(self._ssig1, self._csig1)

628 except AttributeError:

629 return NAN # see .geodesicx.gxline._GeodesicLineExact

630

631 equatorarc = a1

632

633 def ArcPosition(self, a12, *outmask):

634 try:

635 d = _GeodesicLine.ArcPosition(self, a12, *outmask)

636 except (TypeError, ValueError) as x:

637 _raiseX(self, x, a12, *outmask)

638 return GDict(d)

639

640 @Property_RO

641 def azi0(self): # see .css.CassiniSoldner.forward4

642 '''Get the I{equatorial azimuth} (C{degrees}), the azimuth of the

643 geodesic line as it crosses the equator in a northward direction.

644

645 @see: U{EquatorialAzimuth<https://GeographicLib.SourceForge.io/

646 C++/doc/classGeographicLib_1_1GeodesicLine.html>}

647 '''

648 try:

649 return _atan2d(self._salp0, self._calp0)

650 except AttributeError:

651 return NAN # see .geodesicx.gxline._GeodesicLineExact

652

653 equatorazimuth = azi0

654

655 def Position(self, s12, *outmask):

656 try:

657 d = _GeodesicLine.Position(self, s12, *outmask)

658 except (TypeError, ValueError) as x:

659 _raiseX(self, x, s12, *outmask)

660 return GDict(d)

661

662 # GeodesicLine.ArcPosition.__doc__ = _GeodesicLine.ArcPosition.__doc__

663 # GeodesicLine.Position.__doc__ = _GeodesicLine.Position.__doc__

664 return GeodesicLine

665

666 @Property_RO

667 def Geodesic_WGS84(self):

668 '''Get the I{wrapped} C{Geodesic.WGS84} I{instance} provided the

669 U{geographiclib<https://PyPI.org/project/geographiclib>} package

670 is installed, otherwise an C{ImportError}.

671 '''

672 return _EWGS84.geodesic

673

674 @Property_RO

675 def geographiclib(self):

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

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

678 otherwise an C{ImportError}.

679 '''

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

681 from geographiclib.geodesic import Geodesic

682 g.Geodesic = Geodesic

683 from geographiclib.geodesicline import GeodesicLine

684 g.GeodesicLine = GeodesicLine

685 from geographiclib.geomath import Math

686 g.Math = Math

687 return g

688

689 @Property_RO # MCCABE 13

690 def Math(self):

691 '''Get the C{Math} class, provided the U{geographiclib

692 <https://PyPI.org/project/geographiclib>} package is

693 installed, otherwise C{None}.

694 '''

695 try:

696 g = self.geographiclib

697 M = g.Math

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

699 if _K_2_0:

700 M = None

701# elif not _K_2_0: # XXX set 2.0?

702# _K_2_0 = True

703 except (AttributeError, ImportError):

704 M = None

705 return M

706

707_wrapped = _Wrapped() # PYCHOK singleton, .datum, .test/base.py

708

709

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

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

712

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

714

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

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

717 '''

718 try:

719 return _wrapped.Math.AngRound(x)

720 except AttributeError:

721 pass

722 if x:

723 y = _1_16th - fabs(x)

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

725 x = _copysign(_1_16th - y, x)

726 else:

727 x = _0_0 # -0 to 0

728 return x

729

730

731 def _atan2d(y, x):

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

733 '''

734 try:

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

736 except AttributeError:

737 return atan2d(y, x)

738

739

740 def _cbrt(x):

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

742 '''

743 try:

744 return _wrapped.Math.cbrt(x)

745 except AttributeError:

746 return cbrt(x)

747

748

749 def _copyBit(x, y):

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

751 '''

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

753

754

755 def _diff182(deg0, deg):

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

757

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

759 '''

760 try:

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

762 except AttributeError:

763 pass

764 if _K_2_0: # geographiclib 2.0

765 d, t = _sum2(fremainder(-deg0, _360_0),

766 fremainder( deg, _360_0))

767 d, t = _sum2(fremainder( d, _360_0), t)

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

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

770 else:

771 d, t = _sum2(_norm180(-deg0), _norm180(deg))

772 d = _norm180(d)

773 if t > 0 and d == _180_0:

774 d = _N_180_0

775 d, t = _sum2(d, t)

776 return d, t

777

778

779# def _Equidistant(equidistant, exact=False, geodsolve=False):

780# # (INTERNAL) Get the C{EquidistantExact}, C{-GeodSolve} or

781# # C{-Karney} class if B{C{equidistant}} in not callable.

782# if equidistant is None or not callable(equidistant):

783# if exact:

784# equidistant = _MODS.azimuthal.EquidistantExact

785# elif geodsolve:

786# equidistant = _MODS.azimuthal.EquidistantGeodSolve

787# else:

788# equidistant = _MODS.azimuthal.EquidistantKarney

789# return equidistant

790

791

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

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

794

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

796 '''

797 try:

798 return _wrapped.Math.LatFix(deg)

799 except AttributeError:

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

801

802

803 def _isfinite(x): # mimick Math.AngNormalize

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

805

806 @return: C{True} if finite.

807 '''

808 try:

809 return _wrapped.Math.isfinite(x)

810 except AttributeError:

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

812

813

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

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

816

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

818 '''

819 try:

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

821 except AttributeError:

822 return norm2(x, y)

823

824

825 def _norm180(deg): # mimick Math.AngNormalize

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

827

828 @return: Reduced angle C{degrees}.

829 '''

830 try:

831 return _wrapped.Math.AngNormalize(deg)

832 except AttributeError:

833 pass

834 d = fremainder(deg, _360_0)

835 if d in (_180_0, -_180_0):

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

837 return d

838

839

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

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

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

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

844 or C{_wrapped.Geodesic} instance.

845 '''

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

847 raise _ValueError(wrap=wrap)

848

849 if _MODS.booleans.isBoolean(points):

850 # recursive call for each boolean clip

851

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

853 return _polygon(geodesic, clip, *args)

854

855 if not closed: # closed only

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

857

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

859

860 gP = geodesic.Polygon(line)

861 _A = gP.AddPoint

862

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

864 p0 = Ps[0]

865

866 # note, lon deltas are unrolled, by default

867 _A(p0.lat, p0.lon)

868 for p in Ps.iterate(closed=closed):

869 _A(p.lat, p.lon)

870 if closed and line and p != p0:

871 _A(p0.lat, p0.lon)

872

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

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

875

876

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

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

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

880

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

882 '''

883 s, t = cs[i], _0_0

884 _s2_ = _sum2_

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

886 s, t = _s2_(s * x, t * x, c)

887 return s # + t

888

889

890 def _raiseX(inst, x, *args): # PYCHOK no cover

891 '''(INTERNAL) Throw a C{GeodesicError} for C{geographiclib} issue B{C{x}} .

892 '''

893 n = _DOT_(classname(inst), callername(up=2, underOK=True))

894 raise GeodesicError(unstr(n, *args), cause=x)

895

896

897 def _remainder(x, y):

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

899

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

901 '''

902 try:

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

904 except AttributeError:

905 return fremainder(x, y)

906

907

908 if _K_2_0:

909 from pygeodesy.basics import signBit as _signBit

910 from math import cos as _cos, sin as _sin

911

912 def _sincos2(rad):

913 return _sin(rad), _cos(rad)

914

915 else:

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

917

918 def _signBit(x):

919 '''(INTERNAL) GeographicLin 1.52-.

920 '''

921 return x < 0

922

923

924 def _sincos2d(deg):

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

926

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

928 '''

929 try:

930 return _wrapped.Math.sincosd(deg)

931 except AttributeError:

932 return sincos2d(deg)

933

934

935 def _sincos2de(deg, t):

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

937

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

939 '''

940 try:

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

942 except AttributeError:

943 return sincos2d(deg, adeg=t)

944

945

946 def _sum2(u, v): # mimick Math::sum, actually sum2

947 '''Error-free summation like C{Math::sum}.

948

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

950

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

952

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

954 '''

955 try:

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

957 except AttributeError:

958 pass

959 s = u + v

960 r = s - v

961 t = s - r

962 # if Algorithm_3_1:

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

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

965 # r -= u

966 # t -= v

967 # t += r

968 # t = -t

969 # else:

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

971 return s, t

972

973

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

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

976

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

978

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

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

981 function C{_sum2} above.

982

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

984 '''

985 _s2, _u0 = _sum2, unsigned0

986 for v in vs:

987 if v:

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

989 if s:

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

991 if s:

992 t += u # accumulate u into t

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

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

995 else:

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

997 else:

998 s, t = _u0(t), u

999 return s, t

1000

1001

1002 def _tand(x):

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

1004 '''

1005 try:

1006 return _wrapped.Math.tand(x)

1007 except AttributeError:

1008 return tand(x)

1009

1010

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

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

1013

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

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

1016 '''

1017 if wrap:

1018 d, t = _diff182(lon1, lon2)

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

1020 else:

1021 lon2 = _norm180(lon2)

1022 return (lon2 - lon1), lon2

1023

1024

1025 def _unsigned2(x):

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

1027 '''

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

1029

1030

1031__all__ += _ALL_DOCS(_CapsBase)

1032

1033# **) MIT License

1034#

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

1036#

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

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

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

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

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

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

1043#

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

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

1046#

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

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

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

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

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

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

1053# OTHER DEALINGS IN THE SOFTWARE.