Coverage for pygeodesy/nvectorBase.py: 95%
242 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-08-02 18:24 -0400
« prev ^ index » next coverage.py v7.6.0, created at 2024-08-02 18:24 -0400
2# -*- coding: utf-8 -*-
4u'''(INTERNAL) Private elliposiodal and spherical C{Nvector} base classes
5L{LatLonNvectorBase} and L{NvectorBase} and function L{sumOf}.
7Pure Python implementation of C{n-vector}-based geodesy tools for ellipsoidal
8earth models, transcoded from JavaScript originals by I{(C) Chris Veness 2005-2016}
9and published under the same MIT Licence**, see U{Vector-based geodesy
10<https://www.Movable-Type.co.UK/scripts/latlong-vectors.html>}.
11'''
13# from pygeodesy.basics import map1 # from .namedTuples
14from pygeodesy.constants import EPS, EPS1, EPS_2, R_M, _2_0, _N_2_0
15# from pygeodesy.datums import _spherical_datum # from .formy
16from pygeodesy.errors import IntersectionError, _ValueError, VectorError, \
17 _xattrs, _xkwds, _xkwds_pop2
18from pygeodesy.fmath import fdot, fidw, hypot_ # PYCHOK fdot shared
19from pygeodesy.fsums import Fsum, fsumf_
20from pygeodesy.formy import _isequalTo, n_xyz2latlon, n_xyz2philam, \
21 _spherical_datum
22# from pygeodesy.internals import _under # from .named
23from pygeodesy.interns import NN, _1_, _2_, _3_, _bearing_, _coincident_, \
24 _COMMASPACE_, _distance_, _h_, _insufficient_, \
25 _intersection_, _no_, _point_, _pole_, _SPACE_
26from pygeodesy.latlonBase import LatLonBase, _ALL_DOCS, _ALL_LAZY, _MODS
27# from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS # from .latlonBase
28from pygeodesy.named import _xother3, _under
29from pygeodesy.namedTuples import Trilaterate5Tuple, Vector3Tuple, \
30 Vector4Tuple, map1
31from pygeodesy.props import deprecated_method, Property_RO, property_doc_, \
32 property_RO, property_ROnce, _update_all
33from pygeodesy.streprs import Fmt, hstr, unstr
34from pygeodesy.units import Bearing, Height, Radius_, Scalar
35from pygeodesy.utily import sincos2d, _unrollon, _unrollon3
36from pygeodesy.vector3d import Vector3d, _xyzhdlln4
38from math import fabs, sqrt
40__all__ = _ALL_LAZY.nvectorBase
41__version__ = '24.07.12'
44class NvectorBase(Vector3d): # XXX kept private
45 '''Base class for ellipsoidal and spherical C{Nvector}s.
46 '''
47 _datum = None # L{Datum}, overriden
48 _h = Height(h=0) # height (C{meter})
49 _H = NN # height prefix (C{str}), '↑' in JS version
51 def __init__(self, x_xyz, y=None, z=None, h=0, datum=None, **ll_name):
52 '''New n-vector normal to the earth's surface.
54 @arg x_xyz: X component of vector (C{scalar}) or (3-D) vector
55 (C{Nvector}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}).
56 @kwarg y: Y component of vector (C{scalar}), required if B{C{x_xyz}} is
57 C{scalar} and same units as B{C{x_xyz}}, ignored otherwise.
58 @kwarg z: Z component of vector (C{scalar}), like B{C{y}}.
59 @kwarg h: Optional height above surface (C{meter}).
60 @kwarg datum: Optional, I{pass-thru} datum (L{Datum}).
61 @kwarg ll_name: Optional C{B{name}=NN} (C{str}) and optional, original
62 latlon C{B{ll}=None} (C{LatLon}).
64 @raise TypeError: Non-scalar B{C{x}}, B{C{y}} or B{C{z}} coordinate or
65 B{C{x_xyz}} not an C{Nvector}, L{Vector3Tuple} or
66 L{Vector4Tuple} or invalid B{C{datum}}.
67 '''
68 h, d, ll, n = _xyzhdlln4(x_xyz, h, datum, **ll_name)
69 Vector3d.__init__(self, x_xyz, y=y, z=z, ll=ll, name=n)
70 if h:
71 self.h = h
72 if d is not None:
73 self._datum = _spherical_datum(d, name=n) # pass-thru
75 @Property_RO
76 def datum(self):
77 '''Get the I{pass-thru} datum (C{Datum}) or C{None}.
78 '''
79 return self._datum
81 @property_ROnce
82 def Ecef(self):
83 '''Get the ECEF I{class} (L{EcefKarney}), I{once}.
84 '''
85 return _MODS.ecef.EcefKarney
87 @property_RO
88 def ellipsoidalNvector(self):
89 '''Get the C{Nvector type} iff ellipsoidal, overloaded in L{pygeodesy.ellipsoidalNvector.Nvector}.
90 '''
91 return False
93 @property_doc_(''' the height above surface (C{meter}).''')
94 def h(self):
95 '''Get the height above surface (C{meter}).
96 '''
97 return self._h
99 @h.setter # PYCHOK setter!
100 def h(self, h):
101 '''Set the height above surface (C{meter}).
103 @raise TypeError: If B{C{h}} invalid.
105 @raise VectorError: If B{C{h}} invalid.
106 '''
107 h = Height(h=h, Error=VectorError)
108 if self._h != h:
109 _update_all(self)
110 self._h = h
112 @property_doc_(''' the height prefix (C{str}).''')
113 def H(self):
114 '''Get the height prefix (C{str}).
115 '''
116 return self._H
118 @H.setter # PYCHOK setter!
119 def H(self, H):
120 '''Set the height prefix (C{str}).
121 '''
122 self._H = str(H) if H else NN
124 def hStr(self, prec=-2, m=NN):
125 '''Return a string for the height B{C{h}}.
127 @kwarg prec: Number of (decimal) digits, unstripped (C{int}).
128 @kwarg m: Optional unit of the height (C{str}).
130 @see: Function L{pygeodesy.hstr}.
131 '''
132 return NN(self.H, hstr(self.h, prec=prec, m=m))
134 @Property_RO
135 def isEllipsoidal(self):
136 '''Check whether this n-vector is ellipsoidal (C{bool} or C{None} if unknown).
137 '''
138 return self.datum.isEllipsoidal if self.datum else None
140 @Property_RO
141 def isSpherical(self):
142 '''Check whether this n-vector is spherical (C{bool} or C{None} if unknown).
143 '''
144 return self.datum.isSpherical if self.datum else None
146 @Property_RO
147 def lam(self):
148 '''Get the (geodetic) longitude in C{radians} (C{float}).
149 '''
150 return self.philam.lam
152 @Property_RO
153 def lat(self):
154 '''Get the (geodetic) latitude in C{degrees} (C{float}).
155 '''
156 return self.latlon.lat
158 @Property_RO
159 def latlon(self):
160 '''Get the (geodetic) lat-, longitude in C{degrees} (L{LatLon2Tuple}C{(lat, lon)}).
161 '''
162 return n_xyz2latlon(self.x, self.y, self.z, name=self.name)
164 @Property_RO
165 def latlonheight(self):
166 '''Get the (geodetic) lat-, longitude in C{degrees} and height (L{LatLon3Tuple}C{(lat, lon, height)}).
167 '''
168 return self.latlon.to3Tuple(self.h)
170 @Property_RO
171 def latlonheightdatum(self):
172 '''Get the lat-, longitude in C{degrees} with height and datum (L{LatLon4Tuple}C{(lat, lon, height, datum)}).
173 '''
174 return self.latlonheight.to4Tuple(self.datum)
176 @Property_RO
177 def lon(self):
178 '''Get the (geodetic) longitude in C{degrees} (C{float}).
179 '''
180 return self.latlon.lon
182 @Property_RO
183 def phi(self):
184 '''Get the (geodetic) latitude in C{radians} (C{float}).
185 '''
186 return self.philam.phi
188 @Property_RO
189 def philam(self):
190 '''Get the (geodetic) lat-, longitude in C{radians} (L{PhiLam2Tuple}C{(phi, lam)}).
191 '''
192 return n_xyz2philam(self.x, self.y, self.z, name=self.name)
194 @Property_RO
195 def philamheight(self):
196 '''Get the (geodetic) lat-, longitude in C{radians} and height (L{PhiLam3Tuple}C{(phi, lam, height)}).
197 '''
198 return self.philam.to3Tuple(self.h)
200 @Property_RO
201 def philamheightdatum(self):
202 '''Get the lat-, longitude in C{radians} with height and datum (L{PhiLam4Tuple}C{(phi, lam, height, datum)}).
203 '''
204 return self.philamheight.to4Tuple(self.datum)
206 @property_RO
207 def sphericalNvector(self):
208 '''Get the C{Nvector type} iff spherical, overloaded in L{pygeodesy.sphericalNvector.Nvector}.
209 '''
210 return False
212 @deprecated_method
213 def to2ab(self): # PYCHOK no cover
214 '''DEPRECATED, use property L{philam}.
216 @return: A L{PhiLam2Tuple}C{(phi, lam)}.
217 '''
218 return self.philam
220 @deprecated_method
221 def to3abh(self, height=None): # PYCHOK no cover
222 '''DEPRECATED, use property L{philamheight} or C{philam.to3Tuple(B{height})}.
224 @kwarg height: Optional height, overriding this
225 n-vector's height (C{meter}).
227 @return: A L{PhiLam3Tuple}C{(phi, lam, height)}.
229 @raise ValueError: Invalid B{C{height}}.
230 '''
231 return self.philamheight if height in (None, self.h) else \
232 self.philam.to3Tuple(height)
234 def toCartesian(self, h=None, Cartesian=None, datum=None, **Cartesian_kwds):
235 '''Convert this n-vector to C{Nvector}-based cartesian (ECEF) coordinates.
237 @kwarg h: Optional height, overriding this n-vector's height (C{meter}).
238 @kwarg Cartesian: Optional class to return the (ECEF) coordinates
239 (C{Cartesian}).
240 @kwarg datum: Optional datum (C{Datum}), overriding this datum.
241 @kwarg Cartesian_kwds: Optional, additional B{C{Cartesian}} keyword
242 arguments, ignored if C{B{Cartesian} is None}.
244 @return: The cartesian (ECEF) coordinates (B{C{Cartesian}}) or
245 if C{B{Cartesian} is None}, an L{Ecef9Tuple}C{(x, y, z,
246 lat, lon, height, C, M, datum)} with C{C} and C{M} if
247 available.
249 @raise TypeError: Invalid B{C{Cartesian}} or B{C{Cartesian_kwds}}
250 argument.
252 @raise ValueError: Invalid B{C{h}}.
253 '''
254 D = _spherical_datum(datum or self.datum, name=self.name)
255 E = D.ellipsoid
256 h = self.h if h is None else Height(h)
258 x, y, z = self.x, self.y, self.z
259 # Kenneth Gade eqn 22
260 n = E.b / hypot_(x * E.a_b, y * E.a_b, z)
261 r = h + n * E.a2_b2
263 x *= r
264 y *= r
265 z *= h + n
267 if Cartesian is None:
268 r = self.Ecef(D).reverse(x, y, z, M=True)
269 else:
270 kwds = _xkwds(Cartesian_kwds, datum=D) # h=0
271 r = Cartesian(x, y, z, **kwds)
272 return self._xnamed(r)
274 @deprecated_method
275 def to2ll(self): # PYCHOK no cover
276 '''DEPRECATED, use property L{latlon}.
278 @return: A L{LatLon2Tuple}C{(lat, lon)}.
279 '''
280 return self.latlon
282 @deprecated_method
283 def to3llh(self, height=None): # PYCHOK no cover
284 '''DEPRECATED, use property C{latlonheight} or C{latlon.to3Tuple(B{height})}.
286 @kwarg height: Optional height, overriding this
287 n-vector's height (C{meter}).
289 @return: A L{LatLon3Tuple}C{(lat, lon, height)}.
291 @raise ValueError: Invalid B{C{height}}.
292 '''
293 return self.latlonheight if height in (None, self.h) else \
294 self.latlon.to3Tuple(height)
296 def toLatLon(self, height=None, LatLon=None, datum=None, **LatLon_kwds):
297 '''Convert this n-vector to an C{Nvector}-based geodetic point.
299 @kwarg height: Optional height, overriding this n-vector's
300 height (C{meter}).
301 @kwarg LatLon: Optional class to return the geodetic point
302 (C{LatLon}) or C{None}.
303 @kwarg datum: Optional, spherical datum (C{Datum}).
304 @kwarg LatLon_kwds: Optional, additional B{C{LatLon}} keyword
305 arguments, ignored if C{B{LatLon} is None}.
307 @return: The geodetic point (C{LatLon}) or if C{B{LatLon} is None},
308 an L{Ecef9Tuple}C{(x, y, z, lat, lon, height, C, M,
309 datum)} with C{C} and C{M} if available.
311 @raise TypeError: Invalid B{C{LatLon}} or B{C{LatLon_kwds}}
312 argument.
314 @raise ValueError: Invalid B{C{height}}.
315 '''
316 d = _spherical_datum(datum or self.datum, name=self.name)
317 h = self.h if height is None else Height(height)
318 # use self.Cartesian(Cartesian=None) for better accuracy of the height
319 # than self.Ecef(d).forward(self.lat, self.lon, height=h, M=True)
320 if LatLon is None:
321 r = self.toCartesian(h=h, Cartesian=None, datum=d)
322 else:
323 kwds = _xkwds(LatLon_kwds, height=h, datum=d)
324 r = LatLon(self.lat, self.lon, **self._name1__(kwds))
325 return r
327 def toStr(self, prec=5, fmt=Fmt.PAREN, sep=_COMMASPACE_): # PYCHOK expected
328 '''Return a string representation of this n-vector.
330 Height component is only included if non-zero.
332 @kwarg prec: Number of (decimal) digits, unstripped (C{int}).
333 @kwarg fmt: Enclosing backets format (C{str}).
334 @kwarg sep: Optional separator between components (C{str}).
336 @return: Comma-separated C{"(x, y, z [, h])"} enclosed in
337 B{C{fmt}} brackets (C{str}).
338 '''
339 t = Vector3d.toStr(self, prec=prec, fmt=NN, sep=sep)
340 if self.h:
341 t = sep.join((t, self.hStr()))
342 return (fmt % (t,)) if fmt else t
344 def toVector3d(self, norm=True):
345 '''Convert this n-vector to a 3-D vector, I{ignoring height}.
347 @kwarg norm: If C{True}, normalize the 3-D vector (C{bool}).
349 @return: The (normalized) vector (L{Vector3d}).
350 '''
351 v = Vector3d.unit(self) if norm else self
352 return Vector3d(v.x, v.y, v.z, name=self.name)
354 @deprecated_method
355 def to4xyzh(self, h=None): # PYCHOK no cover
356 '''DEPRECATED, use property L{xyzh} or C{xyz.to4Tuple(B{h})}.'''
357 return self.xyzh if h in (None, self.h) else Vector4Tuple(
358 self.x, self.y, self.z, h, name=self.name)
360 def unit(self, ll=None):
361 '''Normalize this n-vector to unit length.
363 @kwarg ll: Optional, original latlon (C{LatLon}).
365 @return: Normalized vector (C{Nvector}).
366 '''
367 return _xattrs(Vector3d.unit(self, ll=ll), self, _under(_h_))
369 @Property_RO
370 def xyzh(self):
371 '''Get this n-vector's components (L{Vector4Tuple}C{(x, y, z, h)})
372 '''
373 return self.xyz.to4Tuple(self.h)
376NorthPole = NvectorBase(0, 0, +1, name='NorthPole') # North pole (C{Nvector})
377SouthPole = NvectorBase(0, 0, -1, name='SouthPole') # South pole (C{Nvector})
380class _N_vector_(NvectorBase):
381 '''(INTERNAL) Minimal, low-overhead C{n-vector}.
382 '''
383 def __init__(self, x, y, z, h=0, **name):
384 self._x, self._y, self._z = x, y, z
385 if h:
386 self._h = h
387 if name:
388 self.name = name
391class LatLonNvectorBase(LatLonBase):
392 '''(INTERNAL) Base class for n-vector-based ellipsoidal and
393 spherical C{LatLon} classes.
394 '''
396 def _update(self, updated, *attrs, **setters): # PYCHOK _Nv=None
397 '''(INTERNAL) Zap cached attributes if updated.
399 @see: C{ellipsoidalNvector.LatLon} and C{sphericalNvector.LatLon}
400 for the special case of B{C{_Nv}}.
401 '''
402 if updated:
403 _Nv, setters = _xkwds_pop2(setters, _Nv=None)
404 if _Nv is not None:
405 if _Nv._fromll is not None:
406 _Nv._fromll = None
407 self._Nv = None
408 LatLonBase._update(self, updated, *attrs, **setters)
410# def distanceTo(self, other, **kwds): # PYCHOK no cover
411# '''I{Must be overloaded}.'''
412# self._notOverloaded(other, **kwds)
414 def intersections2(self, radius1, other, radius2, **kwds): # PYCHOK expected
415 '''B{Not implemented}, throws a C{NotImplementedError} always.'''
416 self._notImplemented(radius1, other, radius2, **kwds)
418 def others(self, *other, **name_other_up):
419 '''Refined class comparison.
421 @arg other: The other instance (C{LatLonNvectorBase}).
422 @kwarg name_other_up: Overriding C{name=other} and C{up=1}
423 keyword arguments.
425 @return: The B{C{other}} if compatible.
427 @raise TypeError: Incompatible B{C{other}} C{type}.
428 '''
429 if other:
430 other0 = other[0]
431 if isinstance(other0, (self.__class__, LatLonNvectorBase)): # XXX NvectorBase?
432 return other0
434 other, name, up = _xother3(self, other, **name_other_up)
435 if not isinstance(other, (self.__class__, LatLonNvectorBase)): # XXX NvectorBase?
436 LatLonBase.others(self, other, name=name, up=up + 1)
437 return other
439 def toNvector(self, **Nvector_and_kwds): # PYCHOK signature
440 '''Convert this point to C{Nvector} components, I{including height}.
442 @kwarg Nvector_and_kwds: Optional C{Nvector} class and C{Nvector} keyword arguments,
443 Specify C{B{Nvector}=...} to override this C{Nvector} class
444 or use C{B{Nvector}=None}.
446 @return: An C{Nvector} or if C{Nvector is None}, a L{Vector4Tuple}C{(x, y, z, h)}.
448 @raise TypeError: Invalid C{Nvector} or other B{C{Nvector_and_kwds}} item.
449 '''
450 return LatLonBase.toNvector(self, **_xkwds(Nvector_and_kwds, Nvector=NvectorBase))
452 def triangulate(self, bearing1, other, bearing2, height=None, wrap=False): # PYCHOK signature
453 '''Locate a point given this, an other point and the (initial) bearing
454 from this and the other point.
456 @arg bearing1: Bearing at this point (compass C{degrees360}).
457 @arg other: The other point (C{LatLon}).
458 @arg bearing2: Bearing at the other point (compass C{degrees360}).
459 @kwarg height: Optional height at the triangulated point, overriding
460 the mean height (C{meter}).
461 @kwarg wrap: If C{True}, use this and the B{C{other}} point
462 I{normalized} (C{bool}).
464 @return: Triangulated point (C{LatLon}).
466 @raise TypeError: Invalid B{C{other}} point.
468 @raise Valuerror: Points coincide.
469 '''
470 return _triangulate(self, bearing1, self.others(other), bearing2,
471 height=height, wrap=wrap, LatLon=self.classof)
473 def trilaterate(self, distance1, point2, distance2, point3, distance3,
474 radius=R_M, height=None, useZ=False, wrap=False):
475 '''Locate a point at given distances from this and two other points.
477 @arg distance1: Distance to this point (C{meter}, same units
478 as B{C{radius}}).
479 @arg point2: Second reference point (C{LatLon}).
480 @arg distance2: Distance to point2 (C{meter}, same units as
481 B{C{radius}}).
482 @arg point3: Third reference point (C{LatLon}).
483 @arg distance3: Distance to point3 (C{meter}, same units as
484 B{C{radius}}).
485 @kwarg radius: Mean earth radius (C{meter}).
486 @kwarg height: Optional height at trilaterated point, overriding
487 the mean height (C{meter}, same units as B{C{radius}}).
488 @kwarg useZ: Include Z component iff non-NaN, non-zero (C{bool}).
489 @kwarg wrap: If C{True}, use this, B{C{point2}} and B{C{point3}}
490 I{normalized} (C{bool}).
492 @return: Trilaterated point (C{LatLon}).
494 @raise IntersectionError: No intersection, trilateration failed.
496 @raise TypeError: Invalid B{C{point2}} or B{C{point3}}.
498 @raise ValueError: Some B{C{points}} coincide or invalid B{C{distance1}},
499 B{C{distance2}}, B{C{distance3}} or B{C{radius}}.
501 @see: U{Trilateration<https://WikiPedia.org/wiki/Trilateration>},
502 Veness' JavaScript U{Trilateration<https://www.Movable-Type.co.UK/
503 scripts/latlong-vectors.html>} and method C{LatLon.trilaterate5}
504 of other, non-C{Nvector LatLon} classes.
505 '''
506 return _trilaterate(self, distance1, self.others(point2=point2), distance2,
507 self.others(point3=point3), distance3,
508 radius=radius, height=height, useZ=useZ,
509 wrap=wrap, LatLon=self.classof)
511 def trilaterate5(self, distance1, point2, distance2, point3, distance3, # PYCHOK signature
512 area=False, eps=EPS1, radius=R_M, wrap=False):
513 '''B{Not implemented} for C{B{area}=True} and falls back to method
514 C{trilaterate} otherwise.
516 @return: A L{Trilaterate5Tuple}C{(min, minPoint, max, maxPoint, n)}
517 with a single trilaterated intersection C{minPoint I{is}
518 maxPoint}, C{min I{is} max} the nearest intersection
519 margin and count C{n = 1}.
521 @raise NotImplementedError: Keyword argument C{B{area}=True} not
522 (yet) supported.
524 @see: Method L{trilaterate} for other and more details.
525 '''
526 if area:
527 self._notImplemented(area=area)
529 t = _trilaterate(self, distance1, self.others(point2=point2), distance2,
530 self.others(point3=point3), distance3,
531 radius=radius, useZ=True, wrap=wrap,
532 LatLon=self.classof)
533 # ... and handle B{C{eps}} and C{IntersectionError}
534 # like function C{.latlonBase._trilaterate5}
535 d = self.distanceTo(t, radius=radius, wrap=wrap) # PYCHOK distanceTo
536 d = min(fabs(distance1 - d), fabs(distance2 - d), fabs(distance3 - d))
537 if d < eps: # min is max, minPoint is maxPoint
538 return Trilaterate5Tuple(d, t, d, t, 1) # n = 1
539 t = _SPACE_(_no_(_intersection_), Fmt.PAREN(min.__name__, Fmt.f(d, prec=3)))
540 raise IntersectionError(area=area, eps=eps, radius=radius, wrap=wrap, txt=t)
543def _nsumOf(nvs, h_None, Vector, Vector_kwds): # .sphericalNvector, .vector3d
544 '''(INTERNAL) Separated to allow callers to embellish exceptions.
545 '''
546 X, Y, Z, n = Fsum(), Fsum(), Fsum(), 0
547 H = Fsum() if h_None is None else n
548 for n, v in enumerate(nvs or ()): # one pass
549 X += v.x
550 Y += v.y
551 Z += v.z
552 H += v.h
553 if n < 1:
554 raise ValueError(_SPACE_(Fmt.PARENSPACED(len=n), _insufficient_))
556 x, y, z = map1(float, X, Y, Z)
557 h = H.fover(n) if h_None is None else h_None
558 return Vector3Tuple(x, y, z).to4Tuple(h) if Vector is None else \
559 Vector(x, y, z, **_xkwds(Vector_kwds, h=h))
562def sumOf(nvectors, Vector=None, h=None, **Vector_kwds):
563 '''Return the I{vectorial} sum of two or more n-vectors.
565 @arg nvectors: Vectors to be added (C{Nvector}[]).
566 @kwarg Vector: Optional class for the vectorial sum (C{Nvector})
567 or C{None}.
568 @kwarg h: Optional height, overriding the mean height (C{meter}).
569 @kwarg Vector_kwds: Optional, additional B{C{Vector}} keyword
570 arguments, ignored if C{B{Vector} is None}.
572 @return: Vectorial sum (B{C{Vector}}) or a L{Vector4Tuple}C{(x, y,
573 z, h)} if C{B{Vector} is None}.
575 @raise VectorError: No B{C{nvectors}}.
576 '''
577 try:
578 return _nsumOf(nvectors, h, Vector, Vector_kwds)
579 except (TypeError, ValueError) as x:
580 raise VectorError(nvectors=nvectors, Vector=Vector, cause=x)
583def _triangulate(point1, bearing1, point2, bearing2, height=None,
584 wrap=False, **LatLon_and_kwds):
585 # (INTERNAL) Locate a point given two known points and initial
586 # bearings from those points, see C{LatLon.triangulate} above
588 def _gc(p, b, _i_):
589 n = p.toNvector()
590 de = NorthPole.cross(n, raiser=_pole_).unit() # east vector @ n
591 dn = n.cross(de) # north vector @ n
592 s, c = sincos2d(Bearing(b, name=_bearing_ + _i_))
593 dest = de.times(s)
594 dnct = dn.times(c)
595 d = dnct.plus(dest) # direction vector @ n
596 return n.cross(d) # great circle point + bearing
598 if wrap:
599 point2 = _unrollon(point1, point2, wrap=wrap)
600 if _isequalTo(point1, point2, eps=EPS):
601 raise _ValueError(points=point2, wrap=wrap, txt=_coincident_)
603 gc1 = _gc(point1, bearing1, _1_) # great circle p1 + b1
604 gc2 = _gc(point2, bearing2, _2_) # great circle p2 + b2
606 n = gc1.cross(gc2, raiser=_point_) # n-vector of intersection point
607 h = point1._havg(point2, h=height)
608 kwds = _xkwds(LatLon_and_kwds, height=h)
609 return n.toLatLon(**kwds) # Nvector(n.x, n.y, n.z).toLatLon(...)
612def _trilaterate(point1, distance1, point2, distance2, point3, distance3,
613 radius=R_M, height=None, useZ=False,
614 wrap=False, **LatLon_and_kwds):
615 # (INTERNAL) Locate a point at given distances from
616 # three other points, see LatLon.triangulate above
618 def _nr2(p, d, r, _i_, *qs): # .toNvector and angular distance squared
619 for q in qs:
620 if _isequalTo(p, q, eps=EPS):
621 raise _ValueError(points=p, txt=_coincident_)
622 return p.toNvector(), (Scalar(d, name=_distance_ + _i_) / r)**2
624 p1, r = point1, Radius_(radius)
625 p2, p3, _ = _unrollon3(p1, point2, point3, wrap)
627 n1, r12 = _nr2(p1, distance1, r, _1_)
628 n2, r22 = _nr2(p2, distance2, r, _2_, p1)
629 n3, r32 = _nr2(p3, distance3, r, _3_, p1, p2)
631 # the following uses x,y coordinate system with origin at n1, x axis n1->n2
632 y = n3.minus(n1)
633 x = n2.minus(n1)
634 z = None
636 d = x.length # distance n1->n2
637 if d > EPS_2: # and y.length > EPS_2:
638 X = x.unit() # unit vector in x direction n1->n2
639 i = X.dot(y) # signed magnitude of x component of n1->n3
640 Y = y.minus(X.times(i)).unit() # unit vector in y direction
641 j = Y.dot(y) # signed magnitude of y component of n1->n3
642 if fabs(j) > EPS_2:
643 # courtesy of U{Carlos Freitas<https://GitHub.com/mrJean1/PyGeodesy/issues/33>}
644 x = fsumf_(r12, -r22, d**2) / (d * _2_0) # n1->intersection x- and ...
645 y = fsumf_(r12, -r32, i**2, j**2, x * i * _N_2_0) / (j * _2_0) # ... y-component
646 # courtesy of U{AleixDev<https://GitHub.com/mrJean1/PyGeodesy/issues/43>}
647 z = fsumf_(max(r12, r22, r32), -(x**2), -(y**2)) # XXX not just r12!
648 if z > EPS:
649 n = n1.plus(X.times(x)).plus(Y.times(y))
650 if useZ: # include Z component
651 Z = X.cross(Y) # unit vector perpendicular to plane
652 n = n.plus(Z.times(sqrt(z)))
653 if height is None:
654 h = fidw((point1.height, point2.height, point3.height),
655 map1(fabs, distance1, distance2, distance3))
656 else:
657 h = Height(height)
658 kwds = _xkwds(LatLon_and_kwds, height=h)
659 return n.toLatLon(**kwds) # Nvector(n.x, n.y, n.z).toLatLon(...)
661 # no intersection, d < EPS_2 or fabs(j) < EPS_2 or z < EPS
662 t = _SPACE_(_no_, _intersection_, NN)
663 raise IntersectionError(point1=point1, distance1=distance1,
664 point2=point2, distance2=distance2,
665 point3=point3, distance3=distance3,
666 txt=unstr(t, z=z, useZ=useZ, wrap=wrap))
669__all__ += _ALL_DOCS(LatLonNvectorBase, NvectorBase, sumOf) # classes
671# **) MIT License
672#
673# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
674#
675# Permission is hereby granted, free of charge, to any person obtaining a
676# copy of this software and associated documentation files (the "Software"),
677# to deal in the Software without restriction, including without limitation
678# the rights to use, copy, modify, merge, publish, distribute, sublicense,
679# and/or sell copies of the Software, and to permit persons to whom the
680# Software is furnished to do so, subject to the following conditions:
681#
682# The above copyright notice and this permission notice shall be included
683# in all copies or substantial portions of the Software.
684#
685# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
686# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
687# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
688# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
689# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
690# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
691# OTHER DEALINGS IN THE SOFTWARE.