Coverage for pygeodesy/ellipsoidalExact.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-04-21 13:14 -0400

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, _TOL_M, \ 

16 _intersection3, _intersections2 

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

18from pygeodesy.karney import fabs, _polygon, Property_RO, _xkwds 

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

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

21# from pygeodesy.props import Property_RO # from .karney 

22 

23# from math import fabs # from .karney 

24 

25__all__ = _ALL_LAZY.ellipsoidalExact 

26__version__ = '23.04.11' 

27 

28 

29class Cartesian(CartesianEllipsoidalBase): 

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

31 ''' 

32 

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

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

35 

36 @kwarg LatLon_and_kwds: Optional L{LatLon} and L{LatLon} keyword 

37 arguments as C{datum}. Use C{B{LatLon}=..., 

38 B{datum}=...} to override this L{LatLon} class 

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

40 

41 @return: The geodetic point (L{LatLon}) or if B{C{LatLon}} is C{None}, 

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

43 with C{C} and C{M} if available. 

44 

45 @raise TypeError: Invalid B{C{LatLon_and_kwds}} argument. 

46 ''' 

47 kwds = _xkwds(LatLon_and_kwds, LatLon=LatLon, datum=self.datum) 

48 return CartesianEllipsoidalBase.toLatLon(self, **kwds) 

49 

50 

51class LatLon(LatLonEllipsoidalBaseDI): 

52 '''An ellipsoidal L{LatLon} like L{ellipsoidalKarney.LatLon} but using 

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

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

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

56 and an (initial) bearing. 

57 ''' 

58 

59 @Property_RO 

60 def Equidistant(self): 

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

62 ''' 

63 return _MODS.azimuthal.EquidistantExact 

64 

65 @Property_RO 

66 def geodesicx(self): 

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

68 ''' 

69 return self.datum.ellipsoid.geodesicx 

70 

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

72 

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

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

75 

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

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

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

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

80 

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

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

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

84 

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

86 B{C{Cartesian_datum_kwds}}. 

87 ''' 

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

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

90 

91 

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

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

94 

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

96 L{BooleanGH}). 

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

98 @kwarg wrap: Wrap and unroll longitudes (C{bool}). 

99 

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

101 ellipsoid axes). 

102 

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

104 

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

106 

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

108 longitudes not supported. 

109 

110 @see: Functions L{pygeodesy.areaOf}, L{ellipsoidalGeodSolve.areaOf}, 

111 L{ellipsoidalKarney.areaOf}, L{sphericalNvector.areaOf} and 

112 L{sphericalTrigonometry.areaOf}. 

113 

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

119 

120 

121def intersection3(start1, end1, start2, end2, height=None, wrap=True, 

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

123 '''Iteratively compute the intersection point of two lines, each defined 

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

125 initial bearing from North. 

126 

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

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

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

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

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

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

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

134 or C{None} for the mean height. 

135 @kwarg wrap: Wrap and unroll longitudes (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: Optional class to return the intersection points (L{LatLon}) 

142 or C{None}. 

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

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

145 

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

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

148 lon, height, datum)}. 

149 

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

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

152 

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

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

155 

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

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

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

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

160 earth perimeter). 

161 

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

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

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

165 BOUNDARIES} for more details about the iteration algorithm. 

166 ''' 

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

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

169 

170 

171def intersections2(center1, radius1, center2, radius2, height=None, wrap=True, 

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

173 '''Iteratively compute the intersection points of two circles, each defined 

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

175 

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

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

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

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

180 B{C{radius1}}). 

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

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

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

184 @kwarg wrap: Wrap and unroll longitudes (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: Optional class to return the intersection points (L{LatLon}) 

191 or C{None}. 

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

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

194 

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

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

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

198 aka the I{radical center}. 

199 

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

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

202 

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

204 or invalid B{C{equidistant}}. 

205 

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

207 

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

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

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

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

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

213 intersections. 

214 ''' 

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

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

217 

218 

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

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

221 

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

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

224 @kwarg wrap: Wrap and unroll longitudes (C{bool}). 

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) 

237 if a < 0: 

238 return True 

239 elif a > 0: 

240 return False 

241 raise _areaError(points) 

242 

243 

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

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

246 '''Iteratively locate the closest point on the geodesic between 

247 two other (ellispoidal) 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: Wrap and unroll longitudes (C{bool}). 

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

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

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

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

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

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

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

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

268 

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

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

271 

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

273 or 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 return _nearestOn(point, point1, point2, within=within, height=height, wrap=wrap, 

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

284 

285 

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

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

288 

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

290 L{BooleanGH}). 

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

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

293 @kwarg wrap: Wrap and unroll longitudes (C{bool}). 

294 

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

296 ellipsoid axes). 

297 

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

299 

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

301 

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

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

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

305 

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

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

308 L{sphericalTrigonometry.perimeterOf}. 

309 ''' 

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

311 

312 

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

314 areaOf, # functions 

315 intersection3, intersections2, isclockwise, ispolar, 

316 nearestOn, perimeterOf) 

317 

318# **) MIT License 

319# 

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

321# 

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

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

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

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

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

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

328# 

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

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

331# 

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

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

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

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

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

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

338# OTHER DEALINGS IN THE SOFTWARE.