Coverage for pygeodesy/ellipsoidalGeodSolve.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-25 13:15 -0400

1 

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

3 

4u'''Exact ellipsoidal geodesy, intended I{for testing purposes only}. 

5 

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 module 

9L{geodsolve}, a wrapper invoking I{Karney}'s U{GeodSolve 

10<https://GeographicLib.SourceForge.io/C++/doc/GeodSolve.1.html>} utility. 

11''' 

12 

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

14from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase, \ 

15 _nearestOn, _WGS84 

16from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, _TOL_M, \ 

17 _intersection3, _intersections2 

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 

23 

24# from math import fabs # from .karney 

25 

26__all__ = _ALL_LAZY.ellipsoidalGeodSolve 

27__version__ = '24.08.13' 

28 

29 

30class 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} 

40 class 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 

52class LatLon(LatLonEllipsoidalBaseDI): 

53 '''An ellipsoidal L{LatLon} like L{ellipsoidalKarney.LatLon} but using (exact) 

54 geodesic I{wrapper} L{GeodesicSolve} to compute the geodesic distance, 

55 initial and final bearing (azimuths) between two given points or the 

56 destination point given a start point and an (initial) bearing. 

57 ''' 

58 

59 @Property_RO 

60 def Equidistant(self): 

61 '''Get the prefered azimuthal equidistant projection I{class} (L{EquidistantGeodSolve}). 

62 ''' 

63 return _MODS.azimuthal.EquidistantGeodSolve 

64 

65 @Property_RO 

66 def geodesicx(self): 

67 '''Get this C{LatLon}'s (exact) geodesic (L{GeodesicSolve}). 

68 ''' 

69 return self.datum.ellipsoid.geodsolve 

70 

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

72 

73 def toCartesian(self, **Cartesian_datum_kwds): # PYCHOK Cartesian=Cartesian, datum=None 

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

75 

76 @kwarg Cartesian_datum_kwds: Optional L{Cartesian}, B{C{datum}} and other keyword 

77 arguments, ignored if C{B{Cartesian} is None}. Use C{B{Cartesian}=Class} 

78 to override this L{Cartesian} class or set C{B{Cartesian}=None}. 

79 

80 @return: The cartesian (ECEF) coordinates (L{Cartesian}) or if C{B{Cartesian} is 

81 None}, an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with 

82 C{C} and C{M} if available. 

83 

84 @raise TypeError: Invalid B{C{Cartesian}}, B{C{datum}} or other B{C{Cartesian_datum_kwds}}. 

85 ''' 

86 kwds = _xkwds(Cartesian_datum_kwds, Cartesian=Cartesian, datum=self.datum) 

87 return LatLonEllipsoidalBaseDI.toCartesian(self, **kwds) 

88 

89 

90def areaOf(points, datum=_WGS84, wrap=True): 

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

92 

93 @arg points: The polygon points (L{LatLon}[], L{BooleanFHP} or L{BooleanGH}). 

94 @kwarg datum: Optional datum (L{Datum}). 

95 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{points}} (C{bool}). 

96 

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

98 

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

100 

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

102 

103 @raise ValueError: Invalid C{B{wrap}=False}, unwrapped, unrolled longitudes not supported. 

104 

105 @see: Functions L{pygeodesy.areaOf}, L{ellipsoidalExact.areaOf}, L{ellipsoidalKarney.areaOf}, 

106 L{sphericalNvector.areaOf} and L{sphericalTrigonometry.areaOf}. 

107 ''' 

108 return fabs(_polygon(datum.ellipsoid.geodsolve, points, True, False, wrap)) 

109 

110 

111def intersection3(start1, end1, start2, end2, height=None, wrap=False, # was=True 

112 equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds): 

113 '''I{Iteratively} compute the intersection point of two lines, each defined 

114 by two (ellipsoidal) points or by an (ellipsoidal) start point and an 

115 (initial) bearing from North. 

116 

117 @arg start1: Start point of the first line (L{LatLon}). 

118 @arg end1: End point of the first line (L{LatLon}) or the initial bearing 

119 at the first point (compass C{degrees360}). 

120 @arg start2: Start point of the second line (L{LatLon}). 

121 @arg end2: End point of the second line (L{LatLon}) or the initial bearing 

122 at the second point (compass C{degrees360}). 

123 @kwarg height: Optional height at the intersection (C{meter}, conventionally) 

124 or C{None} for the mean height. 

125 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{start2}} 

126 and B{C{end*}} points (C{bool}). 

127 @kwarg equidistant: An azimuthal equidistant projection (I{class} or function 

128 L{pygeodesy.equidistant}) or C{None} for the preferred 

129 C{B{start1}.Equidistant}. 

130 @kwarg tol: Tolerance for convergence and for skew line distance and length 

131 (C{meter}, conventionally). 

132 @kwarg LatLon: Optional class to return the intersection points (L{LatLon}) 

133 or C{None}. 

134 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments, 

135 ignored if C{B{LatLon} is None}. 

136 

137 @return: An L{Intersection3Tuple}C{(point, outside1, outside2)} with C{point} 

138 a B{C{LatLon}} or if C{B{LatLon} is None}, a L{LatLon4Tuple}C{(lat, 

139 lon, height, datum)}. 

140 

141 @raise IntersectionError: Skew, colinear, parallel or otherwise 

142 non-intersecting lines or no convergence 

143 for the given B{C{tol}}. 

144 

145 @raise TypeError: Invalid or non-ellipsoidal B{C{start1}}, B{C{end1}}, 

146 B{C{start2}} or B{C{end2}} or invalid B{C{equidistant}}. 

147 

148 @note: For each line specified with an initial bearing, a pseudo-end point 

149 is computed as the C{destination} along that bearing at about 1.5 

150 times the distance from the start point to an initial gu-/estimate 

151 of the intersection point (and between 1/8 and 3/8 of the authalic 

152 earth perimeter). 

153 

154 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/ 

155 calculating-intersection-of-two-circles>} and U{Karney's paper 

156 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME 

157 BOUNDARIES} for more details about the iteration algorithm. 

158 ''' 

159 return _intersection3(start1, end1, start2, end2, height=height, wrap=wrap, 

160 equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) 

161 

162 

163def intersections2(center1, radius1, center2, radius2, height=None, wrap=False, # was=True 

164 equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds): 

165 '''I{Iteratively} compute the intersection points of two circles, each defined 

166 by an (ellipsoidal) center point and a radius. 

167 

168 @arg center1: Center of the first circle (L{LatLon}). 

169 @arg radius1: Radius of the first circle (C{meter}, conventionally). 

170 @arg center2: Center of the second circle (L{LatLon}). 

171 @arg radius2: Radius of the second circle (C{meter}, same units as 

172 B{C{radius1}}). 

173 @kwarg height: Optional height for the intersection points (C{meter}, 

174 conventionally) or C{None} for the I{"radical height"} 

175 at the I{radical line} between both centers. 

176 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll B{C{center2}} 

177 (C{bool}). 

178 @kwarg equidistant: An azimuthal equidistant projection (I{class} or 

179 function L{pygeodesy.equidistant}) or C{None} for 

180 the preferred C{B{center1}.Equidistant}. 

181 @kwarg tol: Convergence tolerance (C{meter}, same units as B{C{radius1}} 

182 and B{C{radius2}}). 

183 @kwarg LatLon: Optional class to return the intersection points (L{LatLon}) 

184 or C{None}. 

185 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword arguments, 

186 ignored if C{B{LatLon} is None}. 

187 

188 @return: 2-Tuple of the intersection points, each a B{C{LatLon}} instance 

189 or L{LatLon4Tuple}C{(lat, lon, height, datum)} if C{B{LatLon} is 

190 None}. For abutting circles, both points are the same instance, 

191 aka the I{radical center}. 

192 

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

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

195 

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

197 or invalid B{C{equidistant}}. 

198 

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

200 

201 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/ 

202 calculating-intersection-of-two-circles>}, U{Karney's paper 

203 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME BOUNDARIES}, 

204 U{Circle-Circle<https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>} and 

205 U{Sphere-Sphere<https://MathWorld.Wolfram.com/Sphere-SphereIntersection.html>} 

206 intersections. 

207 ''' 

208 return _intersections2(center1, radius1, center2, radius2, height=height, wrap=wrap, 

209 equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) 

210 

211 

212def isclockwise(points, datum=_WGS84, wrap=True): 

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

214 

215 @arg points: The path or polygon points (C{LatLon}[]). 

216 @kwarg datum: Optional datum (L{Datum}). 

217 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the 

218 B{C{points}} (C{bool}). 

219 

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

221 

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

223 

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

225 

226 @raise ValueError: The B{C{points}} enclose a pole or zero 

227 area. 

228 

229 @see: L{pygeodesy.isclockwise}. 

230 ''' 

231 a = _polygon(datum.ellipsoid.geodsolve, points, True, False, wrap) 

232 if a < 0: 

233 return True 

234 elif a > 0: 

235 return False 

236 raise _areaError(points) 

237 

238 

239def nearestOn(point, point1, point2, within=True, height=None, wrap=False, 

240 equidistant=None, tol=_TOL_M, LatLon=LatLon, **LatLon_kwds): 

241 '''I{Iteratively} locate the closest point on the geodesic between 

242 two other (ellipsoidal) points. 

243 

244 @arg point: Reference point (C{LatLon}). 

245 @arg point1: Start point of the geodesic (C{LatLon}). 

246 @arg point2: End point of the geodesic (C{LatLon}). 

247 @kwarg within: If C{True}, return the closest point I{between} 

248 B{C{point1}} and B{C{point2}}, otherwise the 

249 closest point elsewhere on the geodesic (C{bool}). 

250 @kwarg height: Optional height for the closest point (C{meter}, 

251 conventionally) or C{None} or C{False} for the 

252 interpolated height. If C{False}, the closest 

253 takes the heights of the points into account. 

254 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll both 

255 B{C{point1}} and B{C{point2}} (C{bool}). 

256 @kwarg equidistant: An azimuthal equidistant projection (I{class} 

257 or function L{pygeodesy.equidistant}) or C{None} 

258 for the preferred C{B{point}.Equidistant}. 

259 @kwarg tol: Convergence tolerance (C{meter}). 

260 @kwarg LatLon: Optional class to return the closest point 

261 (L{LatLon}) or C{None}. 

262 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword 

263 arguments, ignored if C{B{LatLon} is None}. 

264 

265 @return: Closest point, a B{C{LatLon}} instance or if C{B{LatLon} 

266 is None}, a L{LatLon4Tuple}C{(lat, lon, height, datum)}. 

267 

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

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

270 

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

272 

273 @see: U{The B{ellipsoidal} case<https://GIS.StackExchange.com/questions/48937/ 

274 calculating-intersection-of-two-circles>} and U{Karney's paper 

275 <https://ArXiv.org/pdf/1102.1215.pdf>}, pp 20-21, section B{14. MARITIME 

276 BOUNDARIES} for more details about the iteration algorithm. 

277 ''' 

278 return _nearestOn(point, point1, point2, within=within, height=height, wrap=wrap, 

279 equidistant=equidistant, tol=tol, LatLon=LatLon, **LatLon_kwds) 

280 

281 

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

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

284 

285 @arg points: The polygon points (L{LatLon}[], L{BooleanFHP} or 

286 L{BooleanGH}). 

287 @kwarg closed: Optionally, close the polygon (C{bool}). 

288 @kwarg datum: Optional datum (L{Datum}). 

289 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the 

290 B{C{points}} (C{bool}). 

291 

292 @return: Perimeter (C{meter}, same as units of the B{C{datum}}'s 

293 ellipsoid axes). 

294 

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

296 

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

298 

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

300 longitudes not supported or C{B{closed}=False} 

301 with C{B{points}} a composite. 

302 

303 @see: Functions L{pygeodesy.perimeterOf}, L{ellipsoidalExact.perimeterOf}, 

304 L{ellipsoidalKarney.perimeterOf}, L{sphericalNvector.perimeterOf} 

305 and L{sphericalTrigonometry.perimeterOf}. 

306 ''' 

307 return _polygon(datum.ellipsoid.geodsolve, points, closed, True, wrap) 

308 

309 

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

311 areaOf, # functions 

312 intersection3, intersections2, isclockwise, ispolar, 

313 nearestOn, perimeterOf) 

314 

315# **) MIT License 

316# 

317# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved. 

318# 

319# Permission is hereby granted, free of charge, to any person obtaining a 

320# copy of this software and associated documentation files (the "Software"), 

321# to deal in the Software without restriction, including without limitation 

322# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

323# and/or sell copies of the Software, and to permit persons to whom the 

324# Software is furnished to do so, subject to the following conditions: 

325# 

326# The above copyright notice and this permission notice shall be included 

327# in all copies or substantial portions of the Software. 

328# 

329# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

330# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

331# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

332# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

333# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

334# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

335# OTHER DEALINGS IN THE SOFTWARE.