# Coverage for pygeodesy/ellipsoidalKarney.py : 100%

## 43 statements 43 run 0 missing 4 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 | # -*- coding: utf-8 -*- u'''Ellipsoidal, I{Karney}-based geodesy. Ellipsoidal geodetic (lat-/longitude) L{LatLon} and geocentric (ECEF) L{Cartesian} classes and functions L{areaOf}, L{intersections2}, L{isclockwise}, L{nearestOn} and L{perimeterOf}, all requiring I{Charles Karney}'s U{geographiclib <https://PyPI.org/project/geographiclib>} Python package to be installed. Here's an example usage of C{ellipsoidalKarney}: >>> from pygeodesy.ellipsoidalKarney import LatLon >>> Newport_RI = LatLon(41.49008, -71.312796) >>> Cleveland_OH = LatLon(41.499498, -81.695391) >>> Newport_RI.distanceTo(Cleveland_OH) 866,455.4329098687 # meter You can change the ellipsoid model used by the I{Karney} formulae as follows: >>> from pygeodesy import Datums >>> from pygeodesy.ellipsoidalKarney import LatLon >>> p = LatLon(0, 0, datum=Datums.OSGB36) or by converting to anothor datum: >>> p = p.toDatum(Datums.OSGB36) ''' from pygeodesy.datums import _WGS84 from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, \ _intersection3, _intersections2, \ _nearestOn # from pygeodesy.errors import _xkwds # from .karney from pygeodesy.karney import _polygon, _TOL_M, _xkwds from pygeodesy.lazily import _ALL_LAZY, _ALL_OTHER from pygeodesy.points import _areaError, ispolar # PYCHOK exported from pygeodesy.props import deprecated_method, Property_RO # from pygeodesy.units import _1mm as _TOL_M # from .karney __all__ = _ALL_LAZY.ellipsoidalKarney __version__ = '21.08.07' class Cartesian(CartesianEllipsoidalBase): '''Extended to convert C{Karney}-based L{Cartesian} to C{Karney}-based L{LatLon} points. ''' def toLatLon(self, **LatLon_datum_kwds): # PYCHOK LatLon=LatLon, datum=None '''Convert this cartesian point to a C{Karney}-based geodetic point. @kwarg LatLon_datum_kwds: Optional L{LatLon}, B{C{datum}} and other keyword arguments, ignored if C{B{LatLon} is None}. Use C{B{LatLon}=...} to override this L{LatLon} class or specify C{B{LatLon} is None}. @return: The geodetic point (L{LatLon}) or if B{C{LatLon}} is C{None}, an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with C{C} and C{M} if available. @raise TypeError: Invalid B{C{LatLon}}, B{C{datum}} or other B{C{LatLon_datum_kwds}}. ''' kwds = _xkwds(LatLon_datum_kwds, LatLon=LatLon, datum=self.datum) return CartesianEllipsoidalBase.toLatLon(self, **kwds) class LatLon(LatLonEllipsoidalBaseDI): '''An ellipsoidal L{LatLon} similar to L{ellipsoidalVincenty.LatLon} but using I{Charles F. F. Karney}'s Python U{geographiclib <https://PyPI.org/project/geographiclib>} to compute the geodesic distance, initial and final bearing (azimuths) between two given points or the destination point given a start point and an (initial) bearing. @note: This L{LatLon} require the U{geographiclib <https://PyPI.org/project/geographiclib>} package. ''' @deprecated_method def bearingTo(self, other, wrap=False): # PYCHOK no cover '''DEPRECATED, use method L{initialBearingTo}. ''' return self.initialBearingTo(other, wrap=wrap) @Property_RO def Equidistant(self): '''Get the prefered azimuthal equidistant projection I{class} (L{EquidistantKarney}). ''' from pygeodesy.azimuthal import EquidistantKarney return EquidistantKarney @Property_RO def geodesic(self): '''Get this C{LatLon}'s I{wrapped} U{Karney Geodesic <https://GeographicLib.SourceForge.io/html/python/code.html>}, provided package U{geographiclib <https://PyPI.org/project/geographiclib>} is installed. ''' return self.datum.ellipsoid.geodesic def toCartesian(self, **Cartesian_datum_kwds): # PYCHOK Cartesian=Cartesian, datum=None '''Convert this point to C{Karney}-based cartesian (ECEF) coordinates. @kwarg Cartesian_datum_kwds: Optional L{Cartesian}, B{C{datum}} and other keyword arguments, ignored if C{B{Cartesian} is None}. Use C{B{Cartesian}=...} to override this L{Cartesian} class or set C{B{Cartesian} is None}. @return: The cartesian (ECEF) coordinates (L{Cartesian}) or if B{C{Cartesian}} is C{None}, an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with C{C} and C{M} if available. @raise TypeError: Invalid B{C{Cartesian}}, B{C{datum}} or other B{C{Cartesian_datum_kwds}}. ''' kwds = _xkwds(Cartesian_datum_kwds, Cartesian=Cartesian, datum=self.datum) return LatLonEllipsoidalBaseDI.toCartesian(self, **kwds) def areaOf(points, datum=_WGS84, wrap=True): '''Compute the area of an (ellipsoidal) polygon. @arg points: The polygon points (L{LatLon}[]). @kwarg datum: Optional datum (L{Datum}). @kwarg wrap: Wrap and unroll longitudes (C{bool}). @return: Area (C{meter}, same as units of the B{C{datum}}'s ellipsoid axes, I{squared}). @raise ImportError: Package U{geographiclib <https://PyPI.org/project/geographiclib>} not installed or not found. @raise PointsError: Insufficient number of B{C{points}}. @raise TypeError: Some B{C{points}} are not L{LatLon}. @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled longitudes not supported. @note: This function requires the U{geographiclib <https://PyPI.org/project/geographiclib>} package. @see: L{pygeodesy.areaOf}, L{ellipsoidalExact.areaOf}, L{ellipsoidalGeodSolve.areaOf}, L{sphericalNvector.areaOf} and L{sphericalTrigonometry.areaOf}. ''' return abs(_polygon(datum.ellipsoid.geodesic, points, True, False, wrap)) def intersection3(start1, end1, start2, end2, height=None, wrap=True, equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds): '''Interatively compute the intersection point of two paths, each defined by an (ellipsoidal) start and end point. @arg start1: Start point of the first path (L{LatLon}). @arg end1: End point of the first path (L{LatLon}). @arg start2: Start point of the second path (L{LatLon}). @arg end2: End point of the second path (L{LatLon}). @kwarg height: Optional height at the intersection (C{meter}, conventionally) or C{None} for the mean height. @kwarg wrap: Wrap and unroll longitudes (C{bool}). @kwarg equidistant: An azimuthal equidistant projection (I{class} or function L{equidistant}) or C{None} for the preferred C{B{start1}.Equidistant}. @kwarg tol: Tolerance for skew line distance and length and for convergence (C{meter}, conventionally). @kwarg LatLon: Optional class to return the intersection point (L{LatLon}) or C{None}. @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments, ignored if C{B{LatLon} is None}. @return: An L{Intersection3Tuple}C{(point, outside1, outside2)} with C{point} a B{C{LatLon}} or if C{B{LatLon} is None}, a L{LatLon4Tuple}C{(lat, lon, height, datum)}. @raise ImportError: Package U{geographiclib <https://PyPI.org/project/geographiclib>} not installed or not found, but only if C{B{equidistant}=}L{EquidistantKarney}. @raise IntersectionError: Skew, colinear, parallel or otherwise non-intersecting paths or no convergence for the B{C{tol}}. @raise TypeError: Invalid or non-ellipsoidal B{C{start1}}, B{C{end1}}, B{C{start2}} or B{C{end2}} or invalide B{C{equidistant}}. ''' return _intersection3(start1, end1, start2, end2, height=height, wrap=wrap, equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) def intersections2(center1, radius1, center2, radius2, height=None, wrap=True, equidistant=None, tol=_TOL_M, LatLon=LatLon, **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 (L{LatLon}). @arg radius1: Radius of the first circle (C{meter}, conventionally). @arg center2: Center of the second circle (L{LatLon}). @arg radius2: Radius of the second circle (C{meter}, same units as B{C{radius1}}). @kwarg height: Optional height for the intersection points (C{meter}, conventionally) or C{None} for the I{"radical height"} at the I{radical line} between both centers. @kwarg wrap: Wrap and unroll longitudes (C{bool}). @kwarg equidistant: An azimuthal equidistant projection (I{class} or function L{equidistant}) or C{None} for the preferred C{B{center1}.Equidistant}. @kwarg tol: Convergence tolerance (C{meter}, same units as B{C{radius1}} and B{C{radius2}}). @kwarg LatLon: Optional class to return the intersection points (L{LatLon}) or C{None}. @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments, ignored if C{B{LatLon} is None}. @return: 2-Tuple of the intersection points, each a B{C{LatLon}} instance or L{LatLon4Tuple}C{(lat, lon, height, datum)} if C{B{LatLon} is None}. For abutting circles, both points are the same instance, aka I{radical center}. @raise ImportError: 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 the B{C{tol}}. @raise TypeError: Invalid or non-ellipsoidal B{C{center1}} or B{C{center2}} or invalid B{C{equidistant}}. @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 B{14. MARITIME BOUNDARIES}, U{circle-circle<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>} and U{sphere-sphere<https://MathWorld.Wolfram.com/Sphere-SphereIntersection.html>} intersections. ''' return _intersections2(center1, radius1, center2, radius2, height=height, wrap=wrap, equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) def isclockwise(points, datum=_WGS84, wrap=True): '''Determine the direction of a path or polygon. @arg points: The path or polygon points (C{LatLon}[]). @kwarg datum: Optional datum (L{Datum}). @kwarg wrap: Wrap and unroll longitudes (C{bool}). @return: C{True} if B{C{points}} are clockwise, C{False} otherwise. @raise ImportError: Package U{geographiclib <https://PyPI.org/project/geographiclib>} not installed or not found. @raise PointsError: Insufficient number of B{C{points}}. @raise TypeError: Some B{C{points}} are not C{LatLon}. @raise ValueError: The B{C{points}} enclose a pole or zero area. @note: This function requires the U{geographiclib <https://PyPI.org/project/geographiclib>} package. @see: L{pygeodesy.isclockwise}. ''' a = _polygon(datum.ellipsoid.geodesic, points, True, False, wrap) if a > 0: return True elif a < 0: return False raise _areaError(points) def nearestOn(point, point1, point2, within=True, height=None, wrap=False, equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds): '''Iteratively locate the closest point on the arc between two other (ellipsoidal) points. @arg point: Reference point (C{LatLon}). @arg point1: Start point of the arc (C{LatLon}). @arg point2: End point of the arc (C{LatLon}). @kwarg within: If C{True} return the closest point I{between} B{C{point1}} and B{C{point2}}, otherwise the closest point elsewhere on the arc (C{bool}). @kwarg height: Optional height for the closest point (C{meter}, conventionally) or C{None} or C{False} for the interpolated height. If C{False}, the distance between points takes height into account. @kwarg wrap: Wrap and unroll longitudes (C{bool}). @kwarg equidistant: An azimuthal equidistant projection (I{class} or function L{equidistant}) or C{None} for the preferred C{B{point}.Equidistant}. @kwarg tol: Convergence tolerance (C{meter}). @kwarg LatLon: Optional class to return the closest point (L{LatLon}) or C{None}. @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments, ignored if C{B{LatLon} is None}. @return: Closest point, a B{C{LatLon}} instance or if C{B{LatLon} is None}, a L{LatLon4Tuple}C{(lat, lon, height, datum)}. @raise ImportError: Package U{geographiclib <https://PyPI.org/project/geographiclib>} not installed or not found. @raise TypeError: Invalid or non-ellipsoidal B{C{point}}, B{C{point1}} or B{C{point2}} or invalid B{C{equidistant}}. @raise ValueError: No convergence for the B{C{tol}}. ''' return _nearestOn(point, point1, point2, within=within, height=height, wrap=wrap, equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) def perimeterOf(points, closed=False, datum=_WGS84, wrap=True): '''Compute the perimeter of an (ellipsoidal) polygon. @arg points: The polygon points (L{LatLon}[]). @kwarg closed: Optionally, close the polygon (C{bool}). @kwarg datum: Optional datum (L{Datum}). @kwarg wrap: Wrap and unroll longitudes (C{bool}). @return: Perimeter (C{meter}, same as units of the B{C{datum}}'s ellipsoid axes). @raise ImportError: Package U{geographiclib <https://PyPI.org/project/geographiclib>} not installed or not found. @raise PointsError: Insufficient number of B{C{points}}. @raise TypeError: Some B{C{points}} are not L{LatLon}. @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled longitudes not supported. @note: This function requires the U{geographiclib <https://PyPI.org/project/geographiclib>} package. @see: L{pygeodesy.perimeterOf}, L{ellipsoidalExact.perimeterOf}, L{ellipsoidalGeodSolve.perimeterOf}, L{sphericalNvector.perimeterOf} and L{sphericalTrigonometry.perimeterOf}. ''' return _polygon(datum.ellipsoid.geodesic, points, closed, True, wrap) __all__ += _ALL_OTHER(Cartesian, LatLon, # classes areaOf, # functions intersection3, intersections2, isclockwise, ispolar, nearestOn, perimeterOf) # **) 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.