Coverage for pygeodesy/ellipsoidalExact.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-12-02 13:46 -0500

1 

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

3 

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

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 classes 

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

10''' 

11 

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 

23 

24# from math import fabs # from .karney 

25 

26__all__ = _ALL_LAZY.ellipsoidalExact 

27__version__ = '23.11.08' 

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

40 or specify C{B{LatLon}=None}. 

41 

42 @return: The geodetic point (L{LatLon}) or if B{C{LatLon}} is C{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 

54 exact geodesic classes L{GeodesicExact} and L{GeodesicLineExact} to 

55 compute the geodesic distance, initial and final bearing (azimuths) 

56 between two given points or the destination point given a start point 

57 and an (initial) bearing. 

58 ''' 

59 

60 @Property_RO 

61 def Equidistant(self): 

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

63 ''' 

64 return _MODS.azimuthal.EquidistantExact 

65 

66 @Property_RO 

67 def geodesicx(self): 

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

69 ''' 

70 return self.datum.ellipsoid.geodesicx 

71 

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

73 

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

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

76 

77 @kwarg Cartesian_datum_kwds: Optional L{Cartesian}, B{C{datum}} and 

78 other keyword arguments, ignored if C{B{Cartesian} 

79 is None}. Use C{B{Cartesian}=...} to override this 

80 L{Cartesian} class or set C{B{Cartesian}=None}. 

81 

82 @return: The cartesian (ECEF) coordinates as (L{Cartesian}) or if 

83 B{C{Cartesian}} is C{None}, an L{Ecef9Tuple}C{(x, y, z, lat, 

84 lon, height, C, M, datum)} with C{C} and C{M} if available. 

85 

86 @raise TypeError: Invalid B{C{Cartesian}}, B{C{datum}} or other 

87 B{C{Cartesian_datum_kwds}}. 

88 ''' 

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

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

91 

92 

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

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

95 

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

97 L{BooleanGH}). 

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

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

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

101 

102 @return: Area (C{meter} I{squared}, same units as the B{C{datum}}'s 

103 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 @note: The U{area of a polygon enclosing a pole<https://GeographicLib.SourceForge.io/ 

117 C++/doc/classGeographicLib_1_1GeodesicExact.html#a3d7a9155e838a09a48dc14d0c3fac525>} 

118 can be found by adding half the datum's ellipsoid surface area to the polygon's area. 

119 ''' 

120 return fabs(_polygon(datum.ellipsoid.geodesicx, points, True, False, wrap)) 

121 

122 

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

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

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

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

127 initial bearing from North. 

128 

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

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

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

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

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

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

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

136 or C{None} for the mean height. 

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

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

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

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

141 C{B{start1}.Equidistant}. 

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

143 (C{meter}, conventionally). 

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

145 or C{None}. 

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

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

148 

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

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

151 lon, height, datum)}. 

152 

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

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

155 

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

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

158 

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

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

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

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

163 earth perimeter). 

164 

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

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

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

168 BOUNDARIES} for more details about the iteration algorithm. 

169 ''' 

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

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

172 

173 

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

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

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

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

178 

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

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

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

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

183 B{C{radius1}}). 

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

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

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

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

188 (C{bool}). 

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

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

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

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

193 and B{C{radius2}}). 

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

195 or C{None}. 

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

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

198 

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

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

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

202 aka the I{radical center}. 

203 

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

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

206 

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

208 or invalid B{C{equidistant}}. 

209 

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

211 

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

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

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

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

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

217 intersections. 

218 ''' 

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

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

221 

222 

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

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

225 

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

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

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

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

230 

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

232 

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

234 

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

236 

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

238 

239 @see: L{pygeodesy.isclockwise}. 

240 ''' 

241 a = _polygon(datum.ellipsoid.geodesicx, points, True, False, wrap) 

242 if a < 0: 

243 return True 

244 elif a > 0: 

245 return False 

246 raise _areaError(points) 

247 

248 

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

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

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

252 two other (ellispoidal) points. 

253 

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

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

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

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

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

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

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

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

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

263 takes the heights of the points into account. 

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

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

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

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

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

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

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

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

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

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

274 

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

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

277 

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

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

280 

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

282 

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

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

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

286 BOUNDARIES} for more details about the iteration algorithm. 

287 ''' 

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

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

290 

291 

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

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

294 

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

296 L{BooleanGH}). 

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

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

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

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

301 

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

303 ellipsoid axes). 

304 

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

306 

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

308 

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

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

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

312 

313 @see: Functions L{pygeodesy.perimeterOf}, L{ellipsoidalGeodSolve.perimeterOf}, 

314 L{ellipsoidalKarney.perimeterOf}, L{sphericalNvector.perimeterOf} and 

315 L{sphericalTrigonometry.perimeterOf}. 

316 ''' 

317 return _polygon(datum.ellipsoid.geodesicx, points, closed, True, wrap) 

318 

319 

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

321 areaOf, intersecant2, # from .ellipsoidalBase 

322 intersection3, intersections2, isclockwise, ispolar, 

323 nearestOn, perimeterOf) 

324 

325# **) MIT License 

326# 

327# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved. 

328# 

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

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

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

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

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

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

335# 

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

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

338# 

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

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

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

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

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

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

345# OTHER DEALINGS IN THE SOFTWARE.