Coverage for pygeodesy/ellipsoidalExact.py: 100%

1

2# -*- coding: utf-8 -*-

3

4 u'''Exact ellipsoidal geodesy using I{Karney}'s Exact Geodesic.

5

6 Ellipsoidal geodetic (lat-/longitude) L{LatLon} and geocentric

7(ECEF) L{Cartesian} classes and functions L{areaOf}, L{intersections2},

8 L{isclockwise}, L{nearestOn} and L{perimeterOf} based on classes

9 L{GeodesicExact}, L{GeodesicAreaExact} and L{GeodesicLineExact}.

10'''

11

12# from pygeodesy.datums import _WGS84 # from .ellipsoidalBase

13 from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase, \

14 _nearestOn, Property_RO, _WGS84

15 from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, \

16 _intersection3, _intersections2, \

17 _TOL_M, intersecant2

18# from pygeodesy.errors import _xkwds # from .karney

19 from pygeodesy.karney import _polygon, fabs, _xkwds

20 from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, _ALL_OTHER

21 from pygeodesy.points import _areaError, ispolar # PYCHOK exported

22# from pygeodesy.props import Property_RO # from .ellipsoidalBase

23

24# from math import fabs # from .karney

25

26 __all__ = _ALL_LAZY.ellipsoidalExact

27 __version__ = '25.08.28'

28

29

30 class Cartesian(CartesianEllipsoidalBase):

31 '''Extended to convert exact L{Cartesian} to exact L{LatLon} points.

32 '''

33

34 def toLatLon(self, **LatLon_and_kwds): # PYCHOK LatLon=LatLon, datum=None

35 '''Convert this cartesian point to an exact geodetic point.

36

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}.

41

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.

45

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)

50

51

52 class 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 '''

57

58 @Property_RO

59 def Equidistant(self):

60 '''Get the prefered azimuthal equidistant projection I{class} (L{EquidistantExact}).

61 '''

62 return _MODS.azimuthal.EquidistantExact

63

64 @Property_RO

65 def geodesicx(self):

66 '''Get this C{LatLon}'s exact geodesic (L{GeodesicExact}).

67 '''

68 return self.datum.ellipsoid.geodesicx

69

70 geodesic = geodesicx # for C{._Direct} and C{._Inverse}

71

72 def toCartesian(self, **Cartesian_datum_kwds): # PYCHOK Cartesian=Cartesian, ...

73 '''Convert this point to exact cartesian (ECEF) coordinates.

74

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}.

79

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.

83

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)

89

90

91 def areaOf(points, datum=_WGS84, wrap=True, polar=False):

92 '''Compute the area of an (ellipsoidal) polygon or composite.

93

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>}.

102

103 @return: Area (C{meter} I{squared}, same units as the B{C{datum}}'s ellipsoid axes).

104

105 @raise PointsError: Insufficient number of B{C{points}}.

106

107 @raise TypeError: Some B{C{points}} are not L{LatLon}.

108

109 @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled

110 longitudes not supported.

111

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))

117

118

119 def 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.

123

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}.

142

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)}.

146

147 @raise IntersectionError: Skew, colinear, parallel or otherwise non-intersecting

148 lines or no convergence for the given B{C{tol}}.

149

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}}.

152

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).

157

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)

166

167

168 def 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.

172

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}.

191

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}.

196

197 @raise IntersectionError: Concentric, antipodal, invalid or non-intersecting

198 circles or no convergence for the B{C{tol}}.

199

200 @raise TypeError: Invalid or non-ellipsoidal B{C{center1}} or B{C{center2}}

201 or invalid B{C{equidistant}}.

202

203 @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{height}}.

204

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)

215

216

217 def isclockwise(points, datum=_WGS84, wrap=True, polar=False):

218 '''Determine the direction of a path or polygon.

219

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>}.

225

226 @return: C{True} if B{C{points}} are clockwise, C{False} otherwise.

227

228 @raise PointsError: Insufficient number of B{C{points}}.

229

230 @raise TypeError: Some B{C{points}} are not C{LatLon}.

231

232 @raise ValueError: The B{C{points}} enclose a pole or zero area.

233

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)

242

243

244 def 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.

248

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}.

268

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)}.

271

272 @raise TypeError: Invalid or non-ellipsoidal B{C{point}}, B{C{point1}} or

273 B{C{point2}} or invalid B{C{equidistant}}.

274

275 @raise ValueError: No convergence for the B{C{tol}}.

276

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)

285

286

287 def perimeterOf(points, closed=False, datum=_WGS84, wrap=True):

288 '''Compute the perimeter of an (ellipsoidal) polygon or composite.

289

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}).

294

295 @return: Perimeter (C{meter}, same units as the B{C{datum}}'s ellipsoid axes).

296

297 @raise PointsError: Insufficient number of B{C{points}}.

298

299 @raise TypeError: Some B{C{points}} are not L{LatLon}.

300

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.

303

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)

309

310

311 __all__ += _ALL_OTHER(Cartesian, LatLon, # classes

312 areaOf, intersecant2, # from .ellipsoidalBase

313 intersection3, intersections2, isclockwise, ispolar,

314 nearestOn, perimeterOf)

315

316# **) MIT License

317#

318# Copyright (C) 2016-2026 -- 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.