1

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

3

4 u'''(INTERNAL) Private, ellipsoidal Direct/Inverse geodesy base

5 class C{LatLonEllipsoidalBaseDI} and functions.

6'''

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

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

9

10 from pygeodesy.basics import isLatLon, _xsubclassof

11 from pygeodesy.constants import EPS, MAX, PI, PI2, PI_4, isnear0, isnear1, \

12 _EPSqrt as _TOL, _0_0, _0_5, _1_5, _3_0

13# from pygeodesy.dms import F_DMS # _MODS

14 from pygeodesy.ellipsoidalBase import LatLonEllipsoidalBase, _TOL_M, property_RO

15 from pygeodesy.errors import _AssertionError, IntersectionError, _IsnotError, \

16 _or, _ValueError, _xellipsoidal, _xError, _xkwds_not

17 from pygeodesy.fmath import favg, fmean_

18 from pygeodesy.fsums import Fmt, fsumf_

19 from pygeodesy.formy import _isequalTo, opposing, _radical2

20 from pygeodesy.interns import _antipodal_, _concentric_, _ellipsoidal_, \

21 _exceed_PI_radians_, _low_, _near_, \

22 _SPACE_, _too_

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

24 from pygeodesy.namedTuples import Bearing2Tuple, Destination2Tuple, \

25 Intersection3Tuple, NearestOn2Tuple, \

26 NearestOn8Tuple, _LL4Tuple

27# from pygeodesy.props import property_RO # from .ellipsoidalBase

28# from pygeodesy.streprs import Fmt # from .fsums

29 from pygeodesy.units import _fi_j2, _isDegrees, _isHeight, _isRadius, \

30 Radius_, Scalar

31 from pygeodesy.utily import m2km, unroll180, _unrollon, _unrollon3, \

32 _Wrap, wrap360

33

34 from math import degrees, radians

35

36__all__ = _ALL_LAZY.ellipsoidalBaseDI

37__version__ = '24.10.19'

38

39_polar__ = 'polar?'

40_B2END = _1_5 # _intersect3 bearing to pseudo-end point factor

41_TRIPS = 33 # _intersect3, _intersects2, _nearestOn interations, 6..9 sufficient?

42

43

44 class LatLonEllipsoidalBaseDI(LatLonEllipsoidalBase):

45 '''(INTERNAL) Base class for C{ellipsoidal*.LatLon} classes

46 with I{overloaded} C{Direct} and C{Inverse} methods.

47 '''

48

49 def bearingTo2(self, other, wrap=False):

50 '''Compute the initial and final bearing (forward and reverse

51 azimuth) from this to an other point, using this C{Inverse}

52 method. See methods L{initialBearingTo} and L{finalBearingTo}

53 for more details.

54

55 @arg other: The other point (C{LatLon}).

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

57 B{C{other}} point (C{bool}).

58

59 @return: A L{Bearing2Tuple}C{(initial, final)}.

60

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

62

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

64 ellipsoids are not compatible.

65 '''

66 r = self._Inverse(other, wrap)

67 return Bearing2Tuple(r.initial, r.final, name=self.name)

68

69 def destination(self, distance, bearing, height=None):

70 '''Compute the destination point after having travelled for

71 the given distance from this point along a geodesic given

72 by an initial bearing, using this C{Direct} method. See

73 method L{destination2} for more details.

74

75 @arg distance: Distance (C{meter}).

76 @arg bearing: Initial bearing in (compass C{degrees360}).

77 @kwarg height: Optional height, overriding the default

78 height (C{meter}, same units as C{distance}).

79

80 @return: The destination point (C{LatLon}).

81 '''

82 return self._Direct(distance, bearing, self.classof, height).destination

83

84 def destination2(self, distance, bearing, height=None):

85 '''Compute the destination point and the final bearing (reverse

86 azimuth) after having travelled for the given distance from

87 this point along a geodesic given by an initial bearing,

88 using this C{Direct} method.

89

90 The distance must be in the same units as this point's datum

91 axes, conventionally C{meter}. The distance is measured on

92 the surface of the ellipsoid, ignoring this point's height.

93

94 The initial and final bearing (forward and reverse azimuth)

95 are in compass C{degrees360}.

96

97 The destination point's height and datum are set to this

98 point's height and datum, unless the former is overridden.

99

100 @arg distance: Distance (C{meter}).

101 @arg bearing: Initial bearing (compass C{degrees360}).

102 @kwarg height: Optional height, overriding the default

103 height (C{meter}, same units as C{distance}).

104

105 @return: A L{Destination2Tuple}C{(destination, final)}.

106 '''

107 r = self._Direct(distance, bearing, self.classof, height)

108 return self._xnamed(r)

109

110 def _Direct(self, distance, bearing, LL, height): # overloaded by I{Vincenty}

111 '''(INTERNAL) I{Karney}'s C{Direct} method.

112

113 @return: A L{Destination2Tuple}C{(destination, final)} or a

114 L{Destination3Tuple}C{(lat, lon, final)} if C{B{LL} is None}.

115 '''

116 g = self.geodesic

117 r = g.Direct3(self.lat, self.lon, bearing, distance)

118 if LL:

119 r = self._Direct2Tuple(LL, height, r)

120 return r

121

122 def _Direct2Tuple(self, LL, height, r):

123 '''(INTERNAL) Helper for C{._Direct} result L{Destination2Tuple}.

124 '''

125 h = self.height if height is None else height

126 d = LL(*_Wrap.latlon(r.lat, r.lon), height=h, **_xkwds_not(None,

127 datum=self.datum, name=self.name,

128 epoch=self.epoch, reframe=self.reframe))

129 return Destination2Tuple(d, wrap360(r.final))

130

131 def distanceTo(self, other, wrap=False, **unused): # ignore radius=R_M

132 '''Compute the distance between this and an other point along

133 a geodesic, using this C{Inverse} method. See method

134 L{distanceTo3} for more details.

135

136 @arg other: The other point (C{LatLon}).

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

138 B{C{other}} point (C{bool}).

139

140 @return: Distance (C{meter}).

141

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

143

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

145 ellipsoids are not compatible.

146 '''

147 return self._Inverse(other, wrap, azis=False).distance

148

149 def distanceTo3(self, other, wrap=False):

150 '''Compute the distance, the initial and final bearing along

151 a geodesic between this and an other point, using this

152 C{Inverse} method.

153

154 The distance is in the same units as this point's datum axes,

155 conventionally meter. The distance is measured on the surface

156 of the ellipsoid, ignoring this point's height.

157

158 The initial and final bearing (forward and reverse azimuth)

159 are in compass C{degrees360} from North.

160

161 @arg other: Destination point (C{LatLon}).

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

163 B{C{other}} point (C{bool}).

164

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

166

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

168

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

170 ellipsoids are not compatible.

171 '''

172 return self._xnamed(self._Inverse(other, wrap))

173

174 def finalBearingOn(self, distance, bearing):

175 '''Compute the final bearing (reverse azimuth) after having

176 travelled for the given distance along a geodesic given by

177 an initial bearing from this point, using this C{Direct}

178 method. See method L{destination2} for more details.

179

180 @arg distance: Distance (C{meter}).

181 @arg bearing: Initial bearing (compass C{degrees360}).

182

183 @return: Final bearing (compass C{degrees360}).

184 '''

185 return self._Direct(distance, bearing, None, None).final

186

187 def finalBearingTo(self, other, wrap=False):

188 '''Compute the final bearing (reverse azimuth) after having

189 travelled along a geodesic from this point to an other

190 point, using this C{Inverse} method. See method

191 L{distanceTo3} for more details.

192

193 @arg other: The other point (C{LatLon}).

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

195 the B{C{other}} point (C{bool}).

196

197 @return: Final bearing (compass C{degrees360}).

198

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

200

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

202 ellipsoids are not compatible.

203 '''

204 return self._Inverse(other, wrap).final

205

206 @property_RO

207 def geodesic(self): # overloaded by I{Karney}'s, N/A for I{Vincenty}

208 '''N/A, invalid (C{None} I{always}).

209 '''

210 return None # PYCHOK no cover

211

212 def _g_gl_p3(self, point, other, exact, wrap):

213 '''(INTERNAL) Helper for methods C{.intersecant2} and C{.plumbTo}.

214 '''

215 p = _unrollon(self, self.others(point=point), wrap=wrap)

216 g = self.datum.ellipsoid.geodesic_(exact=exact)

217 gl = g._DirectLine( p, other) if _isDegrees(other) else \

218 g._InverseLine(p, self.others(other), wrap)

219 return g, gl, p

220

221 def initialBearingTo(self, other, wrap=False):

222 '''Compute the initial bearing (forward azimuth) to travel

223 along a geodesic from this point to an other point,

224 using this C{Inverse} method. See method L{distanceTo3}

225 for more details.

226

227 @arg other: The other point (C{LatLon}).

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

229 the B{C{other}} point (C{bool}).

230

231 @return: Initial bearing (compass C{degrees360}).

232

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

234

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

236 ellipsoids are not compatible.

237 '''

238 return self._Inverse(other, wrap).initial

239

240 def intermediateTo(self, other, fraction, height=None, wrap=False):

241 '''Return the point at given fraction along the geodesic between

242 this and an other point, using this C{Direct} and C{Inverse}

243 methods.

244

245 @arg other: The other point (C{LatLon}).

246 @arg fraction: Fraction between both points (C{scalar}, 0.0

247 at this and 1.0 at the other point.

248 @kwarg height: Optional height, overriding the fractional

249 height (C{meter}).

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

251 B{C{other}} point (C{bool}).

252

253 @return: Intermediate point (C{LatLon}).

254

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

256

257 @raise UnitError: Invalid B{C{fraction}} or B{C{height}}.

258

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

260 ellipsoids are not compatible.

261

262 @see: Methods L{distanceTo3}, L{destination}, C{midpointTo} and

263 C{rhumbMidpointTo}.

264 '''

265 f = Scalar(fraction=fraction)

266 if isnear0(f):

267 r = self

268 elif isnear1(f) and not wrap:

269 r = self.others(other)

270 else: # negative fraction OK

271 t = self.distanceTo3(other, wrap=wrap)

272 h = self._havg(other, f=f, h=height)

273 r = self.destination(t.distance * f, t.initial, height=h)

274 return r

275

276 def intersecant2(self, circle, point, other, exact=False, height=None, # PYCHOK signature

277 wrap=False, tol=_TOL):

278 '''Compute the intersections of a circle and a geodesic (line) given as

279 two points or as a point and bearing.

280

281 @arg circle: Radius of the circle centered at this location (C{meter},

282 conventionally) or a point on the circle (same C{LatLon} class).

283 @arg point: A location on the geodesic (same C{LatLon} class).

284 @arg other: An other point on the geodesic (same C{LatLon} class) or the

285 (forward) bearing at the B{C{point}} (compass C{degrees}).

286 @kwarg exact: Exact C{geodesic...} to use (C{bool} or C{Geodesic...}), see

287 method L{Ellipsoid.geodesic_}.

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

289 conventionally) or C{None} for interpolated heights.

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

291 B{C{circle}} and/or B{C{other}} (C{bool}).

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

293

294 @return: 2-Tuple of the intersection points (representing a geodesic chord),

295 each an instance of same C{LatLon} class. Both points are the same

296 instance if the geodesic (line) is tangential to the circle.

297

298 @raise IntersectionError: The circle and geodesic do not intersect.

299

300 @raise TypeError: If B{C{point}} is not same C{LatLon} class or B{C{circle}}

301 or B{C{other}} invalid.

302

303 @raise UnitError: Invalid B{C{circle}}, B{C{other}}, B{C{exact}} or

304 B{C{height}}.

305

306 @see: Method L{rhumbIntersecant2<LatLonBase.rhumbIntersecant2>}.

307 '''

308 try:

309 g, gl, p = self._g_gl_p3(point, other, exact, wrap)

310 r = Radius_(circle=circle) if _isRadius(circle) else \

311 g._Inverse(self, self.others(circle=circle), wrap).s12

312

313 P, Q = _MODS.geodesicw._Intersecant2(gl, self.lat, self.lon, r, tol=tol,

314 form=_MODS.dms.F_DMS)

315 return self._intersecend2(p, other, wrap, height, g, P, Q,

316 self.intersecant2)

317 except (TypeError, ValueError) as x:

318 raise _xError(x, center=self, circle=circle, point=point, other=other,

319 exact=exact, wrap=wrap)

320

321 def _Inverse(self, other, wrap, **unused): # azis=False, overloaded by I{Vincenty}

322 '''(INTERNAL) I{Karney}'s C{Inverse} method.

323

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

325 '''

326 _ = self.ellipsoids(other)

327 g = self.geodesic

328 _, lon = unroll180(self.lon, other.lon, wrap=wrap)

329 return g.Inverse3(self.lat, self.lon, other.lat, lon)

330

331 def nearestOn8(self, points, closed=False, height=None, wrap=False,

332 equidistant=None, tol=_TOL_M):

333 '''I{Iteratively} locate the point on a path or polygon closest

334 to this point.

335

336 @arg points: The path or polygon points (C{LatLon}[]).

337 @kwarg closed: Optionally, close the polygon (C{bool}).

338 @kwarg height: Optional height, overriding the height of this and all

339 other points (C{meter}, conventionally). If C{B{height}

340 is None}, each point's height is taken into account to

341 compute distances.

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

343 (C{bool}).

344

345 @return: A L{NearestOn8Tuple}C{(closest, distance, fi, j, start, end,

346 initial, final)} with C{distance} in C{meter}, conventionally

347 and with the C{closest}, the C{start} the C{end} point each

348 an instance of same C{LatLon} class.

349

350 @raise PointsError: Insufficient number of B{C{points}}.

351

352 @raise TypeError: Some B{C{points}} or B{C{equidistant}} invalid.

353

354 @raise ValueError: Some B{C{points}}' datum or ellipsoid incompatible

355 or no convergence for the given B{C{tol}}.

356

357 @see: Function L{pygeodesy.nearestOn6} and method C{nearestOn6}.

358 '''

359 _d3 = self.distanceTo3 # Distance3Tuple

360 _n3 = _nearestOn3

361 try:

362 Ps = self.PointsIter(points, loop=1, wrap=wrap)

363 p1 = c = s = e = Ps[0]

364 _ = self.ellipsoids(p1)

365 c3 = _d3(c, wrap=wrap) # XXX wrap=False?

366

367 except (TypeError, ValueError) as x:

368 raise _xError(x, Fmt.INDEX(points=0), p1, this=self, tol=tol,

369 closed=closed, height=height, wrap=wrap)

370

371 # get the azimuthal equidistant projection, once

372 A = _Equidistant00(equidistant, c)

373 b = _Box(c, c3.distance)

374 m = f = i = 0 # p1..p2 == points[i]..[j]

375

376 kwds = dict(within=True, height=height, tol=tol,

377 LatLon=self.classof, # this LatLon

378 datum=self.datum, epoch=self.epoch, reframe=self.reframe)

379 try:

380 for j, p2 in Ps.enumerate(closed=closed):

381 if wrap and j != 0: # not Ps.looped

382 p2 = _unrollon(p1, p2)

383 # skip edge if no overlap with box around closest

384 if j < 4 or b.overlaps(p1.lat, p1.lon, p2.lat, p2.lon):

385 p, t, _ = _n3(self, p1, p2, A, **kwds)

386 d3 = _d3(p, wrap=False) # already unrolled

387 if d3.distance < c3.distance:

388 c3, c, s, e, f = d3, p, p1, p2, (i + t)

389 b = _Box(c, c3.distance)

390 m = max(m, c.iteration)

391 p1, i = p2, j

392

393 except (TypeError, ValueError) as x:

394 raise _xError(x, Fmt.INDEX(points=i), p1,

395 Fmt.INDEX(points=j), p2, this=self, tol=tol,

396 closed=closed, height=height, wrap=wrap)

397

398 f, j = _fi_j2(f, len(Ps)) # like .vector3d.nearestOn6

399

400 n = self.nearestOn8.__name__ # _DUNDER_nameof

401 c.rename(n)

402 if s is not c:

403 s = s.copy(name=n)

404 if e is not c:

405 e = e.copy(name=n) # name__=self.nearestOn8

406 return NearestOn8Tuple(c, c3.distance, f, j, s, e, c3.initial, c3.final,

407 iteration=m) # ._iteration for tests

408

409 def plumbTo(self, point, other, exact=False, height=None, # PYCHOK signature

410 wrap=False, tol=_TOL):

411 '''Compute the I{perpendicular} intersection of a geodesic (line) with

412 a geodesic from this point.

413

414 @arg point: A location (C{LatLon}) on the geodesic (line).

415 @arg other: An other point (C{LatLon}) on the geodesic (line) or the

416 (forward) bearing at the B{C{point}} (compass C{degrees}).

417 @kwarg exact: Exact C{geodesic...} to use (C{bool} or C{Geodesic...}),

418 see method L{Ellipsoid.geodesic_}.

419 @kwarg height: Optional height for the intersection point (C{meter},

420 conventionally) or C{None} for an interpolated height.

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

422 and/or B{C{other}} (C{bool}).

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

424

425 @return: The intersection point, an instance of same C{LatLon} class.

426

427 @raise TypeError: If B{C{point}} or B{C{other}} not same C{LatLon} class.

428

429 @raise UnitError: Invalid B{C{other}}, B{C{exact}} or B{C{height}}.

430 '''

431 try:

432 g, gl, p = self._g_gl_p3(point, other, exact, wrap)

433

434 P = _MODS.geodesicw._PlumbTo(gl, self.lat, self.lon, tol=tol)

435 h = self._havg(p, h=height)

436 p = self.classof(P.lat2, P.lon2, datum=self.datum, height=h) # name=n

437 p._iteration = P.iteration

438 except (TypeError, ValueError) as x:

439 raise _xError(x, plumb=self, point=point, other=other,

440 exact=exact, wrap=wrap)

441 return p

442

443

444 class _Box(object):

445 '''Bounding box around a C{LatLon} point.

446

447 @see: Function C{_box4} in .clipy.py.

448 '''

449 def __init__(self, center, distance):

450 '''New L{_Box} around point.

451

452 @arg center: The center point (C{LatLon}).

453 @arg distance: Radius, half-size of the box

454 (C{meter}, conventionally)

455 '''

456 m = Radius_(distance=distance)

457 E = center.ellipsoid() # XXX see above

458 d = E.m2degrees(m) + _0_5 # some margin

459 self._N = center.lat + d

460 self._S = center.lat - d

461 self._E = center.lon + d

462 self._W = center.lon - d

463

464 def overlaps(self, lat1, lon1, lat2, lon2):

465 '''Check whether this box overlaps an other box.

466

467 @arg lat1: Latitude of a box corner (C{degrees}).

468 @arg lon1: Longitude of a box corner (C{degrees}).

469 @arg lat2: Latitude of the opposing corner (C{degrees}).

470 @arg lon2: Longitude of the opposing corner (C{degrees}).

471

472 @return: C{True} if there is some overlap, C{False}

473 otherwise (C{bool}).

474 '''

475 if lat1 > lat2:

476 lat1, lat2 = lat2, lat1

477 if lat1 > self._N or lat2 < self._S:

478 return False

479 if lon1 > lon2:

480 lon1, lon2 = lon2, lon1

481 if lon1 > self._E or lon2 < self._W:

482 return False

483 return True

484

485

486 class _Tol(object):

487 '''Handle a tolerance in C{meter} as C{degrees} and C{meter}.

488 '''

489 _deg = 0 # tol in degrees

490 _lat = 0

491 _m = 0 # tol in meter

492 _min = MAX # degrees

493 _prev = None

494 _r = 0

495

496 def __init__(self, tol_m, E, lat, *lats):

497 '''New L{_Tol}.

498

499 @arg tol_m: Tolerance (C{meter}, only).

500 @arg E: Earth ellipsoid (L{Ellipsoid}).

501 @arg lat: Latitude (C{degrees}).

502 @arg lats: Additional latitudes (C{degrees}).

503 '''

504 self._lat = fmean_(lat, *lats) if lats else lat

505 self._r = max(EPS, E.rocMean(self._lat))

506 self._m = max(EPS, tol_m)

507 self._deg = max(EPS, degrees(self._m / self._r)) # avoid m2degrees!

508

509 @property_RO

510 def degrees(self):

511 '''Get this tolerance in C{degrees}.

512 '''

513 return self._deg

514

515 def degrees2m(self, deg):

516 '''Convert B{C{deg}} to meter at the same C{lat} and earth radius.

517 '''

518 return self.radius * radians(deg) / PI2 # avoid degrees2m!

519

520 def degError(self, Error=_ValueError):

521 '''Compose an error with the C{deg}rees minimum.

522 '''

523 return self.mError(self.degrees2m(self._min), Error=Error)

524

525 def done(self, deg):

526 '''Check C{deg} vs. tolerance and previous value.

527 '''

528 if deg < self._deg or deg == self._prev:

529 return True

530 self._min = min(self._min, deg)

531 self._prev = deg

532 return False

533

534 @property_RO

535 def lat(self):

536 '''Get the mean latitude in C{degrees}.

537 '''

538 return self._lat

539

540 def mError(self, m, Error=_ValueError):

541 '''Compose an error with B{C{m}}eter minimum.

542 '''

543 t = _SPACE_(Fmt.tolerance(self.meter), _too_(_low_))

544 if m2km(m) > self.meter:

545 t = _or(t, _antipodal_, _near_(_polar__))

546 return Error(Fmt.no_convergence(m), txt=t)

547

548 @property_RO

549 def meter(self):

550 '''Get this tolerance in C{meter}.

551 '''

552 return self._m

553

554 @property_RO

555 def radius(self):

556 '''Get the earth radius in C{meter}.

557 '''

558 return self._r

559

560 def reset(self):

561 '''Reset tolerances.

562 '''

563 self._min = MAX

564 self._prev = None

565

566

567 def _Equidistant00(equidistant, p1):

568 '''(INTERNAL) Get an C{Equidistant*(0, 0, ...)} instance.

569 '''

570 if equidistant is None or not callable(equidistant):

571 equidistant = p1.Equidistant

572 else:

573 _xsubclassof(*_MODS.azimuthal._Equidistants,

574 equidistant=equidistant)

575 return equidistant(0, 0, p1.datum)

576

577

578 def intersecant2(center, circle, point, other, **exact_height_wrap_tol):

579 '''Compute the intersections of a circle and a geodesic given as two points

580 or as a point and (forward) bearing.

581

582 @arg center: Center of the circle (C{LatLon}).

583 @arg circle: Radius of the circle (C{meter}, conventionally) or a point on

584 the circle (C{LatLon}, as B{C{center}}).

585 @arg point: A point of the geodesic (C{LatLon}, as B{C{center}}).

586 @arg other: An other point of the geodesic (C{LatLon}, as B{C{center}}) or

587 the (forward) bearing at the B{C{point}} (compass C{degrees}).

588 @kwarg exact_height_wrap_tol: Optional keyword arguments, see below.

589

590 @raise NotImplementedError: Method C{intersecant2} not available.

591

592 @raise TypeError: If B{C{center}}, B{C{point}} or B{C{circle}} or B{C{other}}

593 points not ellipsoidal or not compatible with B{C{center}}.

594

595 @see: Method C{LatLon.intersecant2} of class L{ellipsoidalExact.LatLon},

596 L{ellipsoidalKarney.LatLon} or L{ellipsoidalVincenty.LatLon}.

597 '''

598 if not isLatLon(center, ellipsoidal=True): # isinstance(center, LatLonEllipsoidalBase)

599 raise _IsnotError(_ellipsoidal_, center=center)

600 return center.intersecant2(circle, point, other, **exact_height_wrap_tol)

601

602

603 def _intersect3(s1, end1, s2, end2, height=None, wrap=False, # MCCABE 16 was=True

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

605 '''(INTERNAL) Intersect two (ellipsoidal) lines, see ellipsoidal method

606 L{intersection3}, separated to allow callers to embellish any exceptions.

607 '''

608 _LLS = _MODS.sphericalTrigonometry.LatLon

609 _si = _MODS.sphericalTrigonometry._intersect

610 _vi3 = _MODS.vector3d._intersect3d3

611

612 def _b_d(s, e, w, t, h=_0_0):

613 # compute opposing and distance

614 t = s.classof(t.lat, t.lon, height=h, name=t.name)

615 t = s.distanceTo3(t, wrap=w) # Distance3Tuple

616 b = opposing(e, t.initial) # "before" start

617 return b, t.distance

618

619 def _b_e(s, e, w, t):

620 # compute an end point along the initial bearing about

621 # 1.5 times the distance to the gu-/estimate, at least

622 # 1/8 and at most 3/8 of the earth perimeter like the

623 # radians in .sphericalTrigonometry._int3d2 and bearing

624 # comparison in .sphericaltrigonometry._intb

625 b, d = _b_d(s, e, w, t, h=t.height)

626 m = s.ellipsoid().R2x * PI_4 # authalic exact

627 d = min(max(d * _B2END, m), m * _3_0)

628 e = s.destination(d, e)

629 return b, (_unrollon(s, e) if w else e)

630

631 def _e_ll(s, e, w, **end):

632 # return 2-tuple (end, False if bearing else True)

633 ll = not _isDegrees(e)

634 if ll:

635 e = s.others(**end)

636 if w: # unroll180 == .karney._unroll2

637 e = _unrollon(s, e)

638 return e, ll

639

640 def _o(o, b, n, s, t, e):

641 # determine C{o}utside before, on or after start point

642 if not o: # intersection may be on start

643 if _isequalTo(s, t, eps=e.degrees):

644 return o

645 return -n if b else n

646

647 E = s1.ellipsoids(s2)

648

649 e1, ll1 = _e_ll(s1, end1, wrap, end1=end1)

650 e2, ll2 = _e_ll(s2, end2, wrap, end2=end2)

651

652 e = _Tol(tol, E, s1.lat, (e1.lat if ll1 else s1.lat),

653 s2.lat, (e2.lat if ll2 else s2.lat))

654

655 # get the azimuthal equidistant projection

656 A = _Equidistant00(equidistant, s1)

657

658 # gu-/estimate initial intersection, spherically ...

659 t = _si(_LLS(s1), (_LLS(e1) if ll1 else e1),

660 _LLS(s2), (_LLS(e2) if ll2 else e2),

661 height=height, wrap=False, LatLon=_LLS) # unrolled already

662 h, n = t.height, t.name

663

664 if not ll1:

665 b1, e1 = _b_e(s1, e1, wrap, t)

666 if not ll2:

667 b2, e2 = _b_e(s2, e2, wrap, t)

668

669 # ... and iterate as Karney describes, for references

670 # @see: Function L{ellipsoidalKarney.intersection3}.

671 for i in range(1, _TRIPS):

672 A.reset(t.lat, t.lon) # gu-/estimate as origin

673 # convert start and end points to projection

674 # space and compute an intersection there

675 v, o1, o2 = _vi3(*A._forwards(s1, e1, s2, e2),

676 eps=e.meter, useZ=False)

677 # convert intersection back to geodetic

678 t, d = A._reverse2(v)

679 if e.done(d): # below tol or unchanged?

680 break

681 else:

682 raise e.degError(Error=IntersectionError)

683

684 # like .sphericalTrigonometry._intersect, if this intersection

685 # is "before" the first point, use the antipodal intersection

686 if not (ll1 or ll2): # end1 and end2 are an initial bearing

687 b1, _ = _b_d(s1, end1, wrap, t)

688 if b1:

689 t = t.antipodal()

690 b1 = not b1

691 b2, _ = _b_d(s2, end2, wrap, t)

692

693 r = _LL4Tuple(t.lat, t.lon, h, t.datum, LatLon, LatLon_kwds, inst=s1,

694 iteration=i, name=n)

695 return Intersection3Tuple(r, (o1 if ll1 else _o(o1, b1, 1, s1, t, e)),

696 (o2 if ll2 else _o(o2, b2, 2, s2, t, e)))

697

698

699 def _intersection3(start1, end1, start2, end2, height=None, wrap=False, # was=True

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

701 '''(INTERNAL) Iteratively compute the intersection point of two lines,

702 each defined by two (ellipsoidal) points or an (ellipsoidal) start

703 point and an initial bearing from North.

704 '''

705 s1 = _xellipsoidal(start1=start1)

706 s2 = s1.others(start2=start2)

707 try:

708 return _intersect3(s1, end1, s2, end2, height=height, wrap=wrap,

709 equidistant=equidistant, tol=tol,

710 LatLon=LatLon, **LatLon_kwds)

711 except (TypeError, ValueError) as x:

712 raise _xError(x, start1=start1, end1=end1, start2=start2, end2=end2)

713

714

715 def _intersections2(center1, radius1, center2, radius2, height=None, wrap=False, # was=True

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

717 '''(INTERNAL) Iteratively compute the intersection points of two circles,

718 each defined by an (ellipsoidal) center point and a radius.

719 '''

720 c1 = _xellipsoidal(center1=center1)

721 c2 = c1.others(center2=center2)

722 try:

723 return _intersects2(c1, radius1, c2, radius2, height=height, wrap=wrap,

724 equidistant=equidistant, tol=tol,

725 LatLon=LatLon, **LatLon_kwds)

726 except (TypeError, ValueError) as x:

727 raise _xError(x, center1=center1, radius1=radius1,

728 center2=center2, radius2=radius2)

729

730

731 def _intersects2(c1, radius1, c2, radius2, height=None, wrap=False, # MCCABE 16 was=True

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

733 '''(INTERNAL) Intersect two (ellipsoidal) circles, see L{_intersections2}

734 above, separated to allow callers to embellish any exceptions.

735 '''

736 _LLS = _MODS.sphericalTrigonometry.LatLon

737 _si2 = _MODS.sphericalTrigonometry._intersects2

738 _vi2 = _MODS.vector3d._intersects2

739

740 def _ll4(t, h, n, c):

741 return _LL4Tuple(t.lat, t.lon, h, t.datum, LatLon, LatLon_kwds, inst=c,

742 iteration=t.iteration, name=n)

743

744 r1 = Radius_(radius1=radius1)

745 r2 = Radius_(radius2=radius2)

746

747 E = c1.ellipsoids(c2)

748 # get the azimuthal equidistant projection

749 A = _Equidistant00(equidistant, c1)

750

751 if r1 < r2:

752 c1, c2 = c2, c1

753 r1, r2 = r2, r1

754

755 if r1 > (min(E.b, E.a) * PI):

756 raise _ValueError(_exceed_PI_radians_)

757

758 if wrap: # unroll180 == .karney._unroll2

759 c2 = _unrollon(c1, c2)

760

761 # distance between centers and radii are

762 # measured along the ellipsoid's surface

763 m = c1.distanceTo(c2, wrap=False) # meter

764 if m < max(r1 - r2, EPS):

765 raise IntersectionError(_near_(_concentric_)) # XXX ConcentricError?

766 if fsumf_(r1, r2, -m) < 0:

767 raise IntersectionError(_too_(Fmt.distant(m)))

768

769 f = _radical2(m, r1, r2).ratio # "radical fraction"

770 e = _Tol(tol, E, favg(c1.lat, c2.lat, f=f))

771

772 # gu-/estimate initial intersections, spherically ...

773 t1, t2 = _si2(_LLS(c1), r1, _LLS(c2), r2, radius=e.radius,

774 height=height, wrap=False, too_d=m) # unrolled already

775 h, n = t1.height, t1.name

776

777 # ... and iterate as Karney describes, for references

778 # @see: Function L{ellipsoidalKarney.intersections2}.

779 ts, ta = [], None

780 for t in ((t1,) if t1 is t2 else (t1, t2)):

781 for i in range(1, _TRIPS):

782 A.reset(t.lat, t.lon) # gu-/estimate as origin

783 # convert centers to projection space and

784 # compute the intersections there

785 t1, t2 = A._forwards(c1, c2)

786 v1, v2 = _vi2(t1, r1, # XXX * t1.scale?,

787 t2, r2, # XXX * t2.scale?,

788 sphere=False, too_d=m)

789 # convert intersections back to geodetic

790 if v1 is v2: # abutting

791 t, d = A._reverse2(v1)

792 else: # consider the closer intersection

793 t1, d1 = A._reverse2(v1)

794 t2, d2 = A._reverse2(v2)

795 t, d = (t1, d1) if d1 < d2 else (t2, d2)

796 if e.done(d): # below tol or unchanged?

797 t._iteration = i # _NamedTuple._iteration

798 ts.append(t)

799 if v1 is v2: # abutting

800 ta = t

801 break

802 else:

803 raise e.degError(Error=IntersectionError)

804 e.reset()

805

806 if ta: # abutting circles

807 pass # PYCHOK no cover

808 elif len(ts) == 2:

809 return (_ll4(ts[0], h, n, c1),

810 _ll4(ts[1], h, n, c2))

811 elif len(ts) == 1: # PYCHOK no cover

812 ta = ts[0] # assume abutting

813 else: # PYCHOK no cover

814 raise _AssertionError(ts=ts)

815 r = _ll4(ta, h, n, c1)

816 return r, r

817

818

819 def _nearestOn2(p, point1, point2, within=True, height=None, wrap=False, # was=True

820 equidistant=None, tol=_TOL_M, **LatLon_and_kwds):

821 '''(INTERNAL) Closest point and fraction, like L{_intersects2} above,

822 separated to allow callers to embellish any exceptions.

823 '''

824 p1 = p.others(point1=point1)

825 p2 = p.others(point2=point2)

826

827 _ = p.ellipsoids(p1)

828# E = p.ellipsoids(p2) # done in _nearestOn3

829

830 # get the azimuthal equidistant projection

831 A = _Equidistant00(equidistant, p)

832

833 p1, p2, _ = _unrollon3(p, p1, p2, wrap) # XXX don't unroll?

834 r, f, _ = _nearestOn3(p, p1, p2, A, within=within, height=height,

835 tol=tol, **LatLon_and_kwds)

836 return NearestOn2Tuple(r, f)

837

838

839 def _nearestOn3(p, p1, p2, A, within=True, height=None, tol=_TOL_M,

840 LatLon=None, **LatLon_kwds):

841 # Only in function C{_nearestOn2} and method C{nearestOn8} above

842 _LLS = _MODS.sphericalNvector.LatLon

843 _V3d = _MODS.vector3d.Vector3d

844 _vn2 = _MODS.vector3d._nearestOn2

845

846 E = p.ellipsoids(p2)

847 e = _Tol(tol, E, p.lat, p1.lat, p2.lat)

848

849 # gu-/estimate initial nearestOn, spherically ... wrap=False, only!

850 # using sphericalNvector.LatLon.nearestOn for within=False support

851 t = _LLS(p).nearestOn(_LLS(p1), _LLS(p2), within=within,

852 height=height)

853 n, h = t.name, t.height

854 if height is None:

855 h1 = p1.height # use heights as pseudo-Z in projection space

856 h2 = p2.height # to be included in the closest function

857 h0 = favg(h1, h2)

858 else: # ignore heights in distances, Z=0

859 h0 = h1 = h2 = _0_0

860

861 # ... and iterate to find the closest (to the origin with .z

862 # to interpolate height) as Karney describes, for references

863 # @see: Function L{ellipsoidalKarney.nearestOn}.

864 vp, f = _V3d(_0_0, _0_0, h0), None

865 for i in range(1, _TRIPS):

866 A.reset(t.lat, t.lon) # gu-/estimate as origin

867 # convert points to projection space and compute

868 # the nearest one (and its height) there

869 s, t = A._forwards(p1, p2)

870 v, f = _vn2(vp, _V3d(s.x, s.y, h1),

871 _V3d(t.x, t.y, h2), within=within)

872 # convert nearest one back to geodetic

873 t, d = A._reverse2(v)

874 if e.done(d): # below tol or unchanged?

875 break

876 else:

877 raise e.degError()

878

879 if height is None:

880 h = v.z # nearest

881 elif _isHeight(height):

882 h = height

883 r = _LL4Tuple(t.lat, t.lon, h, t.datum, LatLon, LatLon_kwds, inst=p,

884 iteration=i, name=n)

885 return r, f, e # fraction or None

886

887

888__all__ += _ALL_DOCS(LatLonEllipsoidalBaseDI, intersecant2)

889

890# **) MIT License

891#

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

893#

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

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

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

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

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

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

900#

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

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

903#

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

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

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

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

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

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

910# OTHER DEALINGS IN THE SOFTWARE.