Coverage for pygeodesy/ellipsoidalKarney.py: 100%
40 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-04 12:01 -0400
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-04 12:01 -0400
2# -*- coding: utf-8 -*-
4u'''Ellipsoidal, I{Karney}-based geodesy.
6Ellipsoidal geodetic (lat-/longitude) L{LatLon} and geocentric (ECEF) L{Cartesian}
7classes and functions L{areaOf}, L{intersection3}, L{intersections2}, L{isclockwise},
8L{nearestOn} and L{perimeterOf}, requiring I{Charles F.F. Karney}'s U{geographiclib
9<https://PyPI.org/project/geographiclib>} Python package to be installed.
11A usage example of C{ellipsoidalKarney}:
13 >>> from pygeodesy.ellipsoidalKarney import LatLon
14 >>> Newport_RI = LatLon(41.49008, -71.312796)
15 >>> Cleveland_OH = LatLon(41.499498, -81.695391)
16 >>> Newport_RI.distanceTo(Cleveland_OH)
17 866,455.4329098687 # meter
19You can change the ellipsoid model used by the I{Karney} formulae
20as follows:
22 >>> from pygeodesy import Datums
23 >>> from pygeodesy.ellipsoidalKarney import LatLon
24 >>> p = LatLon(0, 0, datum=Datums.OSGB36)
26or by converting to anothor datum:
28 >>> p = p.toDatum(Datums.OSGB36)
29'''
31from pygeodesy.datums import _WGS84
32from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase, _nearestOn
33from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, \
34 _intersection3, _intersections2, \
35 _TOL_M, intersecant2
36# from pygeodesy.errors import _xkwds # from .karney
37from pygeodesy.karney import _polygon, fabs, _xkwds
38from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, _ALL_OTHER
39from pygeodesy.points import _areaError, ispolar # PYCHOK exported
40from pygeodesy.props import deprecated_method, Property_RO
42# from math import fabs # from .karney
44__all__ = _ALL_LAZY.ellipsoidalKarney
45__version__ = '24.08.13'
48class Cartesian(CartesianEllipsoidalBase):
49 '''Extended to convert C{Karney}-based L{Cartesian} to C{Karney}-based L{LatLon} points.
50 '''
52 def toLatLon(self, **LatLon_and_kwds): # PYCHOK LatLon=LatLon, datum=None
53 '''Convert this cartesian point to a C{Karney}-based geodetic point.
55 @kwarg LatLon_and_kwds: Optional L{LatLon} and L{LatLon} keyword arguments
56 as C{datum}. Use C{B{LatLon}=..., B{datum}=...} to override
57 this L{LatLon} class or specify C{B{LatLon}=None}.
59 @return: The geodetic point (L{LatLon}) or if C{B{LatLon} is None}, an
60 L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with C{C}
61 and C{M} if available.
63 @raise TypeError: Invalid B{C{LatLon_and_kwds}} argument.
64 '''
65 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon, datum=self.datum)
66 return CartesianEllipsoidalBase.toLatLon(self, **kwds)
69class LatLon(LatLonEllipsoidalBaseDI):
70 '''An ellipsoidal L{LatLon} similar to L{ellipsoidalVincenty.LatLon} but using
71 I{Karney}'s Python U{geographiclib<https://PyPI.org/project/geographiclib>}
72 to compute geodesic distances, bearings (azimuths), etc.
73 '''
75 @deprecated_method
76 def bearingTo(self, other, wrap=False): # PYCHOK no cover
77 '''DEPRECATED, use method L{initialBearingTo}.
78 '''
79 return self.initialBearingTo(other, wrap=wrap)
81 @Property_RO
82 def Equidistant(self):
83 '''Get the prefered azimuthal equidistant projection I{class} (L{EquidistantKarney}).
84 '''
85 return _MODS.azimuthal.EquidistantKarney
87 @Property_RO
88 def geodesic(self):
89 '''Get this C{LatLon}'s I{wrapped} U{geodesic.Geodesic
90 <https://GeographicLib.SourceForge.io/Python/doc/code.html>}, provided
91 I{Karney}'s U{geographiclib<https://PyPI.org/project/geographiclib>}
92 package is installed.
93 '''
94 return self.datum.ellipsoid.geodesic
96 def toCartesian(self, **Cartesian_datum_kwds): # PYCHOK Cartesian=Cartesian, datum=None
97 '''Convert this point to C{Karney}-based cartesian (ECEF) coordinates.
99 @kwarg Cartesian_datum_kwds: Optional L{Cartesian}, B{C{datum}} and other keyword
100 arguments, ignored if C{B{Cartesian} is None}. Use C{B{Cartesian}=...} to
101 override this L{Cartesian} class or set C{B{Cartesian}=None}.
103 @return: The cartesian (ECEF) coordinates (L{Cartesian}) or if C{B{Cartesian} is
104 None}, an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with
105 C{C} and C{M} if available.
107 @raise TypeError: Invalid B{C{Cartesian}}, B{C{datum}} or other B{C{Cartesian_datum_kwds}}.
108 '''
109 kwds = _xkwds(Cartesian_datum_kwds, Cartesian=Cartesian, datum=self.datum)
110 return LatLonEllipsoidalBaseDI.toCartesian(self, **kwds)
113def areaOf(points, datum=_WGS84, wrap=True):
114 '''Compute the area of an (ellipsoidal) polygon or composite using I{Karney}'s
115 U{geographiclib<https://PyPI.org/project/geographiclib>} package.
117 @arg points: The polygon points (L{LatLon}[], L{BooleanFHP} or L{BooleanGH}).
118 @kwarg datum: Optional datum (L{Datum}).
119 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{points}} (C{bool}).
121 @return: Area (C{meter}, same as units of the B{C{datum}}'s ellipsoid axes, I{squared}).
123 @raise ImportError: Package U{geographiclib<https://PyPI.org/project/geographiclib>}
124 not installed or not found.
126 @raise PointsError: Insufficient number of B{C{points}}.
128 @raise TypeError: Some B{C{points}} are not L{LatLon}.
130 @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled longitudes not supported.
132 @see: Functions L{pygeodesy.areaOf}, L{ellipsoidalExact.areaOf}, L{ellipsoidalGeodSolve.areaOf},
133 L{sphericalNvector.areaOf} and L{sphericalTrigonometry.areaOf}.
135 @note: The U{area of a polygon enclosing a pole<https://GeographicLib.SourceForge.io/
136 C++/doc/classGeographicLib_1_1GeodesicExact.html#a3d7a9155e838a09a48dc14d0c3fac525>}
137 can be found by adding half the datum's ellipsoid surface area to the polygon's area.
138 '''
139 return fabs(_polygon(datum.ellipsoid.geodesic, points, True, False, wrap))
142def intersection3(start1, end1, start2, end2, height=None, wrap=False, # was=True
143 equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds):
144 '''I{Iteratively} compute the intersection point of two lines, each defined
145 by two (ellipsoidal) points or by an (ellipsoidal) start point and an
146 (initial) bearing from North.
148 @arg start1: Start point of the first line (L{LatLon}).
149 @arg end1: End point of the first line (L{LatLon}) or the initial bearing
150 at the first point (compass C{degrees360}).
151 @arg start2: Start point of the second line (L{LatLon}).
152 @arg end2: End point of the second line (L{LatLon}) or the initial bearing
153 at the second point (compass C{degrees360}).
154 @kwarg height: Optional height at the intersection (C{meter}, conventionally)
155 or C{None} for the mean height.
156 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{start2}}
157 and B{C{end*}} points (C{bool}).
158 @kwarg equidistant: An azimuthal equidistant projection (I{class} or function
159 L{pygeodesy.equidistant}) or C{None} for the preferred
160 C{B{start1}.Equidistant}.
161 @kwarg tol: Tolerance for convergence and for skew line distance and length
162 (C{meter}, conventionally).
163 @kwarg LatLon: Optional class to return the intersection points (L{LatLon})
164 or C{None}.
165 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments,
166 ignored if C{B{LatLon} is None}.
168 @return: An L{Intersection3Tuple}C{(point, outside1, outside2)} with C{point}
169 a B{C{LatLon}} or if C{B{LatLon} is None}, a L{LatLon4Tuple}C{(lat,
170 lon, height, datum)}.
172 @raise IntersectionError: Skew, colinear, parallel or otherwise
173 non-intersecting lines or no convergence
174 for the given B{C{tol}}.
176 @raise TypeError: Invalid or non-ellipsoidal B{C{start1}}, B{C{end1}},
177 B{C{start2}} or B{C{end2}} or invalid B{C{equidistant}}.
179 @note: For each line specified with an initial bearing, a pseudo-end point
180 is computed as the C{destination} along that bearing at about 1.5
181 times the distance from the start point to an initial gu-/estimate
182 of the intersection point (and between 1/8 and 3/8 of the authalic
183 earth perimeter).
185 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/
186 calculating-intersection-of-two-circles>} and U{Karney's paper
187 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME
188 BOUNDARIES} for more details about the iteration algorithm.
189 '''
190 return _intersection3(start1, end1, start2, end2, height=height, wrap=wrap,
191 equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds)
194def intersections2(center1, radius1, center2, radius2, height=None, wrap=False, # was=True
195 equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds):
196 '''I{Iteratively} compute the intersection points of two circles, each defined
197 by an (ellipsoidal) center point and a radius.
199 @arg center1: Center of the first circle (L{LatLon}).
200 @arg radius1: Radius of the first circle (C{meter}, conventionally).
201 @arg center2: Center of the second circle (L{LatLon}).
202 @arg radius2: Radius of the second circle (C{meter}, same units as
203 B{C{radius1}}).
204 @kwarg height: Optional height for the intersection points (C{meter},
205 conventionally) or C{None} for the I{"radical height"}
206 at the I{radical line} between both centers.
207 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{center2}}
208 (C{bool}).
209 @kwarg equidistant: An azimuthal equidistant projection (I{class} or
210 function L{pygeodesy.equidistant}) or C{None} for
211 the preferred C{B{center1}.Equidistant}.
212 @kwarg tol: Convergence tolerance (C{meter}, same units as B{C{radius1}}
213 and B{C{radius2}}).
214 @kwarg LatLon: Optional class to return the intersection points (L{LatLon})
215 or C{None}.
216 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments,
217 ignored if C{B{LatLon} is None}.
219 @return: 2-Tuple of the intersection points, each a B{C{LatLon}} instance
220 or L{LatLon4Tuple}C{(lat, lon, height, datum)} if C{B{LatLon} is
221 None}. For abutting circles, both points are the same instance,
222 aka the I{radical center}.
224 @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting
225 circles or no convergence for the B{C{tol}}.
227 @raise TypeError: Invalid or non-ellipsoidal B{C{center1}} or B{C{center2}}
228 or invalid B{C{equidistant}}.
230 @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{height}}.
232 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/
233 calculating-intersection-of-two-circles>}, U{Karney's paper
234 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME BOUNDARIES},
235 U{circle-circle<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>} and
236 U{sphere-sphere<https://MathWorld.Wolfram.com/Sphere-SphereIntersection.html>}
237 intersections.
238 '''
239 return _intersections2(center1, radius1, center2, radius2, height=height, wrap=wrap,
240 equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds)
243def isclockwise(points, datum=_WGS84, wrap=True):
244 '''Determine the direction of a path or polygon using I{Karney}'s
245 U{geographiclib<https://PyPI.org/project/geographiclib>} package.
247 @arg points: The path or polygon points (C{LatLon}[]).
248 @kwarg datum: Optional datum (L{Datum}).
249 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the
250 B{C{points}} (C{bool}).
252 @return: C{True} if B{C{points}} are clockwise, C{False} otherwise.
254 @raise ImportError: Package U{geographiclib
255 <https://PyPI.org/project/geographiclib>}
256 not installed or not found.
258 @raise PointsError: Insufficient number of B{C{points}}.
260 @raise TypeError: Some B{C{points}} are not C{LatLon}.
262 @raise ValueError: The B{C{points}} enclose a pole or zero area.
264 @see: L{pygeodesy.isclockwise}.
265 '''
266 a = _polygon(datum.ellipsoid.geodesic, points, True, False, wrap)
267 if a < 0:
268 return True
269 elif a > 0:
270 return False
271 raise _areaError(points)
274def nearestOn(point, point1, point2, within=True, height=None, wrap=False,
275 equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds):
276 '''I{Iteratively} locate the closest point on the geodesic between
277 two other (ellipsoidal) points.
279 @arg point: Reference point (C{LatLon}).
280 @arg point1: Start point of the geodesic (C{LatLon}).
281 @arg point2: End point of the geodesic (C{LatLon}).
282 @kwarg within: If C{True}, return the closest point I{between}
283 B{C{point1}} and B{C{point2}}, otherwise the
284 closest point elsewhere on the geodesic (C{bool}).
285 @kwarg height: Optional height for the closest point (C{meter},
286 conventionally) or C{None} or C{False} for the
287 interpolated height. If C{False}, the closest
288 takes the heights of the points into account.
289 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll both
290 B{C{point1}} and B{C{point2}} (C{bool}).
291 @kwarg equidistant: An azimuthal equidistant projection (I{class}
292 or function L{pygeodesy.equidistant}) or C{None}
293 for the preferred C{B{point}.Equidistant}.
294 @kwarg tol: Convergence tolerance (C{meter}).
295 @kwarg LatLon: Optional class to return the closest point
296 (L{LatLon}) or C{None}.
297 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword
298 arguments, ignored if C{B{LatLon} is None}.
300 @return: Closest point, a B{C{LatLon}} instance or if C{B{LatLon}
301 is None}, a L{LatLon4Tuple}C{(lat, lon, height, datum)}.
303 @raise ImportError: Package U{geographiclib
304 <https://PyPI.org/project/geographiclib>}
305 not installed or not found.
307 @raise TypeError: Invalid or non-ellipsoidal B{C{point}}, B{C{point1}}
308 or B{C{point2}} or invalid B{C{equidistant}}.
310 @raise ValueError: No convergence for the B{C{tol}}.
312 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/
313 calculating-intersection-of-two-circles>} and U{Karney's paper
314 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME
315 BOUNDARIES} for more details about the iteration algorithm.
316 '''
317 return _nearestOn(point, point1, point2, within=within, height=height, wrap=wrap,
318 equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds)
321def perimeterOf(points, closed=False, datum=_WGS84, wrap=True):
322 '''Compute the perimeter of an (ellipsoidal) polygon or composite using I{Karney}'s
323 U{geographiclib<https://PyPI.org/project/geographiclib>} package.
325 @arg points: The polygon points (L{LatLon}[], L{BooleanFHP} or
326 L{BooleanGH}).
327 @kwarg closed: Optionally, close the polygon (C{bool}).
328 @kwarg datum: Optional datum (L{Datum}).
329 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the
330 B{C{points}} (C{bool}).
332 @return: Perimeter (C{meter}, same as units of the B{C{datum}}'s
333 ellipsoid axes).
335 @raise ImportError: Package U{geographiclib
336 <https://PyPI.org/project/geographiclib>}
337 not installed or not found.
339 @raise PointsError: Insufficient number of B{C{points}}.
341 @raise TypeError: Some B{C{points}} are not L{LatLon} or C{B{closed}=False}
342 with B{C{points}} a composite.
344 @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled
345 longitudes not supported or C{B{closed}=False}
346 with C{B{points}} a composite.
348 @see: Functions L{pygeodesy.perimeterOf}, L{ellipsoidalExact.perimeterOf},
349 L{ellipsoidalGeodSolve.perimeterOf}, L{sphericalNvector.perimeterOf}
350 and L{sphericalTrigonometry.perimeterOf}.
351 '''
352 return _polygon(datum.ellipsoid.geodesic, points, closed, True, wrap)
355__all__ += _ALL_OTHER(Cartesian, LatLon, # classes
356 areaOf, intersecant2, # from .ellipsoidalBase
357 intersection3, intersections2, isclockwise, ispolar,
358 nearestOn, perimeterOf)
360# **) MIT License
361#
362# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.
363#
364# Permission is hereby granted, free of charge, to any person obtaining a
365# copy of this software and associated documentation files (the "Software"),
366# to deal in the Software without restriction, including without limitation
367# the rights to use, copy, modify, merge, publish, distribute, sublicense,
368# and/or sell copies of the Software, and to permit persons to whom the
369# Software is furnished to do so, subject to the following conditions:
370#
371# The above copyright notice and this permission notice shall be included
372# in all copies or substantial portions of the Software.
373#
374# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
375# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
376# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
377# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
378# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
379# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
380# OTHER DEALINGS IN THE SOFTWARE.