Coverage for pygeodesy/ellipsoidalVincenty.py: 99%

175 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-04-05 15:46 -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-2016} 

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 

54from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase, _nearestOn 

55from pygeodesy.ellipsoidalBaseDI import _intersection3, _intersections2, \ 

56 LatLonEllipsoidalBaseDI, _TOL_M 

57from pygeodesy.errors import _and, _ValueError, _xkwds 

58from pygeodesy.fmath import Fpolynomial, hypot, hypot1 

59from pygeodesy.interns import _ambiguous_, _antipodal_, _COLONSPACE_, _to_, _SPACE_ 

60from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _ALL_OTHER 

61from pygeodesy.namedTuples import Destination2Tuple, Destination3Tuple, \ 

62 Distance3Tuple 

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

64from pygeodesy.props import deprecated_function, deprecated_method, \ 

65 Property_RO, property_doc_ 

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

67from pygeodesy.units import Number_, Scalar_ 

68from pygeodesy.utily import atan2b, atan2d, sincos2, sincos2d, unroll180, wrap180 

69 

70from math import atan2, cos, degrees, fabs, radians, tan 

71 

72__all__ = _ALL_LAZY.ellipsoidalVincenty 

73__version__ = '23.03.20' 

74 

75_antipodal_to_ = _SPACE_(_antipodal_, _to_) 

76_limit_ = 'limit' # PYCHOK used! 

77 

78 

79class VincentyError(_ValueError): 

80 '''Error raised from I{Vincenty}'s C{direct} and C{inverse} methods 

81 for coincident points or lack of convergence. 

82 ''' 

83 pass 

84 

85 

86class Cartesian(CartesianEllipsoidalBase): 

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

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

89 ''' 

90 @Property_RO 

91 def Ecef(self): 

92 '''Get the ECEF I{class} (L{EcefVeness}), I{lazily}. 

93 ''' 

94 return _MODS.ecef.EcefVeness 

95 

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

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

98 

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

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

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

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

103 

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

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

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

107 

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

109 ''' 

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

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

112 

113 

114class LatLon(LatLonEllipsoidalBaseDI): 

115 '''Using the formulae devised by U{I{Thaddeus Vincenty (1975)} 

116 <https://WikiPedia.org/wiki/Vincenty's_formulae>} for an (oblate) 

117 ellipsoidal model of the earth to compute the geodesic distance 

118 and bearings between two given points or the destination point 

119 given an start point and (initial) bearing. 

120 

121 Set the earth model to be used with the keyword argument 

122 datum. The default is Datums.WGS84, which is the most globally 

123 accurate. For other models, see the Datums in module datum. 

124 

125 Note: This implementation of I{Vincenty} methods may not converge 

126 for some valid points, raising a L{VincentyError}. In that case, 

127 a result may be obtained by increasing the tolerance C{epsilon} 

128 and/or iteration C{limit}, see properties L{LatLon.epsilon} and 

129 L{LatLon.iterations}. 

130 ''' 

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

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

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

134 

135 @deprecated_method 

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

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

138 ''' 

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

140 

141 @Property_RO 

142 def Ecef(self): 

143 '''Get the ECEF I{class} (L{EcefVeness}), I{lazily}. 

144 ''' 

145 return _MODS.ecef.EcefVeness 

146 

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

148 def epsilon(self): 

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

150 ''' 

151 return self._epsilon 

152 

153 @epsilon.setter # PYCHOK setter! 

154 def epsilon(self, epsilon): 

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

156 

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

158 

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

160 ''' 

161 self._epsilon = Scalar_(epsilon=epsilon) 

162 

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

164 def iterations(self): 

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

166 ''' 

167 return self._iterations - 1 

168 

169 @iterations.setter # PYCHOK setter! 

170 def iterations(self, limit): 

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

172 

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

174 

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

176 ''' 

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

178 

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

180 '''Convert this point to C{Vincenty}-based cartesian (ECEF) 

181 coordinates. 

182 

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

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

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

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

187 

188 @return: The cartesian point (L{Cartesian}) or if B{C{Cartesian}} 

189 is C{None}, an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, 

190 C, M, datum)} with C{C} and C{M} if available. 

191 

192 @raise TypeError: Invalid B{C{Cartesian}}, B{C{datum}} or other 

193 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} 

205 ellipsoids are not compatible. 

206 

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

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

209 limits. 

210 ''' 

211 E = self.ellipsoid() 

212 f = E.f 

213 

214 sb, cb = sincos2d(bearing) 

215 s1, c1, t1 = _sincostan3(self.phi, f) 

216 

217 eps = self.epsilon 

218 s12 = atan2(t1, cb) * _2_0 

219 sa, ca2 = _sincos22(c1 * sb) 

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

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

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

223 ss, cs = sincos2(s) 

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

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

226 e = fabs(s - e) 

227 if e < eps: 

228 self._iteration = i 

229 break 

230 else: 

231 t = self._no_convergence(e) 

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

233 

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

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

236 d = atan2b(sa, -t) 

237 if llr: 

238 b = cb * ss 

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

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

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

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

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

244 else: 

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

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, _ = _sincostan3( self.phi, f) 

265 s2, c2, _ = _sincostan3(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) 

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 

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

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 _sincos22(sa): 

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

377 ca2 = _1_0 - sa**2 

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

379 

380 

381def _sincostan3(a, f): 

382 # I{Reduced} C{(sin(B{a}), cos(B{a}), tan(B{a}))} 

383 if a: # see L{sincostan3} 

384 t = (_1_0 - f) * tan(a) 

385 if t: 

386 c = _1_0 / hypot1(t) 

387 s = t * c 

388 return s, c, t 

389 return _0_0, _1_0, _0_0 

390 

391 

392@deprecated_function 

393def areaOf(points, **datum_wrap): 

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

395 ''' 

396 try: 

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

398 except ImportError: 

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

400 

401 

402def intersection3(start1, end1, start2, end2, height=None, wrap=True, 

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

404 '''Iteratively compute the intersection point of two paths, each defined 

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

406 initial bearing from North. 

407 

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

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

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

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

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

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

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

415 or C{None} for the mean height. 

416 @kwarg wrap: Wrap and unroll longitudes (C{bool}). 

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

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

419 C{B{start1}.Equidistant}. 

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

421 (C{meter}, conventionally). 

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

423 or C{None}. 

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

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

426 

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

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

429 lon, height, datum)}. 

430 

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

432 non-intersecting paths or no convergence 

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

434 

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

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

437 

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

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

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

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

442 earth perimeter). 

443 

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

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

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

447 BOUNDARIES} for more details about the iteration algorithm. 

448 ''' 

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

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

451 

452 

453def intersections2(center1, radius1, center2, radius2, height=None, wrap=True, 

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

455 '''Iteratively compute the intersection points of two circles, each defined 

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

457 

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

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

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

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

462 B{C{radius1}}). 

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

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

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

466 @kwarg wrap: Wrap and unroll longitudes (C{bool}). 

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

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

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

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

471 and B{C{radius2}}). 

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

473 or C{None}. 

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

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

476 

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

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

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

480 aka the I{radical center}. 

481 

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

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

484 

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

486 or invalid B{C{equidistant}}. 

487 

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

489 

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

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

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

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

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

495 intersections. 

496 ''' 

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

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

499 

500 

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

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

503 '''Iteratively locate the closest point on the geodesic between 

504 two other (ellipsoidal) points. 

505 

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

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

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

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

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

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

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

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

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

515 takes the heights of the points into account. 

516 @kwarg wrap: Wrap and unroll longitudes (C{bool}). 

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

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

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

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

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

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

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

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

525 

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

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

528 

529 @raise ImportError: Package U{geographiclib 

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

531 not installed or not found, but only if 

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

533 

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

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

536 

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

538 

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

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

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

542 BOUNDARIES} for more details about the iteration algorithm. 

543 ''' 

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

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

546 

547 

548@deprecated_function 

549def perimeterOf(points, **closed_datum_wrap): 

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

551 ''' 

552 try: 

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

554 except ImportError: 

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

556 

557 

558__all__ += _ALL_OTHER(Cartesian, LatLon, 

559 intersection3, intersections2, ispolar, # from .points 

560 nearestOn) + _ALL_DOCS(areaOf, perimeterOf) # deprecated 

561 

562# **) MIT License 

563# 

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

565# 

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

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

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

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

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

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

572# 

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

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

575# 

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

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

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

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

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

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

582# OTHER DEALINGS IN THE SOFTWARE.