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 intersecant2, _nearestOn, _WGS84

15 from pygeodesy.ellipsoidalBaseDI import LatLonEllipsoidalBaseDI, _TOL_M, \

16 _intersection3, _intersections2

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

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

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

20 from 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.10.08'

27

28

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

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

92 def 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: If C{True}, wrap or I{normalize} and unroll the

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

100

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

102 ellipsoid axes).

103

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

105

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

107

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

109 longitudes not supported.

110

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

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

113 L{sphericalTrigonometry.areaOf}.

114

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

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

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

118 '''

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

120

121

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

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

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

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

126 initial bearing from North.

127

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

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

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

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

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

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

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

135 or C{None} for the mean height.

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

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

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

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

140 C{B{start1}.Equidistant}.

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

142 (C{meter}, conventionally).

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

144 or C{None}.

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

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

147

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

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

150 lon, height, datum)}.

151

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

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

154

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

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

157

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

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

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

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

162 earth perimeter).

163

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

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

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

167 BOUNDARIES} for more details about the iteration algorithm.

168 '''

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

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

171

172

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

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

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

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

177

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

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

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

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

182 B{C{radius1}}).

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

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

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

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

187 (C{bool}).

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

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

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

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

192 and B{C{radius2}}).

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

194 or C{None}.

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

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

197

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

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

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

201 aka the I{radical center}.

202

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

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

205

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

207 or invalid B{C{equidistant}}.

208

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

210

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

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

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

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

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

216 intersections.

217 '''

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

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

220

221

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

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

224

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

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

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

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

229

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

231

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

233

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

235

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

237

238 @see: L{pygeodesy.isclockwise}.

239 '''

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

241 if a < 0:

242 return True

243 elif a > 0:

244 return False

245 raise _areaError(points)

246

247

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

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

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

251 two other (ellispoidal) points.

252

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

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

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

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

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

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

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

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

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

262 takes the heights of the points into account.

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

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

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

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

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

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

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

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

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

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

273

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

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

276

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

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

279

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

281

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

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

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

285 BOUNDARIES} for more details about the iteration algorithm.

286 '''

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

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

289

290

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

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

293

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

295 L{BooleanGH}).

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

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

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

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

300

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

302 ellipsoid axes).

303

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

305

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

307

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

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

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

311

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

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

314 L{sphericalTrigonometry.perimeterOf}.

315 '''

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

317

318

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

320 areaOf, intersecant2, # from .ellipsoidalBase

321 intersection3, intersections2, isclockwise, ispolar,

322 nearestOn, perimeterOf)

323

324# **) MIT License

325#

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

327#

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

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

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

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

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

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

334#

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

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

337#

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

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

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

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

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

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

344# OTHER DEALINGS IN THE SOFTWARE.