Coverage for pygeodesy/ellipsoidalExact.py: 100%
42 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-04-09 11:05 -0400
« prev ^ index » next coverage.py v7.6.1, created at 2025-04-09 11:05 -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 fabs, _polygon, 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__ = '24.11.06'
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):
92 '''Compute the area of an (ellipsoidal) polygon or composite.
94 @arg points: The polygon points (L{LatLon}[], L{BooleanFHP} or
95 L{BooleanGH}).
96 @kwarg datum: Optional datum (L{Datum}).
97 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the
98 B{C{points}} (C{bool}).
100 @return: Area (C{meter} I{squared}, same units as the B{C{datum}}'s
101 ellipsoid axes).
103 @raise PointsError: Insufficient number of B{C{points}}.
105 @raise TypeError: Some B{C{points}} are not L{LatLon}.
107 @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled
108 longitudes not supported.
110 @see: Functions L{pygeodesy.areaOf}, L{ellipsoidalGeodSolve.areaOf},
111 L{ellipsoidalKarney.areaOf}, L{sphericalNvector.areaOf} and
112 L{sphericalTrigonometry.areaOf}.
114 @note: The U{area of a polygon enclosing a pole<https://GeographicLib.SourceForge.io/
115 C++/doc/classGeographicLib_1_1GeodesicExact.html#a3d7a9155e838a09a48dc14d0c3fac525>}
116 can be found by adding half the datum's ellipsoid surface area to the polygon's area.
117 '''
118 return fabs(_polygon(datum.ellipsoid.geodesicx, points, True, False, wrap))
121def intersection3(start1, end1, start2, end2, height=None, wrap=False, # was=True
122 equidistant=None, tol=_TOL_M, **LatLon_and_kwds):
123 '''I{Iteratively} compute the intersection point of two geodesic lines, each
124 given as two points or as an start point and a bearing from North.
126 @arg start1: Start point of the first line (L{LatLon}).
127 @arg end1: End point of the first line (L{LatLon}) or the initial bearing
128 at B{C{start1}} (compass C{degrees360}).
129 @arg start2: Start point of the second line (L{LatLon}).
130 @arg end2: End point of the second line (L{LatLon}) or the initial bearing
131 at B{C{start2}} (compass C{degrees360}).
132 @kwarg height: Optional height at the intersection (C{meter}, conventionally)
133 or C{None} for the mean height.
134 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{start2}} and
135 both B{C{end*}} points (C{bool}).
136 @kwarg equidistant: An azimuthal equidistant projection (I{class} or function
137 L{pygeodesy.equidistant}) or C{None} for the preferred
138 C{B{start1}.Equidistant}.
139 @kwarg tol: Tolerance for convergence and for skew line distance and length
140 (C{meter}, conventionally).
141 @kwarg LatLon_and_kwds: Optional class C{B{LatLon}=}L{LatLon} to return the
142 intersection points and optionally, additional B{C{LatLon}}
143 keyword arguments, ignored if C{B{LatLon}=None}.
145 @return: An L{Intersection3Tuple}C{(point, outside1, outside2)} with C{point}
146 a B{C{LatLon}} or if C{B{LatLon} is None}, a L{LatLon4Tuple}C{(lat,
147 lon, height, datum)}.
149 @raise IntersectionError: Skew, colinear, parallel or otherwise non-intersecting
150 lines or no convergence for the given B{C{tol}}.
152 @raise TypeError: Invalid or non-ellipsoidal B{C{start1}}, B{C{end1}},
153 B{C{start2}} or B{C{end2}} or invalid B{C{equidistant}}.
155 @note: For each line specified with an initial bearing, a pseudo-end point is
156 computed as the C{destination} along that bearing at about 1.5 times the
157 distance from the start point to an initial gu-/estimate of the intersection
158 point (and between 1/8 and 3/8 of the authalic earth perimeter).
160 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/
161 calculating-intersection-of-two-circles>} and U{Karney's paper
162 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME
163 BOUNDARIES} for more details about the iteration algorithm.
164 '''
165 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon)
166 return _intersection3(start1, end1, start2, end2, height=height, wrap=wrap,
167 equidistant=equidistant, tol=tol, **kwds)
170def intersections2(center1, radius1, center2, radius2, height=None, wrap=False, # was=True
171 equidistant=None, tol=_TOL_M, **LatLon_and_kwds):
172 '''I{Iteratively} compute the intersection points of two circles, each defined
173 by an (ellipsoidal) center point and a radius.
175 @arg center1: Center of the first circle (L{LatLon}).
176 @arg radius1: Radius of the first circle (C{meter}, conventionally).
177 @arg center2: Center of the second circle (L{LatLon}).
178 @arg radius2: Radius of the second circle (C{meter}, same units as
179 B{C{radius1}}).
180 @kwarg height: Optional height for the intersection points (C{meter},
181 conventionally) or C{None} for the I{"radical height"}
182 at the I{radical line} between both centers.
183 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{center2}}
184 (C{bool}).
185 @kwarg equidistant: An azimuthal equidistant projection (I{class} or
186 function L{pygeodesy.equidistant}) or C{None} for
187 the preferred C{B{center1}.Equidistant}.
188 @kwarg tol: Convergence tolerance (C{meter}, same units as B{C{radius1}}
189 and B{C{radius2}}).
190 @kwarg LatLon_and_kwds: Optional class C{B{LatLon}=}L{LatLon} to return the
191 intersection points and optionally, additional B{C{LatLon}}
192 keyword arguments, ignored if C{B{LatLon}=None}.
194 @return: 2-Tuple of the intersection points, each a B{C{LatLon}} instance
195 or L{LatLon4Tuple}C{(lat, lon, height, datum)} if C{B{LatLon} is
196 None}. For abutting circles, both points are the same instance,
197 aka the I{radical center}.
199 @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting
200 circles or no convergence for the B{C{tol}}.
202 @raise TypeError: Invalid or non-ellipsoidal B{C{center1}} or B{C{center2}}
203 or invalid B{C{equidistant}}.
205 @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{height}}.
207 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/
208 calculating-intersection-of-two-circles>}, U{Karney's paper
209 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME BOUNDARIES},
210 U{circle-circle<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>} and
211 U{sphere-sphere<https://MathWorld.Wolfram.com/Sphere-SphereIntersection.html>}
212 intersections.
213 '''
214 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon)
215 return _intersections2(center1, radius1, center2, radius2, height=height, wrap=wrap,
216 equidistant=equidistant, tol=tol, **kwds)
219def isclockwise(points, datum=_WGS84, wrap=True):
220 '''Determine the direction of a path or polygon.
222 @arg points: The path or polygon points (C{LatLon}[]).
223 @kwarg datum: Optional datum (L{Datum}).
224 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the
225 B{C{points}} (C{bool}).
227 @return: C{True} if B{C{points}} are clockwise, C{False} otherwise.
229 @raise PointsError: Insufficient number of B{C{points}}.
231 @raise TypeError: Some B{C{points}} are not C{LatLon}.
233 @raise ValueError: The B{C{points}} enclose a pole or zero area.
235 @see: L{pygeodesy.isclockwise}.
236 '''
237 a = _polygon(datum.ellipsoid.geodesicx, points, True, False, wrap)
238 if a < 0:
239 return True
240 elif a > 0:
241 return False
242 raise _areaError(points)
245def nearestOn(point, point1, point2, within=True, height=None, wrap=False,
246 equidistant=None, tol=_TOL_M, **LatLon_and_kwds):
247 '''I{Iteratively} locate the closest point on the geodesic (line)
248 between two other (ellipsoidal) points.
250 @arg point: Reference point (C{LatLon}).
251 @arg point1: Start point of the geodesic (C{LatLon}).
252 @arg point2: End point of the geodesic (C{LatLon}).
253 @kwarg within: If C{True}, return the closest point I{between}
254 B{C{point1}} and B{C{point2}}, otherwise the
255 closest point elsewhere on the geodesic (C{bool}).
256 @kwarg height: Optional height for the closest point (C{meter},
257 conventionally) or C{None} or C{False} for the
258 interpolated height. If C{False}, the closest
259 takes the heights of the points into account.
260 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll both
261 B{C{point1}} and B{C{point2}} (C{bool}).
262 @kwarg equidistant: An azimuthal equidistant projection (I{class}
263 or function L{pygeodesy.equidistant}) or C{None}
264 for the preferred C{B{point}.Equidistant}.
265 @kwarg tol: Convergence tolerance (C{meter}).
266 @kwarg LatLon_and_kwds: Optional class C{B{LatLon}=}L{LatLon} to return the
267 closest point and optionally, additional B{C{LatLon}} keyword
268 arguments, ignored if C{B{LatLon}=None}.
270 @return: Closest point, a B{C{LatLon}} instance or if C{B{LatLon} is None},
271 a L{LatLon4Tuple}C{(lat, lon, height, datum)}.
273 @raise TypeError: Invalid or non-ellipsoidal B{C{point}}, B{C{point1}} or
274 B{C{point2}} or invalid B{C{equidistant}}.
276 @raise ValueError: No convergence for the B{C{tol}}.
278 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/
279 calculating-intersection-of-two-circles>} and U{Karney's paper
280 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME
281 BOUNDARIES} for more details about the iteration algorithm.
282 '''
283 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon)
284 return _nearestOn(point, point1, point2, within=within, height=height, wrap=wrap,
285 equidistant=equidistant, tol=tol, **kwds)
288def perimeterOf(points, closed=False, datum=_WGS84, wrap=True):
289 '''Compute the perimeter of an (ellipsoidal) polygon or composite.
291 @arg points: The polygon points (L{LatLon}[], L{BooleanFHP} or
292 L{BooleanGH}).
293 @kwarg closed: Optionally, close the polygon (C{bool}).
294 @kwarg datum: Optional datum (L{Datum}).
295 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the
296 B{C{points}} (C{bool}).
298 @return: Perimeter (C{meter}, same units as the B{C{datum}}'s
299 ellipsoid axes).
301 @raise PointsError: Insufficient number of B{C{points}}.
303 @raise TypeError: Some B{C{points}} are not L{LatLon}.
305 @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled
306 longitudes not supported or C{B{closed}=False}
307 with C{B{points}} a composite.
309 @see: Functions L{pygeodesy.perimeterOf}, L{ellipsoidalGeodSolve.perimeterOf},
310 L{ellipsoidalKarney.perimeterOf}, L{sphericalNvector.perimeterOf} and
311 L{sphericalTrigonometry.perimeterOf}.
312 '''
313 return _polygon(datum.ellipsoid.geodesicx, points, closed, True, wrap)
316__all__ += _ALL_OTHER(Cartesian, LatLon, # classes
317 areaOf, intersecant2, # from .ellipsoidalBase
318 intersection3, intersections2, isclockwise, ispolar,
319 nearestOn, perimeterOf)
321# **) MIT License
322#
323# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.
324#
325# Permission is hereby granted, free of charge, to any person obtaining a
326# copy of this software and associated documentation files (the "Software"),
327# to deal in the Software without restriction, including without limitation
328# the rights to use, copy, modify, merge, publish, distribute, sublicense,
329# and/or sell copies of the Software, and to permit persons to whom the
330# Software is furnished to do so, subject to the following conditions:
331#
332# The above copyright notice and this permission notice shall be included
333# in all copies or substantial portions of the Software.
334#
335# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
336# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
337# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
338# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
339# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
340# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
341# OTHER DEALINGS IN THE SOFTWARE.