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.04.11'

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: Wrap and unroll longitudes (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=True,

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

162 '''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: Wrap and unroll longitudes (C{bool}).

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

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

177 C{B{start1}.Equidistant}.

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

179 (C{meter}, conventionally).

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

181 or C{None}.

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

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

184

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

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

187 lon, height, datum)}.

188

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

190 non-intersecting lines or no convergence

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

192

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

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

195

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

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

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

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

200 earth perimeter).

201

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

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

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

205 BOUNDARIES} for more details about the iteration algorithm.

206 '''

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

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

209

210

211 def intersections2(center1, radius1, center2, radius2, height=None, wrap=True,

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

213 '''Iteratively compute the intersection points of two circles, each defined

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

215

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

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

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

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

220 B{C{radius1}}).

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

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

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

224 @kwarg wrap: Wrap and unroll longitudes (C{bool}).

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

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

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

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

229 and B{C{radius2}}).

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

231 or C{None}.

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

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

234

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

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

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

238 aka the I{radical center}.

239

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

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

242

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

244 or invalid B{C{equidistant}}.

245

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

247

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

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

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

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

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

253 intersections.

254 '''

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

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

257

258

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

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

261

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

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

264 @kwarg wrap: Wrap and unroll longitudes (C{bool}).

265

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

267

268 @raise ImportError: Package U{geographiclib

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

270 not installed or not found.

271

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

273

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

275

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

277 area.

278

279 @note: This function requires the U{geographiclib

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

281

282 @see: L{pygeodesy.isclockwise}.

283 '''

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

285 if a < 0:

286 return True

287 elif a > 0:

288 return False

289 raise _areaError(points)

290

291

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

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

294 '''Iteratively locate the closest point on the geodesic between

295 two other (ellipsoidal) points.

296

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

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

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

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

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

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

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

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

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

306 takes the heights of the points into account.

307 @kwarg wrap: Wrap and unroll longitudes (C{bool}).

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

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

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

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

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

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

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

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

316

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

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

319

320 @raise ImportError: Package U{geographiclib

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

322 not installed or not found.

323

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

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

326

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

328

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

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

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

332 BOUNDARIES} for more details about the iteration algorithm.

333 '''

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

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

336

337

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

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

340

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

342 L{BooleanGH}).

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

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

345 @kwarg wrap: Wrap and unroll longitudes (C{bool}).

346

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

348 ellipsoid axes).

349

350 @raise ImportError: Package U{geographiclib

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

352 not installed or not found.

353

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

355

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

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

358

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

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

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

362

363 @note: This function requires the U{geographiclib

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

365

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

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

368 and L{sphericalTrigonometry.perimeterOf}.

369 '''

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

371

372

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

374 areaOf, # functions

375 intersection3, intersections2, isclockwise, ispolar,

376 nearestOn, perimeterOf)

377

378# **) MIT License

379#

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

381#

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

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

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

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

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

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

388#

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

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

391#

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

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

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

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

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

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

398# OTHER DEALINGS IN THE SOFTWARE.