1

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

3

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

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

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

10 Python package to be installed.

11

12 Here'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

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

21 as follows:

22

23 >>> from pygeodesy import Datums

24 >>> from pygeodesy.ellipsoidalKarney import LatLon

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

26

27 or by converting to anothor datum:

28

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

30'''

31

32 from pygeodesy.datums import _WGS84

33 from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase, _nearestOn

34 from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, \

35 _intersection3, _intersections2, \

36 _TOL_M, intersecant2

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

38 from pygeodesy.karney import _polygon, fabs, _xkwds

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

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

41 from pygeodesy.props import deprecated_method, Property_RO

42

43# from math import fabs # from .karney

44

45__all__ = _ALL_LAZY.ellipsoidalKarney

46__version__ = '24.02.21'

47

48

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

72 class 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 geodesic

76 distances, bearings (azimuths), etc.

77

78 @note: This L{LatLon} class requires the U{geographiclib

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

80 '''

81

82 @deprecated_method

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

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

85 '''

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

87

88 @Property_RO

89 def Equidistant(self):

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

91 '''

92 return _MODS.azimuthal.EquidistantKarney

93

94 @Property_RO

95 def geodesic(self):

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

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

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

99 package is installed.

100 '''

101 return self.datum.ellipsoid.geodesic

102

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

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

105

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

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

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

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

110

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

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

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

114 available.

115

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

117 B{C{Cartesian_datum_kwds}}.

118 '''

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

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

121

122

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

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

125

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

127 or L{BooleanGH}).

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

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

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

131

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

133 ellipsoid axes, I{squared}).

134

135 @raise ImportError: Package U{geographiclib

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

137 not installed or not found.

138

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

140

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

142

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

144 longitudes not supported.

145

146 @note: This function requires the U{geographiclib

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

148

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

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

151 and L{sphericalTrigonometry.areaOf}.

152

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

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

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

156 '''

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

158

159

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

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

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

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

164 (initial) bearing from North.

165

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

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

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

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

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

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

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

173 or C{None} for the mean height.

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

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

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

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

178 C{B{start1}.Equidistant}.

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

180 (C{meter}, conventionally).

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

182 or C{None}.

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

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

185

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

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

188 lon, height, datum)}.

189

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

191 non-intersecting lines or no convergence

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

193

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

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

196

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

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

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

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

201 earth perimeter).

202

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

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

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

206 BOUNDARIES} for more details about the iteration algorithm.

207 '''

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

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

210

211

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

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

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

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

216

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

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

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

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

221 B{C{radius1}}).

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

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

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

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

226 (C{bool}).

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

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

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

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

231 and B{C{radius2}}).

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

233 or C{None}.

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

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

236

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

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

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

240 aka the I{radical center}.

241

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

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

244

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

246 or invalid B{C{equidistant}}.

247

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

249

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

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

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

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

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

255 intersections.

256 '''

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

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

259

260

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

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

263

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

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

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

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

268

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

270

271 @raise ImportError: Package U{geographiclib

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

273 not installed or not found.

274

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

276

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

278

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

280 area.

281

282 @note: This function requires the U{geographiclib

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

284

285 @see: L{pygeodesy.isclockwise}.

286 '''

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

288 if a < 0:

289 return True

290 elif a > 0:

291 return False

292 raise _areaError(points)

293

294

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

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

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

298 two other (ellipsoidal) points.

299

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

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

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

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

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

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

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

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

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

309 takes the heights of the points into account.

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

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

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

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

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

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

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

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

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

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

320

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

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

323

324 @raise ImportError: Package U{geographiclib

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

326 not installed or not found.

327

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

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

330

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

332

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

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

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

336 BOUNDARIES} for more details about the iteration algorithm.

337 '''

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

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

340

341

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

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

344

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

346 L{BooleanGH}).

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

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

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

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

351

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

353 ellipsoid axes).

354

355 @raise ImportError: Package U{geographiclib

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

357 not installed or not found.

358

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

360

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

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

363

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

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

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

367

368 @note: This function requires the U{geographiclib

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

370

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

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

373 and L{sphericalTrigonometry.perimeterOf}.

374 '''

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

376

377

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

379 areaOf, intersecant2, # from .ellipsoidalBase

380 intersection3, intersections2, isclockwise, ispolar,

381 nearestOn, perimeterOf)

382

383# **) MIT License

384#

385# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.

386#

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

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

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

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

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

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

393#

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

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

396#

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

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

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

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

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

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

403# OTHER DEALINGS IN THE SOFTWARE.