# Coverage for pygeodesy/karney.py : 97%

## 115 statements 111 run 4 missing 0 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 | # -*- coding: utf-8 -*- u'''Wrapper around I{Charles Karney}'s Python classes C{Geodesic} and C{GeodesicLine} and functions C{AngDiff}, C{AngNormalize}, C{LatFix} and C{sum} from U{geographiclib<https://PyPI.org/project/geographiclib>}, provided that package is installed. Following are U{PyGeodesy<https://PyPI.org/project/PyGeodesy>} classes and functions transcribed from I{Karney}'s original U{GeographicLib <https://GeographicLib.SourceForge.io/html/annotated.html>} in C++: - L{CassiniSoldner} -- U{CassiniSoldner<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1CassiniSoldner.html>} - L{EcefKarney} -- U{Geocentric<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1Geocentric.html>} - L{EcefCartesian} -- U{LocalCartesian<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1LocalCartesian.html>} - L{EquidistantKarney} -- U{AzimuthalEquidistant<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1AzimuthalEquidistant.html>} - L{Elliptic} -- U{EllipticFunction<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1EllipticFunction.html>} - L{Etm}, L{ExactTransverseMercator} -- U{TransverseMercatorExact <https://GeographicLib.SourceForge.io/html/classGeographicLib_1_1TransverseMercatorExact.html>} - L{GeoidKarney} -- U{Geoid<https://GeographicLib.SourceForge.io/html/geoid.html>} - L{Georef} -- U{Georef<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1Georef.html>} - L{GnomonicKarney} -- U{Gnomonic<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1Gnomonic.html>} - L{Ups} -- U{PolarStereographic<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1PolarStereographic.html>} - L{Utm} -- U{TransverseMercator<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1TransverseMercator.html>} - L{UtmUps}, L{Epsg} -- U{UTMUPS<https://GeographicLib.SourceForge.io/html/ classGeographicLib_1_1UTMUPS.html>} - L{atan2d}, L{sincos2}, L{sincos2d}-- U{Math<https://GeographicLib.sourceforge.io/html/ classGeographicLib_1_1Math.html>} The following U{PyGeodesy<https://PyPI.org/project/PyGeodesy>} classes and module are wrappers around some of I{Karney}'s Python U{geographiclib <https://PyPI.org/project/geographiclib>}: - L{ellipsoidalKarney}, L{FrechetKarney}, L{HeightIDWkarney}, L{karney} ''' from pygeodesy.basics import NAN, property_RO from pygeodesy.datum import Datums from pygeodesy.lazily import _ALL_LAZY from pygeodesy.named import Destination3Tuple, Distance3Tuple from pygeodesy.utily import unroll180, wrap360 from math import fmod __all__ = _ALL_LAZY.karney __version__ = '20.08.10' class _Adict(dict): '''(INTERNAL) Basic C{dict} with key I{and} attribute access to the items (minimal version of _NamedDict). ''' def __getattr__(self, name): try: return self[name] except KeyError: return dict.__getattr__(self, name) class _Wrapped(object): ''''(INTERNAL) Wrapper for some of I{Karney}'s U{geographiclib <https://PyPI.org/project/geographiclib>} classes. ''' _Geodesic = None _GeodesicLine = None _geoMath = None _Math = None @property_RO def Geodesic(self): '''(INTERNAL) Get the wrapped C{Geodesic} class, provided the U{geographiclib<https://PyPI.org/project/geographiclib>} package is installed, otherwise throw an C{ImportError}. ''' if _Wrapped._Geodesic is None: from geographiclib.geodesic import Geodesic as _Geodesic class Geodesic(_Geodesic): '''I{Karney}'s U{Geodesic<https://GeographicLib.SourceForge.io/html/ python/code.html#geographiclib.geodesic.Geodesic>} wrapper. ''' def Direct(self, lat1, lon1, azi1, s12, *outmask): '''Return the C{Direct} result. ''' d = _Geodesic.Direct(self, lat1, lon1, azi1, s12, *outmask) return _Adict(d) def Direct3(self, lat1, lon1, azi1, s12): # PYCHOK outmask '''Return the destination lat, lon and reverse azimth in C{degrees}. ''' m = self.AZIMUTH | self.LATITUDE | self.LONGITUDE d = self.Direct(lat1, lon1, azi1, s12, m) return Destination3Tuple(d.lat2, d.lon2, d.azi2) def Inverse(self, lat1, lon1, lat2, lon2, *outmask): '''Return the C{Inverse} result. ''' d = _Geodesic.Inverse(self, lat1, lon1, lat2, lon2, *outmask) return _Adict(d) def Inverse1(self, lat1, lon1, lat2, lon2, wrap=False): '''Return the non-negative I{angular} distance in C{degrees}. ''' # see .FrechetKarney.distance, .HausdorffKarney._distance # and .HeightIDWkarney._distances _, lon2 = unroll180(lon1, lon2, wrap=wrap) # self.LONG_UNROLL d = self.Inverse(lat1, lon1, lat2, lon2) # XXX self.DISTANCE needed for 'a12'? return abs(d.a12) def Inverse3(self, lat1, lon1, lat2, lon2): # PYCHOK outmask '''Return the distance in C{meter} and the forward and reverse azimuths in C{degrees}. ''' m = self.DISTANCE | self.AZIMUTH d = self.Inverse(lat1, lon1, lat2, lon2, m) return Distance3Tuple(d.s12, wrap360(d.azi1), wrap360(d.azi2)) def Line(self, lat1, lon1, azi1, *caps): return _wrapped.GeodesicLine(self, lat1, lon1, azi1, *caps) # Geodesic.Direct.__doc__ = _Geodesic.Direct.__doc__ # Geodesic.Inverse.__doc__ = _Geodesic.Inverse.__doc__ # Geodesic.Line.__doc__ = _Geodesic.Line.__doc__ _Wrapped._Geodesic = Geodesic return _Wrapped._Geodesic @property_RO def GeodesicLine(self): '''(INTERNAL) Get the wrapped C{GeodesicLine} class, provided the U{geographiclib<https://PyPI.org/project/geographiclib>} package is installed, otherwise throw an C{ImportError}. ''' if _Wrapped._GeodesicLine is None: from geographiclib.geodesicline import GeodesicLine as _GeodesicLine class GeodesicLine(_GeodesicLine): '''I{Karney}'s U{GeodesicLine <https://GeographicLib.SourceForge.io/html/ python/code.html#geographiclib.geodesicline.GeodesicLine>} wrapper. ''' def ArcPosition(self, a12, *outmask): d = _GeodesicLine.ArcPosition(self, a12, *outmask) return _Adict(d) def Position(self, s12, *outmask): d = _GeodesicLine.Position(self, s12, *outmask) return _Adict(d) # GeodesicLine.ArcPosition.__doc__ = _GeodesicLine.ArcPosition.__doc__ # GeodesicLine.Position.__doc__ = _GeodesicLine.Position.__doc__ _Wrapped._GeodesicLine = GeodesicLine return _Wrapped._GeodesicLine @property_RO def Geodesic_WGS84(self): '''(INTERNAL) Get the wrapped C{Geodesic.WGS84} I{instance} iff- the U{geographiclib<https://PyPI.org/project/geographiclib>} package is installed, otherwise throw an C{ImportError}. ''' return Datums.WGS84.ellipsoid.geodesic @property_RO def geoMath(self): '''(INTERNAL) Get the C{Math} class if the U{geographiclib <https://PyPI.org/project/geographiclib>} package is installed or C{False} otherwise. ''' if self._geoMath is None: try: self._geoMath = self.Math except ImportError: self._geoMath = False return self._geoMath @property_RO def Math(self): '''(INTERNAL) Get the C{Math} class, provided the U{geographiclib<https://PyPI.org/project/geographiclib>} package is installed, otherwise throw an C{ImportError}. ''' if _Wrapped._Math is None: from geographiclib.geomath import Math _Wrapped._Math = Math return _Wrapped._Math _wrapped = _Wrapped() # imported by .datum def _diff182(deg0, deg): # mimick Math.AngDiff '''Compute C{deg - deg0}, reduced to C{[-180,180]} accurately. @return: 2-Tuple C{(delta_angle, residual)} in C{degrees}. ''' M = _wrapped.geoMath if M: return M.AngDiff(deg0, deg) d, t = _sum2(_norm180(-deg0), _norm180(deg)) d = _norm180(d) if d == 180 and t > 0: d = -180 return _sum2(d, t) def _fix90(deg): # mimick Math.LatFix '''Replace angle in C{degrees} outside [-90,90] by NAN. @return: Angle C{degrees} or NAN. ''' M = _wrapped.geoMath if M: return M.LatFix(deg) return NAN if abs(deg) > 90 else deg def _fsum2_(*vs): # see .test/testKarney.py '''Cascaded summation, like C{.fmath.fsum_}. @arg vs: Values to be added (C{scalar}[]). @return: 2-Tuple C{(sum_of_vs, residual)}. @note: NOT "error-free", see .test/testKarney.py. @see: U{Algorithm 4.1<http://www.ti3.TUHH.De/paper/rump/OgRuOi05.pdf>}. ''' s = r = 0.0 for v in vs: s, t = _sum2(s, float(v)) if t: r, t = _sum2(r, t) # inlieu of r += t return (s + r), t def _norm180(deg): # mimick Math.AngNormalize '''Reduce angle in C{degrees} to (-180,180]. @return: Reduced angle C{degrees}. ''' M = _wrapped.geoMath if M: return M.AngNormalize(deg) # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 # fmod( 0, 360) == 0.0 # fmod( 360, 360) == 0.0 # fmod(-0, 360) == 0.0 # fmod(-0.0, 360) == -0.0 # fmod(-360, 360) == -0.0 # however, using the % operator ... # 0 % 360 == 0 # 360 % 360 == 0 # 360.0 % 360 == 0.0 # -0 % 360 == 0 # -360 % 360 == 0 # -0.0 % 360 == 0.0 # -360.0 % 360 == 0.0 # On Windows 32-bit with Python 2.7, math.fmod(-0.0, 360) # == +0.0. This fixes this bug. See also Math::AngNormalize # in the C++ library. Math::sincosd has a similar fix. d = fmod(deg, 360) if deg else deg return (d + 360) if d <= -180 else (d if d <= 180 else (d - 360)) def _sum2(u, v): # mimick Math::sum, actually sum2 '''Error-free summation like C{Math::sum}. @return: 2-Tuple C{(sum_u_plus_v, residual)}. @note: The C{residual} can be the same as B{C{u}} or B{C{v}}. @see: U{Algorithm 3.1<http://www.ti3.TUHH.De/paper/rump/OgRuOi05.pdf>}. ''' M = _wrapped.geoMath if M: return M.sum(u, v) s = u + v r = s - v t = s - r # if True: # Algorithm 3.1 t = (u - r) + (v - t) # else: # in Math C/C++ # r -= u # t -= v # t = -(r + t) # u + v = s + t # = round(u + v) + t return s, t def _unroll2(lon1, lon2, wrap=False): # see .ellipsoidalBase._intersect22 '''Unroll B{C{lon2 - lon1}} like C{geodesic.Geodesic.Inverse}. @return: 2-Tuple C{(lon2 - lon1, lon2)} with B{C{lon2}} unrolled if B{C{wrap}} is C{True}, normalized otherwise. ''' if wrap: d, t = _diff182(lon1, lon2) lon2 = (lon1 + d) + t # _fsum2_(lon1, d, t) else: lon2 = _norm180(lon2) return (lon2 - lon1), lon2 # **) 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.