Coverage for pygeodesy/geodesicw.py: 90%

221 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-09 11:05 -0400

1 

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

3 

4u'''Wrapper around Python classes C{geodesic.Geodesic} and C{geodesicline.GeodesicLine} from 

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

6that package is installed. 

7 

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

9either by C{key} or by C{attribute} name. 

10 

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

12L{pygeodesy.geodesici}, L{pygeodesy.geodesicx} and L{pygeodesy.karney} will use U{GeographicLib 2.0 

13<https://GeographicLib.SourceForge.io/C++/doc/>} transcoding, otherwise C{1.52} or older. 

14''' 

15 

16from pygeodesy.basics import _copysign, _xinstanceof 

17from pygeodesy.constants import EPS, NAN, _EPSqrt as _TOL, _0_5 

18from pygeodesy.datums import _earth_datum, _WGS84, _EWGS84 

19# from pygeodesy.dms import F_D # from .latlonBase 

20# from pygeodesy.ellipsoids import _EWGS84 # from .datums 

21from pygeodesy.errors import _AssertionError, GeodesicError, \ 

22 IntersectionError 

23from pygeodesy.fsums import Fsum, Fmt, unstr 

24from pygeodesy.internals import _DUNDER_nameof, _under 

25from pygeodesy.interns import NN, _DOT_, _SPACE_, _to_, _too_ 

26from pygeodesy.karney import _atan2d, Caps, Direct9Tuple, GDict, \ 

27 Inverse10Tuple, _kWrapped 

28from pygeodesy.latlonBase import LatLonBase as _LLB, F_D, Radius_ 

29from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS 

30from pygeodesy.named import callername, classname, _name1__, _name2__ 

31from pygeodesy.namedTuples import Destination3Tuple, Distance3Tuple 

32from pygeodesy.props import Property, Property_RO, property_RO, \ 

33 property_ROver 

34# from pygeodesy.streprs import Fmt, unstr # from .fsums 

35# from pygeodesy.units import Radius_ # from .latlonBase 

36from pygeodesy.utily import _unrollon, _Wrap, wrap360, fabs # PYCHOK used! 

37 

38from contextlib import contextmanager 

39# from math import fabs # from .utily 

40 

41__all__ = _ALL_LAZY.geodesicw 

42__version__ = '24.11.02' 

43 

44_plumb_ = 'plumb' 

45_TRIPS = 65 

46 

47 

48class _gWrapped(_kWrapped): 

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

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

51 ''' 

52 

53 @property_ROver # MCCABE 24 

54 def Geodesic(self): 

55 '''Get the I{wrapped} C{geodesic.Geodesic} class from I{Karney}'s Python 

56 U{geographiclib<https://GitHub.com/geographiclib/geographiclib-python>}, 

57 provided the latter is installed. 

58 ''' 

59 _Geodesic = self.geographiclib.Geodesic 

60 if not (Caps.LATITUDE == _Geodesic.LATITUDE and 

61 Caps.LONGITUDE == _Geodesic.LONGITUDE and 

62 Caps.AZIMUTH == _Geodesic.AZIMUTH and 

63 Caps.DISTANCE == _Geodesic.DISTANCE and 

64 Caps.DISTANCE_IN == _Geodesic.DISTANCE_IN and 

65 Caps.REDUCEDLENGTH == _Geodesic.REDUCEDLENGTH and 

66 Caps.GEODESICSCALE == _Geodesic.GEODESICSCALE and 

67 Caps.AREA == _Geodesic.AREA and 

68 Caps.ALL == _Geodesic.ALL): 

69 raise _AssertionError(Caps=bin(Caps.ALL), 

70 Geodesic=bin(_Geodesic.ALL)) 

71 

72 class Geodesic(_Geodesic): 

73 '''I{Wrapper} for I{Karney}'s Python U{geodesic.Geodesic 

74 <https://PyPI.org/project/geographiclib>} class. 

75 ''' 

76 _datum = _WGS84 

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

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

79 _name = NN 

80 

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

82 '''New I{wrapped} C{geodesic.Geodesic} instance. 

83 

84 @arg a_ellipsoid: The equatorial radius I{a} (C{meter}, conventionally), 

85 an ellipsoid (L{Ellipsoid}) or a datum (L{Datum}). 

86 @arg f: The ellipsoid's flattening (C{scalar}), required if B{C{a_ellipsoid}) 

87 is C{meter}, ignored otherwise. 

88 @kwarg name: Optional C{B{name}=NN} (C{str}). 

89 ''' 

90 _earth_datum(self, a_ellipsoid, f=f, **name) # raiser=NN 

91 E = self.ellipsoid 

92 with _wargs(self, *E.a_f, **name) as args: 

93 _Geodesic.__init__(self, *args) 

94 if name: 

95 self._name, _ = _name2__(name, _or_nameof=E) 

96 

97 def ArcDirect(self, lat1, lon1, azi1, a12, outmask=Caps._STD): # PYCHOK no cover 

98 '''Return the C{_Geodesic.ArcDirect} result as L{GDict}. 

99 ''' 

100 with _wargs(self, lat1, lon1, azi1, a12, outmask) as args: 

101 d = _Geodesic.ArcDirect(self, *args) 

102 return GDict(d) 

103 

104 def ArcDirectLine(self, lat1, lon1, azi1, a12, caps=Caps._STD_LINE, **name): # PYCHOK no cover 

105 '''Return the C{_Geodesic.ArcDirectLine} as I{wrapped} C{GeodesicLine}. 

106 ''' 

107 return self._GenDirectLine(lat1, lon1, azi1, True, a12, caps, **name) 

108 

109 Area = _Geodesic.Polygon # like GeodesicExact.Area 

110 

111 @property_RO 

112 def datum(self): 

113 '''Get this geodesic's datum (C{Datum}). 

114 ''' 

115 return self._datum 

116 

117 @Property 

118 def debug(self): 

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

120 ''' 

121 return bool(self._debug) 

122 

123 @debug.setter # PYCHOK setter! 

124 def debug(self, debug): 

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

126 details in L{GDict} results. 

127 ''' 

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

129 

130 def Direct(self, lat1, lon1, azi1, s12=0, outmask=Caps._STD): 

131 '''Return the C{_Geodesic.Direct} result as L{GDict}. 

132 ''' 

133 with _wargs(self, lat1, lon1, azi1, s12, outmask) as args: 

134 d = _Geodesic.Direct(self, *args) 

135 return GDict(d) 

136 

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

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

139 in C{degrees} as L{Destination3Tuple}. 

140 ''' 

141 d = self.Direct(lat1, lon1, azi1, s12, outmask=Caps._DIRECT3) 

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

143 

144 def _DirectLine(self, ll1, azi12, s12=0, **caps_name): 

145 '''(INTERNAL) Short-cut version. 

146 ''' 

147 return self.DirectLine(ll1.lat, ll1.lon, azi12, s12, **caps_name) 

148 

149 def DirectLine(self, lat1, lon1, azi1, s12, caps=Caps._STD_LINE, **name): 

150 '''Return the C{_Geodesic.DirectLine} as I{wrapped} C{GeodesicLine}. 

151 ''' 

152 return self._GenDirectLine(lat1, lon1, azi1, False, s12, caps, **name) 

153 

154 @Property_RO 

155 def ellipsoid(self): 

156 '''Get this geodesic's ellipsoid (C{Ellipsoid}). 

157 ''' 

158 return self.datum.ellipsoid 

159 

160 @property_RO 

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

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

163 ''' 

164 return getattr(self, _under(Geodesic.f1.name), self.ellipsoid.f1) 

165 

166 def _GDictDirect(self, lat, lon, azi, arcmode, s12_a12, outmask=Caps._STD): 

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

168 ''' 

169 with _wargs(self, lat, lon, azi, arcmode, s12_a12, outmask) as args: 

170 t = _Geodesic._GenDirect(self, *args) 

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

172 

173 def _GDictInverse(self, lat1, lon1, lat2, lon2, outmask=Caps._STD): 

174 '''(INTERNAL) Get C{_Geodesic._GenInverse} result as L{Inverse10Tuple}. 

175 ''' 

176 with _wargs(self, lat1, lon1, lat2, lon2, outmask) as args: 

177 t = _Geodesic._GenInverse(self, *args) 

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

179 

180 def _GenDirectLine(self, lat1, lon1, azi1, arcmode, s12_a12, *caps, **name): 

181 '''(INTERNAL) Invoked by C{_Geodesic.DirectLine} and C{-.ArcDirectLine}, 

182 returning the result as a I{wrapped} C{GeodesicLine}. 

183 ''' 

184 with _wargs(self, lat1, lon1, azi1, arcmode, s12_a12, *caps, **name) as args: 

185 t = _Geodesic._GenDirectLine(self, *args) 

186 return self._Line13(t, **name) 

187 

188 def _Inverse(self, ll1, ll2, wrap, **outmask): 

189 '''(INTERNAL) Short-cut version, see .ellipsoidalBaseDI.intersecant2. 

190 ''' 

191 if wrap: 

192 ll2 = _unrollon(ll1, _Wrap.point(ll2)) 

193 return self.Inverse(ll1.lat, ll1.lon, ll2.lat, ll2.lon, **outmask) 

194 

195 def Inverse(self, lat1, lon1, lat2, lon2, outmask=Caps._STD): 

196 '''Return the C{_Geodesic.Inverse} result as L{GDict}. 

197 ''' 

198 with _wargs(self, lat1, lon1, lat2, lon2, outmask) as args: 

199 d = _Geodesic.Inverse(self, *args) 

200 return GDict(d) 

201 

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

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

204 

205 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll 

206 B{C{lat2}} and BC{lon2}} (C{bool}). 

207 ''' 

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

209 # and .HeightIDWkarney._distances 

210 if wrap: 

211 _, lat2, lon2 = _Wrap.latlon3(lat1, lat2, lon2, True) # _Geodesic.LONG_UNROLL 

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

213 # XXX _Geodesic.DISTANCE needed for 'a12'? 

214 return fabs(r.a12) 

215 

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

217 '''Return the distance in C{meter} and the forward and reverse 

218 azimuths in C{degrees} as L{Distance3Tuple}. 

219 ''' 

220 r = self.Inverse(lat1, lon1, lat2, lon2, outmask=Caps._INVERSE3) 

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

222 

223 def _InverseLine(self, ll1, ll2, wrap, **caps_name): 

224 '''(INTERNAL) Short-cut version. 

225 ''' 

226 if wrap: 

227 ll2 = _unrollon(ll1, _Wrap.point(ll2)) 

228 return self.InverseLine(ll1.lat, ll1.lon, ll2.lat, ll2.lon, **caps_name) 

229 

230 def InverseLine(self, lat1, lon1, lat2, lon2, caps=Caps._STD_LINE, **name): 

231 '''Return the C{_Geodesic.InverseLine} as I{wrapped} C{GeodesicLine}. 

232 ''' 

233 with _wargs(self, lat1, lon1, lat2, lon2, caps, **name) as args: 

234 t = _Geodesic.InverseLine(self, *args) 

235 return self._Line13(t, **name) 

236 

237 def Line(self, lat1, lon1, azi1, caps=Caps._STD_LINE, **name): 

238 '''Set up a I{wrapped} C{GeodesicLine} to compute several points 

239 along a single, I{wrapped} (this) geodesic. 

240 ''' 

241 return _wrapped.GeodesicLine(self, lat1, lon1, azi1, caps=caps, **name) 

242 

243 def _Line13(self, t, **name): 

244 '''(INTERNAL) Wrap C{_GeodesicLine}, add distance and arc length 

245 to reference point 3. 

246 ''' 

247 gl = _wrapped.GeodesicLine(self, t.lat1, t.lon1, t.azi1, caps=t.caps, 

248 salp1=t.salp1, calp1=t.calp1, **name) 

249 gl.a13, gl.s13 = t.a13, t.s13 

250 return gl 

251 

252 @property_RO 

253 def name(self): 

254 '''Get the name (C{str}). 

255 ''' 

256 return self._name 

257 

258# Polygon = _Geodesic.Polygon 

259 

260 WGS84 = None # _EWGS84.geodesicw recusion 

261 

262 # Geodesic.ArcDirect.__doc__ = _Geodesic.ArcDirect.__doc__ 

263 # Geodesic.Direct.__doc__ = _Geodesic.Direct.__doc__ 

264 # Geodesic.Inverse.__doc__ = _Geodesic.Inverse.__doc__ 

265 # Geodesic.InverseLine.__doc__ = _Geodesic.InverseLinr.__doc__ 

266 # Geodesic.Line.__doc__ = _Geodesic.Line.__doc__ 

267 return Geodesic # overwrite property_ROver 

268 

269 @property_ROver # MCCABE 16 

270 def GeodesicLine(self): 

271 '''Get the I{wrapped} C{geodesicline.GeodesicLine} class from I{Karney}'s 

272 Python U{geographiclib<https://GitHub.com/geographiclib/geographiclib-python>}, 

273 provided the latter is installed. 

274 ''' 

275 _GeodesicLine = self.geographiclib.GeodesicLine 

276 

277 class GeodesicLine(_GeodesicLine): 

278 '''I{Wrapper} for I{Karney}'s Python U{geodesicline.GeodesicLine 

279 <https://PyPI.org/project/geographiclib>} class. 

280 ''' 

281 _geodesic = None 

282 _name = NN 

283 

284 def __init__(self, geodesic, lat1, lon1, azi1, **caps_name_): # salp1=NAN, calp1=NAN 

285 '''New I{wrapped} C{geodesicline.GeodesicLine} instance. 

286 

287 @arg geodesic: A I{wrapped} C{Geodesic} instance. 

288 @arg lat1: Latitude of the first points (C{degrees}). 

289 @arg lon1: Longitude of the first points (C{degrees}). 

290 @arg azi1: Azimuth at the first points (compass C{degrees360}). 

291 @kwarg caps_name_: Optional keyword arguments C{B{caps}=Caps.STANDARD}, 

292 a bit-or'ed combination of L{Caps} values specifying the 

293 capabilities the C{GeodesicLine} instance should possess, 

294 an optional C{B{name}=NN} plus C{salp1=NAN} and C{calp1=NAN} 

295 for I{INTERNAL} use. 

296 ''' 

297 _xinstanceof(_wrapped.Geodesic, geodesic=geodesic) 

298 with _wargs(self, geodesic, lat1, lon1, azi1, **caps_name_) as args: 

299 name, caps_ = _name2__(caps_name_, _or_nameof=geodesic) 

300 _GeodesicLine.__init__(self, *args, **caps_) # XXX avoid updates? 

301 if name: 

302 self._name = name 

303 self._geodesic = geodesic 

304 

305 @Property_RO 

306 def a1(self): 

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

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

309 

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

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

312 ''' 

313 try: 

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

315 except AttributeError: 

316 return NAN # see .geodesicx.gxline._GeodesicLineExact 

317 

318 equatorarc = a1 

319 

320 def Arc(self): 

321 '''Return the angular distance to point 3 (C{degrees} or C{NAN}). 

322 ''' 

323 return self.a13 

324 

325 def ArcPosition(self, a12, outmask=Caps._STD): 

326 '''Return the position at C{B{a12} degrees} on this line. 

327 

328 @arg a12: Angular distance from this line's first point 

329 (C{degrees}). 

330 

331 @see: Method L{Position} for further details. 

332 ''' 

333 with _wargs(self, a12, outmask) as args: 

334 d = _GeodesicLine.ArcPosition(self, *args) 

335 return GDict(d) 

336 

337 @Property_RO 

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

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

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

341 

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

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

344 ''' 

345 try: 

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

347 except AttributeError: 

348 return NAN # see .geodesicx.gxline._GeodesicLineExact 

349 

350 equatorazimuth = azi0 

351 

352 def Distance(self): 

353 '''Return the distance to reference point 3 (C{meter} or C{NAN}). 

354 ''' 

355 return self.s13 

356 

357 @property_RO 

358 def geodesic(self): 

359 '''Get the I{wrapped} geodesic (L{Geodesic}). 

360 ''' 

361 return self._geodesic 

362 

363 def Intersecant2(self, lat0, lon0, radius, tol=_TOL): 

364 '''Compute the intersection(s) of this geodesic line and a circle. 

365 

366 @arg lat0: Latitude of the circle center (C{degrees}). 

367 @arg lon0: Longitude of the circle center (C{degrees}). 

368 @arg radius: Radius of the circle (C{meter}, conventionally). 

369 @kwarg tol: Convergence tolerance (C{scalar}). 

370 

371 @return: 2-Tuple C{(P, Q)} with both intersections points (representing 

372 a geodesic chord), each a L{GDict} from method L{Position} and 

373 extended to 14 items C{lat1, lon1, azi1, lat2, lon2, azi2, a12, 

374 s12, lat0, lon0, azi0, a02, s02, at} with the circle center 

375 C{lat0}, C{lon0}, azimuth C{azi0} at the intersection, distance 

376 C{a02} in C{degrees} and C{s02} in C{meter} along the geodesic 

377 from the circle center to the intersection C{lat2, lon2} and 

378 the angle C{at} between the geodesic and this line at the 

379 intersection. The I{geodesic} azimuth at the intersection is 

380 C{(at + azi2)}. If this line is tangential to the circle, both 

381 intersections are the same L{GDict} instance. 

382 

383 @raise IntersectionError: The circle and this geodesic line do not 

384 intersect. 

385 

386 @raise UnitError: Invalid B{C{radius}}. 

387 ''' 

388 return _Intersecant2(self, lat0, lon0, radius, tol=tol) 

389 

390 def PlumbTo(self, lat0, lon0, est=None, tol=_TOL): 

391 '''Compute the I{perpendicular} intersection of this geodesic line 

392 with a geodesic from the given point. 

393 

394 @arg lat0: Latitude of the point (C{degrees}). 

395 @arg lon0: Longitude of the point (C{degrees}). 

396 @kwarg est: Optional, initial estimate for the distance C{s12} of 

397 the intersection I{along} this geodesic line (C{meter}). 

398 @kwarg tol: Convergence tolerance (C(meter)). 

399 

400 @return: The intersection point on this geodesic line, a L{GDict} 

401 from method L{Position} extended to 14 items C{lat1, lon1, 

402 azi1, lat2, lon2, azi2, a12, s12, lat0, lon0, azi0, a02, 

403 s02, at} with C{a02} and C{s02} the distance in C{degrees} 

404 and C{meter} from the given point C{lat0, lon0} to the 

405 intersection C{lat2, lon2}, azimuth C{azi0} at the given 

406 point and the (perpendicular) angle C{at} between the 

407 geodesic and this line at the intersection point. The 

408 geodesic azimuth at the intersection is C{(at + azi2)}. 

409 See method L{Position} for further details. 

410 

411 @see: Methods C{Intersecant2}, C{Intersection} and C{Position}. 

412 ''' 

413 return _PlumbTo(self, lat0, lon0, est=est, tol=tol) 

414 

415 def Position(self, s12, outmask=Caps._STD): 

416 '''Return the position at distance C{B{s12} meter} on this line. 

417 

418 @arg s12: Distance from this line's first point (C{meter}). 

419 @kwarg outmask: Bit-or'ed combination of L{Caps} values specifying 

420 the quantities to be returned. 

421 

422 @return: A L{GDict} with up to 12 items C{lat1, lon1, azi1, lat2, 

423 lon2, azi2, m12, a12, s12, M12, M21, S12} with C{lat1}, 

424 C{lon1}, C{azi1} and arc length C{a12} always included, 

425 except when C{a12=NAN}. 

426 ''' 

427 with _wargs(self, s12, outmask) as args: 

428 d = _GeodesicLine.Position(self, *args) 

429 return GDict(d) 

430 

431 # GeodesicLine.ArcPosition.__doc__ = _GeodesicLine.ArcPosition.__doc__ 

432 # GeodesicLine.Position.__doc__ = _GeodesicLine.Position.__doc__ 

433 return GeodesicLine # overwrite property_ROver 

434 

435 @property_ROver 

436 def Geodesic_WGS84(self): 

437 '''Get the I{wrapped} C{Geodesic(WGS84)} singleton, provided the 

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

439 is installed, otherwise an C{ImportError}. 

440 ''' 

441 return _EWGS84.geodesicw # overwrite property_ROver 

442 

443_wrapped = _gWrapped() # PYCHOK singleton, .ellipsoids, .test/base.py 

444 

445 

446class Geodesic(_gWrapped): # overwritten by 1st instance 

447 '''I{Wrapper} around I{Karney}'s class U{geographiclib.geodesic.Geodesic 

448 <https://GeographicLib.SourceForge.io/Python/doc/code.html>}. 

449 ''' 

450 def __new__(unused, a_ellipsoid=_EWGS84, f=None, **name): 

451 '''Return a I{wrapped} C{geodesic.Geodesic} instance from I{Karney}'s 

452 Python U{geographiclib<https://PyPI.org/project/geographiclib>}, 

453 provide the latter is installed, otherwise an C{ImportError}. 

454 

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

456 or the equatorial radius I{a} of the ellipsoid (C{meter}). 

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

458 B{C{a_ellipsoid}}) is C{meter}, ignored otherwise. 

459 @kwarg name: Optional C{B{name}=NN} (C{str}). 

460 ''' 

461 g = _wrapped.Geodesic(a_ellipsoid, f=f, **name) 

462 _MODS.geodesicw.Geodesic = g.__class__ # overwrite class 

463 return g 

464 

465 

466class GeodesicLine(_gWrapped): # overwritten by 1st instance 

467 '''I{Wrapper} around I{Karney}'s class U{geographiclib.geodesicline.GeodesicLine 

468 <https://GeographicLib.SourceForge.io/Python/doc/code.html>}. 

469 ''' 

470 def __new__(unused, geodesic, lat1, lon1, azi1, caps=Caps._STD_LINE, **name): 

471 '''Return a I{wrapped} C{geodesicline.GeodesicLine} instance from I{Karney}'s 

472 Python U{geographiclib<https://PyPI.org/project/geographiclib>}, provided 

473 the latter is installed, otherwise an C{ImportError}. 

474 

475 @arg geodesic: A I{wrapped} L{Geodesic} instance. 

476 @arg lat1: Latitude of the first points (C{degrees}). 

477 @arg lon1: Longitude of the first points (C{degrees}). 

478 @arg azi1: Azimuth at the first points (compass C{degrees360}). 

479 @kwarg caps: Optional, bit-or'ed combination of L{Caps} values specifying 

480 the capabilities the C{GeodesicLine} instance should possess, 

481 i.e., which quantities can be returned by methods 

482 C{GeodesicLine.Position} and C{GeodesicLine.ArcPosition}. 

483 @kwarg name: Optional C{B{name}=NN} (C{str}). 

484 ''' 

485 gl = _wrapped.GeodesicLine(geodesic, lat1, lon1, azi1, caps=caps, **name) 

486 _MODS.geodesicw.GeodesicLine = gl.__class__ # overwrite class 

487 return gl 

488 

489 

490def Geodesic_WGS84(): 

491 '''Get the I{wrapped} L{Geodesic}C{(WGS84)} singleton, provided 

492 U{geographiclib<https://PyPI.org/project/geographiclib>} is 

493 installed, otherwise an C{ImportError}. 

494 ''' 

495 return _wrapped.Geodesic_WGS84 

496 

497 

498class _wargs(object): # see also .formy._idllmn6, .latlonBase._toCartesian3, .vector2d._numpy 

499 '''(INTERNAL) C{geographiclib} arguments and exception handler. 

500 ''' 

501 @contextmanager # <https://www.Python.org/dev/peps/pep-0343/> Examples 

502 def __call__(self, inst, *args, **kwds): 

503 '''(INTERNAL) Yield C{tuple(B{args})} with any errors raised 

504 as L{GeodesicError} embellished with all B{C{kwds}}. 

505 ''' 

506 try: 

507 yield args 

508 except Exception as x: 

509 u = _DOT_(classname(inst), callername(up=2, underOK=True)) 

510 raise GeodesicError(unstr(u, *args, **_name1__(kwds)), cause=x) 

511 

512_wargs = _wargs() # PYCHOK singleton 

513 

514 

515def _Intersecant2(gl, lat0, lon0, radius, tol=_TOL, form=F_D): # MCCABE in LatLonEllipsoidalBaseDI.intersecant2, .geodesicx.gxline.Intersecant2 

516 # (INTERNAL) Return the intersections of a circle at C{lat0, lon0} 

517 # and a geodesic line as a 2-Tuple C{(P, Q)}, each a C{GDict}. 

518 r = Radius_(radius) 

519 n = _DUNDER_nameof(_Intersecant2)[1:] 

520 _P = gl.Position 

521 _I = gl.geodesic.Inverse 

522 

523 def _R3(s): 

524 # radius, intersection, etc. at distance C{s} 

525 P = _P(s) 

526 d = _I(lat0, lon0, P.lat2, P.lon2) 

527 return fabs(d.s12), P, d 

528 

529 def _bisect2(s, c, Rc, r, tol, _R3): 

530 _s = Fsum(c).fsumf_ 

531 for i in range(_TRIPS): 

532 b = _s(s) 

533 Rb, P, d = _R3(b) 

534 if Rb > r: 

535 break 

536 else: # b >>> s and c >>> s 

537 raise ValueError(Fmt.no_convergence(b, s)) 

538 # Rb > r > Rc 

539 for i in range(_TRIPS): # 47-48 

540 s = (b + c) * _0_5 

541 R, P, d = _R3(s) 

542 if Rb > R > r: 

543 b, Rb = s, R 

544 elif Rc < R < r: 

545 c, Rc = s, R 

546# else: 

547# break 

548 t = fabs(b - c) 

549 if t < tol: # or fabs(R - r) < tol: 

550 break 

551 else: # t = min(t, fabs(R - r)) 

552 raise ValueError(Fmt.no_convergence(t, tol)) 

553 i += C.iteration # combine iterations 

554 P.set_(lat0=lat0, lon0=lon0, azi0=d.azi1, iteration=i, 

555 a02=d.a12, s02=d.s12, at=d.azi2 - P.azi2, name=n) 

556 return P, s 

557 

558 # get the perpendicular intersection of 2 geodesics, 

559 # one the plumb, pseudo-rhumb line to the other 

560 C = _PlumbTo(gl, lat0, lon0, tol=tol) 

561 try: 

562 a = fabs(C.s02) # distance between centers 

563 if a < r: 

564 c = C.s12 # distance along pseudo-rhumb line 

565 h = _copysign(r, c) # past half chord length 

566 P, p = _bisect2( h, c, a, r, tol, _R3) 

567 Q, q = _bisect2(-h, c, a, r, tol, _R3) 

568 if fabs(p - q) < max(EPS, tol): 

569 Q = P 

570 elif a > r: 

571 raise ValueError(_too_(Fmt.distant(a))) 

572 else: # tangential 

573 P = Q = C 

574 except Exception as x: 

575 t = _LLB(C.lat2, C.lon2).toStr(form=form) 

576 t = _SPACE_(x, _plumb_, _to_, Fmt.PAREN(t)) 

577 raise IntersectionError(t, txt=None, cause=x) 

578 

579 return P, Q 

580 

581 

582def _PlumbTo(gl, lat0, lon0, est=None, tol=_TOL): 

583 # (INTERNAL) Return the I{perpendicular} intersection of 

584 # a geodesic line C{gl} and geodesic from C{(lat0, lon0)}. 

585 pl = _MODS.rhumb.bases._PseudoRhumbLine(gl) 

586 return pl.PlumbTo(lat0, lon0, exact=gl.geodesic, 

587 est=est, tol=tol) 

588 

589# **) MIT License 

590# 

591# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved. 

592# 

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

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

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

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

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

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

599# 

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

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

602# 

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

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

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

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

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

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

609# OTHER DEALINGS IN THE SOFTWARE.