# Coverage for pygeodesy/sphericalBase.py : 94%

## 144 statements 135 run 9 missing 6 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 | # -*- coding: utf-8 -*- u'''(INTERNAL) Spherical base classes C{CartesianSphericalBase} and C{LatLonSphericalBase} used by C{.sphericalNvector} or C{.sphericalTrigonometry}. Pure Python implementation of geodetic (lat-/longitude) functions, transcribed in part from JavaScript originals by I{(C) Chris Veness 2011-2016} and published under the same MIT Licence**, see U{Latitude/Longitude<https://www.Movable-Type.co.UK/scripts/latlong.html>}. @newfield example: Example, Examples ''' # make sure int/int division yields float quotient, see .basics from __future__ import division from pygeodesy.basics import map1, property_doc_ from pygeodesy.cartesianBase import CartesianBase from pygeodesy.datums import Datums, _spherical_datum from pygeodesy.ellipsoids import R_M, R_MA from pygeodesy.errors import IntersectionError from pygeodesy.fmath import favg, fdot from pygeodesy.interns import EPS, NN, PI, PI2, PI_2, _COMMA_, \ _too_, _distant_, _exceed_PI_radians_, \ _near_concentric_, _1_0, _180_0, _360_0 from pygeodesy.latlonBase import LatLonBase, _trilaterate5 # PYCHOK passed from pygeodesy.lazily import _ALL_DOCS from pygeodesy.named import _xnamed from pygeodesy.namedTuples import Bearing2Tuple from pygeodesy.nvectorBase import NvectorBase from pygeodesy.units import Bearing_, Distance, Height, Radius, Radius_ from pygeodesy.utily import acos1, atan2b, degrees90, degrees180, \ sincos2, tanPI_2_2, wrapPI from math import cos, hypot, log, sin, sqrt __all__ = () __version__ = '20.12.22' def _angular(distance, radius): # PYCHOK for export '''(INTERNAL) Return the angular distance in radians. @raise ValueError: Invalid B{C{distance}} or B{C{radius}}. ''' return Distance(distance) / Radius_(radius) def _rads3(rad1, rad2, radius): # in .sphericalTrigonometry '''(INTERNAL) Convert radii to radians. ''' r1 = Radius_(rad1=rad1) r2 = Radius_(rad2=rad2) if radius is not None: # convert radii to radians r = _1_0 / Radius_(radius=radius) r1 *= r r2 *= r x = r1 < r2 if x: r1, r2 = r2, r1 if r1 > PI: raise IntersectionError(rad1=rad1, rad2=rad2, txt=_exceed_PI_radians_) return r1, r2, x class CartesianSphericalBase(CartesianBase): '''(INTERNAL) Base class for spherical C{Cartesian}s. ''' _datum = Datums.Sphere # L{Datum} def intersections2(self, rad1, other, rad2, radius=R_M): '''Compute the intersection points of two circles each defined by a center point and a radius. @arg rad1: Radius of the this circle (C{meter} or C{radians}, see B{C{radius}}). @arg other: Center of the other circle (C{Cartesian}). @arg rad2: Radius of the other circle (C{meter} or C{radians}, see B{C{radius}}). @kwarg radius: Mean earth radius (C{meter} or C{None} if both B{C{rad1}} and B{C{rad2}} are given in C{radians}). @return: 2-Tuple of the intersection points, each C{Cartesian}. The intersection points are the same C{Cartesian} instance for abutting circles. @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting circles. @raise TypeError: If B{C{other}} is not C{Cartesian}. @raise ValueError: Invalid B{C{rad1}}, B{C{rad2}} or B{C{radius}}. @see: U{Calculating intersection of two Circles <https://GIS.StackExchange.com/questions/48937/ calculating-intersection-of-two-circles>} and method or function C{trilaterate3d2}. ''' x1, x2 = self, self.others(other) r1, r2, x = _rads3(rad1, rad2, radius) if x: x1, x2 = x2, x1 n, q = x1.cross(x2), x1.dot(x2) n2, q21 = n.length2, _1_0 - q**2 if min(abs(q21), n2) < EPS: raise IntersectionError(center=self, other=other, txt=_near_concentric_) try: cr1, cr2 = cos(r1), cos(r2) a = (cr1 - q * cr2) / q21 b = (cr2 - q * cr1) / q21 x0 = x1.times(a).plus(x2.times(b)) except ValueError: raise IntersectionError(center=self, rad1=rad1, other=other, rad2=rad2) x = _1_0 - x0.length2 # XXX x0.dot(x0) if x < EPS: raise IntersectionError(center=self, rad1=rad1, other=other, rad2=rad2, txt=_too_(_distant_)) n = n.times(sqrt(x / n2)) if n.length > EPS: x1, x2 = x0.plus(n), x0.minus(n) else: # abutting circles x1 = x2 = x0 for x in (x1, x2): x.datum = self.datum x.name = self.intersections2.__name__ return x1, x2 class LatLonSphericalBase(LatLonBase): '''(INTERNAL) Base class for spherical C{LatLon}s. ''' _datum = Datums.Sphere # spherical L{Datum} def __init__(self, lat, lon, height=0, datum=None, name=NN): '''Create a spherical C{LatLon} point frome the given lat-, longitude and height on the given datum. @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, spherical datum to use (L{Datum}, L{Ellipsoid}, L{Ellipsoid2}, L{a_f2Tuple}) or C{scalar} earth radius). @kwarg name: Optional name (string). @raise TypeError: If B{C{datum}} invalid or not not spherical. @example: >>> p = LatLon(51.4778, -0.0016) # height=0, datum=Datums.WGS84 ''' LatLonBase.__init__(self, lat, lon, height=height, name=name) if datum not in (None, self.datum): self._datum = _spherical_datum(datum, name=self.name, raiser=True) def bearingTo2(self, other, wrap=False, raiser=False): '''Return the initial and final bearing (forward and reverse azimuth) from this to an other point. @arg other: The other point (C{LatLon}). @kwarg wrap: Wrap and unroll longitudes (C{bool}). @kwarg raiser: Optionally, raise L{CrossError} (C{bool}). @return: A L{Bearing2Tuple}C{(initial, final)}. @raise TypeError: The I{other} point is not spherical. @see: Methods C{initialBearingTo} and C{finalBearingTo}. ''' # .initialBearingTo is inside .-Nvector and .-Trigonometry i = self.initialBearingTo(other, wrap=wrap, raiser=raiser) # PYCHOK .initialBearingTo f = self.finalBearingTo( other, wrap=wrap, raiser=raiser) return self._xnamed(Bearing2Tuple(i, f)) @property_doc_(''' this point'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 spherical datum (L{Datum}, L{Ellipsoid}, L{Ellipsoid2}, L{a_f2Tuple}) or C{scalar} earth radius). @raise TypeError: If B{C{datum}} invalid or not not spherical. ''' d = _spherical_datum(datum, name=self.name, raiser=True) self._update(d != self._datum) self._datum = d def finalBearingTo(self, other, wrap=False, raiser=False): '''Return the final bearing (reverse azimuth) from this to an other point. @arg other: The other point (spherical C{LatLon}). @kwarg wrap: Wrap and unroll longitudes (C{bool}). @kwarg raiser: Optionally, raise L{CrossError} (C{bool}). @return: Final bearing (compass C{degrees360}). @raise TypeError: The I{other} point is not spherical. @example: >>> p = LatLon(52.205, 0.119) >>> q = LatLon(48.857, 2.351) >>> b = p.finalBearingTo(q) # 157.9 ''' self.others(other) # final bearing is the reverse of the other, initial one; # .initialBearingTo is inside .-Nvector and .-Trigonometry b = other.initialBearingTo(self, wrap=wrap, raiser=raiser) return (b + _180_0) % _360_0 # == wrap360 since b >= 0 def maxLat(self, bearing): '''Return the maximum latitude reached when travelling on a great circle on given bearing from this point based on Clairaut's formula. The maximum latitude is independent of longitude and the same for all points on a given latitude. Negate the result for the minimum latitude (on the Southern hemisphere). @arg bearing: Initial bearing (compass C{degrees360}). @return: Maximum latitude (C{degrees90}). @raise ValueError: Invalid B{C{bearing}}. @JSname: I{maxLatitude}. ''' m = acos1(abs(sin(Bearing_(bearing)) * cos(self.phi))) return degrees90(m) def minLat(self, bearing): '''Return the minimum latitude reached when travelling on a great circle on given bearing from this point. @arg bearing: Initial bearing (compass C{degrees360}). @return: Minimum latitude (C{degrees90}). @see: Method L{maxLat} for more details. @raise ValueError: Invalid B{C{bearing}}. @JSname: I{minLatitude}. ''' return -self.maxLat(bearing) def parse(self, strllh, height=0, sep=_COMMA_, name=NN): '''Parse a string representing a similar, spherical 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}). @kwarg sep: Optional separator (C{str}). @kwarg name: Optional instance name (C{str}), overriding this name. @return: The similar point (spherical C{LatLon}). @raise ParseError: Invalid B{C{strllh}}. ''' from pygeodesy.dms import parse3llh r = self.classof(*parse3llh(strllh, height=height, sep=sep)) return _xnamed(r, name or self.name, force=True) def _rhumb3(self, other): '''(INTERNAL) Rhumb_ helper function. @arg other: The other point (spherical C{LatLon}). ''' self.others(other) a1, b1 = self.philam a2, b2 = other.philam # if |db| > 180 take shorter rhumb # line across the anti-meridian db = wrapPI(b2 - b1) dp = log(tanPI_2_2(a2) / tanPI_2_2(a1)) return (a2 - a1), db, dp def rhumbBearingTo(self, other): '''Return the initial bearing (forward azimuth) from this to an other point along a rhumb (loxodrome) line. @arg other: The other point (spherical C{LatLon}). @return: Initial bearing (compass C{degrees360}). @raise TypeError: The I{other} point is not spherical. @example: >>> p = LatLon(51.127, 1.338) >>> q = LatLon(50.964, 1.853) >>> b = p.rhumbBearingTo(q) # 116.7 ''' _, db, dp = self._rhumb3(other) return atan2b(db, dp) def rhumbDestination(self, distance, bearing, radius=R_M, height=None): '''Return the destination point having travelled along a rhumb (loxodrome) line from this point the given distance on the given bearing. @arg distance: Distance travelled (C{meter}, same units as I{radius}). @arg bearing: Bearing from this point (compass C{degrees360}). @kwarg radius: Mean earth radius (C{meter}). @kwarg height: Optional height, overriding the default height (C{meter}, same unit as I{radius}). @return: The destination point (spherical C{LatLon}). @raise ValueError: Invalid B{C{distance}}, B{C{bearing}}, B{C{radius}} or B{C{height}}. @example: >>> p = LatLon(51.127, 1.338) >>> q = p.rhumbDestination(40300, 116.7) # 50.9642°N, 001.8530°E @JSname: I{rhumbDestinationPoint} ''' r = _angular(distance, radius) a1, b1 = self.philam sb, cb = sincos2(Bearing_(bearing)) da = r * cb a2 = a1 + da # normalize latitude if past pole if a2 > PI_2: a2 = PI - a2 elif a2 < -PI_2: a2 = -PI - a2 dp = log(tanPI_2_2(a2) / tanPI_2_2(a1)) # E-W course becomes ill-conditioned with 0/0 q = (da / dp) if abs(dp) > EPS else cos(a1) b2 = (b1 + r * sb / q) if abs(q) > EPS else b1 h = self.height if height is None else Height(height) return self.classof(degrees90(a2), degrees180(b2), height=h) def rhumbDistanceTo(self, other, radius=R_M): '''Return the distance from this to an other point along a rhumb (loxodrome) line. @arg other: The other point (spherical C{LatLon}). @kwarg radius: Mean earth radius (C{meter}). @return: Distance (C{meter}, the same units as I{radius}). @raise TypeError: The B{C{other}} point is not spherical. @raise ValueError: Invalid B{C{radius}}. @example: >>> p = LatLon(51.127, 1.338) >>> q = LatLon(50.964, 1.853) >>> d = p.rhumbDistanceTo(q) # 403100 ''' # see <https://www.EdWilliams.org/avform.htm#Rhumb> da, db, dp = self._rhumb3(other) # on Mercator projection, longitude distances shrink # by latitude; the 'stretch factor' q becomes ill- # conditioned along E-W line (0/0); use an empirical # tolerance to avoid it q = (da / dp) if abs(dp) > EPS else cos(self.phi) return hypot(da, q * db) * Radius(radius) def rhumbMidpointTo(self, other, height=None): '''Return the (loxodromic) midpoint between this and an other point. @arg other: The other point (spherical LatLon). @kwarg height: Optional height, overriding the mean height (C{meter}). @return: The midpoint (spherical C{LatLon}). @raise TypeError: The I{other} point is not spherical. @raise ValueError: Invalid B{C{height}}. @example: >>> p = LatLon(51.127, 1.338) >>> q = LatLon(50.964, 1.853) >>> m = p.rhumb_midpointTo(q) >>> m.toStr() # '51.0455°N, 001.5957°E' ''' self.others(other) # see <https://MathForum.org/library/drmath/view/51822.html> a1, b1 = self.philam a2, b2 = other.philam if abs(b2 - b1) > PI: b1 += PI2 # crossing anti-meridian a3 = favg(a1, a2) b3 = favg(b1, b2) f1 = tanPI_2_2(a1) if abs(f1) > EPS: f2 = tanPI_2_2(a2) f = f2 / f1 if abs(f) > EPS: f = log(f) if abs(f) > EPS: f3 = tanPI_2_2(a3) b3 = fdot(map1(log, f1, f2, f3), -b2, b1, b2 - b1) / f h = self._havg(other) if height is None else Height(height) return self.classof(degrees90(a3), degrees180(b3), height=h) def toNvector(self, Nvector=NvectorBase, **Nvector_kwds): # PYCHOK signature '''Convert this point to C{Nvector} components, I{including height}. @kwarg Nvector_kwds: Optional, additional B{C{Nvector}} keyword arguments, ignored if B{C{Nvector=None}}. @return: An B{C{Nvector}} or a L{Vector4Tuple}C{(x, y, z, h)} if B{C{Nvector}} is C{None}. @raise TypeError: Invalid B{C{Nvector}} or B{C{Nvector_kwds}}. ''' return LatLonBase.toNvector(self, Nvector=Nvector, **Nvector_kwds) def toWm(self, radius=R_MA): '''Convert this point to a I{WM} coordinate. @kwarg radius: Optional earth radius (C{meter}). @return: The WM coordinate (L{Wm}). @see: Function L{toWm} in module L{webmercator} for details. ''' from pygeodesy.webmercator import toWm # PYCHOK recursive import return toWm(self, radius=radius) __all__ += _ALL_DOCS(CartesianSphericalBase, LatLonSphericalBase) # **) MIT License # # Copyright (C) 2016-2021 -- 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.