Coverage for pygeodesy/latlonBase.py: 93%
471 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-12-12 17:20 -0500
« prev ^ index » next coverage.py v7.6.1, created at 2024-12-12 17:20 -0500
2# -*- coding: utf-8 -*-
4u'''(INTERNAL) Base class L{LatLonBase} for all elliposiodal, spherical and N-vectorial C{LatLon} classes.
6@see: I{(C) Chris Veness 2005-2024}' U{latlong<https://www.Movable-Type.co.UK/scripts/latlong.html>},
7 U{-ellipsoidal<https://www.Movable-Type.co.UK/scripts/geodesy/docs/latlon-ellipsoidal.js.html>} and
8 U{-vectors<https://www.Movable-Type.co.UK/scripts/latlong-vectors.html>} and I{Charles Karney}'s
9 U{Rhumb<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1Rhumb.html>} and
10 U{RhumbLine<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1RhumbLine.html>} classes.
11'''
13from pygeodesy.basics import isstr, map1, _xinstanceof, _passarg
14from pygeodesy.constants import EPS, EPS0, EPS1, EPS4, INT0, R_M, \
15 _EPSqrt as _TOL, _0_0, _0_5, _1_0, \
16 _360_0, _umod_360
17from pygeodesy.datums import _spherical_datum
18from pygeodesy.dms import F_D, F_DMS, latDMS, lonDMS, parse3llh
19# from pygeodesy.ecef import EcefKarney # _MODS
20from pygeodesy.errors import _AttributeError, IntersectionError, \
21 _incompatible, _IsnotError, _TypeError, \
22 _ValueError, _xattr, _xdatum, _xError, \
23 _xkwds, _xkwds_item2, _xkwds_not
24# from pygeodesy.fmath import favg # _MODS
25# from pygeodesy.formy import antipode, compassAngle, cosineAndoyerLambert_, \
26# cosineForsytheAndoyerLambert_, cosineLaw, \
27# equirectangular, euclidean, flatLocal_, \
28# flatPolar, _hartzell, haversine, isantipode, \
29# _isequalTo, isnormal, normal, philam2n_xyz, \
30# thomas_, vincentys # as _formy
31# from pygeodesy.internals import _passarg # from .basics
32from pygeodesy.interns import NN, _COMMASPACE_, _concentric_, _height_, \
33 _intersection_, _LatLon_, _m_, _negative_, \
34 _no_, _overlap_, _too_, _point_ # PYCHOK used!
35# from pygeodesy.iters import PointsIter, points2 # _MODS
36# from pygeodesy.karney import Caps # _MODS
37from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS
38from pygeodesy.named import _name2__, _NamedBase, _NamedLocal, Fmt
39from pygeodesy.namedTuples import Bounds2Tuple, LatLon2Tuple, PhiLam2Tuple, \
40 Trilaterate5Tuple, Vector3Tuple
41# from pygeodesy.nvectorBase import _N_vector_ # _MODS
42from pygeodesy.props import deprecated_method, Property, Property_RO, \
43 property_RO, _update_all
44# from pygeodesy.streprs import Fmt, hstr # from .named, _MODS
45from pygeodesy.units import _isDegrees, _isRadius, Distance_, Lat, Lon, \
46 Height, Radius, Radius_, Scalar, Scalar_
47from pygeodesy.utily import sincos2d_, _unrollon, _unrollon3, _Wrap
48# from pygeodesy.vector2d import _circin6, Circin6Tuple, _circum3, circum4_, \
49# Circum3Tuple, _radii11ABC4 # _MODS
50# from pygeodesy.vector3d import nearestOn6, Vector3d # _MODS
52from contextlib import contextmanager
53from math import asin, cos, degrees, fabs, radians
55__all__ = _ALL_LAZY.latlonBase
56__version__ = '24.12.06'
58_formy = _MODS.into(formy=__name__)
61class LatLonBase(_NamedBase, _NamedLocal):
62 '''(INTERNAL) Base class for ellipsoidal and spherical C{LatLon}s.
63 '''
64 _clipid = INT0 # polygonal clip, see .booleans
65 _datum = None # L{Datum}, to be overriden
66 _height = INT0 # height (C{meter}), default
67 _lat = 0 # latitude (C{degrees})
68 _lon = 0 # longitude (C{degrees})
70 def __init__(self, lat_llh, lon=None, height=0, datum=None, **wrap_name):
71 '''New C{LatLon}.
73 @arg lat_llh: Latitude (C{degrees} or DMS C{str} with N or S suffix) or
74 a previous C{LatLon} instance provided C{B{lon}=None}.
75 @kwarg lon: Longitude (C{degrees} or DMS C{str} with E or W suffix) or
76 C(None), indicating B{C{lat_llh}} is a C{LatLon}.
77 @kwarg height: Optional height above (or below) the earth surface
78 (C{meter}, conventionally).
79 @kwarg datum: Optional datum (L{Datum}, L{Ellipsoid}, L{Ellipsoid2},
80 L{a_f2Tuple} or I{scalar} radius) or C{None}.
81 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and optional keyword
82 argument C{B{wrap}=False}, if C{True}, wrap or I{normalize}
83 B{C{lat}} and B{C{lon}} (C{bool}).
85 @return: New instance (C{LatLon}).
87 @raise RangeError: A B{C{lon}} or C{lat} value outside the valid
88 range and L{rangerrors} set to C{True}.
90 @raise TypeError: If B{C{lat_llh}} is not a C{LatLon}.
92 @raise UnitError: Invalid C{lat}, B{C{lon}} or B{C{height}}.
93 '''
94 w, n = self._wrap_name2(**wrap_name)
95 if n:
96 self.name = n
98 if lon is None:
99 lat, lon, height = _latlonheight3(lat_llh, height, w)
100 elif w:
101 lat, lon = _Wrap.latlonDMS2(lat_llh, lon)
102 else:
103 lat = lat_llh
105 self._lat = Lat(lat) # parseDMS2(lat, lon)
106 self._lon = Lon(lon) # PYCHOK LatLon2Tuple
107 if height: # elevation
108 self._height = Height(height)
109 if datum is not None:
110 self._datum = _spherical_datum(datum, name=self.name)
112 def __eq__(self, other):
113 return self.isequalTo(other)
115 def __ne__(self, other):
116 return not self.isequalTo(other)
118 def __str__(self):
119 return self.toStr(form=F_D, prec=6)
121 def antipode(self, height=None):
122 '''Return the antipode, the point diametrically opposite to
123 this point.
125 @kwarg height: Optional height of the antipode (C{meter}),
126 this point's height otherwise.
128 @return: The antipodal point (C{LatLon}).
129 '''
130 a = _formy.antipode(*self.latlon)
131 h = self._heigHt(height)
132 return self.classof(*a, height=h)
134 @deprecated_method
135 def bounds(self, wide, tall, radius=R_M): # PYCHOK no cover
136 '''DEPRECATED, use method C{boundsOf}.'''
137 return self.boundsOf(wide, tall, radius=radius)
139 def boundsOf(self, wide, tall, radius=R_M, height=None, **name):
140 '''Return the SW and NE lat-/longitude of a great circle
141 bounding box centered at this location.
143 @arg wide: Longitudinal box width (C{meter}, same units as
144 B{C{radius}} or C{degrees} if C{B{radius} is None}).
145 @arg tall: Latitudinal box size (C{meter}, same units as
146 B{C{radius}} or C{degrees} if C{B{radius} is None}).
147 @kwarg radius: Mean earth radius (C{meter}) or C{None} if I{both}
148 B{C{wide}} and B{C{tall}} are in C{degrees}.
149 @kwarg height: Height for C{latlonSW} and C{latlonNE} (C{meter}),
150 overriding the point's height.
151 @kwarg name: Optional C{B{name}=NN} (C{str}).
153 @return: A L{Bounds2Tuple}C{(latlonSW, latlonNE)}, the lower-left
154 and upper-right corner (C{LatLon}).
156 @see: U{https://www.Movable-Type.co.UK/scripts/latlong-db.html}
157 '''
158 w = Scalar_(wide=wide) * _0_5
159 t = Scalar_(tall=tall) * _0_5
160 if radius is not None:
161 r = Radius_(radius)
162 c = cos(self.phi)
163 w = degrees(asin(w / r) / c) if fabs(c) > EPS0 else _0_0 # XXX
164 t = degrees(t / r)
165 y, t = self.lat, fabs(t)
166 x, w = self.lon, fabs(w)
168 h = self._heigHt(height)
169 sw = self.classof(y - t, x - w, height=h)
170 ne = self.classof(y + t, x + w, height=h)
171 return Bounds2Tuple(sw, ne, name=self._name__(name))
173 def chordTo(self, other, height=None, wrap=False):
174 '''Compute the length of the chord through the earth between
175 this and an other point.
177 @arg other: The other point (C{LatLon}).
178 @kwarg height: Overriding height for both points (C{meter}),
179 or if C{None}, use each point's height.
180 @kwarg wrap: If C{True}, wrap or I{normalize} the B{C{other}}
181 point (C{bool}).
183 @return: The chord length (conventionally C{meter}).
185 @raise TypeError: The B{C{other}} point is not C{LatLon}.
186 '''
187 def _v3d(ll, V3d=_MODS.vector3d.Vector3d):
188 t = ll.toEcef(height=height) # .toVector(Vector=V3d)
189 return V3d(t.x, t.y, t.z)
191 p = self.others(other)
192 if wrap:
193 p = _Wrap.point(p)
194 return _v3d(self).minus(_v3d(p)).length
196 def circin6(self, point2, point3, eps=EPS4, **wrap_name):
197 '''Return the radius and center of the I{inscribed} aka I{In-}circle
198 of the (planar) triangle formed by this and two other points.
200 @arg point2: Second point (C{LatLon}).
201 @arg point3: Third point (C{LatLon}).
202 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2}.
203 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and optional keyword
204 argument C{B{wrap}=False}, if C{True}, wrap or I{normalize}
205 the B{C{points}} (C{bool}).
207 @return: A L{Circin6Tuple}C{(radius, center, deltas, cA, cB, cC)}. The
208 C{center} and contact points C{cA}, C{cB} and C{cC}, each an
209 instance of this (sub-)class, are co-planar with this and the
210 two given points, see the B{Note} below.
212 @raise ImportError: Package C{numpy} not found, not installed or older
213 than version 1.10.
215 @raise IntersectionError: Near-coincident or -colinear points or
216 a trilateration or C{numpy} issue.
218 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}.
220 @note: The C{center} is trilaterated in cartesian (ECEF) space and converted
221 back to geodetic lat-, longitude and height. The latter, conventionally
222 in C{meter} indicates whether the C{center} is above, below or on the
223 surface of the earth model. If C{deltas} is C{None}, the C{center} is
224 I{un}ambigous. Otherwise C{deltas} is a L{LatLon3Tuple}C{(lat, lon,
225 height)} representing the differences between both results from
226 L{pygeodesy.trilaterate3d2} and C{center} is the mean thereof.
228 @see: Function L{pygeodesy.circin6}, method L{circum3}, U{Incircle
229 <https://MathWorld.Wolfram.com/Incircle.html>} and U{Contact Triangle
230 <https://MathWorld.Wolfram.com/ContactTriangle.html>}.
231 '''
232 w, n = self._wrap_name2(**wrap_name)
234 with _toCartesian3(self, point2, point3, w) as cs:
235 m = _MODS.vector2d
236 r, c, d, A, B, C = m._circin6(*cs, eps=eps, useZ=True, dLL3=True,
237 datum=self.datum) # PYCHOK unpack
238 return m.Circin6Tuple(r, c.toLatLon(), d, A.toLatLon(),
239 B.toLatLon(),
240 C.toLatLon(), name=n)
242 def circum3(self, point2, point3, circum=True, eps=EPS4, **wrap_name):
243 '''Return the radius and center of the smallest circle I{through} or I{containing}
244 this and two other points.
246 @arg point2: Second point (C{LatLon}).
247 @arg point3: Third point (C{LatLon}).
248 @kwarg circum: If C{True}, return the C{circumradius} and C{circumcenter},
249 always, ignoring the I{Meeus}' Type I case (C{bool}).
250 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2}.
251 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and optional keyword
252 argument C{B{wrap}=False}, if C{True}, wrap or I{normalize}
253 the B{C{points}} (C{bool}).
255 @return: A L{Circum3Tuple}C{(radius, center, deltas)}. The C{center}, an
256 instance of this (sub-)class, is co-planar with this and the two
257 given points. If C{deltas} is C{None}, the C{center} is
258 I{un}ambigous. Otherwise C{deltas} is a L{LatLon3Tuple}C{(lat,
259 lon, height)} representing the difference between both results
260 from L{pygeodesy.trilaterate3d2} and C{center} is the mean thereof.
262 @raise ImportError: Package C{numpy} not found, not installed or older than
263 version 1.10.
265 @raise IntersectionError: Near-concentric, -coincident or -colinear points,
266 incompatible C{Ecef} classes or a trilateration
267 or C{numpy} issue.
269 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}.
271 @note: The C{center} is trilaterated in cartesian (ECEF) space and converted
272 back to geodetic lat-, longitude and height. The latter, conventionally
273 in C{meter} indicates whether the C{center} is above, below or on the
274 surface of the earth model. If C{deltas} is C{None}, the C{center} is
275 I{un}ambigous. Otherwise C{deltas} is a L{LatLon3Tuple}C{(lat, lon,
276 height)} representing the difference between both results from
277 L{pygeodesy.trilaterate3d2} and C{center} is the mean thereof.
279 @see: Function L{pygeodesy.circum3} and methods L{circin6} and L{circum4_}.
280 '''
281 w, n = self._wrap_name2(**wrap_name)
283 with _toCartesian3(self, point2, point3, w, circum=circum) as cs:
284 m = _MODS.vector2d
285 r, c, d = m._circum3(*cs, circum=circum, eps=eps, useZ=True, dLL3=True, # XXX -3d2
286 clas=cs[0].classof, datum=self.datum) # PYCHOK unpack
287 return m.Circum3Tuple(r, c.toLatLon(), d, name=n)
289 def circum4_(self, *points, **wrap_name):
290 '''Best-fit a sphere through this and two or more other points.
292 @arg points: The other points (each a C{LatLon}).
293 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and optional keyword argument
294 C{B{wrap}=False}, if C{True}, wrap or I{normalize} the B{C{points}}
295 (C{bool}).
297 @return: A L{Circum4Tuple}C{(radius, center, rank, residuals)} with C{center} an
298 instance of this (sub-)class.
300 @raise ImportError: Package C{numpy} not found, not installed or older than
301 version 1.10.
303 @raise NumPyError: Some C{numpy} issue.
305 @raise TypeError: One of the B{C{points}} invalid.
307 @raise ValueError: Too few B{C{points}}.
309 @see: Function L{pygeodesy.circum4_} and L{circum3}.
310 '''
311 w, n = self._wrap_name2(**wrap_name)
313 def _cs(ps, C, w):
314 _wp = _Wrap.point if w else _passarg
315 for i, p in enumerate(ps):
316 yield C(i=i, points=_wp(p))
318 C = self._toCartesianEcef
319 c = C(point=self)
320 t = _MODS.vector2d.circum4_(c, Vector=c.classof, *_cs(points, C, w))
321 c = t.center.toLatLon(LatLon=self.classof)
322 return t.dup(center=c, name=n)
324 @property
325 def clipid(self):
326 '''Get the (polygonal) clip (C{int}).
327 '''
328 return self._clipid
330 @clipid.setter # PYCHOK setter!
331 def clipid(self, clipid):
332 '''Get the (polygonal) clip (C{int}).
333 '''
334 self._clipid = int(clipid)
336 @deprecated_method
337 def compassAngle(self, other, **adjust_wrap): # PYCHOK no cover
338 '''DEPRECATED, use method L{compassAngleTo}.'''
339 return self.compassAngleTo(other, **adjust_wrap)
341 def compassAngleTo(self, other, **adjust_wrap):
342 '''Return the angle from North for the direction vector between
343 this and an other point.
345 Suitable only for short, non-near-polar vectors up to a few
346 hundred Km or Miles. Use method C{initialBearingTo} for
347 larger distances.
349 @arg other: The other point (C{LatLon}).
350 @kwarg adjust_wrap: Optional keyword arguments for function
351 L{pygeodesy.compassAngle}.
353 @return: Compass angle from North (C{degrees360}).
355 @raise TypeError: The B{C{other}} point is not C{LatLon}.
357 @note: Courtesy of Martin Schultz.
359 @see: U{Local, flat earth approximation
360 <https://www.EdWilliams.org/avform.htm#flat>}.
361 '''
362 p = self.others(other)
363 return _formy.compassAngle(self.lat, self.lon, p.lat, p.lon, **adjust_wrap)
365 def cosineAndoyerLambertTo(self, other, **wrap):
366 '''Compute the distance between this and an other point using the U{Andoyer-Lambert correction<https://
367 navlib.net/wp-content/uploads/2013/10/admiralty-manual-of-navigation-vol-1-1964-english501c.pdf>}
368 of the U{Law of Cosines<https://www.Movable-Type.co.UK/scripts/latlong.html#cosine-law>} formula.
370 @arg other: The other point (C{LatLon}).
371 @kwarg wrap: Optional keyword argument C{B{wrap}=False}, if C{True}, wrap or I{normalize}
372 and unroll the B{C{other}} point (C{bool}).
374 @return: Distance (C{meter}, same units as the axes of this point's datum ellipsoid).
376 @raise TypeError: The B{C{other}} point is not C{LatLon}.
378 @see: Function L{pygeodesy.cosineAndoyerLambert} and methods
379 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo},
380 C{distanceTo*}, L{equirectangularTo}, L{euclideanTo},
381 L{flatLocalTo}/L{hubenyTo}, L{flatPolarTo}, L{haversineTo},
382 L{thomasTo} and L{vincentysTo}.
383 '''
384 return self._distanceTo_(_formy.cosineAndoyerLambert_, other, **wrap)
386 def cosineForsytheAndoyerLambertTo(self, other, **wrap):
387 '''Compute the distance between this and an other point using the U{Forsythe-Andoyer-Lambert
388 correction<https://www2.UNB.Ca/gge/Pubs/TR77.pdf>} of the U{Law of Cosines
389 <https://www.Movable-Type.co.UK/scripts/latlong.html#cosine-law>} formula.
391 @arg other: The other point (C{LatLon}).
392 @kwarg wrap: Optional keyword argument C{B{wrap}=False}, if C{True}, wrap or I{normalize}
393 and unroll the B{C{other}} point (C{bool}).
395 @return: Distance (C{meter}, same units as the axes of this point's datum ellipsoid).
397 @raise TypeError: The B{C{other}} point is not C{LatLon}.
399 @see: Function L{pygeodesy.cosineForsytheAndoyerLambert} and methods
400 L{cosineAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},
401 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo}/L{hubenyTo},
402 L{flatPolarTo}, L{haversineTo}, L{thomasTo} and L{vincentysTo}.
403 '''
404 return self._distanceTo_(_formy.cosineForsytheAndoyerLambert_, other, **wrap)
406 def cosineLawTo(self, other, radius=None, **wrap):
407 '''Compute the distance between this and an other point using the U{spherical Law of
408 Cosines<https://www.Movable-Type.co.UK/scripts/latlong.html#cosine-law>} formula.
410 @arg other: The other point (C{LatLon}).
411 @kwarg radius: Mean earth radius (C{meter}) or C{None} for the mean radius of this
412 point's datum ellipsoid.
413 @kwarg wrap: Optional keyword argument C{B{wrap}=False}, if C{True}, wrap or
414 I{normalize} and unroll the B{C{other}} point (C{bool}).
416 @return: Distance (C{meter}, same units as B{C{radius}}).
418 @raise TypeError: The B{C{other}} point is not C{LatLon}.
420 @see: Function L{pygeodesy.cosineLaw} and methods L{cosineAndoyerLambertTo},
421 L{cosineForsytheAndoyerLambertTo}, C{distanceTo*}, L{equirectangularTo},
422 L{euclideanTo}, L{flatLocalTo}/L{hubenyTo}, L{flatPolarTo},
423 L{haversineTo}, L{thomasTo} and L{vincentysTo}.
424 '''
425 return self._distanceTo(_formy.cosineLaw, other, radius, **wrap)
427 @property_RO
428 def datum(self): # PYCHOK no cover
429 '''I{Must be overloaded}.'''
430 self._notOverloaded()
432 def destinationXyz(self, delta, LatLon=None, **LatLon_kwds):
433 '''Calculate the destination using a I{local} delta from this point.
435 @arg delta: Local delta to the destination (L{XyzLocal}, L{Aer}, L{Enu}, L{Ned}
436 or L{Local9Tuple}).
437 @kwarg LatLon: Optional (geodetic) class to return the destination or C{None}.
438 @kwarg LatLon_kwds: Optionally, additional B{C{LatLon}} keyword arguments,
439 ignored if C{B{LatLon} is None}.
441 @return: An B{C{LatLon}} instance or if C{B{LatLon} is None}, a
442 L{LatLon4Tuple}C{(lat, lon, height, datum)} or L{LatLon3Tuple}C{(lat,
443 lon, height)} if a C{datum} keyword is specified or not.
445 @raise TypeError: Invalid B{C{delta}}, B{C{LatLon}} or B{C{LatLon_kwds}} item.
446 '''
447 t = self._Ltp._local2ecef(delta, nine=True)
448 return t.toLatLon(LatLon=LatLon, **_xkwds(LatLon_kwds, name=self.name))
450 def _distanceTo(self, func, other, radius=None, **kwds):
451 '''(INTERNAL) Helper for distance methods C{<func>To}.
452 '''
453 p, r = self.others(other, up=2), radius
454 if r is None:
455 r = self._datum.ellipsoid.R1 if self._datum else R_M
456 return func(self.lat, self.lon, p.lat, p.lon, radius=r, **kwds)
458 def _distanceTo_(self, func_, other, wrap=False, radius=None):
459 '''(INTERNAL) Helper for (ellipsoidal) distance methods C{<func>To}.
460 '''
461 p = self.others(other, up=2)
462 D = self.datum
463 lam21, phi2, _ = _Wrap.philam3(self.lam, p.phi, p.lam, wrap)
464 r = func_(phi2, self.phi, lam21, datum=D)
465 return r * (D.ellipsoid.a if radius is None else radius)
467 @Property_RO
468 def _Ecef_forward(self):
469 '''(INTERNAL) Helper for L{_ecef9} and L{toEcef} (C{callable}).
470 '''
471 return self.Ecef(self.datum, name=self.name).forward
473 @Property_RO
474 def _ecef9(self):
475 '''(INTERNAL) Helper for L{toCartesian}, L{toEcef} and L{toCartesian} (L{Ecef9Tuple}).
476 '''
477 return self._Ecef_forward(self, M=True)
479 @property_RO
480 def ellipsoidalLatLon(self):
481 '''Get the C{LatLon type} iff ellipsoidal, overloaded in L{LatLonEllipsoidalBase}.
482 '''
483 return False
485 @deprecated_method
486 def equals(self, other, eps=None): # PYCHOK no cover
487 '''DEPRECATED, use method L{isequalTo}.'''
488 return self.isequalTo(other, eps=eps)
490 @deprecated_method
491 def equals3(self, other, eps=None): # PYCHOK no cover
492 '''DEPRECATED, use method L{isequalTo3}.'''
493 return self.isequalTo3(other, eps=eps)
495 def equirectangularTo(self, other, **radius_adjust_limit_wrap):
496 '''Compute the distance between this and an other point
497 using the U{Equirectangular Approximation / Projection
498 <https://www.Movable-Type.co.UK/scripts/latlong.html#equirectangular>}.
500 Suitable only for short, non-near-polar distances up to a
501 few hundred Km or Miles. Use method L{haversineTo} or
502 C{distanceTo*} for more accurate and/or larger distances.
504 @arg other: The other point (C{LatLon}).
505 @kwarg radius_adjust_limit_wrap: Optional keyword arguments
506 for function L{pygeodesy.equirectangular},
507 overriding the default mean C{radius} of this
508 point's datum ellipsoid.
510 @return: Distance (C{meter}, same units as B{C{radius}}).
512 @raise TypeError: The B{C{other}} point is not C{LatLon}.
514 @see: Function L{pygeodesy.equirectangular} and methods L{cosineAndoyerLambertTo},
515 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},
516 C{euclideanTo}, L{flatLocalTo}/L{hubenyTo}, L{flatPolarTo},
517 L{haversineTo}, L{thomasTo} and L{vincentysTo}.
518 '''
519 return self._distanceTo(_formy.equirectangular, other, **radius_adjust_limit_wrap)
521 def euclideanTo(self, other, **radius_adjust_wrap):
522 '''Approximate the C{Euclidian} distance between this and
523 an other point.
525 See function L{pygeodesy.euclidean} for the available B{C{options}}.
527 @arg other: The other point (C{LatLon}).
528 @kwarg radius_adjust_wrap: Optional keyword arguments for function
529 L{pygeodesy.euclidean}, overriding the default mean
530 C{radius} of this point's datum ellipsoid.
532 @return: Distance (C{meter}, same units as B{C{radius}}).
534 @raise TypeError: The B{C{other}} point is not C{LatLon}.
536 @see: Function L{pygeodesy.euclidean} and methods L{cosineAndoyerLambertTo},
537 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},
538 L{equirectangularTo}, L{flatLocalTo}/L{hubenyTo}, L{flatPolarTo},
539 L{haversineTo}, L{thomasTo} and L{vincentysTo}.
540 '''
541 return self._distanceTo(_formy.euclidean, other, **radius_adjust_wrap)
543 def flatLocalTo(self, other, radius=None, **wrap):
544 '''Compute the distance between this and an other point using the
545 U{ellipsoidal Earth to plane projection
546 <https://WikiPedia.org/wiki/Geographical_distance#Ellipsoidal_Earth_projected_to_a_plane>}
547 aka U{Hubeny<https://www.OVG.AT/de/vgi/files/pdf/3781/>} formula.
549 @arg other: The other point (C{LatLon}).
550 @kwarg radius: Mean earth radius (C{meter}) or C{None} for the I{equatorial
551 radius} of this point's datum ellipsoid.
552 @kwarg wrap: Optional keyword argument C{B{wrap}=False}, if C{True}, wrap
553 or I{normalize} and unroll the B{C{other}} point (C{bool}).
555 @return: Distance (C{meter}, same units as B{C{radius}}).
557 @raise TypeError: The B{C{other}} point is not C{LatLon}.
559 @raise ValueError: Invalid B{C{radius}}.
561 @see: Function L{pygeodesy.flatLocal}/L{pygeodesy.hubeny}, methods
562 L{cosineAndoyerLambertTo}, L{cosineForsytheAndoyerLambertTo},
563 L{cosineLawTo}, C{distanceTo*}, L{equirectangularTo}, L{euclideanTo},
564 L{flatPolarTo}, L{haversineTo}, L{thomasTo} and L{vincentysTo} and
565 U{local, flat Earth approximation<https://www.edwilliams.org/avform.htm#flat>}.
566 '''
567 r = radius if radius in (None, R_M, _1_0, 1) else Radius(radius)
568 return self._distanceTo_(_formy.flatLocal_, other, radius=r, **wrap) # PYCHOK kwargs
570 hubenyTo = flatLocalTo # for Karl Hubeny
572 def flatPolarTo(self, other, **radius_wrap):
573 '''Compute the distance between this and an other point using
574 the U{polar coordinate flat-Earth<https://WikiPedia.org/wiki/
575 Geographical_distance#Polar_coordinate_flat-Earth_formula>} formula.
577 @arg other: The other point (C{LatLon}).
578 @kwarg radius_wrap: Optional C{B{radius}=R_M} and C{B{wrap}=False} for
579 function L{pygeodesy.flatPolar}, overriding the default
580 C{mean radius} of this point's datum ellipsoid.
582 @return: Distance (C{meter}, same units as B{C{radius}}).
584 @raise TypeError: The B{C{other}} point is not C{LatLon}.
586 @see: Function L{pygeodesy.flatPolar} and methods L{cosineAndoyerLambertTo},
587 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},
588 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo}/L{hubenyTo},
589 L{haversineTo}, L{thomasTo} and L{vincentysTo}.
590 '''
591 return self._distanceTo(_formy.flatPolar, other, **radius_wrap)
593 def hartzell(self, los=False, earth=None):
594 '''Compute the intersection of a Line-Of-Sight from this (geodetic) Point-Of-View
595 (pov) with this point's ellipsoid surface.
597 @kwarg los: Line-Of-Sight, I{direction} to the ellipsoid (L{Los}, L{Vector3d}),
598 C{True} for the I{normal, plumb} onto the surface or I{False} or
599 C{None} to point to the center of the ellipsoid.
600 @kwarg earth: The earth model (L{Datum}, L{Ellipsoid}, L{Ellipsoid2}, L{a_f2Tuple}
601 or C{scalar} radius in C{meter}), overriding this point's C{datum}
602 ellipsoid.
604 @return: The intersection (C{LatLon}) with attribute C{.height} set to the distance
605 to this C{pov}.
607 @raise IntersectionError: Null or bad C{pov} or B{C{los}}, this C{pov} is inside
608 the ellipsoid or B{C{los}} points outside or away from
609 the ellipsoid.
611 @raise TypeError: Invalid B{C{los}} or invalid or undefined B{C{earth}} or C{datum}.
613 @see: Function L{hartzell<pygeodesy.formy.hartzell>} for further details.
614 '''
615 return _formy._hartzell(self, los, earth, LatLon=self.classof)
617 def haversineTo(self, other, **radius_wrap):
618 '''Compute the distance between this and an other point using the U{Haversine
619 <https://www.Movable-Type.co.UK/scripts/latlong.html>} formula.
621 @arg other: The other point (C{LatLon}).
622 @kwarg radius_wrap: Optional C{B{radius}=R_M} and C{B{wrap}=False} for function
623 L{pygeodesy.haversine}, overriding the default C{mean radius} of
624 this point's datum ellipsoid.
626 @return: Distance (C{meter}, same units as B{C{radius}}).
628 @raise TypeError: The B{C{other}} point is not C{LatLon}.
630 @see: Function L{pygeodesy.haversine} and methods L{cosineAndoyerLambertTo},
631 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},
632 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo}/L{hubenyTo},
633 L{flatPolarTo}, L{thomasTo} and L{vincentysTo}.
634 '''
635 return self._distanceTo(_formy.haversine, other, **radius_wrap)
637 def _havg(self, other, f=_0_5, h=None):
638 '''(INTERNAL) Weighted, average height.
640 @arg other: An other point (C{LatLon}).
641 @kwarg f: Optional fraction (C{float}).
642 @kwarg h: Overriding height (C{meter}).
644 @return: Average, fractional height (C{float}) or the
645 overriding height B{C{h}} (C{Height}).
646 '''
647 return Height(h) if h is not None else \
648 _MODS.fmath.favg(self.height, other.height, f=f)
650 @Property
651 def height(self):
652 '''Get the height (C{meter}).
653 '''
654 return self._height
656 @height.setter # PYCHOK setter!
657 def height(self, height):
658 '''Set the height (C{meter}).
660 @raise TypeError: Invalid B{C{height}} C{type}.
662 @raise ValueError: Invalid B{C{height}}.
663 '''
664 h = Height(height)
665 if self._height != h:
666 _update_all(self)
667 self._height = h
669 def _heigHt(self, height):
670 '''(INTERNAL) Overriding this C{height}.
671 '''
672 return self.height if height is None else Height(height)
674 def height4(self, earth=None, normal=True, LatLon=None, **LatLon_kwds):
675 '''Compute the projection of this point on and the height above or below
676 this datum's ellipsoid surface.
678 @kwarg earth: A datum, ellipsoid, triaxial ellipsoid or earth radius,
679 I{overriding} this datum (L{Datum}, L{Ellipsoid},
680 L{Ellipsoid2}, L{a_f2Tuple}, L{Triaxial}, L{Triaxial_},
681 L{JacobiConformal} or C{meter}, conventionally).
682 @kwarg normal: If C{True}, the projection is the normal to this ellipsoid's
683 surface, otherwise the intersection of the I{radial} line to
684 this ellipsoid's center (C{bool}).
685 @kwarg LatLon: Optional class to return the projection, height and datum
686 (C{LatLon}) or C{None}.
687 @kwarg LatLon_kwds: Optionally, additional B{C{LatLon}} keyword arguments,
688 ignored if C{B{LatLon} is None}.
690 @note: Use keyword argument C{height=0} to override C{B{LatLon}.height}
691 to {0} or any other C{scalar}, conventionally in C{meter}.
693 @return: A B{C{LatLon}} instance or if C{B{LatLon} is None}, a L{Vector4Tuple}C{(x,
694 y, z, h)} with the I{projection} C{x}, C{y} and C{z} coordinates and
695 height C{h} in C{meter}, conventionally.
697 @raise TriaxialError: No convergence in triaxial root finding.
699 @raise TypeError: Invalid B{C{LatLon}}, B{C{LatLon_kwds}} item, B{C{earth}}
700 or triaxial B{C{earth}} couldn't be converted to biaxial
701 B{C{LatLon}} datum.
703 @see: Methods L{Ellipsoid.height4} and L{Triaxial_.height4} for more information.
704 '''
705 c = self.toCartesian()
706 if LatLon is None:
707 r = c.height4(earth=earth, normal=normal)
708 else:
709 c = c.height4(earth=earth, normal=normal, Cartesian=c.classof, height=0)
710 r = c.toLatLon(LatLon=LatLon, **_xkwds(LatLon_kwds, datum=c.datum, height=c.height))
711 if r.datum != c.datum:
712 raise _TypeError(earth=earth, datum=r.datum)
713 return r
715 def heightStr(self, prec=-2, m=_m_):
716 '''Return this point's B{C{height}} as C{str}ing.
718 @kwarg prec: Number of (decimal) digits, unstripped (C{int}).
719 @kwarg m: Optional unit of the height (C{str}).
721 @see: Function L{pygeodesy.hstr}.
722 '''
723 return _MODS.streprs.hstr(self.height, prec=prec, m=m)
725 def intersecant2(self, *args, **kwds): # PYCHOK no cover
726 '''B{Not implemented}, throws a C{NotImplementedError} always.'''
727 self._notImplemented(*args, **kwds)
729 def _intersecend2(self, p, q, wrap, height, g_or_r, P, Q, unused): # in .LatLonEllipsoidalBaseDI.intersecant2
730 '''(INTERNAL) Interpolate 2 heights along a geodesic or rhumb
731 line and return the 2 intersecant points accordingly.
732 '''
733 if height is None:
734 hp = hq = _xattr(p, height=INT0)
735 h = _xattr(q, height=hp) # if isLatLon(q) else hp
736 if h != hp:
737 s = g_or_r._Inverse(p, q, wrap).s12
738 if s: # fmath.fidw?
739 s = (h - hp) / s # slope
740 hq += s * Q.s12
741 hp += s * P.s12
742 else:
743 hp = hq = _MODS.fmath.favg(hp, h)
744 else:
745 hp = hq = Height(height)
747# n = self.name or unused.__name__
748 p = q = self.classof(P.lat2, P.lon2, datum=g_or_r.datum, height=hp) # name=n
749 p._iteration = P.iteration
750 if P is not Q:
751 q = self.classof(Q.lat2, Q.lon2, datum=g_or_r.datum, height=hq) # name=n
752 q._iteration = Q.iteration
753 return p, q
755 @deprecated_method
756 def isantipode(self, other, eps=EPS): # PYCHOK no cover
757 '''DEPRECATED, use method L{isantipodeTo}.'''
758 return self.isantipodeTo(other, eps=eps)
760 def isantipodeTo(self, other, eps=EPS):
761 '''Check whether this and an other point are antipodal, on diametrically
762 opposite sides of the earth.
764 @arg other: The other point (C{LatLon}).
765 @kwarg eps: Tolerance for near-equality (C{degrees}).
767 @return: C{True} if points are antipodal within the given tolerance,
768 C{False} otherwise.
769 '''
770 p = self.others(other)
771 return _formy.isantipode(*(self.latlon + p.latlon), eps=eps)
773 @Property_RO
774 def isEllipsoidal(self):
775 '''Check whether this point is ellipsoidal (C{bool} or C{None} if unknown).
776 '''
777 return _xattr(self.datum, isEllipsoidal=None)
779 def isequalTo(self, other, eps=None):
780 '''Compare this point with an other point, I{ignoring} height.
782 @arg other: The other point (C{LatLon}).
783 @kwarg eps: Tolerance for equality (C{degrees}).
785 @return: C{True} if both points are identical, I{ignoring} height,
786 C{False} otherwise.
788 @raise TypeError: The B{C{other}} point is not C{LatLon} or mismatch
789 of the B{C{other}} and this C{class} or C{type}.
791 @raise UnitError: Invalid B{C{eps}}.
793 @see: Method L{isequalTo3}.
794 '''
795 return _formy._isequalTo(self, self.others(other), eps=eps)
797 def isequalTo3(self, other, eps=None):
798 '''Compare this point with an other point, I{including} height.
800 @arg other: The other point (C{LatLon}).
801 @kwarg eps: Tolerance for equality (C{degrees}).
803 @return: C{True} if both points are identical I{including} height,
804 C{False} otherwise.
806 @raise TypeError: The B{C{other}} point is not C{LatLon} or mismatch
807 of the B{C{other}} and this C{class} or C{type}.
809 @see: Method L{isequalTo}.
810 '''
811 return self.height == self.others(other).height and \
812 _formy._isequalTo(self, other, eps=eps)
814 @Property_RO
815 def isnormal(self):
816 '''Return C{True} if this point is normal (C{bool}),
817 meaning C{abs(lat) <= 90} and C{abs(lon) <= 180}.
819 @see: Methods L{normal}, L{toNormal} and functions L{isnormal
820 <pygeodesy.isnormal>} and L{normal<pygeodesy.normal>}.
821 '''
822 return _formy.isnormal(self.lat, self.lon, eps=0)
824 @Property_RO
825 def isSpherical(self):
826 '''Check whether this point is spherical (C{bool} or C{None} if unknown).
827 '''
828 return _xattr(self.datum, isSpherical=None)
830 @Property_RO
831 def lam(self):
832 '''Get the longitude (B{C{radians}}).
833 '''
834 return radians(self.lon)
836 @Property
837 def lat(self):
838 '''Get the latitude (C{degrees90}).
839 '''
840 return self._lat
842 @lat.setter # PYCHOK setter!
843 def lat(self, lat):
844 '''Set the latitude (C{str[N|S]} or C{degrees}).
846 @raise ValueError: Invalid B{C{lat}}.
847 '''
848 lat = Lat(lat) # parseDMS(lat, suffix=_NS_, clip=90)
849 if self._lat != lat:
850 _update_all(self)
851 self._lat = lat
853 @Property
854 def latlon(self):
855 '''Get the lat- and longitude (L{LatLon2Tuple}C{(lat, lon)}).
856 '''
857 return LatLon2Tuple(self._lat, self._lon, name=self.name)
859 @latlon.setter # PYCHOK setter!
860 def latlon(self, latlonh):
861 '''Set the lat- and longitude and optionally the height (2- or 3-tuple
862 or comma- or space-separated C{str} of C{degrees90}, C{degrees180}
863 and C{meter}).
865 @raise TypeError: Height of B{C{latlonh}} not C{scalar} or B{C{latlonh}}
866 not C{list} or C{tuple}.
868 @raise ValueError: Invalid B{C{latlonh}} or M{len(latlonh)}.
870 @see: Function L{pygeodesy.parse3llh} to parse a B{C{latlonh}} string
871 into a 3-tuple C{(lat, lon, h)}.
872 '''
873 if isstr(latlonh):
874 latlonh = parse3llh(latlonh, height=self.height)
875 else:
876 _xinstanceof(list, tuple, latlonh=latlonh)
877 if len(latlonh) == 3:
878 h = Height(latlonh[2], name=Fmt.SQUARE(latlonh=2))
879 elif len(latlonh) != 2:
880 raise _ValueError(latlonh=latlonh)
881 else:
882 h = self.height
884 llh = Lat(latlonh[0]), Lon(latlonh[1]), h # parseDMS2(latlonh[0], latlonh[1])
885 if (self._lat, self._lon, self._height) != llh:
886 _update_all(self)
887 self._lat, self._lon, self._height = llh
889 def latlon2(self, ndigits=0):
890 '''Return this point's lat- and longitude in C{degrees}, rounded.
892 @kwarg ndigits: Number of (decimal) digits (C{int}).
894 @return: A L{LatLon2Tuple}C{(lat, lon)}, both C{float} and rounded
895 away from zero.
897 @note: The C{round}ed values are always C{float}, also if B{C{ndigits}}
898 is omitted.
899 '''
900 return LatLon2Tuple(round(self.lat, ndigits),
901 round(self.lon, ndigits), name=self.name)
903 @deprecated_method
904 def latlon_(self, ndigits=0): # PYCHOK no cover
905 '''DEPRECATED, use method L{latlon2}.'''
906 return self.latlon2(ndigits=ndigits)
908 latlon2round = latlon_ # PYCHOK no cover
910 @Property
911 def latlonheight(self):
912 '''Get the lat-, longitude and height (L{LatLon3Tuple}C{(lat, lon, height)}).
913 '''
914 return self.latlon.to3Tuple(self.height)
916 @latlonheight.setter # PYCHOK setter!
917 def latlonheight(self, latlonh):
918 '''Set the lat- and longitude and optionally the height
919 (2- or 3-tuple or comma- or space-separated C{str} of
920 C{degrees90}, C{degrees180} and C{meter}).
922 @see: Property L{latlon} for more details.
923 '''
924 self.latlon = latlonh
926 @Property
927 def lon(self):
928 '''Get the longitude (C{degrees180}).
929 '''
930 return self._lon
932 @lon.setter # PYCHOK setter!
933 def lon(self, lon):
934 '''Set the longitude (C{str[E|W]} or C{degrees}).
936 @raise ValueError: Invalid B{C{lon}}.
937 '''
938 lon = Lon(lon) # parseDMS(lon, suffix=_EW_, clip=180)
939 if self._lon != lon:
940 _update_all(self)
941 self._lon = lon
943 def nearestOn6(self, points, closed=False, height=None, wrap=False):
944 '''Locate the point on a path or polygon closest to this point.
946 Points are converted to and distances are computed in I{geocentric},
947 cartesian space.
949 @arg points: The path or polygon points (C{LatLon}[]).
950 @kwarg closed: Optionally, close the polygon (C{bool}).
951 @kwarg height: Optional height, overriding the height of this and all
952 other points (C{meter}). If C{None}, take the height
953 of points into account for distances.
954 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{points}}
955 (C{bool}).
957 @return: A L{NearestOn6Tuple}C{(closest, distance, fi, j, start, end)}
958 with the C{closest}, the C{start} and the C{end} point each an
959 instance of this C{LatLon} and C{distance} in C{meter}, same
960 units as the cartesian axes.
962 @raise PointsError: Insufficient number of B{C{points}}.
964 @raise TypeError: Some B{C{points}} or some B{C{points}}' C{Ecef} invalid.
966 @raise ValueError: Some B{C{points}}' C{Ecef} is incompatible.
968 @see: Function L{nearestOn6<pygeodesy.nearestOn6>}.
969 '''
970 def _cs(Ps, h, w, C):
971 p = None # not used
972 for i, q in Ps.enumerate():
973 if w and i:
974 q = _unrollon(p, q)
975 yield C(height=h, i=i, up=3, points=q)
976 p = q
978 C = self._toCartesianEcef # to verify datum and Ecef
979 Ps = self.PointsIter(points, wrap=wrap)
981 c = C(height=height, this=self) # this Cartesian
982 t = _MODS.vector3d.nearestOn6(c, _cs(Ps, height, wrap, C), closed=closed)
983 c, s, e = t.closest, t.start, t.end
985 kwds = _xkwds_not(None, LatLon=self.classof, # this LatLon
986 height=height)
987 _r = self.Ecef(self.datum).reverse
988 p = _r(c).toLatLon(**kwds)
989 s = _r(s).toLatLon(**kwds) if s is not c else p
990 e = _r(e).toLatLon(**kwds) if e is not c else p
991 return t.dup(closest=p, start=s, end=e)
993 def nearestTo(self, *args, **kwds): # PYCHOK no cover
994 '''B{Not implemented}, throws a C{NotImplementedError} always.'''
995 self._notImplemented(*args, **kwds)
997 def normal(self):
998 '''Normalize this point I{in-place} to C{abs(lat) <= 90} and C{abs(lon) <= 180}.
1000 @return: C{True} if this point was I{normal}, C{False} if it wasn't (but is now).
1002 @see: Property L{isnormal} and method L{toNormal}.
1003 '''
1004 n = self.isnormal
1005 if not n:
1006 self.latlon = _formy.normal(*self.latlon)
1007 return n
1009 @property_RO
1010 def _N_vector(self):
1011 '''(INTERNAL) Get the C{Nvector} (C{nvectorBase._N_vector_})
1012 '''
1013 _N = _MODS.nvectorBase._N_vector_
1014 return _N(*self._n_xyz3, h=self.height, name=self.name)
1016 @Property_RO
1017 def _n_xyz3(self):
1018 '''(INTERNAL) Get the n-vector components as L{Vector3Tuple}.
1019 '''
1020 return philam2n_xyz(self.phi, self.lam, name=self.name)
1022 @Property_RO
1023 def phi(self):
1024 '''Get the latitude (B{C{radians}}).
1025 '''
1026 return radians(self.lat)
1028 @Property_RO
1029 def philam(self):
1030 '''Get the lat- and longitude (L{PhiLam2Tuple}C{(phi, lam)}).
1031 '''
1032 return PhiLam2Tuple(self.phi, self.lam, name=self.name)
1034 def philam2(self, ndigits=0):
1035 '''Return this point's lat- and longitude in C{radians}, rounded.
1037 @kwarg ndigits: Number of (decimal) digits (C{int}).
1039 @return: A L{PhiLam2Tuple}C{(phi, lam)}, both C{float} and rounded
1040 away from zero.
1042 @note: The C{round}ed values are C{float}, always.
1043 '''
1044 return PhiLam2Tuple(round(self.phi, ndigits),
1045 round(self.lam, ndigits), name=self.name)
1047 @Property_RO
1048 def philamheight(self):
1049 '''Get the lat-, longitude in C{radians} and height (L{PhiLam3Tuple}C{(phi, lam, height)}).
1050 '''
1051 return self.philam.to3Tuple(self.height)
1053 @deprecated_method
1054 def points(self, points, **closed): # PYCHOK no cover
1055 '''DEPRECATED, use method L{points2}.'''
1056 return self.points2(points, **closed)
1058 def points2(self, points, closed=True):
1059 '''Check a path or polygon represented by points.
1061 @arg points: The path or polygon points (C{LatLon}[])
1062 @kwarg closed: Optionally, consider the polygon closed, ignoring any
1063 duplicate or closing final B{C{points}} (C{bool}).
1065 @return: A L{Points2Tuple}C{(number, points)}, an C{int} and a C{list}
1066 or C{tuple}.
1068 @raise PointsError: Insufficient number of B{C{points}}.
1070 @raise TypeError: Some B{C{points}} are not C{LatLon}.
1071 '''
1072 return _MODS.iters.points2(points, closed=closed, base=self)
1074 def PointsIter(self, points, loop=0, dedup=False, wrap=False):
1075 '''Return a C{PointsIter} iterator.
1077 @arg points: The path or polygon points (C{LatLon}[])
1078 @kwarg loop: Number of loop-back points (non-negative C{int}).
1079 @kwarg dedup: If C{True}, skip duplicate points (C{bool}).
1080 @kwarg wrap: If C{True}, wrap or I{normalize} the enum-/iterated
1081 B{C{points}} (C{bool}).
1083 @return: A new C{PointsIter} iterator.
1085 @raise PointsError: Insufficient number of B{C{points}}.
1086 '''
1087 return _MODS.iters.PointsIter(points, base=self, loop=loop,
1088 dedup=dedup, wrap=wrap)
1090 def radii11(self, point2, point3, wrap=False):
1091 '''Return the radii of the C{Circum-}, C{In-}, I{Soddy} and C{Tangent}
1092 circles of a (planar) triangle formed by this and two other points.
1094 @arg point2: Second point (C{LatLon}).
1095 @arg point3: Third point (C{LatLon}).
1096 @kwarg wrap: If C{True}, wrap or I{normalize} B{C{point2}} and
1097 B{C{point3}} (C{bool}).
1099 @return: L{Radii11Tuple}C{(rA, rB, rC, cR, rIn, riS, roS, a, b, c, s)}.
1101 @raise IntersectionError: Near-coincident or -colinear points.
1103 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}.
1105 @see: Function L{pygeodesy.radii11}, U{Incircle
1106 <https://MathWorld.Wolfram.com/Incircle.html>}, U{Soddy Circles
1107 <https://MathWorld.Wolfram.com/SoddyCircles.html>} and U{Tangent
1108 Circles<https://MathWorld.Wolfram.com/TangentCircles.html>}.
1109 '''
1110 with _toCartesian3(self, point2, point3, wrap) as cs:
1111 return _MODS.vector2d._radii11ABC4(*cs, useZ=True)[0]
1113 def _rhumb3(self, exact, radius): # != .sphericalBase._rhumbs3
1114 '''(INTERNAL) Get the C{rhumb} for this point's datum or for
1115 the B{C{radius}}' earth model iff non-C{None}.
1116 '''
1117 try:
1118 d = self._rhumb3dict
1119 t = d[(exact, radius)]
1120 except KeyError:
1121 D = self.datum if radius is None else \
1122 _spherical_datum(radius) # ellipsoidal OK
1123 try:
1124 r = D.ellipsoid.rhumb_(exact=exact) # or D.isSpherical
1125 except AttributeError as x:
1126 raise _AttributeError(datum=D, radius=radius, cause=x)
1127 t = r, D, _MODS.karney.Caps
1128 if len(d) > 2:
1129 d.clear() # d[:] = {}
1130 d[(exact, radius)] = t # cache 3-tuple
1131 return t
1133 @Property_RO
1134 def _rhumb3dict(self): # in ._update
1135 return {} # 3-item cache
1137 def rhumbAzimuthTo(self, other, exact=False, radius=None, wrap=False, b360=False):
1138 '''Return the azimuth (bearing) of a rhumb line (loxodrome) between this and
1139 an other (ellipsoidal) point.
1141 @arg other: The other point (C{LatLon}).
1142 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method
1143 L{Ellipsoid.rhumb_}.
1144 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},
1145 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's datum.
1146 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{other}} point (C{bool}).
1147 @kwarg b360: If C{True}, return the azimuth as bearing in compass degrees (C{bool}).
1149 @return: Rhumb azimuth (C{degrees180} or compass C{degrees360}).
1151 @raise TypeError: The B{C{other}} point is incompatible or B{C{radius}} is invalid.
1152 '''
1153 r, _, Cs = self._rhumb3(exact, radius)
1154 z = r._Inverse(self, other, wrap, outmask=Cs.AZIMUTH).azi12
1155 return _umod_360(z + _360_0) if b360 else z
1157 def rhumbDestination(self, distance, azimuth, radius=None, height=None, exact=False, **name):
1158 '''Return the destination point having travelled the given distance from this point along
1159 a rhumb line (loxodrome) of the given azimuth.
1161 @arg distance: Distance travelled (C{meter}, same units as this point's datum (ellipsoid)
1162 axes or B{C{radius}}, may be negative.
1163 @arg azimuth: Azimuth (bearing) of the rhumb line (compass C{degrees}).
1164 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},
1165 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's datum.
1166 @kwarg height: Optional height, overriding the default height (C{meter}).
1167 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method L{Ellipsoid.rhumb_}.
1168 @kwarg name: Optional C{B{name}=NN} (C{str}).
1170 @return: The destination point (ellipsoidal C{LatLon}).
1172 @raise TypeError: Invalid B{C{radius}}.
1174 @raise ValueError: Invalid B{C{distance}}, B{C{azimuth}}, B{C{radius}} or B{C{height}}.
1175 '''
1176 r, D, _ = self._rhumb3(exact, radius)
1177 d = r._Direct(self, azimuth, distance)
1178 h = self._heigHt(height)
1179 return self.classof(d.lat2, d.lon2, datum=D, height=h, **name)
1181 def rhumbDistanceTo(self, other, exact=False, radius=None, wrap=False):
1182 '''Return the distance from this to an other point along a rhumb line (loxodrome).
1184 @arg other: The other point (C{LatLon}).
1185 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method L{Ellipsoid.rhumb_}.
1186 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},
1187 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's datum.
1188 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{other}} point (C{bool}).
1190 @return: Distance (C{meter}, the same units as this point's datum (ellipsoid) axes or B{C{radius}}.
1192 @raise TypeError: The B{C{other}} point is incompatible or B{C{radius}} is invalid.
1194 @raise ValueError: Invalid B{C{radius}}.
1195 '''
1196 r, _, Cs = self._rhumb3(exact, radius)
1197 return r._Inverse(self, other, wrap, outmask=Cs.DISTANCE).s12
1199 def rhumbIntersecant2(self, circle, point, other, height=None,
1200 **exact_radius_wrap_eps_tol):
1201 '''Compute the intersections of a circle and a rhumb line given as two points or as a
1202 point and azimuth.
1204 @arg circle: Radius of the circle centered at this location (C{meter}), or a point
1205 on the circle (same C{LatLon} class).
1206 @arg point: The start point of the rhumb line (same C{LatLon} class).
1207 @arg other: An other point I{on} (same C{LatLon} class) or the azimuth I{of} (compass
1208 C{degrees}) the rhumb line.
1209 @kwarg height: Optional height for the intersection points (C{meter}, conventionally)
1210 or C{None} for interpolated heights.
1211 @kwarg exact_radius_wrap_eps_tol: Optional keyword arguments, see methods L{rhumbLine}
1212 and L{RhumbLineAux.Intersecant2} or L{RhumbLine.Intersecant2}.
1214 @return: 2-Tuple of the intersection points (representing a chord), each an instance of
1215 this class. Both points are the same instance if the rhumb line is tangent to
1216 the circle.
1218 @raise IntersectionError: The circle and rhumb line do not intersect.
1220 @raise TypeError: Invalid B{C{point}}, B{C{circle}} or B{C{other}}.
1222 @raise ValueError: Invalid B{C{circle}}, B{C{other}}, B{C{height}} or B{C{exact_radius_wrap}}.
1224 @see: Methods L{RhumbLineAux.Intersecant2} and L{RhumbLine.Intersecant2}.
1225 '''
1226 def _kwds3(eps=EPS, tol=_TOL, wrap=False, **kwds):
1227 return kwds, wrap, dict(eps=eps, tol=tol)
1229 exact_radius, w, eps_tol = _kwds3(**exact_radius_wrap_eps_tol)
1231 p = _unrollon(self, self.others(point=point), wrap=w)
1232 try:
1233 r = Radius_(circle=circle) if _isRadius(circle) else \
1234 self.rhumbDistanceTo(self.others(circle=circle), wrap=w, **exact_radius)
1235 rl = p.rhumbLine(other, wrap=w, **exact_radius)
1236 P, Q = rl.Intersecant2(self.lat, self.lon, r, **eps_tol)
1238 return self._intersecend2(p, other, w, height, rl.rhumb, P, Q,
1239 self.rhumbIntersecant2)
1240 except (TypeError, ValueError) as x:
1241 raise _xError(x, center=self, circle=circle, point=point, other=other,
1242 **exact_radius_wrap_eps_tol)
1244 def rhumbLine(self, other, exact=False, radius=None, wrap=False, **name_caps):
1245 '''Get a rhumb line through this point at a given azimuth or through this and an other point.
1247 @arg other: The azimuth I{of} (compass C{degrees}) or an other point I{on} (same
1248 C{LatLon} class) the rhumb line.
1249 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method L{Ellipsoid.rhumb_}.
1250 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},
1251 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's C{datum}.
1252 @kwarg wrap: If C{True}, wrap or I{normalize} and unroll the B{C{other}} point (C{bool}).
1253 @kwarg name_caps: Optional C{B{name}=str} and C{caps}, see L{RhumbLine} or L{RhumbLineAux} C{B{caps}}.
1255 @return: A C{RhumbLine} instance (C{RhumbLine} or C{RhumbLineAux}).
1257 @raise TypeError: Invalid B{C{radius}} or B{C{other}} not C{scalar} nor same C{LatLon} class.
1259 @see: Modules L{rhumb.aux_} and L{rhumb.ekx}.
1260 '''
1261 r, _, Cs = self._rhumb3(exact, radius)
1262 kwds = _xkwds(name_caps, name=self.name, caps=Cs.LINE_OFF)
1263 rl = r._DirectLine( self, other, **kwds) if _isDegrees(other) else \
1264 r._InverseLine(self, self.others(other), wrap, **kwds)
1265 return rl
1267 def rhumbMidpointTo(self, other, exact=False, radius=None, height=None, fraction=_0_5, **wrap_name):
1268 '''Return the (loxodromic) midpoint on the rhumb line between this and an other point.
1270 @arg other: The other point (same C{LatLon} class).
1271 @kwarg exact: Exact C{Rhumb...} to use (C{bool} or C{Rhumb...}), see method L{Ellipsoid.rhumb_}.
1272 @kwarg radius: Optional earth radius (C{meter}) or earth model (L{Datum}, L{Ellipsoid},
1273 L{Ellipsoid2} or L{a_f2Tuple}), overriding this point's datum.
1274 @kwarg height: Optional height, overriding the mean height (C{meter}).
1275 @kwarg fraction: Midpoint location from this point (C{scalar}), 0 for this, 1 for the B{C{other}},
1276 0.5 for halfway between this and the B{C{other}} point, may be negative or
1277 greater than 1.
1278 @kwarg wrap_name: Optional C{B{name}=NN} (C{str}) and C{B{wrap}=False}, if C{True}, wrap or
1279 I{normalize} and unroll the B{C{other}} point (C{bool}).
1281 @return: The midpoint at the given B{C{fraction}} along the rhumb line (same C{LatLon} class).
1283 @raise TypeError: The B{C{other}} point is incompatible or B{C{radius}} is invalid.
1285 @raise ValueError: Invalid B{C{height}} or B{C{fraction}}.
1286 '''
1287 w, n = self._wrap_name2(**wrap_name)
1288 r, D, _ = self._rhumb3(exact, radius)
1289 f = Scalar(fraction=fraction)
1290 d = r._Inverse(self, self.others(other), w) # C.AZIMUTH_DISTANCE
1291 d = r._Direct( self, d.azi12, d.s12 * f)
1292 h = self._havg(other, f=f, h=height)
1293 return self.classof(d.lat2, d.lon2, datum=D, height=h, name=n)
1295 @property_RO
1296 def sphericalLatLon(self):
1297 '''Get the C{LatLon type} iff spherical, overloaded in L{LatLonSphericalBase}.
1298 '''
1299 return False
1301 def thomasTo(self, other, **wrap):
1302 '''Compute the distance between this and an other point using U{Thomas'
1303 <https://apps.DTIC.mil/dtic/tr/fulltext/u2/703541.pdf>} formula.
1305 @arg other: The other point (C{LatLon}).
1306 @kwarg wrap: Optional keyword argument C{B{wrap}=False}, if C{True}, wrap
1307 or I{normalize} and unroll the B{C{other}} point (C{bool}).
1309 @return: Distance (C{meter}, same units as the axes of this point's datum ellipsoid).
1311 @raise TypeError: The B{C{other}} point is not C{LatLon}.
1313 @see: Function L{pygeodesy.thomas} and methods L{cosineAndoyerLambertTo},
1314 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},
1315 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo}/L{hubenyTo},
1316 L{flatPolarTo}, L{haversineTo} and L{vincentysTo}.
1317 '''
1318 return self._distanceTo_(_formy.thomas_, other, **wrap)
1320 @deprecated_method
1321 def to2ab(self): # PYCHOK no cover
1322 '''DEPRECATED, use property L{philam}.'''
1323 return self.philam
1325 def toCartesian(self, height=None, Cartesian=None, **Cartesian_kwds):
1326 '''Convert this point to cartesian, I{geocentric} coordinates, also known
1327 as I{Earth-Centered, Earth-Fixed} (ECEF).
1329 @kwarg height: Optional height, overriding this point's height (C{meter},
1330 conventionally).
1331 @kwarg Cartesian: Optional class to return the geocentric coordinates
1332 (C{Cartesian}) or C{None}.
1333 @kwarg Cartesian_kwds: Optionally, additional B{C{Cartesian}} keyword
1334 arguments, ignored if C{B{Cartesian} is None}.
1336 @return: A B{C{Cartesian}} instance or if B{C{Cartesian} is None}, an
1337 L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with
1338 C{C=0} and C{M} if available.
1340 @raise TypeError: Invalid B{C{Cartesian}} or B{C{Cartesian_kwds}} item.
1342 @see: Methods C{toNvector}, C{toVector} and C{toVector3d}.
1343 '''
1344 r = self._ecef9 if height is None else self.toEcef(height=height)
1345 if Cartesian is not None: # class or .classof
1346 r = Cartesian(r, **self._name1__(Cartesian_kwds))
1347 _xdatum(r.datum, self.datum)
1348 return r
1350 def _toCartesianEcef(self, height=None, i=None, up=2, **name_point):
1351 '''(INTERNAL) Convert to cartesian and check Ecef's before and after.
1352 '''
1353 p = self.others(up=up, **name_point)
1354 c = p.toCartesian(height=height)
1355 E = self.Ecef
1356 if E:
1357 for p in (p, c):
1358 e = _xattr(p, Ecef=None)
1359 if e not in (None, E): # PYCHOK no cover
1360 n, _ = _xkwds_item2(name_point)
1361 n = Fmt.INDEX(n, i)
1362 raise _ValueError(n, e, txt=_incompatible(E.__name__)) # txt__
1363 return c
1365 def toDatum(self, datum2, height=None, **name):
1366 '''I{Must be overloaded}.'''
1367 self._notOverloaded(datum2, height=height, **name)
1369 def toEcef(self, height=None, M=False):
1370 '''Convert this point to I{geocentric} coordinates, also known as
1371 I{Earth-Centered, Earth-Fixed} (U{ECEF<https://WikiPedia.org/wiki/ECEF>}).
1373 @kwarg height: Optional height, overriding this point's height (C{meter},
1374 conventionally).
1375 @kwarg M: Optionally, include the rotation L{EcefMatrix} (C{bool}).
1377 @return: An L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M, datum)} with
1378 C{C=0} and C{M} if available.
1380 @raise EcefError: A C{.datum} or an ECEF issue.
1381 '''
1382 return self._ecef9 if height in (None, self.height) else \
1383 self._Ecef_forward(self.lat, self.lon, height=height, M=M)
1385 @deprecated_method
1386 def to3llh(self, height=None): # PYCHOK no cover
1387 '''DEPRECATED, use property L{latlonheight} or C{latlon.to3Tuple(B{height})}.'''
1388 return self.latlonheight if height in (None, self.height) else \
1389 self.latlon.to3Tuple(height)
1391 def toNormal(self, deep=False, **name):
1392 '''Get this point I{normalized} to C{abs(lat) <= 90} and C{abs(lon) <= 180}.
1394 @kwarg deep: If C{True}, make a deep, otherwise a shallow copy (C{bool}).
1395 @kwarg name: Optional C{B{name}=NN} (C{str}).
1397 @return: A copy of this point, I{normalized} (C{LatLon}), optionally renamed.
1399 @see: Property L{isnormal}, method L{normal} and function L{pygeodesy.normal}.
1400 '''
1401 ll = self.copy(deep=deep)
1402 _ = ll.normal()
1403 if name:
1404 ll.rename(name)
1405 return ll
1407 def toNvector(self, h=None, Nvector=None, **name_Nvector_kwds):
1408 '''Convert this point to C{n-vector} (normal to the earth's surface) components,
1409 I{including height}.
1411 @kwarg h: Optional height, overriding this point's height (C{meter}).
1412 @kwarg Nvector: Optional class to return the C{n-vector} components (C{Nvector})
1413 or C{None}.
1414 @kwarg name_Nvector_kwds: Optional C{B{name}=NN} (C{str}) and optionally,
1415 additional B{C{Nvector}} keyword arguments, ignored if C{B{Nvector}
1416 is None}.
1418 @return: An B{C{Nvector}} instance or a L{Vector4Tuple}C{(x, y, z, h)} if
1419 C{B{Nvector} is None}.
1421 @raise TypeError: Invalid B{C{h}}, B{C{Nvector}} or B{C{name_Nvector_kwds}}.
1423 @see: Methods C{toCartesian}, C{toVector} and C{toVector3d}.
1424 '''
1425 h = self._heigHt(h)
1426 if Nvector is None:
1427 r = self._n_xyz3.to4Tuple(h)
1428 n, _ = _name2__(name_Nvector_kwds, _or_nameof=self)
1429 if n:
1430 r.rename(n)
1431 else:
1432 x, y, z = self._n_xyz3
1433 r = Nvector(x, y, z, h=h, ll=self, **self._name1__(name_Nvector_kwds))
1434 return r
1436 def toStr(self, form=F_DMS, joined=_COMMASPACE_, m=_m_, **prec_sep_s_D_M_S): # PYCHOK expected
1437 '''Convert this point to a "lat, lon[, +/-height]" string, formatted in the
1438 given C{B{form}at}.
1440 @kwarg form: The lat-/longitude C{B{form}at} to use (C{str}), see functions
1441 L{pygeodesy.latDMS} or L{pygeodesy.lonDMS}.
1442 @kwarg joined: Separator to join the lat-, longitude and height strings (C{str}
1443 or C{None} or C{NN} for non-joined).
1444 @kwarg m: Optional unit of the height (C{str}), use C{None} to exclude height
1445 from the returned string.
1446 @kwarg prec_sep_s_D_M_S: Optional C{B{prec}ision}, C{B{sep}arator}, B{C{s_D}},
1447 B{C{s_M}}, B{C{s_S}} and B{C{s_DMS}} keyword arguments, see function
1448 L{pygeodesy.toDMS} for details.
1450 @return: This point in the specified C{B{form}at}, etc. (C{str} or a 2- or 3-tuple
1451 C{(lat_str, lon_str[, height_str])} if B{C{joined}} is C{NN} or C{None}).
1453 @see: Function L{pygeodesy.latDMS} or L{pygeodesy.lonDMS} for more details about
1454 keyword arguments C{B{form}at}, C{B{prec}ision}, C{B{sep}arator}, B{C{s_D}},
1455 B{C{s_M}}, B{C{s_S}} and B{C{s_DMS}}.
1456 '''
1457 t = (latDMS(self.lat, form=form, **prec_sep_s_D_M_S),
1458 lonDMS(self.lon, form=form, **prec_sep_s_D_M_S))
1459 if self.height and m is not None:
1460 t += (self.heightStr(m=m),)
1461 return joined.join(t) if joined else t
1463 def toVector(self, Vector=None, **Vector_kwds):
1464 '''Convert this point to a C{Vector} with the I{geocentric} C{(x, y, z)} (ECEF)
1465 coordinates, I{ignoring height}.
1467 @kwarg Vector: Optional class to return the I{geocentric} components (L{Vector3d})
1468 or C{None}.
1469 @kwarg Vector_kwds: Optionally, additional B{C{Vector}} keyword arguments,
1470 ignored if C{B{Vector} is None}.
1472 @return: A B{C{Vector}} instance or a L{Vector3Tuple}C{(x, y, z)} if C{B{Vector}
1473 is None}.
1475 @raise TypeError: Invalid B{C{Vector}} or B{C{Vector_kwds}}.
1477 @see: Methods C{toCartesian}, C{toNvector} and C{toVector3d}.
1478 '''
1479 return self._ecef9.toVector(Vector=Vector, **self._name1__(Vector_kwds))
1481 def toVector3d(self, norm=True, **Vector3d_kwds):
1482 '''Convert this point to a L{Vector3d} with the I{geocentric} C{(x, y, z)}
1483 (ECEF) coordinates, I{ignoring height}.
1485 @kwarg norm: If C{False}, don't normalize the coordinates (C{bool}).
1486 @kwarg Vector3d_kwds: Optional L{Vector3d} keyword arguments.
1488 @return: Named, unit vector or vector (L{Vector3d}).
1490 @raise TypeError: Invalid B{C{Vector3d_kwds}}.
1492 @see: Methods C{toCartesian}, C{toNvector} and C{toVector}.
1493 '''
1494 r = self.toVector(Vector=_MODS.vector3d.Vector3d, **Vector3d_kwds)
1495 if norm:
1496 r = r.unit(ll=self)
1497 return r
1499 def toWm(self, **toWm_kwds):
1500 '''Convert this point to a WM coordinate.
1502 @kwarg toWm_kwds: Optional L{pygeodesy.toWm} keyword arguments.
1504 @return: The WM coordinate (L{Wm}).
1506 @see: Function L{pygeodesy.toWm}.
1507 '''
1508 return _MODS.webmercator.toWm(self, **self._name1__(toWm_kwds))
1510 @deprecated_method
1511 def to3xyz(self): # PYCHOK no cover
1512 '''DEPRECATED, use property L{xyz} or method L{toNvector}, L{toVector},
1513 L{toVector3d} or perhaps (geocentric) L{toEcef}.'''
1514 return self.xyz # self.toVector()
1516# def _update(self, updated, *attrs, **setters):
1517# '''(INTERNAL) See C{_NamedBase._update}.
1518# '''
1519# if updated:
1520# self._rhumb3dict.clear()
1521# return _NamedBase._update(self, updated, *attrs, **setters)
1523 def vincentysTo(self, other, **radius_wrap):
1524 '''Compute the distance between this and an other point using U{Vincenty's
1525 <https://WikiPedia.org/wiki/Great-circle_distance>} spherical formula.
1527 @arg other: The other point (C{LatLon}).
1528 @kwarg radius_wrap: Optional C{B{radius}=R_M} and C{B{wrap}=False} for
1529 function L{pygeodesy.vincentys}, overriding the default
1530 C{mean radius} of this point's datum ellipsoid.
1532 @return: Distance (C{meter}, same units as B{C{radius}}).
1534 @raise TypeError: The B{C{other}} point is not C{LatLon}.
1536 @see: Function L{pygeodesy.vincentys} and methods L{cosineAndoyerLambertTo},
1537 L{cosineForsytheAndoyerLambertTo}, L{cosineLawTo}, C{distanceTo*},
1538 L{equirectangularTo}, L{euclideanTo}, L{flatLocalTo}/L{hubenyTo},
1539 L{flatPolarTo}, L{haversineTo} and L{thomasTo}.
1540 '''
1541 return self._distanceTo(_formy.vincentys, other, **_xkwds(radius_wrap, radius=None))
1543 def _wrap_name2(self, wrap=False, **name):
1544 '''(INTERNAL) Return the C{wrap} and C{name} value.
1545 '''
1546 return wrap, (self._name__(name) if name else NN)
1548 @property_RO
1549 def xyz(self):
1550 '''Get the I{geocentric} C{(x, y, z)} coordinates (L{Vector3Tuple}C{(x, y, z)})
1551 '''
1552 return self._ecef9.xyz
1554 @property_RO
1555 def xyz3(self):
1556 '''Get the I{geocentric} C{(x, y, z)} coordinates as C{3-tuple}.
1557 '''
1558 return tuple(self.xyz)
1560 @Property_RO
1561 def xyzh(self):
1562 '''Get the I{geocentric} C{(x, y, z)} coordinates and height (L{Vector4Tuple}C{(x, y, z, h)})
1563 '''
1564 return self.xyz.to4Tuple(self.height)
1567class _toCartesian3(object): # see also .formy._idllmn6, .geodesicw._wargs, .vector2d._numpy
1568 '''(INTERNAL) Wrapper to convert 2 other points.
1569 '''
1570 @contextmanager # <https://www.Python.org/dev/peps/pep-0343/> Examples
1571 def __call__(self, p, p2, p3, wrap, **kwds):
1572 try:
1573 if wrap:
1574 p2, p3 = map1(_Wrap.point, p2, p3)
1575 kwds = _xkwds(kwds, wrap=wrap)
1576 yield (p. toCartesian().copy(name=_point_), # copy to rename
1577 p._toCartesianEcef(up=4, point2=p2),
1578 p._toCartesianEcef(up=4, point3=p3))
1579 except (AssertionError, TypeError, ValueError) as x: # Exception?
1580 raise _xError(x, point=p, point2=p2, point3=p3, **kwds)
1582_toCartesian3 = _toCartesian3() # PYCHOK singleton
1585def _latlonheight3(latlonh, height, wrap): # in .points.LatLon_.__init__
1586 '''(INTERNAL) Get 3-tuple C{(lat, lon, height)}.
1587 '''
1588 try:
1589 lat, lon = latlonh.lat, latlonh.lon
1590 height = _xattr(latlonh, height=height)
1591 except AttributeError:
1592 raise _IsnotError(_LatLon_, latlonh=latlonh)
1593 if wrap:
1594 lat, lon = _Wrap.latlon(lat, lon)
1595 return lat, lon, height
1598def latlon2n_xyz(lat_ll, lon=None, **name):
1599 '''Convert lat-, longitude to C{n-vector} (I{normal} to the earth's surface) X, Y and Z components.
1601 @arg lat_ll: Latitude (C{degrees}) or a C{LatLon} instance, L{LatLon2Tuple} or other C{LatLon*Tuple}.
1602 @kwarg lon: Longitude (C{degrees}), required if C{B{lon_ll} is degrees}, ignored otherwise.
1603 @kwarg name: Optional C{B{name}=NN} (C{str}).
1605 @return: A L{Vector3Tuple}C{(x, y, z)}.
1607 @see: Function L{philam2n_xyz}.
1609 @note: These are C{n-vector} x, y and z components, I{NOT geocentric} x, y and z (ECEF) coordinates!
1610 '''
1611 lat = lat_ll
1612 if lon is None:
1613 try:
1614 lat, lon = lat_ll.latlon
1615 except AttributeError:
1616 lat = lat_ll.lat
1617 lon = lat_ll.lon
1618 # Kenneth Gade eqn 3, but using right-handed
1619 # vector x -> 0°E,0°N, y -> 90°E,0°N, z -> 90°N
1620 sa, ca, sb, cb = sincos2d_(lat, lon)
1621 return Vector3Tuple(ca * cb, ca * sb, sa, **name)
1624def philam2n_xyz(phi_ll, lam=None, **name):
1625 '''Convert lat-, longitude to C{n-vector} (I{normal} to the earth's surface) X, Y and Z components.
1627 @arg phi_ll: Latitude (C{radians}) or a C{LatLon} instance with C{phi}, C{lam} or C{philam} attributes.
1628 @kwarg lam: Longitude (C{radians}), required if C{B{phi_ll} is radians}, ignored otherwise.
1629 @kwarg name: Optional name (C{str}).
1631 @return: A L{Vector3Tuple}C{(x, y, z)}.
1633 @see: Function L{latlon2n_xyz}.
1635 @note: These are C{n-vector} x, y and z components, I{NOT geocentric} x, y and z (ECEF) coordinates!
1636 '''
1637 phi = phi_ll
1638 if lam is None:
1639 try:
1640 phi, lam = phi_ll.philam
1641 except AttributeError:
1642 phi = phi_ll.phi
1643 lam = phi_ll.lam
1644 return latlon2n_xyz(degrees(phi), degrees(lam), **name)
1647def _trilaterate5(p1, d1, p2, d2, p3, d3, area=True, eps=EPS1, radius=R_M, wrap=False): # MCCABE 13
1648 '''(INTERNAL) Trilaterate three points by I{area overlap} or by I{perimeter intersection} of three circles.
1650 @note: The B{C{radius}} is needed only for C{n-vectorial} and C{sphericalTrigonometry.LatLon.distanceTo}
1651 methods and silently ignored by the C{ellipsoidalExact}, C{-GeodSolve}, C{-Karney} and
1652 C{-Vincenty.LatLon.distanceTo} methods.
1653 '''
1654 p2, p3, w = _unrollon3(p1, p2, p3, wrap)
1655 rw = dict(radius=radius, wrap=w)
1657 r1 = Distance_(distance1=d1)
1658 r2 = Distance_(distance2=d2)
1659 r3 = Distance_(distance3=d3)
1660 m = 0 if area else (r1 + r2 + r3)
1661 pc = 0
1662 t = []
1663 for _ in range(3):
1664 try: # intersection of circle (p1, r1) and (p2, r2)
1665 c1, c2 = p1.intersections2(r1, p2, r2, wrap=w)
1667 if area: # check overlap
1668 if c1 is c2: # abutting
1669 c = c1
1670 else: # nearest point on radical
1671 c = p3.nearestOn(c1, c2, within=True, wrap=w)
1672 d = r3 - p3.distanceTo(c, **rw)
1673 if d > eps: # sufficient overlap
1674 t.append((d, c))
1675 m = max(m, d)
1677 else: # check intersection
1678 for c in ((c1,) if c1 is c2 else (c1, c2)):
1679 d = fabs(r3 - p3.distanceTo(c, **rw))
1680 if d < eps: # below margin
1681 t.append((d, c))
1682 m = min(m, d)
1684 except IntersectionError as x:
1685 if _concentric_ in str(x): # XXX ConcentricError?
1686 pc += 1
1688 p1, r1, p2, r2, p3, r3 = p2, r2, p3, r3, p1, r1 # rotate
1690 if t: # get min, max, points and count ...
1691 t = tuple(sorted(t))
1692 n = len(t), # as 1-tuple
1693 # ... or for a single trilaterated result,
1694 # min *is* max, min- *is* maxPoint and n=1, 2 or 3
1695 return Trilaterate5Tuple(t[0] + t[-1] + n) # *(t[0] + ...)
1697 elif area and pc == 3: # all pairwise concentric ...
1698 r, p = min((r1, p1), (r2, p2), (r3, p3))
1699 m = max(r1, r2, r3)
1700 # ... return "smallest" point twice, the smallest
1701 # and largest distance and n=0 for concentric
1702 return Trilaterate5Tuple(float(r), p, float(m), p, 0)
1704 n, f = (_overlap_, max) if area else (_intersection_, min)
1705 t = _COMMASPACE_(_no_(n), '%s %.3g' % (f.__name__, m))
1706 raise IntersectionError(area=area, eps=eps, wrap=wrap, txt=t)
1709__all__ += _ALL_DOCS(LatLonBase)
1711# **) MIT License
1712#
1713# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.
1714#
1715# Permission is hereby granted, free of charge, to any person obtaining a
1716# copy of this software and associated documentation files (the "Software"),
1717# to deal in the Software without restriction, including without limitation
1718# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1719# and/or sell copies of the Software, and to permit persons to whom the
1720# Software is furnished to do so, subject to the following conditions:
1721#
1722# The above copyright notice and this permission notice shall be included
1723# in all copies or substantial portions of the Software.
1724#
1725# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1726# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1727# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1728# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1729# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1730# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1731# OTHER DEALINGS IN THE SOFTWARE.