Coverage for pygeodesy/ellipsoidalKarney.py: 100%

42 statements  

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

1 

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

3 

4u'''Ellipsoidal, I{Karney}-based geodesy. 

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}, all requiring I{Charles 

9Karney}'s U{geographiclib <https://PyPI.org/project/geographiclib>} 

10Python package to be installed. 

11 

12Here's an example usage of C{ellipsoidalKarney}: 

13 

14 >>> from pygeodesy.ellipsoidalKarney import LatLon 

15 >>> Newport_RI = LatLon(41.49008, -71.312796) 

16 >>> Cleveland_OH = LatLon(41.499498, -81.695391) 

17 >>> Newport_RI.distanceTo(Cleveland_OH) 

18 866,455.4329098687 # meter 

19 

20You can change the ellipsoid model used by the I{Karney} formulae 

21as follows: 

22 

23 >>> from pygeodesy import Datums 

24 >>> from pygeodesy.ellipsoidalKarney import LatLon 

25 >>> p = LatLon(0, 0, datum=Datums.OSGB36) 

26 

27or by converting to anothor datum: 

28 

29 >>> p = p.toDatum(Datums.OSGB36) 

30''' 

31 

32from pygeodesy.datums import _WGS84 

33from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase, _nearestOn 

34from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, \ 

35 _intersection3, _intersections2, \ 

36 _TOL_M, intersecant2 

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

38from pygeodesy.karney import fabs, _polygon, _xkwds 

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

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

41from pygeodesy.props import deprecated_method, Property_RO 

42 

43# from math import fabs # from .karney 

44 

45__all__ = _ALL_LAZY.ellipsoidalKarney 

46__version__ = '23.11.08' 

47 

48 

49class Cartesian(CartesianEllipsoidalBase): 

50 '''Extended to convert C{Karney}-based L{Cartesian} to 

51 C{Karney}-based L{LatLon} points. 

52 ''' 

53 

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

55 '''Convert this cartesian point to a C{Karney}-based geodetic point. 

56 

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

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

59 B{datum}=...} to override this L{LatLon} 

60 class or specify C{B{LatLon}=None}. 

61 

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

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

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

65 

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

67 ''' 

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

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

70 

71 

72class LatLon(LatLonEllipsoidalBaseDI): 

73 '''An ellipsoidal L{LatLon} similar to L{ellipsoidalVincenty.LatLon} 

74 but using I{Charles F. F. Karney}'s Python U{geographiclib 

75 <https://PyPI.org/project/geographiclib>} to compute the geodesic 

76 distance, initial and final bearing (azimuths) between two given 

77 points or the destination point given a start point and an (initial) 

78 bearing. 

79 

80 @note: This L{LatLon} require the U{geographiclib 

81 <https://PyPI.org/project/geographiclib>} package. 

82 ''' 

83 

84 @deprecated_method 

85 def bearingTo(self, other, wrap=False): # PYCHOK no cover 

86 '''DEPRECATED, use method L{initialBearingTo}. 

87 ''' 

88 return self.initialBearingTo(other, wrap=wrap) 

89 

90 @Property_RO 

91 def Equidistant(self): 

92 '''Get the prefered azimuthal equidistant projection I{class} (L{EquidistantKarney}). 

93 ''' 

94 return _MODS.azimuthal.EquidistantKarney 

95 

96 @Property_RO 

97 def geodesic(self): 

98 '''Get this C{LatLon}'s I{wrapped} U{geodesic.Geodesic 

99 <https://GeographicLib.SourceForge.io/Python/doc/code.html>}, provided 

100 I{Karney}'s U{geographiclib<https://PyPI.org/project/geographiclib>} 

101 package is installed. 

102 ''' 

103 return self.datum.ellipsoid.geodesic 

104 

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

106 '''Convert this point to C{Karney}-based cartesian (ECEF) coordinates. 

107 

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

109 and other keyword arguments, ignored if C{B{Cartesian} is None}. 

110 Use C{B{Cartesian}=...} to override this L{Cartesian} class 

111 or set C{B{Cartesian} is None}. 

112 

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

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

115 lat, lon, height, C, M, datum)} with C{C} and C{M} if 

116 available. 

117 

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

119 B{C{Cartesian_datum_kwds}}. 

120 ''' 

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

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

123 

124 

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

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

127 

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

129 or L{BooleanGH}). 

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

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

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

133 

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

135 ellipsoid axes, I{squared}). 

136 

137 @raise ImportError: Package U{geographiclib 

138 <https://PyPI.org/project/geographiclib>} 

139 not installed or not found. 

140 

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

142 

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

144 

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

146 longitudes not supported. 

147 

148 @note: This function requires the U{geographiclib 

149 <https://PyPI.org/project/geographiclib>} package. 

150 

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

152 L{ellipsoidalGeodSolve.areaOf}, L{sphericalNvector.areaOf} 

153 and L{sphericalTrigonometry.areaOf}. 

154 

155 @note: The U{area of a polygon enclosing a pole<https://GeographicLib.SourceForge.io/ 

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

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

158 ''' 

159 return fabs(_polygon(datum.ellipsoid.geodesic, points, True, False, wrap)) 

160 

161 

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

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

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

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

166 (initial) bearing from North. 

167 

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

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

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

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

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

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

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

175 or C{None} for the mean height. 

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

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

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

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

180 C{B{start1}.Equidistant}. 

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

182 (C{meter}, conventionally). 

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: An L{Intersection3Tuple}C{(point, outside1, outside2)} with C{point} 

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

190 lon, height, datum)}. 

191 

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

193 non-intersecting lines or no convergence 

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

195 

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

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

198 

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

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

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

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

203 earth perimeter). 

204 

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

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

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

208 BOUNDARIES} for more details about the iteration algorithm. 

209 ''' 

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

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

212 

213 

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

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

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

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

218 

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

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

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

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

223 B{C{radius1}}). 

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

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

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

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

228 (C{bool}). 

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

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

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

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

233 and B{C{radius2}}). 

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

235 or C{None}. 

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

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

238 

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

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

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

242 aka the I{radical center}. 

243 

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

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

246 

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

248 or invalid B{C{equidistant}}. 

249 

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

251 

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

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

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

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

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

257 intersections. 

258 ''' 

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

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

261 

262 

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

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

265 

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

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

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

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

270 

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

272 

273 @raise ImportError: Package U{geographiclib 

274 <https://PyPI.org/project/geographiclib>} 

275 not installed or not found. 

276 

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

278 

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

280 

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

282 area. 

283 

284 @note: This function requires the U{geographiclib 

285 <https://PyPI.org/project/geographiclib>} package. 

286 

287 @see: L{pygeodesy.isclockwise}. 

288 ''' 

289 a = _polygon(datum.ellipsoid.geodesic, points, True, False, wrap) 

290 if a < 0: 

291 return True 

292 elif a > 0: 

293 return False 

294 raise _areaError(points) 

295 

296 

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

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

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

300 two other (ellipsoidal) points. 

301 

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

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

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

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

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

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

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

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

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

311 takes the heights of the points into account. 

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

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

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

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

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

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

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

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

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

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

322 

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

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

325 

326 @raise ImportError: Package U{geographiclib 

327 <https://PyPI.org/project/geographiclib>} 

328 not installed or not found. 

329 

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

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

332 

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

334 

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

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

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

338 BOUNDARIES} for more details about the iteration algorithm. 

339 ''' 

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

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

342 

343 

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

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

346 

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

348 L{BooleanGH}). 

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

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

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

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

353 

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

355 ellipsoid axes). 

356 

357 @raise ImportError: Package U{geographiclib 

358 <https://PyPI.org/project/geographiclib>} 

359 not installed or not found. 

360 

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

362 

363 @raise TypeError: Some B{C{points}} are not L{LatLon} or C{B{closed}=False} 

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

365 

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

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

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

369 

370 @note: This function requires the U{geographiclib 

371 <https://PyPI.org/project/geographiclib>} package. 

372 

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

374 L{ellipsoidalGeodSolve.perimeterOf}, L{sphericalNvector.perimeterOf} 

375 and L{sphericalTrigonometry.perimeterOf}. 

376 ''' 

377 return _polygon(datum.ellipsoid.geodesic, points, closed, True, wrap) 

378 

379 

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

381 areaOf, intersecant2, # from .ellipsoidalBase 

382 intersection3, intersections2, isclockwise, ispolar, 

383 nearestOn, perimeterOf) 

384 

385# **) MIT License 

386# 

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

388# 

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

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

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

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

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

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

395# 

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

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

398# 

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

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

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

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

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

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

405# OTHER DEALINGS IN THE SOFTWARE.