1

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

3

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

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} based on classes

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

10'''

11

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

13 from pygeodesy.ellipsoidalBase import CartesianEllipsoidalBase, \

14 _nearestOn, _WGS84

15 from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, \

16 _intersection3, _intersections2, \

17 _TOL_M, intersecant2

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

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

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

21 from 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__ = '24.06.11'

28

29

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

52 class 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 geodesic distances, bearings (azimuths), etc.

56 '''

57

58 @Property_RO

59 def Equidistant(self):

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

61 '''

62 return _MODS.azimuthal.EquidistantExact

63

64 @Property_RO

65 def geodesicx(self):

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

67 '''

68 return self.datum.ellipsoid.geodesicx

69

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

71

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

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

74

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

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

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

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

79

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

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

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

83

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

85 B{C{Cartesian_datum_kwds}}.

86 '''

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

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

89

90

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

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

93

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

95 L{BooleanGH}).

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

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

98 B{C{points}} (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

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

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

123 '''I{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: If C{True}, wrap or I{normalize} and unroll the B{C{start2}}

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

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

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

139 C{B{start1}.Equidistant}.

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

141 (C{meter}, conventionally).

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

143 or C{None}.

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

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

146

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

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

149 lon, height, datum)}.

150

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

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

153

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

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

156

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

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

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

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

161 earth perimeter).

162

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

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

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

166 BOUNDARIES} for more details about the iteration algorithm.

167 '''

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

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

170

171

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

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

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

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

176

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

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

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

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

181 B{C{radius1}}).

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

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

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

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

186 (C{bool}).

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

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

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

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

191 and B{C{radius2}}).

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

193 or C{None}.

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

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

196

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

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

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

200 aka the I{radical center}.

201

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

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

204

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

206 or invalid B{C{equidistant}}.

207

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

209

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

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

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

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

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

215 intersections.

216 '''

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

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

219

220

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

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

223

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

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

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

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

228

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

230

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

232

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

234

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

236

237 @see: L{pygeodesy.isclockwise}.

238 '''

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

240 if a < 0:

241 return True

242 elif a > 0:

243 return False

244 raise _areaError(points)

245

246

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

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

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

250 two other (ellispoidal) points.

251

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

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

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

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

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

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

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

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

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

261 takes the heights of the points into account.

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

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

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

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

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

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

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

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

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

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

272

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

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

275

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

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

278

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

280

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

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

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

284 BOUNDARIES} for more details about the iteration algorithm.

285 '''

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

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

288

289

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

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

292

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

294 L{BooleanGH}).

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

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

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

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

299

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

301 ellipsoid axes).

302

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

304

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

306

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

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

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

310

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

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

313 L{sphericalTrigonometry.perimeterOf}.

314 '''

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

316

317

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

319 areaOf, intersecant2, # from .ellipsoidalBase

320 intersection3, intersections2, isclockwise, ispolar,

321 nearestOn, perimeterOf)

322

323# **) MIT License

324#

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

326#

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

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

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

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

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

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

333#

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

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

336#

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

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

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

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

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

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

343# OTHER DEALINGS IN THE SOFTWARE.