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

35 _intersection3, _intersections2

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

37 from pygeodesy.karney import fabs, _polygon, _xkwds

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

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

40 from pygeodesy.props import deprecated_method, Property_RO

41

42# from math import fabs # from .karney

43

44__all__ = _ALL_LAZY.ellipsoidalKarney

45__version__ = '23.05.12'

46

47

48 class Cartesian(CartesianEllipsoidalBase):

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

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

51 '''

52

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

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

55

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

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

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

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

60

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

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

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

64

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

66 '''

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

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

69

70

71 class LatLon(LatLonEllipsoidalBaseDI):

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

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

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

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

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

77 bearing.

78

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

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

81 '''

82

83 @deprecated_method

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

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

86 '''

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

88

89 @Property_RO

90 def Equidistant(self):

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

92 '''

93 return _MODS.azimuthal.EquidistantKarney

94

95 @Property_RO

96 def geodesic(self):

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

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

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

100 package is installed.

101 '''

102 return self.datum.ellipsoid.geodesic

103

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

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

106

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

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

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

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

111

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

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

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

115 available.

116

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

118 B{C{Cartesian_datum_kwds}}.

119 '''

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

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

122

123

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

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

126

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

128 or L{BooleanGH}).

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

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

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

132

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

134 ellipsoid axes, I{squared}).

135

136 @raise ImportError: Package U{geographiclib

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

138 not installed or not found.

139

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

141

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

143

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

145 longitudes not supported.

146

147 @note: This function requires the U{geographiclib

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

149

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

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

152 and L{sphericalTrigonometry.areaOf}.

153

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

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

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

157 '''

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

159

160

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

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

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

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

165 (initial) bearing from North.

166

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

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

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

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

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

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

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

174 or C{None} for the mean height.

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

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

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

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

179 C{B{start1}.Equidistant}.

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

181 (C{meter}, conventionally).

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

183 or C{None}.

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

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

186

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

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

189 lon, height, datum)}.

190

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

192 non-intersecting lines or no convergence

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

194

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

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

197

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

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

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

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

202 earth perimeter).

203

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

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

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

207 BOUNDARIES} for more details about the iteration algorithm.

208 '''

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

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

211

212

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

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

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

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

217

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

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

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

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

222 B{C{radius1}}).

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

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

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

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

227 (C{bool}).

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

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

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

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

232 and B{C{radius2}}).

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

234 or C{None}.

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

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

237

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

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

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

241 aka the I{radical center}.

242

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

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

245

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

247 or invalid B{C{equidistant}}.

248

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

250

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

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

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

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

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

256 intersections.

257 '''

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

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

260

261

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

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

264

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

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

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

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

269

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

271

272 @raise ImportError: Package U{geographiclib

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

274 not installed or not found.

275

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

277

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

279

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

281 area.

282

283 @note: This function requires the U{geographiclib

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

285

286 @see: L{pygeodesy.isclockwise}.

287 '''

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

289 if a < 0:

290 return True

291 elif a > 0:

292 return False

293 raise _areaError(points)

294

295

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

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

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

299 two other (ellipsoidal) points.

300

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

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

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

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

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

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

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

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

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

310 takes the heights of the points into account.

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

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

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

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

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

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

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

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

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

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

321

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

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

324

325 @raise ImportError: Package U{geographiclib

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

327 not installed or not found.

328

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

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

331

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

333

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

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

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

337 BOUNDARIES} for more details about the iteration algorithm.

338 '''

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

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

341

342

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

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

345

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

347 L{BooleanGH}).

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

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

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

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

352

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

354 ellipsoid axes).

355

356 @raise ImportError: Package U{geographiclib

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

358 not installed or not found.

359

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

361

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

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

364

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

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

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

368

369 @note: This function requires the U{geographiclib

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

371

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

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

374 and L{sphericalTrigonometry.perimeterOf}.

375 '''

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

377

378

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

380 areaOf, # functions

381 intersection3, intersections2, isclockwise, ispolar,

382 nearestOn, perimeterOf)

383

384# **) MIT License

385#

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

387#

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

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

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

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

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

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

394#

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

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

397#

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

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

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

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

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

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

404# OTHER DEALINGS IN THE SOFTWARE.