Coverage for pygeodesy/ellipsoidalVincenty.py: 99%

176 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-25 13:15 -0400

1 

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

3 

4u'''Ellipsoidal, I{Vincenty}-based geodesy. 

5 

6I{Thaddeus Vincenty}'s geodetic (lat-/longitude) L{LatLon}, geocentric 

7(ECEF) L{Cartesian} and L{VincentyError} classes and functions L{areaOf}, 

8L{intersections2}, L{nearestOn} and L{perimeterOf}. 

9 

10Pure Python implementation of geodesy tools for ellipsoidal earth models, 

11transcoded from JavaScript originals by I{(C) Chris Veness 2005-2024} 

12and published under the same MIT Licence**, see U{Vincenty geodesics 

13<https://www.Movable-Type.co.UK/scripts/LatLongVincenty.html>}. More 

14at U{geographiclib<https://PyPI.org/project/geographiclib>} and 

15U{GeoPy<https://PyPI.org/project/geopy>}. 

16 

17Calculate geodesic distance between two points using the U{Vincenty 

18<https://WikiPedia.org/wiki/Vincenty's_formulae>} formulae and one of 

19several ellipsoidal earth models. The default model is WGS-84, the 

20most widely used globally-applicable model for the earth ellipsoid. 

21 

22Other ellipsoids offering a better fit to the local geoid include Airy 

23(1830) in the UK, Clarke (1880) in Africa, International 1924 in much 

24of Europe, and GRS-67 in South America. North America (NAD83) and 

25Australia (GDA) use GRS-80, which is equivalent to the WGS-84 model. 

26 

27Great-circle distance uses a I{spherical} model of the earth with the 

28mean earth radius defined by the International Union of Geodesy and 

29Geophysics (IUGG) as M{(2 * a + b) / 3 = 6371008.7714150598} or about 

306,371,009 meter (for WGS-84, resulting in an error of up to about 0.5%). 

31 

32Here's an example usage of C{ellipsoidalVincenty}: 

33 

34 >>> from pygeodesy.ellipsoidalVincenty import LatLon 

35 >>> Newport_RI = LatLon(41.49008, -71.312796) 

36 >>> Cleveland_OH = LatLon(41.499498, -81.695391) 

37 >>> Newport_RI.distanceTo(Cleveland_OH) 

38 866,455.4329158525 # meter 

39 

40To change the ellipsoid model used by the Vincenty formulae use: 

41 

42 >>> from pygeodesy import Datums 

43 >>> from pygeodesy.ellipsoidalVincenty import LatLon 

44 >>> p = LatLon(0, 0, datum=Datums.OSGB36) 

45 

46or by converting to anothor datum: 

47 

48 >>> p = p.toDatum(Datums.OSGB36) 

49''' 

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

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

52 

53from pygeodesy.constants import EPS, EPS0, _0_0, _1_0, _2_0, _3_0, _4_0, _6_0 

54# from pygeodesy.ecef import EcefVeness # _MODS 

55from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase, _nearestOn 

56from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, \ 

57 _intersection3, _intersections2, \ 

58 _TOL_M, intersecant2 

59# from pygeodesy.ellipsoidalExact import areaOf, perimeterOf # _MODS 

60# from pygeodesy.ellipsoidalKarney import areaOf, perimeterOf # _MODS 

61from pygeodesy.errors import _and, _ValueError, _xkwds 

62from pygeodesy.fmath import Fpolynomial, hypot, hypot1 

63from pygeodesy.interns import _ambiguous_, _antipodal_, _COLONSPACE_, \ 

64 _to_, _SPACE_, _limit_ # PYCHOK used! 

65from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS 

66from pygeodesy.namedTuples import Destination2Tuple, Destination3Tuple, \ 

67 Distance3Tuple 

68from pygeodesy.points import Fmt, ispolar # PYCHOK exported 

69from pygeodesy.props import deprecated_function, deprecated_method, \ 

70 property_doc_, property_RO 

71# from pygeodesy.streprs import Fmt # from .points 

72from pygeodesy.units import Number_, Scalar_ 

73from pygeodesy.utily import atan2, atan2b, atan2d, sincos2, sincos2d, \ 

74 unroll180, wrap180 

75 

76from math import cos, degrees, fabs, radians, tan as _tan 

77 

78__all__ = _ALL_LAZY.ellipsoidalVincenty 

79__version__ = '25.04.21' 

80 

81_antipodal_to_ = _SPACE_(_antipodal_, _to_) 

82 

83 

84class VincentyError(_ValueError): 

85 '''Error raised by I{Vincenty}'s C{Direct} and C{Inverse} methods 

86 for coincident points or lack of convergence. 

87 ''' 

88 pass 

89 

90 

91class Cartesian(CartesianEllipsoidalBase): 

92 '''Extended to convert geocentric, L{Cartesian} points to 

93 Vincenty-based, ellipsoidal, geodetic L{LatLon}. 

94 ''' 

95 @property_RO 

96 def Ecef(self): 

97 '''Get the ECEF I{class} (L{EcefVeness}), I{once}. 

98 ''' 

99 return _Ecef() 

100 

101 def toLatLon(self, **LatLon_and_kwds): # PYCHOK LatLon=LatLon, datum=None 

102 '''Convert this cartesian point to a C{Vincenty}-based geodetic point. 

103 

104 @kwarg LatLon_and_kwds: Optional L{LatLon} and L{LatLon} keyword 

105 arguments as C{datum}. Use C{B{LatLon}=..., 

106 B{datum}=...} to override this L{LatLon} 

107 class or specify C{B{LatLon}=None}. 

108 

109 @return: The geodetic point (L{LatLon}) or if C{B{LatLon} is None}, 

110 an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} 

111 with C{C} and C{M} if available. 

112 

113 @raise TypeError: Invalid B{C{LatLon_and_kwds}} argument. 

114 ''' 

115 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon, datum=self.datum) 

116 return CartesianEllipsoidalBase.toLatLon(self, **kwds) 

117 

118 

119class LatLon(LatLonEllipsoidalBaseDI): 

120 '''New point on an (oblate) ellipsoidal earth model, using the formulae devised 

121 by U{I{Thaddeus Vincenty}<https://WikiPedia.org/wiki/Vincenty's_formulae>} 

122 (1975) to compute geodesic distances, bearings (azimuths), etc. 

123 

124 Set the earth model to be used with the keyword argument datum. The default 

125 is C{Datums.WGS84}, which is the most globally accurate. For other models, 

126 see the L{Datums<pygeodesy.datums>}. 

127 

128 @note: This implementation of I{Vincenty} methods may not converge for some 

129 valid points, raising a L{VincentyError}. In that case, a result may 

130 be obtained by increasing the tolerance C{epsilon} and/or iteration 

131 C{limit}, see properties L{LatLon.epsilon} and L{LatLon.iterations}. 

132 ''' 

133 _epsilon = 1e-12 # radians, about 6 um 

134# _iteration = None # iteration number from .named._NamedBase 

135 _iterations = 201 # 5, default max, 200 vs Veness' 1,000 

136 

137 @deprecated_method 

138 def bearingTo(self, other, wrap=False): # PYCHOK no cover 

139 '''DEPRECATED, use method L{initialBearingTo} or L{bearingTo2}. 

140 ''' 

141 return self.initialBearingTo(other, wrap=wrap) 

142 

143 @property_RO 

144 def Ecef(self): 

145 '''Get the ECEF I{class} (L{EcefVeness}), I{once}. 

146 ''' 

147 return _Ecef() 

148 

149 @property_doc_(''' the convergence epsilon (C{radians}).''') 

150 def epsilon(self): 

151 '''Get the convergence epsilon (C{radians}). 

152 ''' 

153 return self._epsilon 

154 

155 @epsilon.setter # PYCHOK setter! 

156 def epsilon(self, epsilon): 

157 '''Set the convergence epsilon (C{radians}). 

158 

159 @raise TypeError: Non-scalar B{C{epsilon}}. 

160 

161 @raise ValueError: Out of bounds B{C{epsilon}}. 

162 ''' 

163 self._epsilon = Scalar_(epsilon=epsilon) 

164 

165 @property_doc_(''' the iteration limit (C{int}).''') 

166 def iterations(self): 

167 '''Get the iteration limit (C{int}). 

168 ''' 

169 return self._iterations - 1 

170 

171 @iterations.setter # PYCHOK setter! 

172 def iterations(self, limit): 

173 '''Set the iteration limit (C{int}). 

174 

175 @raise TypeError: Non-scalar B{C{limit}}. 

176 

177 @raise ValueError: Out-of-bounds B{C{limit}}. 

178 ''' 

179 self._iterations = Number_(limit, name=_limit_, low=4, high=1000) + 1 

180 

181 def toCartesian(self, **Cartesian_datum_kwds): # PYCHOK Cartesian=Cartesian, datum=None 

182 '''Convert this point to C{Vincenty}-based cartesian (ECEF) coordinates. 

183 

184 @kwarg Cartesian_datum_kwds: Optional L{Cartesian}, B{C{datum}} and other 

185 keyword arguments, ignored if C{B{Cartesian}=None}. Use 

186 C{B{Cartesian}=...} to override this L{Cartesian} class 

187 or specify C{B{Cartesian}=None}. 

188 

189 @return: The cartesian point (L{Cartesian}) or if C{B{Cartesian} is None}, 

190 an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with 

191 C{C} and C{M} if available. 

192 

193 @raise TypeError: Invalid B{C{Cartesian}}, B{C{datum}} or other B{C{Cartesian_datum_kwds}}. 

194 ''' 

195 kwds = _xkwds(Cartesian_datum_kwds, Cartesian=Cartesian, 

196 datum=self.datum) 

197 return LatLonEllipsoidalBaseDI.toCartesian(self, **kwds) 

198 

199 def _Direct(self, distance, bearing, llr, height): 

200 '''(INTERNAL) Direct Vincenty method. 

201 

202 @raise TypeError: The B{C{other}} point is not L{LatLon}. 

203 

204 @raise ValueError: If this and the B{C{other}} point's L{Datum} ellipsoids are 

205 not compatible. 

206 

207 @raise VincentyError: Vincenty fails to converge for the current limits, see 

208 L{epsilon<LatLon.epsilon>} and L{iterations<LatLon.iterations>}. 

209 ''' 

210 E = self.ellipsoid() 

211 f = E.f 

212 

213 sb, cb = sincos2d(bearing) 

214 s1, c1, t1 = _sincostan3r(self.phi, f) 

215 

216 eps = self.epsilon 

217 s12 = atan2(t1, cb) * _2_0 

218 sa, ca2 = _sincos22(c1 * sb) 

219 A, B = _AB2(ca2 * E.e22) # e22 == (a / b)**2 - 1 

220 s = d = distance / (A * E.b) 

221 for i in range(1, self._iterations): # 1-origin 

222 ss, cs = sincos2(s) 

223 c2sm, e = cos(s12 + s), s 

224 s = _Ds(B, cs, ss, c2sm, d) 

225 e = fabs(s - e) 

226 if e < eps: 

227 self._iteration = i 

228 break 

229 else: 

230 t = self._no_convergence(e) 

231 raise VincentyError(t, txt=repr(self)) # self.toRepr() 

232 

233 t = s1 * ss - c1 * cs * cb 

234 # final bearing (reverse azimuth +/- 180) 

235 d = atan2b(sa, -t) 

236 if llr: 

237 b = cb * ss 

238 a = atan2d(s1 * cs + c1 * b, hypot(sa, t) * E.b_a) 

239 b = atan2d(sb * ss, -s1 * b + c1 * cs) + self.lon \ 

240 - degrees(_Dl(f, ca2, sa, s, cs, ss, c2sm)) 

241 t = Destination3Tuple(a, wrap180(b), d) 

242 r = self._Direct2Tuple(self.classof, height, t) 

243 else: 

244 r = Destination2Tuple(None, d, name=self.name) 

245 r._iteration = i 

246 return r 

247 

248 def _Inverse(self, other, wrap, azis=True): # PYCHOK signature 

249 '''(INTERNAL) Inverse Vincenty method. 

250 

251 @raise TypeError: The B{C{other}} point is not L{LatLon}. 

252 

253 @raise ValueError: If this and the B{C{other}} point's L{Datum} 

254 ellipsoids are not compatible. 

255 

256 @raise VincentyError: Vincenty fails to converge for the current 

257 L{LatLon.epsilon} and L{LatLon.iterations} 

258 limits and/or if this and the B{C{other}} 

259 point are coincident or near-antipodal. 

260 ''' 

261 E = self.ellipsoids(other) 

262 f = E.f 

263 

264 s1, c1, _ = _sincostan3r( self.phi, f) 

265 s2, c2, _ = _sincostan3r(other.phi, f) 

266 

267 c1c2, s1c2 = c1 * c2, s1 * c2 

268 c1s2, s1s2 = c1 * s2, s1 * s2 

269 

270 eps = self.epsilon 

271 d, _ = unroll180(self.lon, other.lon, wrap=wrap) 

272 dl = ll = radians(d) 

273 for i in range(1, self._iterations): # 1-origin 

274 sll, cll = sincos2(ll) 

275 

276 ss = hypot(c2 * sll, c1s2 - s1c2 * cll) 

277 if ss < EPS: # coincident or antipodal, ... 

278 if self.isantipodeTo(other, eps=eps): 

279 t = self._is_to(other, True) 

280 raise VincentyError(_ambiguous_, txt=t) 

281 self._iteration = i 

282 # return zeros like Karney, unlike Veness 

283 return Distance3Tuple(_0_0, 0, 0, iteration=i) 

284 

285 cs = s1s2 + c1c2 * cll 

286 s, e = atan2(ss, cs), ll 

287 sa, ca2 = _sincos22(c1c2 * sll / ss) 

288 if ca2: 

289 c2sm = cs - _2_0 * s1s2 / ca2 

290 ll = _Dl(f, ca2, sa, s, cs, ss, c2sm, dl) 

291 else: # equatorial line 

292 ll = dl + f * sa * s 

293 e = fabs(ll - e) 

294 if e < eps: 

295 self._iteration = i 

296 break 

297# elif abs(ll) > PI and self.isantipodeTo(other, eps=eps): 

298# # omitted and applied *after* failure to converge below, 

299# # see footnote under Inverse <https://WikiPedia.org/wiki/ 

300# # Vincenty's_formulae> and <https://GitHub.com/chrisveness/ 

301# # geodesy/blob/master/latlon-ellipsoidal-vincenty.js> 

302# raise VincentyError(_ambiguous_, self._is_to(other, True)) 

303 else: 

304 t = self._is_to(other, self.isantipodeTo(other, eps=eps)) 

305 raise VincentyError(self._no_convergence(e), txt=t) 

306 

307 if ca2: # e22 == (a / b)**2 - 1 

308 A, B = _AB2(ca2 * E.e22) 

309 s = -A * _Ds(B, cs, ss, c2sm, -s) 

310 

311 b = E.b 

312# if self.height or other.height: 

313# b += self._havg(other) 

314 d = b * s 

315 

316 if azis: # forward and reverse azimuth 

317 s, c = sincos2(ll) 

318 f = atan2b(c2 * s, c1s2 - s1c2 * c) 

319 r = atan2b(c1 * s, -s1c2 + c1s2 * c) 

320 else: 

321 f = r = _0_0 # NAN 

322 return Distance3Tuple(d, f, r, name=self.name, iteration=i) 

323 

324 def _is_to(self, other, anti): 

325 '''(INTERNAL) Return I{'<self> [antipodal] to <other>'} text (C{str}). 

326 ''' 

327 t = _antipodal_to_ if anti else _to_ 

328 return _SPACE_(repr(self), t, repr(other)) 

329 

330 def _no_convergence(self, e): 

331 '''(INTERNAL) Return I{'no convergence (..): ...'} text (C{str}). 

332 ''' 

333 t = (Fmt.PARENSPACED(*t) for t in ((LatLon.epsilon.name, self.epsilon), 

334 (LatLon.iterations.name, self.iterations))) 

335 return _COLONSPACE_(Fmt.no_convergence(e), _and(*t)) 

336 

337 

338def _AB2(u2): # WGS84 e22 = 0.00673949674227643 

339 # 2-Tuple C{(A, B)} polynomials 

340 if u2: 

341 A = Fpolynomial(u2, 16384, 4096, -768, 320, -175).fover(16384) 

342 B = Fpolynomial(u2, 0, 256, -128, 74, -47).fover( 1024) 

343 return A, B 

344 return _1_0, _0_0 

345 

346 

347def _c2sm2(c2sm): 

348 # C{2 * c2sm**2 - 1} 

349 return c2sm**2 * _2_0 - _1_0 

350 

351 

352def _Dl(f, ca2, sa, s, cs, ss, c2sm, dl=_0_0): 

353 # C{Dl} 

354 if f and sa: 

355 C = f * ca2 / _4_0 

356 C *= f - C * _3_0 + _1_0 

357 if C and ss: 

358 s += C * ss * (c2sm + 

359 C * cs * _c2sm2(c2sm)) 

360 dl += (_1_0 - C) * f * sa * s 

361 return dl 

362 

363 

364def _Ds(B, cs, ss, c2sm, d): 

365 # C{Ds - d} 

366 if B and ss: 

367 c2sm2 = _c2sm2(c2sm) 

368 ss2 = (ss**2 * _4_0 - _3_0) * (c2sm2 * _2_0 - _1_0) 

369 B *= ss * (c2sm + B / _4_0 * (c2sm2 * cs - 

370 B / _6_0 * c2sm * ss2)) 

371 d += B 

372 return d 

373 

374 

375def _Ecef(): 

376 # get the Ecef class and overwrite property_RO 

377 Cartesian.Ecef = LatLon.Ecef = E = _MODS.ecef.EcefVeness 

378 return E 

379 

380 

381def _sincos22(sa): 

382 # 2-Tuple C{(sin(a), cos(a)**2)} 

383 ca2 = _1_0 - sa**2 

384 return sa, (_0_0 if ca2 < EPS0 else ca2) # XXX EPS? 

385 

386 

387def _sincostan3r(a, f): 

388 # I{Reduced} 3-tuple C{(sin(B{a}), cos(B{a}), tan(B{a}))} 

389 if a: # see L{sincostan3} 

390 t = (_1_0 - f) * _tan(a) 

391 if t: 

392 c = _1_0 / hypot1(t) 

393 s = c * t 

394 return s, c, t 

395 return _0_0, _1_0, _0_0 

396 

397 

398@deprecated_function 

399def areaOf(points, **datum_wrap): 

400 '''DEPRECATED, use function L{ellipsoidalExact.areaOf} or L{ellipsoidalKarney.areaOf}. 

401 ''' 

402 try: 

403 return _MODS.ellipsoidalKarney.areaOf(points, **datum_wrap) 

404 except ImportError: 

405 return _MODS.ellipsoidalExact.areaOf(points, **datum_wrap) 

406 

407 

408def intersection3(start1, end1, start2, end2, height=None, wrap=False, # was=True 

409 equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds): 

410 '''I{Iteratively} compute the intersection point of two lines, each defined 

411 by two (ellipsoidal) points or by an (ellipsoidal) start point and an 

412 (initial) bearing from North. 

413 

414 @arg start1: Start point of the first line (L{LatLon}). 

415 @arg end1: End point of the first line (L{LatLon}) or the initial bearing 

416 at the first point (compass C{degrees360}). 

417 @arg start2: Start point of the second line (L{LatLon}). 

418 @arg end2: End point of the second line (L{LatLon}) or the initial bearing 

419 at the second point (compass C{degrees360}). 

420 @kwarg height: Optional height at the intersection (C{meter}, conventionally) 

421 or C{None} for the mean height. 

422 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{start2}} 

423 and B{C{end*}} points (C{bool}). 

424 @kwarg equidistant: An azimuthal equidistant projection (I{class} or function 

425 L{pygeodesy.equidistant}) or C{None} for the preferred 

426 C{B{start1}.Equidistant}. 

427 @kwarg tol: Tolerance for convergence and for skew line distance and length 

428 (C{meter}, conventionally). 

429 @kwarg LatLon: Optional class to return the intersection points (L{LatLon}) 

430 or C{None}. 

431 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments, 

432 ignored if C{B{LatLon} is None}. 

433 

434 @return: An L{Intersection3Tuple}C{(point, outside1, outside2)} with C{point} 

435 a B{C{LatLon}} or if C{B{LatLon} is None}, a L{LatLon4Tuple}C{(lat, 

436 lon, height, datum)}. 

437 

438 @raise IntersectionError: Skew, colinear, parallel or otherwise 

439 non-intersecting lines or no convergence 

440 for the given B{C{tol}}. 

441 

442 @raise TypeError: Invalid or non-ellipsoidal B{C{start1}}, B{C{end1}}, 

443 B{C{start2}} or B{C{end2}} or invalid B{C{equidistant}}. 

444 

445 @note: For each line specified with an initial bearing, a pseudo-end point 

446 is computed as the C{destination} along that bearing at about 1.5 

447 times the distance from the start point to an initial gu-/estimate 

448 of the intersection point (and between 1/8 and 3/8 of the authalic 

449 earth perimeter). 

450 

451 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/ 

452 calculating-intersection-of-two-circles>} and U{Karney's paper 

453 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME 

454 BOUNDARIES} for more details about the iteration algorithm. 

455 ''' 

456 return _intersection3(start1, end1, start2, end2, height=height, wrap=wrap, 

457 equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) 

458 

459 

460def intersections2(center1, radius1, center2, radius2, height=None, wrap=False, # was=True 

461 equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds): 

462 '''I{Iteratively} compute the intersection points of two circles, each defined 

463 by an (ellipsoidal) center point and a radius. 

464 

465 @arg center1: Center of the first circle (L{LatLon}). 

466 @arg radius1: Radius of the first circle (C{meter}, conventionally). 

467 @arg center2: Center of the second circle (L{LatLon}). 

468 @arg radius2: Radius of the second circle (C{meter}, same units as 

469 B{C{radius1}}). 

470 @kwarg height: Optional height for the intersection points (C{meter}, 

471 conventionally) or C{None} for the I{"radical height"} 

472 at the I{radical line} between both centers. 

473 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{center2}} 

474 (C{bool}). 

475 @kwarg equidistant: An azimuthal equidistant projection (I{class} or 

476 function L{pygeodesy.equidistant}) or C{None} for 

477 the preferred C{B{center1}.Equidistant}. 

478 @kwarg tol: Convergence tolerance (C{meter}, same units as B{C{radius1}} 

479 and B{C{radius2}}). 

480 @kwarg LatLon: Optional class to return the intersection points (L{LatLon}) 

481 or C{None}. 

482 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments, 

483 ignored if C{B{LatLon} is None}. 

484 

485 @return: 2-Tuple of the intersection points, each a B{C{LatLon}} instance 

486 or L{LatLon4Tuple}C{(lat, lon, height, datum)} if C{B{LatLon} is 

487 None}. For abutting circles, both points are the same instance, 

488 aka the I{radical center}. 

489 

490 @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting 

491 circles or no convergence for the B{C{tol}}. 

492 

493 @raise TypeError: Invalid or non-ellipsoidal B{C{center1}} or B{C{center2}} 

494 or invalid B{C{equidistant}}. 

495 

496 @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{height}}. 

497 

498 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/ 

499 calculating-intersection-of-two-circles>}, U{Karney's paper 

500 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME BOUNDARIES}, 

501 U{circle-circle<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>} and 

502 U{sphere-sphere<https://MathWorld.Wolfram.com/Sphere-SphereIntersection.html>} 

503 intersections. 

504 ''' 

505 return _intersections2(center1, radius1, center2, radius2, height=height, wrap=wrap, 

506 equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) 

507 

508 

509def nearestOn(point, point1, point2, within=True, height=None, wrap=False, 

510 equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds): 

511 '''I{Iteratively} locate the closest point on the geodesic between 

512 two other (ellipsoidal) points. 

513 

514 @arg point: Reference point (C{LatLon}). 

515 @arg point1: Start point of the geodesic (C{LatLon}). 

516 @arg point2: End point of the geodesic (C{LatLon}). 

517 @kwarg within: If C{True}, return the closest point I{between} 

518 B{C{point1}} and B{C{point2}}, otherwise the 

519 closest point elsewhere on the geodesic (C{bool}). 

520 @kwarg height: Optional height for the closest point (C{meter}, 

521 conventionally) or C{None} or C{False} for the 

522 interpolated height. If C{False}, the closest 

523 takes the heights of the points into account. 

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

525 B{C{point1}} and B{C{point2}} (C{bool}). 

526 @kwarg equidistant: An azimuthal equidistant projection (I{class} 

527 or function L{pygeodesy.equidistant}) or C{None} 

528 for the preferred C{B{point}.Equidistant}. 

529 @kwarg tol: Convergence tolerance (C{meter}). 

530 @kwarg LatLon: Optional class to return the closest point 

531 (L{LatLon}) or C{None}. 

532 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword 

533 arguments, ignored if C{B{LatLon} is None}. 

534 

535 @return: Closest point, a B{C{LatLon}} instance or if C{B{LatLon} 

536 is None}, a L{LatLon4Tuple}C{(lat, lon, height, datum)}. 

537 

538 @raise ImportError: Package U{geographiclib 

539 <https://PyPI.org/project/geographiclib>} 

540 not installed or not found, but only if 

541 C{B{equidistant}=}L{EquidistantKarney}. 

542 

543 @raise TypeError: Invalid or non-ellipsoidal B{C{point}}, B{C{point1}} 

544 or B{C{point2}} or invalid B{C{equidistant}}. 

545 

546 @raise ValueError: No convergence for the B{C{tol}}. 

547 

548 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/ 

549 calculating-intersection-of-two-circles>} and U{Karney's paper 

550 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME 

551 BOUNDARIES} for more details about the iteration algorithm. 

552 ''' 

553 return _nearestOn(point, point1, point2, within=within, height=height, wrap=wrap, 

554 equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) 

555 

556 

557@deprecated_function 

558def perimeterOf(points, **closed_datum_wrap): 

559 '''DEPRECATED, use function L{ellipsoidalExact.perimeterOf} or L{ellipsoidalKarney.perimeterOf}. 

560 ''' 

561 try: 

562 return _MODS.ellipsoidalKarney.perimeterOf(points, **closed_datum_wrap) 

563 except ImportError: 

564 return _MODS.ellipsoidalExact.perimeterOf(points, **closed_datum_wrap) 

565 

566 

567__all__ += _ALL_DOCS(Cartesian, LatLon, intersecant2, # from .ellipsoidalBaseDI 

568 intersection3, intersections2, ispolar, # from .points 

569 nearestOn, 

570 areaOf, perimeterOf) # DEPRECATED 

571 

572# **) MIT License 

573# 

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

575# 

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

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

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

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

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

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

582# 

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

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

585# 

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

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

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

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

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

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

592# OTHER DEALINGS IN THE SOFTWARE.