# Coverage for pygeodesy/karney.py : 97%

## 104 statements 101 run 3 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 | # -*- coding: utf-8 -*- u'''Wrapper around I{Charles Karney's} Python implementation of U{GeographicLib <https://PyPI.org/project/geographiclib>}, provided that package is installed. ''' 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.04.09' 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 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, an C{ImportError} otherwise. ''' if _Wrapped._Geodesic is None: from geographiclib.geodesic import Geodesic as _Geodesic class Geodesic(_Geodesic): '''Karney 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, .HuasdorffKarney._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, an C{ImportError} otherwise. ''' if _Wrapped._GeodesicLine is None: from geographiclib.geodesicline import GeodesicLine as _GeodesicLine class GeodesicLine(_GeodesicLine): '''Karney 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, an C{ImportError} otherwise. ''' 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, an C{ImportError} otherwise. ''' if _Wrapped._Math is None: from geographiclib.geomath import Math _Wrapped._Math = Math return _Wrapped._Math _wrapped = _Wrapped() 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 _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 transformation like C{Math::sum}. @return: 2-Tuple C{(sum_u_plus_v, residual)}. @note: The C{residual} can be the same as one of the first two arguments. ''' M = _wrapped.geoMath if M: return M.sum(u, v) s = u + v r = s - v t = s - r r -= u t -= v t = -(r + t) # u + v = s + t # = round(u + v) + t return s, t # **) 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.