# Coverage for pygeodesy/ellipsoidalBase.py : 96%

## 255 statements 244 run 11 missing 18 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 | # -*- coding: utf-8 -*- u'''(INTERNAL) Ellipsoidal base classes C{CartesianEllipsoidalBase} and C{LatLonEllipsoidalBase}. Pure Python implementation of geodesy tools for ellipsoidal earth models, transcribed in part from JavaScript originals by I{(C) Chris Veness 2005-2016} and published under the same MIT Licence**, see for example U{latlon-ellipsoidal <https://www.Movable-Type.co.UK/scripts/geodesy/docs/latlon-ellipsoidal.js.html>}. @newfield example: Example, Examples ''' from pygeodesy.basics import EPS, PI, property_doc_, property_RO, \ _xinstanceof, _xkwds from pygeodesy.cartesianBase import CartesianBase from pygeodesy.datum import Datum, Datums from pygeodesy.ecef import EcefVeness from pygeodesy.errors import _AssertionError, _incompatible, IntersectionError, \ _IsnotError, _ValueError, _xellipsoidal from pygeodesy.fmath import favg, fsum_ from pygeodesy.interns import _center2_, _COMMA_, _datum_, _ellipsoidal_, \ _exceed_PI_radians_, _Missing, _N_, \ _near_concentric_, NN, _no_convergence_fmt_, \ _no_conversion_, _radius1_, _radius2_, \ _too_distant_fmt_ # PYCHOK used! from pygeodesy.latlonBase import LatLonBase from pygeodesy.lazily import _ALL_DOCS from pygeodesy.named import LatLon4Tuple, Vector3Tuple, _xnamed from pygeodesy.trf import _2epoch, RefFrame, TRFError, _reframeTransforms from pygeodesy.units import Radius_ __all__ = () __version__ = '20.08.12' _TOL_M = 1e-3 # 1 millimeter, in .ellipsoidKarney, -Vincenty _TRIPS = 16 # _intersects2 interations, 6 sufficient class CartesianEllipsoidalBase(CartesianBase): '''(INTERNAL) Base class for ellipsoidal C{Cartesian}s. ''' _datum = Datums.WGS84 #: (INTERNAL) L{Datum}. _Ecef = EcefVeness #: (INTERNAL) Preferred C{Ecef...} class, backward compatible. def convertRefFrame(self, reframe2, reframe, epoch=None): '''Convert this cartesian point from one to an other reference frame. @arg reframe2: Reference frame to convert I{to} (L{RefFrame}). @arg reframe: Reference frame to convert I{from} (L{RefFrame}). @kwarg epoch: Optional epoch to observe for B{C{reframe}}, a fractional calendar year (C{scalar}). @return: The converted point (C{Cartesian}) or this point if conversion is C{nil}. @raise TRFError: No conversion available from B{C{reframe}} to B{C{reframe2}}. @raise TypeError: B{C{reframe2}} or B{C{reframe}} not a L{RefFrame} or B{C{epoch}} not C{scalar}. ''' _xinstanceof(RefFrame, reframe2=reframe2, reframe=reframe) c, d = self, self.datum for t in _reframeTransforms(reframe2, reframe, reframe.epoch if epoch is None else _2epoch(epoch)): c = c._applyHelmert(t, False, datum=d) return c class LatLonEllipsoidalBase(LatLonBase): '''(INTERNAL) Base class for ellipsoidal C{LatLon}s. ''' _convergence = None #: (INTERNAL) UTM/UPS meridian convergence (C{degrees}). _datum = Datums.WGS84 #: (INTERNAL) Datum (L{Datum}). _elevation2 = () #: (INTERNAL) Cached C{elevation2} result. _epoch = None #: (INTERNAL) overriding .reframe.epoch (C{float}). _etm = None #: (INTERNAL) Cached toEtm (L{Etm}). _geoidHeight2 = () #: (INTERNAL) Cached C{geoidHeight2} result. _iteration = None #: (INTERNAL) Iteration number (C{int} or C{None}). _lcc = None #: (INTERNAL) Cached toLcc (C{Lcc}). _osgr = None #: (INTERNAL) Cached toOsgr (C{Osgr}). _reframe = None #: (INTERNAL) reference frame (L{RefFrame}). _scale = None #: (INTERNAL) UTM/UPS scale factor (C{float}). _ups = None #: (INTERNAL) Cached toUps (L{Ups}). _utm = None #: (INTERNAL) Cached toUtm (L{Utm}). _wm = None #: (INTERNAL) Cached toWm (webmercator.Wm instance). def __init__(self, lat, lon, height=0, datum=None, reframe=None, epoch=None, name=NN): '''Create an ellipsoidal C{LatLon} point frome the given lat-, longitude and height on the given datum and with the given reference frame and epoch. @arg lat: Latitude (C{degrees} or DMS C{[N|S]}). @arg lon: Longitude (C{degrees} or DMS C{str[E|W]}). @kwarg height: Optional elevation (C{meter}, the same units as the datum's half-axes). @kwarg datum: Optional, ellipsoidal datum to use (L{Datum}). @kwarg reframe: Optional reference frame (L{RefFrame}). @kwarg epoch: Optional epoch to observe for B{C{reframe}} (C{scalar}), a non-zero, fractional calendar year. @kwarg name: Optional name (string). @raise TypeError: B{C{datum}} is not a L{datum}, B{C{reframe}} is not a L{RefFrame} or B{C{epoch}} is not C{scalar} non-zero. @example: >>> p = LatLon(51.4778, -0.0016) # height=0, datum=Datums.WGS84 ''' LatLonBase.__init__(self, lat, lon, height=height, name=name) if datum and datum != self._datum: self.datum = datum if reframe: self.reframe = reframe self.epoch = epoch def _Radjust2(self, adjust, datum, meter_text2): '''(INTERNAL) Adjust elevation or geoidHeight with difference in Gaussian radii of curvature of given datum and NAD83. @note: This is an arbitrary, possibly incorrect adjustment. ''' if adjust: # Elevation2Tuple or GeoidHeight2Tuple m, t = meter_text2 if isinstance(m, float): n = Datums.NAD83.ellipsoid.rocGauss(self.lat) if min(abs(m), n) > EPS: # use ratio, datum and NAD83 units may differ e = self.ellipsoid(datum).rocGauss(self.lat) if min(abs(e - n), e) > EPS: m *= e / n meter_text2 = meter_text2.classof(m, t) return self._xnamed(meter_text2) def _update(self, updated, *attrs): '''(INTERNAL) Zap cached attributes if updated. ''' if updated: LatLonBase._update(self, updated, '_etm', '_lcc', '_osgr', '_ups', '_utm', '_wm', *attrs) if self._elevation2: self._elevation2 = () if self._geoidHeight2: self._geoidHeight2 = () def antipode(self, height=None): '''Return the antipode, the point diametrically opposite to this point. @kwarg height: Optional height of the antipode, height of this point otherwise (C{meter}). @return: The antipodal point (C{LatLon}). ''' lla = LatLonBase.antipode(self, height=height) if lla.datum != self.datum: lla.datum = self.datum return lla @property_RO def convergence(self): '''Get this point's UTM or UPS meridian convergence (C{degrees}) or C{None} if not converted from L{Utm} or L{Ups}. ''' return self._convergence def convertDatum(self, datum2): '''Convert this point to an other datum. @arg datum2: Datum to convert I{to} (L{Datum}). @return: The converted point (ellipsoidal C{LatLon}). @raise TypeError: The B{C{datum2}} is not a L{Datum}. @example: >>> p = LatLon(51.4778, -0.0016) # default Datums.WGS84 >>> p.convertDatum(Datums.OSGB36) # 51.477284°N, 000.00002°E ''' if self.datum == datum2: return self.copy() c = self.toCartesian().convertDatum(datum2) return c.toLatLon(datum=datum2, LatLon=self.classof) def convertRefFrame(self, reframe2): '''Convert this point to an other reference frame. @arg reframe2: Reference frame to convert I{to} (L{RefFrame}). @return: The converted point (ellipsoidal C{LatLon}) or this point if conversion is C{nil}. @raise TRFError: No B{C{.reframe}} or no conversion available from B{C{.reframe}} to B{C{reframe2}}. @raise TypeError: The B{C{reframe2}} is not a L{RefFrame}. @example: >>> p = LatLon(51.4778, -0.0016, reframe=RefFrames.ETRF2000) # default Datums.WGS84 >>> p.convertRefFrame(RefFrames.ITRF2014) # 51.477803°N, 000.001597°W, +0.01m ''' _xinstanceof(RefFrame, reframe2=reframe2) if not self.reframe: raise TRFError(_no_conversion_, txt='%r.reframe %s' % (self, _Missing)) ts = _reframeTransforms(reframe2, self.reframe, self.epoch) if ts: c = self.toCartesian() for t in ts: c = c._applyHelmert(t, False) ll = c.toLatLon(datum=self.datum, LatLon=self.classof, epoch=self.epoch, reframe=reframe2) # ll.reframe, ll.epoch = reframe2, self.epoch else: ll = self return ll @property_doc_(''' this points's datum (L{Datum}).''') def datum(self): '''Get this point's datum (L{Datum}). ''' return self._datum @datum.setter # PYCHOK setter! def datum(self, datum): '''Set this point's datum I{without conversion}. @arg datum: New datum (L{Datum}). @raise TypeError: The B{C{datum}} is not a L{Datum} or not ellipsoidal. ''' _xinstanceof(Datum, datum=datum) if not datum.isEllipsoidal: raise _IsnotError(_ellipsoidal_, datum=datum) self._update(datum != self._datum) self._datum = datum def distanceTo2(self, other): '''Approximate the distance and (initial) bearing between this and an other (ellipsoidal) point based on the radii of curvature. Suitable only for short distances up to a few hundred Km or Miles and only between non-near-polar points. @arg other: The other point (C{LatLon}). @return: An L{Distance2Tuple}C{(distance, initial)}. @raise TypeError: The B{C{other}} point is not C{LatLon}. @raise ValueError: Incompatible datum ellipsoids. @see: Method L{Ellipsoid.distance2} and U{Local, flat earth approximation<https://www.EdWilliams.org/avform.htm#flat>}. ''' return self.ellipsoids(other).distance2(self.lat, self.lon, other.lat, other.lon) def elevation2(self, adjust=True, datum=Datums.WGS84, timeout=2): '''Return elevation of this point for its or the given datum. @kwarg adjust: Adjust the elevation for a B{C{datum}} other than C{NAD83} (C{bool}). @kwarg datum: Optional datum (L{Datum}). @kwarg timeout: Optional query timeout (C{seconds}). @return: An L{Elevation2Tuple}C{(elevation, data_source)} or C{(None, error)} in case of errors. @note: The adjustment applied is the difference in geocentric earth radius between the B{C{datum}} and C{NAV83} upon which the L{elevations.elevation2} is based. @note: NED elevation is only available for locations within the U{Conterminous US (CONUS) <https://WikiPedia.org/wiki/Contiguous_United_States>}. @see: Function L{elevations.elevation2} and method L{Ellipsoid.Rgeocentric} for further details and possible C{error}s. ''' if not self._elevation2: # get elevation and data source from pygeodesy.elevations import elevation2 self._elevation2 = elevation2(self.lat, self.lon, timeout=timeout) return self._Radjust2(adjust, datum, self._elevation2) def ellipsoid(self, datum=Datums.WGS84): '''Return the ellipsoid of this point's datum or the given datum. @kwarg datum: Default datum (L{Datum}). @return: The ellipsoid (L{Ellipsoid}). ''' return getattr(self, _datum_, datum).ellipsoid def ellipsoids(self, other): '''Check the type and ellipsoid of this and an other point's datum. @arg other: The other point (C{LatLon}). @return: This point's datum ellipsoid (L{Ellipsoid}). @raise TypeError: The B{C{other}} point is not C{LatLon}. @raise ValueError: Incompatible datum ellipsoids. ''' self.others(other, up=2) # ellipsoids' caller E = self.ellipsoid() try: # other may be Sphere, etc. e = other.ellipsoid() except AttributeError: # PYCHOK no cover try: # no ellipsoid method, try datum e = other.datum.ellipsoid except AttributeError: e = E # no datum, XXX assume equivalent? if e != E: raise _ValueError(e.named2, txt=_incompatible(E.named2)) return E @property_doc_(''' this point's observed epoch (C{float}).''') def epoch(self): '''Get this point's observed epoch (C{float}) or C{None}. ''' return self._epoch or (self.reframe.epoch if self.reframe else None) @epoch.setter # PYCHOK setter! def epoch(self, epoch): '''Set or clear this point's observed epoch. @arg epoch: Observed epoch, a fractional calendar year (C{scalar}) or C{None}. @raise TypeError: The B{C{epoch}} is not C{scalar}. ''' self._epoch = None if epoch is None else _2epoch(epoch) def geoidHeight2(self, adjust=False, datum=Datums.WGS84, timeout=2): '''Return geoid height of this point for its or the given datum. @kwarg adjust: Adjust the geoid height for a B{C{datum}} other than C{NAD83/NADV88} (C{bool}). @kwarg datum: Optional datum (L{Datum}). @kwarg timeout: Optional query timeout (C{seconds}). @return: An L{GeoidHeight2Tuple}C{(height, model_name)} or C{(None, error)} in case of errors. @note: The adjustment applied is the difference in geocentric earth radius between the B{C{datum}} and C{NAV83/NADV88} upon which the L{elevations.geoidHeight2} is based. @note: The geoid height is only available for locations within the U{Conterminous US (CONUS) <https://WikiPedia.org/wiki/Contiguous_United_States>}. @see: Function L{elevations.geoidHeight2} and method L{Ellipsoid.Rgeocentric} for further details and possible C{error}s. ''' if not self._geoidHeight2: # get elevation and data source from pygeodesy.elevations import geoidHeight2 self._geoidHeight2 = geoidHeight2(self.lat, self.lon, model=0, timeout=timeout) return self._Radjust2(adjust, datum, self._geoidHeight2) def intersections2(self, radius1, other, radius2, height=None, wrap=True, equidistant=None, tol=_TOL_M): '''Compute the intersection points of two circles each defined by a center point and a radius. @arg radius1: Radius of the this circle (C{meter}). @arg other: Center of the other circle (C{LatLon}). @arg radius2: Radius of the other circle (C{meter}). @kwarg height: Optional height for the intersection points, overriding the "radical height" at the "radical line" between both centers (C{meter}) or C{None}. @kwarg wrap: Wrap and unroll longitudes (C{bool}). @kwarg equidistant: An azimuthal equidistant projection class (L{Equidistant} or L{EquidistantKarney}), function L{azimuthal.equidistant} will be invoked if left unspecified. @kwarg tol: Convergence tolerance (C{meter}). @return: 2-Tuple of the intersection points, each a C{LatLon} instance. For abutting circles, the intersection points are the same instance. @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting circles or no convergence for B{C{tol}}. @raise TypeError: If B{C{other}} is not C{LatLon}. @raise ValueError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{height}}. ''' self.others(other) return _intersects2(self, radius1, other, radius2, height=height, wrap=wrap, equidistant=equidistant, tol=tol, LatLon=self.classof) @property_RO def iteration(self): '''Get the iteration number (C{int} or C{None} if not available/applicable). ''' return self._iteration def parse(self, strllh, height=0, datum=None, sep=_COMMA_, name=NN): '''Parse a string representing a similar, ellipsoidal C{LatLon} point, consisting of C{"lat, lon[, height]"}. @arg strllh: Lat, lon and optional height (C{str}), see function L{parse3llh}. @kwarg height: Optional, default height (C{meter} or C{None}). @kwarg datum: Optional datum (L{Datum}), overriding this datum I{without conversion}. @kwarg sep: Optional separator (C{str}). @kwarg name: Optional instance name (C{str}), overriding this name. @return: The similar point (ellipsoidal C{LatLon}). @raise ParseError: Invalid B{C{strllh}}. ''' from pygeodesy.dms import parse3llh a, b, h = parse3llh(strllh, height=height, sep=sep) r = self.classof(a, b, height=h, datum=self.datum) if datum not in (None, self.datum): r.datum = datum return _xnamed(r, name or self.name, force=True) @property_doc_(''' this point's reference frame (L{RefFrame}).''') def reframe(self): '''Get this point's reference frame (L{RefFrame}) or C{None}. ''' return self._reframe @reframe.setter # PYCHOK setter! def reframe(self, reframe): '''Set or clear this point's reference frame. @arg reframe: Reference frame (L{RefFrame}) or C{None}. @raise TypeError: The B{C{reframe}} is not a L{RefFrame}. ''' if reframe is not None: _xinstanceof(RefFrame, reframe=reframe) self._reframe = reframe elif self.reframe is not None: self._reframe = None @property_RO def scale(self): '''Get this point's UTM grid or UPS point scale factor (C{float}) or C{None} if not converted from L{Utm} or L{Ups}. ''' return self._scale def to3xyz(self): # PYCHOK no cover '''DEPRECATED, use method C{toEcef}. @return: A L{Vector3Tuple}C{(x, y, z)}. @note: Overloads C{LatLonBase.to3xyz} ''' r = self.toEcef() return self._xnamed(Vector3Tuple(r.x, r.y, r.z)) def toEtm(self): '''Convert this C{LatLon} point to an ETM coordinate. @return: The ETM coordinate (L{Etm}). @see: Function L{toEtm8}. ''' if self._etm is None: from pygeodesy.etm import toEtm8, Etm # PYCHOK recursive import self._etm = toEtm8(self, datum=self.datum, Etm=Etm) return self._etm def toLcc(self): '''Convert this C{LatLon} point to a Lambert location. @see: Function L{toLcc} in module L{lcc}. @return: The Lambert location (L{Lcc}). ''' if self._lcc is None: from pygeodesy.lcc import Lcc, toLcc # PYCHOK recursive import self._lcc = toLcc(self, height=self.height, Lcc=Lcc, name=self.name) return self._lcc def toOsgr(self): '''Convert this C{LatLon} point to an OSGR coordinate. @see: Function L{toOsgr} in module L{osgr}. @return: The OSGR coordinate (L{Osgr}). ''' if self._osgr is None: from pygeodesy.osgr import Osgr, toOsgr # PYCHOK recursive import self._osgr = toOsgr(self, datum=self.datum, Osgr=Osgr, name=self.name) return self._osgr def toUps(self, pole=_N_, falsed=True): '''Convert this C{LatLon} point to a UPS coordinate. @kwarg pole: Optional top/center of (stereographic) projection (C{str}, 'N[orth]' or 'S[outh]'). @kwarg falsed: False easting and northing (C{bool}). @return: The UPS coordinate (L{Ups}). @see: Function L{toUps8}. ''' if self._ups is None: from pygeodesy.ups import toUps8, Ups # PYCHOK recursive import self._ups = toUps8(self, datum=self.datum, Ups=Ups, pole=pole, falsed=falsed) return self._ups def toUtm(self): '''Convert this C{LatLon} point to a UTM coordinate. @return: The UTM coordinate (L{Utm}). @see: Function L{toUtm8}. ''' if self._utm is None: from pygeodesy.utm import toUtm8, Utm # PYCHOK recursive import self._utm = toUtm8(self, datum=self.datum, Utm=Utm) return self._utm def toUtmUps(self, pole=NN): '''Convert this C{LatLon} point to a UTM or UPS coordinate. @kwarg pole: Optional top/center of UPS (stereographic) projection (C{str}, 'N[orth]' or 'S[outh]'). @return: The UTM or UPS coordinate (L{Utm} or L{Ups}). @raise TypeError: Result in L{Utm} or L{Ups}. @see: Function L{toUtmUps}. ''' if self._utm: u = self._utm elif self._ups and (self._ups.pole == pole or not pole): u = self._ups else: from pygeodesy.utmups import toUtmUps8, Utm, Ups # PYCHOK recursive import u = toUtmUps8(self, datum=self.datum, Utm=Utm, Ups=Ups, pole=pole) if isinstance(u, Utm): self._utm = u elif isinstance(u, Ups): self._ups = u else: _xinstanceof(Utm, Ups, toUtmUps8=u) return u def toWm(self): '''Convert this C{LatLon} point to a WM coordinate. @see: Function L{toWm} in module L{webmercator}. @return: The WM coordinate (L{Wm}). ''' if self._wm is None: from pygeodesy.webmercator import toWm # PYCHOK recursive import self._wm = toWm(self) return self._wm # (INTERNAL) C{_intersections2} imported by .ellipsoidalKarney and -Vincenty def _intersections2(center1, radius1, center2, radius2, height=None, wrap=True, equidistant=None, tol=_TOL_M, LatLon=None, **LatLon_kwds): '''Iteratively compute the intersection points of two circles each defined by an (ellipsoidal) center point and a radius. @arg center1: Center of the first circle (ellipsoidal C{LatLon}). @arg radius1: Radius of the first circle (C{meter}). @arg center2: Center of the second circle (ellipsoidal C{LatLon}). @arg radius2: Radius of the second circle (C{meter}). @kwarg height: Optional height for the intersection points, overriding the "radical height" at the "radical line" between both centers (C{meter}). @kwarg wrap: Wrap and unroll longitudes (C{bool}). @kwarg equidistant: An azimuthal equidistant projection class (L{Equidistant} or L{EquidistantKarney}) or C{None} for function L{azimuthal.equidistant}. @kwarg tol: Convergence tolerance (C{meter}). @kwarg LatLon: Optional class to return the intersection points (ellipsoidal C{LatLon}) or C{None}. @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments, ignored if B{C{LatLon=None}}. @return: 2-Tuple of the intersection points, each a B{C{LatLon}} instance or L{LatLon4Tuple}C{(lat, lon, height, datum)} if B{C{LatLon}} is C{None}. For abutting circles, the intersection points are the same instance. @raise ImportError: If B{C{equidistant}} is L{EquidistantKarney}) and package U{geographiclib <https://PyPI.org/project/geographiclib>} not installed or not found. @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting circles or no convergence for B{C{tol}}. @raise TypeError: If B{C{center1}} or B{C{center2}} not ellipsoidal. @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{height}}. @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/ calculating-intersection-of-two-circles>}, U{Karney's paper <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section 14 I{Maritime Boundaries}, U{circle-circle<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>} and U{sphere-sphere<https://MathWorld.Wolfram.com/Sphere-SphereIntersection.html>} intersections. ''' c1 = _xellipsoidal(center1=center1) c2 = c1.others(center2, name=_center2_) r1 = Radius_(radius1, name=_radius1_) r2 = Radius_(radius2, name=_radius2_) try: return _intersects2(c1, r1, c2, r2, height=height, wrap=wrap, equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) except (TypeError, ValueError) as x: raise IntersectionError(center1=center1, radius1=radius1, center2=center2, radius2=radius2, txt=str(x)) def _intersects2(c1, r1, c2, r2, height=None, wrap=True, # MCCABE 16 equidistant=None, tol=_TOL_M, LatLon=None, **LatLon_kwds): # (INTERNAL) Intersect two spherical circles, see L{_intersections2} # above, separated to allow callers to embellish any exceptions from pygeodesy.formy import _euclidean, _radical2 from pygeodesy.sphericalTrigonometry import _intersects2 as _si2, LatLon as _LLS from pygeodesy.utily import m2degrees, unroll180 from pygeodesy.vector3d import _intersects2 as _vi2 def _latlon4(t, h, n): if LatLon is None: r = LatLon4Tuple(t.lat, t.lon, h, t.datum) else: kwds = _xkwds(LatLon_kwds, datum=t.datum, height=h) r = LatLon(t.lat, t.lon, **kwds) r._iteration = t.iteration # ._iteration for tests return _xnamed(r, n) if r1 < r2: c1, c2 = c2, c1 r1, r2 = r2, r1 E = c1.ellipsoids(c2) if r1 > (E.b * PI): raise ValueError(_exceed_PI_radians_) if wrap: # unroll180 == .karney._unroll2 _, lon2 = unroll180(c1.lon, c2.lon, wrap=True) if lon2 != c2.lon: c2 = c2.classof(c2.lat, lon2, c2.height, datum=c2.datum) # distance between centers and radii are # measured along the ellipsoid's surface m = c1.distanceTo(c2, wrap=False) # meter if m < max(r1 - r2, EPS): raise ValueError(_near_concentric_) if fsum_(r1, r2, -m) < 0: raise ValueError(_too_distant_fmt_ % (m,)) f = _radical2(m, r1, r2).ratio # "radical lat" r = E.rocMean(favg(c1.lat, c2.lat, f=f)) e = max(m2degrees(tol, radius=r), EPS) # get the azimuthal equidistant projection if equidistant is None: from pygeodesy.azimuthal import equidistant as A else: A = equidistant # preferably EquidistantKarney A = A(0, 0, datum=c1.datum) # gu-/estimate initial intersections, spherically ... t1, t2 = _si2(_LLS(c1.lat, c1.lon, height=c1.height), r1, _LLS(c2.lat, c2.lon, height=c2.height), r2, radius=r, height=height, wrap=False, too_d=m) h, n = t1.height, t1.name # ... and then iterate like Karney suggests to find # tri-points of median lines, @see: references above ts, ta = [], None for t in ((t1,) if t1 is t2 else (t1, t2)): p = None # force d == p False for i in range(_TRIPS): A.reset(t.lat, t.lon) # gu-/estimate as origin # convert centers to projection space t1 = A.forward(c1.lat, c1.lon) t2 = A.forward(c2.lat, c2.lon) # compute intersections in projection space v1, v2 = _vi2(t1, r1, # XXX * t1.scale?, t2, r2, # XXX * t2.scale?, sphere=False, too_d=m) # convert intersections back to geodetic t1 = A.reverse(v1.x, v1.y) t2 = A.reverse(v2.x, v2.y) # consider only the closer intersection d1 = _euclidean(t1.lat - t.lat, t1.lon - t.lon) d2 = _euclidean(t2.lat - t.lat, t2.lon - t.lon) # break if below tolerance or if unchanged t, d = (t1, d1) if d1 < d2 else (t2, d2) if d < e or d == p: t._iteration = i + 1 # _NamedTuple._iteration ts.append(t) if v1 is v2: # abutting ta = t break p = d else: raise ValueError(_no_convergence_fmt_ % (tol,)) if ta: # abutting circles r = _latlon4(ta, h, n) elif len(ts) == 2: return _latlon4(ts[0], h, n), _latlon4(ts[1], h, n) elif len(ts) == 1: # XXX assume abutting r = _latlon4(ts[0], h, n) else: raise _AssertionError(ts=ts) return r, r __all__ += _ALL_DOCS(CartesianEllipsoidalBase, LatLonEllipsoidalBase) # **) MIT License # # Copyright (C) 2016-2020 -- mrJean1 at Gmail -- All Rights Reserved. # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE.