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

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

9 C{attribute} name.

10

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

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

13 and newer transcoding, otherwise C{1.52} 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{Intersector} -- U{Intersect

62 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Intersect.html>} from I{GeographicLib 2.3+}

63

64 - L{JacobiConformal} -- U{JacobiConformal

65 <https://geographiclib.sourceforge.io/C++/doc/classGeographicLib_1_1experimental_1_1JacobiConformal.html>}

66

67 - L{KTransverseMercator} - U{TransverseMercator

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

69

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

71 classGeographicLib_1_1LocalCartesian.html>}

72

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

74

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

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

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

78

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

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

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

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

83 from I{GeographicLib 2.0}

84

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

86

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

88

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

90

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

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

93

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

95

96 2. These C{pygeodesy} modules and classes

97

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

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

100 L{GnomonicKarney}, L{HeightIDWkarney}, L{Intersectool}

101

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

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

104 U{IntersectTool<https://GeographicLib.SourceForge.io/C++/doc/IntersectTool.1.html>} or

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

106

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

108

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

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

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

112

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

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

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

116

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

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

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

120

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

122

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

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

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

126

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

128

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

130

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

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

133

134 5. Spherical functions

135

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

137

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

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

140'''

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

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

143

144 from pygeodesy.basics import _copysign, isint, neg, unsigned0, _xgeographiclib, \

145 _zip, _version_info

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

147 _1_16th, _1_0, _2_0, _180_0, _N_180_0, _360_0

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

149 from pygeodesy.fmath import cbrt, fremainder, norm2

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

151 from pygeodesy.interns import NN, _2_, _a12_, _area_, _azi1_, _azi2_, _azi12_, \

152 _composite_, _lat1_, _lat2_, _lon1_, _lon2_, \

153 _m12_, _M12_, _M21_, _number_, _s12_, _S12_, \

154 _UNDER_, _X_, _BAR_ # PYCHOK used!

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

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

157 from pygeodesy.props import deprecated_method, Property_RO, property_ROnce

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

159 Meter as _M, Meter2 as _M2, Number_

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

161

162# from math import fabs # from .utily

163

164__all__ = _ALL_LAZY.karney

165__version__ = '24.07.16'

166

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

168_perimeter_ = 'perimeter'

169

170

171 class _GTuple(_NamedTuple): # in .testNamedTuples

172 '''(INTERNAL) Helper.

173 '''

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

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

176

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

178 '''

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

180 if updates:

181 r.update(updates)

182 if self._iteration is not None:

183 r._iteration = self._iteration

184 return r

185

186

187 class _Lat(Lat):

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

189 '''

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

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

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

193

194

195 class _Lon(Lon):

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

197 '''

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

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

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

201

202

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

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

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

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

207 '''

208 _Names_ = (_number_, _perimeter_, _area_)

209 _Units_ = ( Number_, _M, _M2)

210

211

212 class Caps(object):

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

214 '''

215 EMPTY = 0 # formerly aka NONE

216 _CAP_1 = 1 << 0 # for goedesici/-w

217 _CAP_1p = 1 << 1 # for goedesici/-w

218 _CAP_2 = 1 << 2

219 _CAP_3 = 1 << 3 # for goedesici/-w

220 _CAP_4 = 1 << 4 # for goedesicw

221 _CAP_ALL = 0x1F

222# _CAP_MASK = _CAP_ALL

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

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

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

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

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

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

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

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

231

232 STANDARD = AZIMUTH | DISTANCE | LATITUDE | LONGITUDE

233

234 _DIRECT3 = AZIMUTH | LATITUDE | LONGITUDE # for goedesicw only

235 _INVERSE3 = AZIMUTH | DISTANCE # for goedesicw only

236 _STD = STANDARD # for goedesicw only

237 _STD_LINE = _STD | DISTANCE_IN # for goedesici/-w

238

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

240 LONG_UNROLL = 1 << 15 # unroll C{lon2} in .Direct and .Position

241# = 1 << 16 # unused

242 LINE_OFF = 1 << 17 # Line without updates from parent geodesic or rhumb

243 REVERSE2 = 1 << 18 # reverse C{azi2}

244 ALL = 0x7F80 | _CAP_ALL # without LONG_UNROLL, LINE_OFF, REVERSE2 and _DEBUG_*

245

246 LATITUDE_LONGITUDE = LATITUDE | LONGITUDE

247 LATITUDE_LONGITUDE_AREA = LATITUDE | LONGITUDE | AREA

248

249 AZIMUTH_DISTANCE = AZIMUTH | DISTANCE

250 AZIMUTH_DISTANCE_AREA = AZIMUTH | DISTANCE | AREA

251

252 _SALP_CALPs_ = 1 << 19 # (INTERNAL) GeodesicExact._GenInverse

253

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

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

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

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

258 _DEBUG_ALL = _DEBUG_AREA | _DEBUG_DIRECT | _DEBUG_INVERSE | \

259 _DEBUG_LINE | _SALP_CALPs_

260

261 _OUT_ALL = ALL # see geographiclib.geodesiccapabilities.py

262 _OUT_MASK = ALL | LONG_UNROLL | REVERSE2 | _DEBUG_ALL

263

264 _AZIMUTH_LATITUDE_LONGITUDE = AZIMUTH | LATITUDE | LONGITUDE

265 _AZIMUTH_LATITUDE_LONG_UNROLL = AZIMUTH | LATITUDE | LONG_UNROLL

266 _DEBUG_DIRECT_LINE = _DEBUG_DIRECT | _DEBUG_LINE

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

268 _REDUCEDLENGTH_GEODESICSCALE = REDUCEDLENGTH | GEODESICSCALE

269# _REDUCEDLENGTH_GEODESICSCALE_DISTANCE = REDUCEDLENGTH | GEODESICSCALE | DISTANCE

270

271 def items(self):

272 '''Yield all I{public} C{Caps} as 2-tuple C{(NAME, mask)}.

273 '''

274 for n, C in Caps.__class__.__dict__.items():

275 if isint(C) and not n.startswith(_UNDER_) \

276 and n.replace(_UNDER_, NN).isupper():

277 yield n, C

278

279 def toStr(self, Cask, sep=_BAR_):

280 '''Return C{Caps} or an C{outmask} as C{str} or tuple of C{str}s.

281 '''

282 s = (n for n, C in sorted(Caps.items())

283 if C and (Cask & C) == C) # and int1s(C) <= 3

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

285

286 Caps = Caps() # PYCHOK singleton

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

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

289

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

291

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

293

294 C{DISTANCE} - compute distance C{s12} and angular distance C{a12},

295

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

297

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

299

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

301

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

303

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

305

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

307

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

309

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

311

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

313

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

315

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

317

318_key2Caps = dict(a12 =Caps.DISTANCE, # in GDict._unCaps

319 azi2=Caps.AZIMUTH,

320 lat2=Caps.LATITUDE,

321 lon2=Caps.LONGITUDE,

322 m12 =Caps.REDUCEDLENGTH,

323 M12 =Caps.GEODESICSCALE,

324 M21 =Caps.GEODESICSCALE,

325 s12 =Caps.DISTANCE,

326 S12 =Caps.AREA)

327

328

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

330 '''(INTERNAL) Base class for C{Geodesic*}, C{Geodesic*Exact}, C{Intersectool} and C{Rhumb*Base}.

331 '''

332 ALL = Caps.ALL

333 AREA = Caps.AREA

334 AZIMUTH = Caps.AZIMUTH

335 DISTANCE = Caps.DISTANCE

336 DISTANCE_IN = Caps.DISTANCE_IN

337 EMPTY = Caps.EMPTY # aka NONE

338 GEODESICSCALE = Caps.GEODESICSCALE

339 LATITUDE = Caps.LATITUDE

340 LINE_CAPS = Caps.LINE_CAPS

341 LINE_OFF = Caps.LINE_OFF

342 LONGITUDE = Caps.LONGITUDE

343 LONG_UNROLL = Caps.LONG_UNROLL

344 REDUCEDLENGTH = Caps.REDUCEDLENGTH

345 STANDARD = Caps.STANDARD

346 _STD_LINE = Caps._STD_LINE # for geodesici

347

348 _caps = 0 # None

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

350

351 @Property_RO

352 def caps(self):

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

354 '''

355 return self._caps

356

357 def caps_(self, caps):

358 '''Check the available capabilities.

359

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

361 for all capabilities to be checked.

362

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

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

365 '''

366 caps &= Caps._OUT_ALL

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

368

369 @property

370 def debug(self):

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

372 '''

373 return bool(self._debug)

374

375 @debug.setter # PYCHOK setter!

376 def debug(self, debug):

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

378 more details in L{GDict} results.

379 '''

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

381

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

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

384 '''

385 if iter is not None:

386 self._iteration = r._iteration = iter

387 return r

388

389

390 class Direct9Tuple(_GTuple):

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

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

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

394 C{meter} I{squared}.

395 '''

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

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

398

399

400 class GDict(ADict): # XXX _NamedDict

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

402

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

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

405 '''

406 def toDirect9Tuple(self, dflt=NAN):

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

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

409

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

411

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

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

414 '''

415 return self._toTuple(Direct9Tuple, dflt)

416

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

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

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

420 result.

421

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

423

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

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

426 '''

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

428

429 def toInverse10Tuple(self, dflt=NAN):

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

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

432

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

434

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

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

437 '''

438 return self._toTuple(Inverse10Tuple, dflt)

439

440 def _toNAN(self, outmask): # .GeodesicLineExact._GenPosition

441 '''(INTERNAL) Convert this C{GDict} to all C{NAN}s.

442 '''

443 d = dict((n, NAN) for n in GeodSolve12Tuple._Names_)

444 return self.set_(**d)._unCaps(outmask)

445

446 @deprecated_method

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

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

449

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

451 '''

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

453

454 def toRhumb8Tuple(self, dflt=NAN):

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

456

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

458

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

460 azi12, s12, S12, a12)}.

461 '''

462 return self._toTuple(Rhumb8Tuple, dflt)

463

464 def toRhumbSolve7Tuple(self, dflt=NAN):

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

466

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

468

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

470 azi12, s12, S12)}.

471 '''

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

473

474 def _toTuple(self, nTuple, dflt):

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

476 '''

477 _g = getattr

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

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

480

481 def _2X(self, gl, _2X=_X_): # .Intersectool, .Intersector

482 '''(INTERNAL) Rename C{-2} attr to C{-X} or C{-M}.

483 '''

484 X = GDict(self)

485 for n in (_lat2_, _lon2_, _azi2_, _s12_, _a12_):

486 if n in X: # X._X = X._2

487 X[n[:-1] + _2X] = X.pop(n)

488 v = getattr(gl, n, X)

489 if v is not X: # X._2 = gl._2

490 X[n] = v

491 return X

492

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

494 '''(INTERNAL) Remove superfluous items.

495 '''

496 for k, C in _key2Caps.items():

497 if k in self and (outmask & C) != C:

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

499 return self

500

501

502 class GeodSolve12Tuple(_GTuple):

503 '''12-Tuple C{(lat1, lon1, azi1, lat2, lon2, azi2, s12, a12, m12, M12, M21, S12)} with

504 angles C{lat1}, C{lon1}, C{azi1}, C{lat2}, C{lon2} and C{azi2} and arc C{a12} all in

505 C{degrees}, initial C{azi1} and final C{azi2} forward azimuths, distance C{s12} and

506 reduced length C{m12} in C{meter}, area C{S12} in C{meter} I{squared} and geodesic

507 scale factors C{M12} and C{M21}, both C{scalar}, see U{GeodSolve

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

509 '''

510 # from GeodSolve --help option -f ... lat1 lon1 azi1 lat2 lon2 azi2 s12 a12 m12 M12 M21 S12

511 _Names_ = (_lat1_, _lon1_, _azi1_, _lat2_, _lon2_, _azi2_, _s12_, _a12_, _m12_, _M12_, _M21_, _S12_)

512 _Units_ = (_Lat, _Lon, _Azi, _Lat, _Lon, _Azi, _M, _Deg, _Pass, _Pass, _Pass, _M2)

513

514

515 class Inverse10Tuple(_GTuple):

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

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

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

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

520 '''

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

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

523

524 def toGDict(self, **updates):

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

526

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

528 '''

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

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

531 **updates) # PYCHOK indent

532

533

534 class _kWrapped(object): # in .geodesicw

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

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

537 '''

538

539 @property_ROnce

540 def geographiclib(self):

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

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

543 otherwise raise a C{LazyImportError}.

544 '''

545 g = _xgeographiclib(self.__class__.__module__, 1, 49)

546 from geographiclib.geodesic import Geodesic

547 g.Geodesic = Geodesic

548 from geographiclib.geodesicline import GeodesicLine

549 g.GeodesicLine = GeodesicLine

550 from geographiclib.geomath import Math

551 g.Math = Math

552 return g

553

554 @property_ROnce

555 def Math(self):

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

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

558 otherwise C{None}.

559 '''

560 try:

561 g = self.geographiclib

562 M = g.Math

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

564 if _K_2_0:

565 M = None

566# elif not _K_2_0: # XXX set 2.0?

567# _K_2_0 = True

568 except (AttributeError, ImportError):

569 M = None

570 return M

571

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

573

574

575 class Rhumb8Tuple(_GTuple):

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

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

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

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

580 '''

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

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

583

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

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

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

587

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

589 @kwarg a12_azi1_azi2_m12_M12_M21: Optional keyword arguments

590 to specify or override L{Inverse10Tuple} items.

591

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

593 m12, M12, M21, S12)}

594 '''

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

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

597 if a12_azi1_azi2_m12_M12_M21:

598 d.update(a12_azi1_azi2_m12_M12_M21)

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

600

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

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

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

604

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

606 @kwarg a12_m12_M12_M21_salp1_calp1_salp2_calp2: Optional keyword

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

608

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

610 m12, M12, M21, S12)}.

611 '''

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

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

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

615 if a12_m12_M12_M21_salp1_calp1_salp2_calp2:

616 d.update(a12_m12_M12_M21_salp1_calp1_salp2_calp2)

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

618

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

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

621 '''

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

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

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

625

626 @deprecated_method

627 def _to7Tuple(self):

628 '''DEPRECATED, do not use!'''

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

630

631

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

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

634

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

636

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

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

639 '''

640 try:

641 return _wrapped.Math.AngRound(x)

642 except AttributeError:

643 if x:

644 y = _1_16th - fabs(x)

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

646 x = _copysign(_1_16th - y, x)

647 else:

648 x = _0_0 # -0 to 0

649 return x

650

651

652 def _atan2d(y, x):

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

654 '''

655 try:

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

657 except AttributeError:

658 return atan2d(y, x)

659

660

661 def _cbrt(x):

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

663 '''

664 try:

665 return _wrapped.Math.cbrt(x)

666 except AttributeError:

667 return cbrt(x)

668

669

670 def _copyBit(x, y):

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

672 '''

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

674

675

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

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

678 '''

679 r = cx - sx

680 if r:

681 r *= (cx + sx) * _2_0

682 return r

683

684

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

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

687

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

689 '''

690 try:

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

692 except AttributeError:

693 if K_2_0 or _K_2_0: # geographiclib 2.0

694 _r, _360 = fremainder, _360_0

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

696 _r( deg, _360))

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

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

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

700 else:

701 _n = _norm180

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

703 d = _n(d)

704 if t > 0 and d == _180_0:

705 d = _N_180_0

706 d, t = _sum2(d, t)

707 return d, t

708

709

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

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

712

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

714 '''

715 try:

716 return _wrapped.Math.LatFix(deg)

717 except AttributeError:

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

719

720

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

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

723

724 @return: C{True} if finite.

725 '''

726 try:

727 return _wrapped.Math.isfinite(x)

728 except AttributeError:

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

730

731

732 def _llz2gl(gl, **llz2): # see .geodesici._llz2G

733 '''(INTERNAL) Set C{gl.lat2, .lon2, .azi2} from C{llz2}.

734 '''

735 if llz2:

736 for n in (_lat2_, _lon2_, _azi2_): # _lat1_, _lon1_, _azi1_

737 v = llz2.get(n, None)

738 if v is not None:

739 setattr(gl, n, v)

740 return gl

741

742

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

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

745

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

747 '''

748 try:

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

750 except AttributeError:

751 return norm2(x, y)

752

753

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

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

756

757 @return: Reduced angle C{degrees}.

758 '''

759 try:

760 return _wrapped.Math.AngNormalize(deg)

761 except AttributeError:

762 d = fremainder(deg, _360_0)

763 if d in (_180_0, _N_180_0):

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

765 return d

766

767

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

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

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

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

772 or C{geodesicw.Geodesic} instance.

773 '''

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

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

776

777 if _MODS.booleans.isBoolean(points):

778 # recursive call for each boolean clip

779

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

781 return _polygon(geodesic, clip, *args)

782

783 if not closed: # closed only

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

785

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

787

788 gP = geodesic.Polygon(line)

789 _A = gP.AddPoint

790

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

792 p1 = p0 = Ps[0]

793

794 # note, lon deltas are unrolled, by default

795 _A(p1.lat, p1.lon)

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

797 if wrap and not Ps.looped:

798 p2 = _unrollon(p1, p2)

799 _A(p2.lat, p2.lon)

800 p1 = p2

801 if closed and line and p1 != p0:

802 _A(p0.lat, p0.lon)

803

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

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

806

807

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

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

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

811

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

813 '''

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

815# try:

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

817# except AttributeError:

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

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

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

821# return s # + t

822 s = cs[i]

823 i += 1

824 if x and i < j:

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

826 return s # + t

827

828

829 def _remainder(x, y):

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

831

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

833 '''

834 try:

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

836 except AttributeError:

837 return fremainder(x, y)

838

839

840 if _K_2_0:

841 from math import cos as _cos, sin as _sin

842

843 def _sincos2(rad):

844 return _sin(rad), _cos(rad)

845

846 _signBit = _MODS.basics.signBit

847 else:

848 _sincos2 = _MODS.utily.sincos2 # PYCHOK shared

849

850 def _signBit(x):

851 '''(INTERNAL) GeographicLib 1.52-.

852 '''

853 return x < 0

854

855

856 def _sincos2d(deg):

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

858

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

860 '''

861 try:

862 return _wrapped.Math.sincosd(deg)

863 except AttributeError:

864 return sincos2d(deg)

865

866

867 def _sincos2de(deg, t):

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

869

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

871 '''

872 try:

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

874 except AttributeError:

875 return sincos2d(deg, adeg=t)

876

877

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

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

880

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

882

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

884

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

886 '''

887 try:

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

889 except AttributeError:

890 s = u + v

891 r = s - v

892 t = s - r

893 # if Algorithm_3_1:

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

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

896 # r -= u

897 # t -= v

898 # t += r

899 # t = -t

900 # else:

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

902 return s, t

903

904

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

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

907

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

909

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

911

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

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

914 function C{_sum2} above.

915

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

917 '''

918 x = _xkwds_get1(x, x=_1_0)

919 p = x != _1_0

920

921 _s2, _u0 = _sum2, unsigned0

922 for v in vs:

923 if p:

924 s *= x

925 t *= x

926 if v:

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

928 if s:

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

930 if s:

931 t += u # accumulate u into t

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

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

934 else:

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

936 else:

937 s, t = _u0(t), u

938 return s, t

939

940

941 def _tand(x):

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

943 '''

944 try:

945 return _wrapped.Math.tand(x)

946 except AttributeError:

947 return tand(x)

948

949

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

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

952

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

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

955 '''

956 if wrap:

957 d, t = _diff182(lon1, lon2)

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

959 else:

960 lon2 = _norm180(lon2)

961 return (lon2 - lon1), lon2

962

963

964 def _unsigned2(x):

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

966 '''

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

968

969

970__all__ += _ALL_DOCS(_CapsBase)

971

972# **) MIT License

973#

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

975#

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

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

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

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

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

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

982#

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

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

985#

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

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

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

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

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

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

992# OTHER DEALINGS IN THE SOFTWARE.