Coverage for pygeodesy/ellipsoidalExact.py: 100%
42 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-29 12:40 -0400
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-29 12:40 -0400
2# -*- coding: utf-8 -*-
4u'''Exact ellipsoidal geodesy using I{Karney}'s Exact Geodesic.
6Ellipsoidal geodetic (lat-/longitude) L{LatLon} and geocentric
7(ECEF) L{Cartesian} classes and functions L{areaOf}, L{intersections2},
8L{isclockwise}, L{nearestOn} and L{perimeterOf} based on classes
9L{GeodesicExact}, L{GeodesicAreaExact} and L{GeodesicLineExact}.
10'''
12# from pygeodesy.datums import _WGS84 # from .ellipsoidalBase
13from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase, \
14 _nearestOn, _WGS84
15from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, \
16 _intersection3, _intersections2, \
17 _TOL_M, intersecant2
18# from pygeodesy.errors import _xkwds # from .karney
19from pygeodesy.karney import _polygon, fabs, Property_RO, _xkwds
20from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, _ALL_OTHER
21from pygeodesy.points import _areaError, ispolar # PYCHOK exported
22# from pygeodesy.props import Property_RO # from .karney
24# from math import fabs # from .karney
26__all__ = _ALL_LAZY.ellipsoidalExact
27__version__ = '25.05.28'
30class Cartesian(CartesianEllipsoidalBase):
31 '''Extended to convert exact L{Cartesian} to exact L{LatLon} points.
32 '''
34 def toLatLon(self, **LatLon_and_kwds): # PYCHOK LatLon=LatLon, datum=None
35 '''Convert this cartesian point to an exact geodetic point.
37 @kwarg LatLon_and_kwds: Optional L{LatLon} and L{LatLon} keyword
38 arguments as C{datum}. Use C{B{LatLon}=...,
39 B{datum}=...} to override this L{LatLon} class
40 or specify C{B{LatLon}=None}.
42 @return: The geodetic point (L{LatLon}) or if C{B{LatLon} is None},
43 an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)}
44 with C{C} and C{M} if available.
46 @raise TypeError: Invalid B{C{LatLon_and_kwds}} argument.
47 '''
48 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon, datum=self.datum)
49 return CartesianEllipsoidalBase.toLatLon(self, **kwds)
52class LatLon(LatLonEllipsoidalBaseDI):
53 '''An ellipsoidal L{LatLon} like L{ellipsoidalKarney.LatLon} but using
54 exact geodesic classes L{GeodesicExact} and L{GeodesicLineExact} to
55 compute geodesic distances, bearings (azimuths), etc.
56 '''
58 @Property_RO
59 def Equidistant(self):
60 '''Get the prefered azimuthal equidistant projection I{class} (L{EquidistantExact}).
61 '''
62 return _MODS.azimuthal.EquidistantExact
64 @Property_RO
65 def geodesicx(self):
66 '''Get this C{LatLon}'s exact geodesic (L{GeodesicExact}).
67 '''
68 return self.datum.ellipsoid.geodesicx
70 geodesic = geodesicx # for C{._Direct} and C{._Inverse}
72 def toCartesian(self, **Cartesian_datum_kwds): # PYCHOK Cartesian=Cartesian, ...
73 '''Convert this point to exact cartesian (ECEF) coordinates.
75 @kwarg Cartesian_datum_kwds: Optional L{Cartesian}, B{C{datum}} and
76 other keyword arguments, ignored if C{B{Cartesian}
77 is None}. Use C{B{Cartesian}=...} to override this
78 L{Cartesian} class or set C{B{Cartesian}=None}.
80 @return: The cartesian (ECEF) coordinates as (L{Cartesian}) or if
81 C{B{Cartesian} is None}, an L{Ecef9Tuple}C{(x, y, z, lat,
82 lon, height, C, M, datum)} with C{C} and C{M} if available.
84 @raise TypeError: Invalid B{C{Cartesian}}, B{C{datum}} or other
85 B{C{Cartesian_datum_kwds}}.
86 '''
87 kwds = _xkwds(Cartesian_datum_kwds, Cartesian=Cartesian, datum=self.datum)
88 return LatLonEllipsoidalBaseDI.toCartesian(self, **kwds)
91def areaOf(points, datum=_WGS84, wrap=True, polar=False):
92 '''Compute the area of an (ellipsoidal) polygon or composite.
94 @arg points: The polygon points (L{LatLon}[], L{BooleanFHP} or L{BooleanGH}).
95 @kwarg datum: Optional datum (L{Datum}).
96 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{points}}
97 (C{bool}).
98 @kwarg polar: Use C{B{polar}=True} if the polygon encloses a pole (C{bool}), see
99 function L{ispolar<pygeodesy.points.ispolar>} and U{area of a polygon
100 enclosing a pole<https://GeographicLib.SourceForge.io/C++/doc/
101 classGeographicLib_1_1GeodesicExact.html#a3d7a9155e838a09a48dc14d0c3fac525>}.
103 @return: Area (C{meter} I{squared}, same units as the B{C{datum}}'s ellipsoid axes).
105 @raise PointsError: Insufficient number of B{C{points}}.
107 @raise TypeError: Some B{C{points}} are not L{LatLon}.
109 @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled
110 longitudes not supported.
112 @see: Functions L{pygeodesy.areaOf}, L{ellipsoidalGeodSolve.areaOf},
113 L{ellipsoidalKarney.areaOf}, L{sphericalNvector.areaOf} and
114 L{sphericalTrigonometry.areaOf}.
115 '''
116 return fabs(_polygon(datum.ellipsoid.geodesicx, points, True, False, wrap, polar))
119def intersection3(start1, end1, start2, end2, height=None, wrap=False, # was=True
120 equidistant=None, tol=_TOL_M, **LatLon_and_kwds):
121 '''I{Iteratively} compute the intersection point of two geodesic lines, each
122 given as two points or as an start point and a bearing from North.
124 @arg start1: Start point of the first line (L{LatLon}).
125 @arg end1: End point of the first line (L{LatLon}) or the initial bearing
126 at B{C{start1}} (compass C{degrees360}).
127 @arg start2: Start point of the second line (L{LatLon}).
128 @arg end2: End point of the second line (L{LatLon}) or the initial bearing
129 at B{C{start2}} (compass C{degrees360}).
130 @kwarg height: Optional height at the intersection (C{meter}, conventionally)
131 or C{None} for the mean height.
132 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{start2}} and
133 both B{C{end*}} points (C{bool}).
134 @kwarg equidistant: An azimuthal equidistant projection (I{class} or function
135 L{pygeodesy.equidistant}) or C{None} for the preferred
136 C{B{start1}.Equidistant}.
137 @kwarg tol: Tolerance for convergence and for skew line distance and length
138 (C{meter}, conventionally).
139 @kwarg LatLon_and_kwds: Optional class C{B{LatLon}=}L{LatLon} to return the
140 intersection points and optionally, additional B{C{LatLon}}
141 keyword arguments, ignored if C{B{LatLon}=None}.
143 @return: An L{Intersection3Tuple}C{(point, outside1, outside2)} with C{point}
144 a B{C{LatLon}} or if C{B{LatLon} is None}, a L{LatLon4Tuple}C{(lat,
145 lon, height, datum)}.
147 @raise IntersectionError: Skew, colinear, parallel or otherwise non-intersecting
148 lines or no convergence for the given B{C{tol}}.
150 @raise TypeError: Invalid or non-ellipsoidal B{C{start1}}, B{C{end1}},
151 B{C{start2}} or B{C{end2}} or invalid B{C{equidistant}}.
153 @note: For each line specified with an initial bearing, a pseudo-end point is
154 computed as the C{destination} along that bearing at about 1.5 times the
155 distance from the start point to an initial gu-/estimate of the intersection
156 point (and between 1/8 and 3/8 of the authalic earth perimeter).
158 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/
159 calculating-intersection-of-two-circles>} and U{Karney's paper
160 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME
161 BOUNDARIES} for more details about the iteration algorithm.
162 '''
163 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon)
164 return _intersection3(start1, end1, start2, end2, height=height, wrap=wrap,
165 equidistant=equidistant, tol=tol, **kwds)
168def intersections2(center1, radius1, center2, radius2, height=None, wrap=False, # was=True
169 equidistant=None, tol=_TOL_M, **LatLon_and_kwds):
170 '''I{Iteratively} compute the intersection points of two circles, each defined
171 by an (ellipsoidal) center point and a radius.
173 @arg center1: Center of the first circle (L{LatLon}).
174 @arg radius1: Radius of the first circle (C{meter}, conventionally).
175 @arg center2: Center of the second circle (L{LatLon}).
176 @arg radius2: Radius of the second circle (C{meter}, same units as
177 B{C{radius1}}).
178 @kwarg height: Optional height for the intersection points (C{meter},
179 conventionally) or C{None} for the I{"radical height"}
180 at the I{radical line} between both centers.
181 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{center2}}
182 (C{bool}).
183 @kwarg equidistant: An azimuthal equidistant projection (I{class} or
184 function L{pygeodesy.equidistant}) or C{None} for
185 the preferred C{B{center1}.Equidistant}.
186 @kwarg tol: Convergence tolerance (C{meter}, same units as B{C{radius1}}
187 and B{C{radius2}}).
188 @kwarg LatLon_and_kwds: Optional class C{B{LatLon}=}L{LatLon} to return the
189 intersection points and optionally, additional B{C{LatLon}}
190 keyword arguments, ignored if C{B{LatLon}=None}.
192 @return: 2-Tuple of the intersection points, each a B{C{LatLon}} instance
193 or L{LatLon4Tuple}C{(lat, lon, height, datum)} if C{B{LatLon} is
194 None}. For abutting circles, both points are the same instance,
195 aka the I{radical center}.
197 @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting
198 circles or no convergence for the B{C{tol}}.
200 @raise TypeError: Invalid or non-ellipsoidal B{C{center1}} or B{C{center2}}
201 or invalid B{C{equidistant}}.
203 @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{height}}.
205 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/
206 calculating-intersection-of-two-circles>}, U{Karney's paper
207 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME BOUNDARIES},
208 U{circle-circle<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>} and
209 U{sphere-sphere<https://MathWorld.Wolfram.com/Sphere-SphereIntersection.html>}
210 intersections.
211 '''
212 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon)
213 return _intersections2(center1, radius1, center2, radius2, height=height, wrap=wrap,
214 equidistant=equidistant, tol=tol, **kwds)
217def isclockwise(points, datum=_WGS84, wrap=True, polar=False):
218 '''Determine the direction of a path or polygon.
220 @arg points: The path or polygon points (C{LatLon}[]).
221 @kwarg datum: Optional datum (L{Datum}).
222 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{points}} (C{bool}).
223 @kwarg polar: Use C{B{polar}=True} if the C{B{points}} enclose a pole (C{bool}),
224 see function U{ispolar<pygeodeys.points.ispolar>}.
226 @return: C{True} if B{C{points}} are clockwise, C{False} otherwise.
228 @raise PointsError: Insufficient number of B{C{points}}.
230 @raise TypeError: Some B{C{points}} are not C{LatLon}.
232 @raise ValueError: The B{C{points}} enclose a pole or zero area.
234 @see: L{pygeodesy.isclockwise}.
235 '''
236 a = _polygon(datum.ellipsoid.geodesicx, points, True, False, wrap, polar)
237 if a < 0:
238 return True
239 elif a > 0:
240 return False
241 raise _areaError(points)
244def nearestOn(point, point1, point2, within=True, height=None, wrap=False,
245 equidistant=None, tol=_TOL_M, **LatLon_and_kwds):
246 '''I{Iteratively} locate the closest point on the geodesic (line)
247 between two other (ellipsoidal) points.
249 @arg point: Reference point (C{LatLon}).
250 @arg point1: Start point of the geodesic (C{LatLon}).
251 @arg point2: End point of the geodesic (C{LatLon}).
252 @kwarg within: If C{True}, return the closest point I{between}
253 B{C{point1}} and B{C{point2}}, otherwise the
254 closest point elsewhere on the geodesic (C{bool}).
255 @kwarg height: Optional height for the closest point (C{meter},
256 conventionally) or C{None} or C{False} for the
257 interpolated height. If C{False}, the closest
258 takes the heights of the points into account.
259 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll both
260 B{C{point1}} and B{C{point2}} (C{bool}).
261 @kwarg equidistant: An azimuthal equidistant projection (I{class}
262 or function L{pygeodesy.equidistant}) or C{None}
263 for the preferred C{B{point}.Equidistant}.
264 @kwarg tol: Convergence tolerance (C{meter}).
265 @kwarg LatLon_and_kwds: Optional class C{B{LatLon}=}L{LatLon} to return the
266 closest point and optionally, additional B{C{LatLon}} keyword
267 arguments, ignored if C{B{LatLon}=None}.
269 @return: Closest point, a B{C{LatLon}} instance or if C{B{LatLon} is None},
270 a L{LatLon4Tuple}C{(lat, lon, height, datum)}.
272 @raise TypeError: Invalid or non-ellipsoidal B{C{point}}, B{C{point1}} or
273 B{C{point2}} or invalid B{C{equidistant}}.
275 @raise ValueError: No convergence for the B{C{tol}}.
277 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/
278 calculating-intersection-of-two-circles>} and U{Karney's paper
279 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME
280 BOUNDARIES} for more details about the iteration algorithm.
281 '''
282 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon)
283 return _nearestOn(point, point1, point2, within=within, height=height, wrap=wrap,
284 equidistant=equidistant, tol=tol, **kwds)
287def perimeterOf(points, closed=False, datum=_WGS84, wrap=True):
288 '''Compute the perimeter of an (ellipsoidal) polygon or composite.
290 @arg points: The polygon points (L{LatLon}[], L{BooleanFHP} or L{BooleanGH}).
291 @kwarg closed: Optionally, close the polygon (C{bool}).
292 @kwarg datum: Optional datum (L{Datum}).
293 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{points}} (C{bool}).
295 @return: Perimeter (C{meter}, same units as the B{C{datum}}'s ellipsoid axes).
297 @raise PointsError: Insufficient number of B{C{points}}.
299 @raise TypeError: Some B{C{points}} are not L{LatLon}.
301 @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled longitudes not
302 supported or C{B{closed}=False} with C{B{points}} a composite.
304 @see: Functions L{pygeodesy.perimeterOf}, L{ellipsoidalGeodSolve.perimeterOf},
305 L{ellipsoidalKarney.perimeterOf}, L{sphericalNvector.perimeterOf} and
306 L{sphericalTrigonometry.perimeterOf}.
307 '''
308 return _polygon(datum.ellipsoid.geodesicx, points, closed, True, wrap, False)
311__all__ += _ALL_OTHER(Cartesian, LatLon, # classes
312 areaOf, intersecant2, # from .ellipsoidalBase
313 intersection3, intersections2, isclockwise, ispolar,
314 nearestOn, perimeterOf)
316# **) MIT License
317#
318# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.
319#
320# Permission is hereby granted, free of charge, to any person obtaining a
321# copy of this software and associated documentation files (the "Software"),
322# to deal in the Software without restriction, including without limitation
323# the rights to use, copy, modify, merge, publish, distribute, sublicense,
324# and/or sell copies of the Software, and to permit persons to whom the
325# Software is furnished to do so, subject to the following conditions:
326#
327# The above copyright notice and this permission notice shall be included
328# in all copies or substantial portions of the Software.
329#
330# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
331# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
332# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
333# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
334# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
335# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
336# OTHER DEALINGS IN THE SOFTWARE.