Coverage for pygeodesy / namedTuples.py: 90%

310 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-07-26 22:05 -0400

1 

2# -*- coding: utf-8 -*- 

3 

4u'''Named tuples. 

5 

6Tuples returned by C{pygeodesy} functions and class methods 

7are all instances of some C{Named...Tuple} class, all sub-classes 

8of C{_NamedTuple} defined in C{pygeodesy.named}. 

9''' 

10 

11from pygeodesy.basics import isinstanceof, issubclassof, map1, _xinstanceof 

12# from pygeodesy.cartesianBase import CartesianBase # _MODS 

13from pygeodesy.constants import INT0, _0_5, fabs # PYCHOK used! _0_5 

14# from pygeodesy.dms import toDMS # _MODS 

15from pygeodesy.errors import _TypeError, _xattr, _xkwds, _xkwds_not, _xkwds_pop2 

16# from pygeodesy.internals import typename # from .named 

17from pygeodesy.interns import NN, _1_, _2_, _a_, _A_, _area_, _angle_, _b_, _B_, \ 

18 _band_, _beta_, _c_, _C_, _D_, _datum_, _distance_, \ 

19 _E_, _easting_, _end_, _fi_, _gamma_, _h_, _height_, \ 

20 _hemipole_, _initial_, _j_, _lam_, _lat_, _lon_, \ 

21 _n_, _northing_, _number_, _outside_, _phi_, _point_, \ 

22 _precision_, _points_, _radius_, _scale_, _start_, \ 

23 _x_, _y_, _z_, _zone_ 

24# from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS # from .named 

25from pygeodesy.named import _NamedTuple, _Pass, _ALL_LAZY, _MODS, typename 

26from pygeodesy.props import deprecated_property_RO, Property_RO, property_RO 

27from pygeodesy.units import Band, Bearing, Degrees, Degrees2, Easting, FIx, \ 

28 Height, Int, Lam, Lat, Lon, Meter, Meter2, \ 

29 Northing, Number_, Phi, Precision_, Radians, \ 

30 Radius, Scalar, Str 

31# from math import fabs # from .constants 

32 

33__all__ = _ALL_LAZY.namedTuples 

34__version__ = '26.07.25' 

35 

36# __DUNDER gets mangled in class 

37_closest_ = 'closest' 

38_destination_ = 'destination' 

39_elel_ = 'll' 

40_final_ = 'final' 

41_fraction_ = 'fraction' 

42 

43 

44class Bearing2Tuple(_NamedTuple): 

45 '''2-Tuple C{(initial, final)} bearings, both in compass C{degrees360}. 

46 ''' 

47 _Names_ = (_initial_, _final_) 

48 _Units_ = ( Bearing, Bearing) 

49 

50 

51class Bounds2Tuple(_NamedTuple): # .geohash.py, .latlonBase.py, .points.py 

52 '''2-Tuple C{(latlonSW, latlonNE)} with the bounds' lower-left and 

53 upper-right corner as C{LatLon} instance. 

54 ''' 

55 _Names_ = ('latlonSW', 'latlonNE') 

56 _Units_ = (_Pass, _Pass) 

57 

58 

59class Bounds4Tuple(_NamedTuple): # .geohash.py, .points.py 

60 '''4-Tuple C{(latS, lonW, latN, lonE)} with the bounds' lower-left 

61 C{(LatS, LonW)} and upper-right C{(latN, lonE)} corner lat- and 

62 longitudes. 

63 ''' 

64 _Names_ = ('latS', 'lonW', 'latN', 'lonE') 

65 _Units_ = ( Lat, Lon, Lat, Lon) 

66 

67 def _dupof(self, which, *args, **name): 

68 kwds = name or dict(name=self.name or typename(which)) 

69 return self.classof(*args, **kwds) 

70 

71 def enclosures(self, S_other, *W_N_E, **name): 

72 '''Get the enclosures of this around an other L{Bounds4Tuple}. 

73 

74 @arg S_other: Bottom C{latS} (C{scalar}) or an other 

75 L{Bounds4Tuple} instance. 

76 @arg W_N_E: Left C{lonW}, top C{latN} and right C{lonE}, 

77 each a (C{scalar}) for C{scalar B{S_other}}. 

78 

79 @return: A L{Bounds4Tuple} with the I{margin} at each of 

80 the 4 sides, positive if this side I{encloses} 

81 (is on the I{outside} of) the other, negative 

82 if not or zero if abutting. 

83 ''' 

84 s, w, n, e, \ 

85 S, W, N, E = self._8_tuple(S_other, W_N_E) 

86 return self._dupof(Bounds4Tuple.enclosures, 

87 map1(float, S - s, W - w, 

88 n - N, e - E), **name) 

89 

90 def isinside(self, lat, lon, eps=0): 

91 '''Are B{C{lat}} and B{C{lon}} both inside these bounds? 

92 

93 @arg lat: Latitude (C{degrees}, scalar or C{str}). 

94 @arg lon: Longitude (C{degrees}, scalar or C{str}). 

95 @kwarg eps: Over-/undersize the C{RD} bounds (C{degrees}). 

96 

97 @return: C{None} if B{C{lat}} or B{C{lon}} is NAN, C{False} 

98 if outside these bounds, C{True} otherwise. 

99 ''' 

100 return _isinside(Lat(lat, clip=0), Lon(lon, clip=0), 

101 Degrees(eps=eps) if eps else 0, self) 

102 

103 @Property_RO 

104 def latC(self): 

105 '''Get the center latitude (C{degrees}). 

106 ''' 

107 return Lat(latC=(self.latS + self.latN) * _0_5) 

108 

109 @Property_RO 

110 def latZ(self): 

111 '''Get the latitudinal size (C{degrees}). 

112 ''' 

113 return Lat(latZ=self.latN - self.latS) 

114 

115 @Property_RO 

116 def lonC(self): 

117 '''Get the center longitude (C{degrees}). 

118 ''' 

119 return Lon(lonC=(self.lonW + self.lonE) * _0_5) 

120 

121 @Property_RO 

122 def lonZ(self): 

123 '''Get the longitudinal size (C{degrees}). 

124 ''' 

125 return Lon(lonZ=self.lonE - self.lonW) 

126 

127 def overlap(self, S_other, *W_N_E, **name): 

128 '''Intersect this with an other L{Bounds4Tuple}. 

129 

130 @arg S_other: Bottom C{latS} (C{scalar}) or an other 

131 L{Bounds4Tuple} instance. 

132 @arg W_N_E: Left C{lonW}, top C{latN} and right C{lonE}, 

133 each a (C{scalar}) for C{scalar B{S_other}}. 

134 

135 @return: C{None} if the bounds do not overlap, otherwise 

136 the intersection of both as a L{Bounds4Tuple}. 

137 ''' 

138 s, w, n, e, \ 

139 S, W, N, E = self._8_tuple(S_other, W_N_E) 

140 return None if s > N or n < S or w > E or e < W else \ 

141 self._dupof(Bounds4Tuple.overlap, 

142 max(s, S), max(w, W), 

143 min(n, N), min(e, E), **name) 

144 

145 def resize(self, eps): 

146 '''Get these bounds, over- or undersize by C{B{eps}}. 

147 

148 @arg eps: In- or decrease (C{degrees}). 

149 

150 @return: A L{Bounds4Tuple}C{(latS, lonW, latN, lonE)} 

151 with all 4 bounds resized. 

152 ''' 

153 return _resize4(self, Degrees(eps=eps)) 

154 

155 def _8_tuple(self, S_other, W_N_E): 

156 # return 8-tuple this (s, w, n, e) + other (S, W, N, E) 

157 if W_N_E: 

158 S_other = Bounds4Tuple(S_other, *W_N_E) 

159 else: 

160 _xinstanceof(Bounds4Tuple, S_other=S_other) 

161 return tuple(self) + S_other.toUnits() 

162 

163 def union(self, S_other, *W_N_E, **name): 

164 '''Union of this and an other L{Bounds4Tuple}. 

165 

166 @arg S_other: Bottom C{latS} (C{scalar}) or an other 

167 L{Bounds4Tuple} instance. 

168 @arg W_N_E: Left C{lonW}, top C{latN} and right C{lonE}, 

169 each a (C{scalar}) for C{scalar B{S_other}}. 

170 

171 @return: The C{union} a L{Bounds4Tuple}. 

172 ''' 

173 s, w, n, e, \ 

174 S, W, N, E = self._8_tuple(S_other, W_N_E) 

175 return self._dupof(Bounds4Tuple.union, 

176 min(s, S), min(w, W), 

177 max(n, N), max(e, E), **name) 

178 

179 

180class Circle4Tuple(_NamedTuple): 

181 '''4-Tuple C{(radius, height, lat, beta)} with the C{radius} and C{height} 

182 of a parallel I{circle of latitude} at (geodetic) latitude C{lat} and 

183 I{parametric (or reduced) auxiliary latitude} C{beta} on a I{biaxial 

184 ellipsoid}. 

185 

186 The C{height} is the (signed) distance along the z-axis between the 

187 parallel and the equator. At near-polar C{lat}s, the C{radius} is C{0}, 

188 the C{height} is the ellipsoid's polar radius (signed) and C{beta} 

189 equals C{lat}. The latter are in C{degrees90}, always. 

190 

191 @see: Class L{Ellipse5Tuple}. 

192 ''' 

193 _Names_ = (_radius_, _height_, _lat_, _beta_) 

194 _Units_ = ( Radius, Height, Lat, Lat) 

195 

196 @property_RO 

197 def abc3(self): 

198 '''Get the non-negative semi-axes as 3-tuple C{(a, b, c)}. 

199 ''' 

200 return map1(fabs, self.radius, self.radius, self.height) # PYCHOK named 

201 

202 

203class Destination2Tuple(_NamedTuple): # .ellipsoidalKarney.py, -Vincenty.py 

204 '''2-Tuple C{(destination, final)}, C{destination} in C{LatLon} 

205 and C{final} bearing in compass C{degrees360}. 

206 ''' 

207 _Names_ = (_destination_, _final_) 

208 _Units_ = (_Pass, Bearing) 

209 

210 

211class Destination3Tuple(_NamedTuple): # .karney.py 

212 '''3-Tuple C{(lat, lon, final)}, destination C{lat}, C{lon} in 

213 C{degrees90} respectively C{degrees180} and C{final} bearing 

214 in compass C{degrees360}. 

215 ''' 

216 _Names_ = (_lat_, _lon_, _final_) 

217 _Units_ = ( Lat, Lon, Bearing) 

218 

219 

220class Distance2Tuple(_NamedTuple): # .datum.py, .ellipsoidalBase.py 

221 '''2-Tuple C{(distance, initial)}, C{distance} in C{meter} and 

222 C{initial} bearing in compass C{degrees360}. 

223 ''' 

224 _Names_ = (_distance_, _initial_) 

225 _Units_ = ( Meter, Bearing) 

226 

227 

228class Distance3Tuple(_NamedTuple): # .ellipsoidalKarney.py, -Vincenty.py 

229 '''3-Tuple C{(distance, initial, final)}, C{distance} in C{meter} 

230 and C{initial} and C{final} bearing, both in compass C{degrees360}. 

231 ''' 

232 _Names_ = (_distance_, _initial_, _final_) 

233 _Units_ = ( Meter, Bearing, Bearing) 

234 

235 

236class Distance4Tuple(_NamedTuple): # .formy.py, .points.py 

237 '''4-Tuple C{(distance2, delta_lat, delta_lon, unroll_lon2)} with 

238 the distance in C{degrees squared}, the latitudinal C{delta_lat 

239 = B{lat2} - B{lat1}}, the wrapped, unrolled and adjusted 

240 longitudinal C{delta_lon = B{lon2} - B{lon1}} and C{unroll_lon2}, 

241 the unrolled or original B{C{lon2}}. 

242 

243 @note: Use Function L{pygeodesy.degrees2m} to convert C{degrees 

244 squared} to C{meter} as M{degrees2m(sqrt(distance2), ...)} 

245 or M{degrees2m(hypot(delta_lat, delta_lon), ...)}. 

246 ''' 

247 _Names_ = ('distance2', 'delta_lat', 'delta_lon', 'unroll_lon2') 

248 _Units_ = ( Degrees2, Degrees, Degrees, Degrees) 

249 

250 

251class EasNor2Tuple(_NamedTuple): # .css, .osgr, .ups, .utm, .utmupsBase 

252 '''2-Tuple C{(easting, northing)}, both in C{meter}, conventionally. 

253 ''' 

254 _Names_ = (_easting_, _northing_) 

255 _Units_ = ( Easting, Northing) 

256 

257 

258class EasNor3Tuple(_NamedTuple): # .css.py, .lcc.py 

259 '''3-Tuple C{(easting, northing, height)}, all in C{meter}, conventionally. 

260 ''' 

261 _Names_ = (_easting_, _northing_, _height_) 

262 _Units_ = ( Easting, Northing, Height) 

263 

264 

265class _Convergence(object): 

266 '''(INTERNAL) DEPRECATED Property C{convergence}, use property C{gamma}.''' 

267 @deprecated_property_RO 

268 def convergence(self): 

269 '''DEPRECATED, use property C{gamma}. 

270 ''' 

271 return self.gamma # PYCHOK self[.] 

272 

273 

274class Ellipse5Tuple(_NamedTuple): # in .triaxials.bases._UnOrderedTriaxialBase.ellipse5 

275 '''5-Tuple C{(a, b, height, lat, beta)} with semi-axes C{a} and C{b} of a parallel 

276 I{ellipse of latitude} at (geodetic) latitude C{lat} and I{parametric (or reduced) 

277 auxiliary latitude} C{beta} of a I{triaxial ellipsoid}. 

278 

279 The C{height} is the (signed) distance between the parallel and the triaxial's 

280 equatorial plane. At near-polar C{lat}s, C{a} and C{b} are C{0}, the C{height} 

281 is the triaxial semi-axis C{c} (signed) and C{beta} equals C{lat}. The latter 

282 are in C{degrees90}, always. 

283 

284 @see: Class L{Circle4Tuple}. 

285 ''' 

286 _Names_ = (_a_, _b_, _height_, _lat_, _beta_) 

287 _Units_ = ( Radius, Radius, Height, Lat, Lat) 

288 

289 @property_RO 

290 def abc3(self): 

291 '''Get the semi-axes as 3-tuple C{(a, b, c)}, non-negative. 

292 ''' 

293 return map1(fabs, self.a, self.b, self.height) # PYCHOK namedTuple 

294 

295 @property_RO 

296 def abc3ordered(self): 

297 '''Get the semi-axes as 3-tuple C{(a, b, c)}, non-negative, ordered. 

298 ''' 

299 return tuple(reversed(sorted(self.abc3))) 

300 

301 def toTriaxial(self, **Triaxial_and_kwds): # like .Ellipse.toTriaxial_ 

302 '''Return a L{Triaxial_<pygeodesy.Triaxial>} from this tuple's semi-axes C{abc3ordered}. 

303 

304 @kwarg Triaxial_and_kwds: Optional C{B{Triaxial}=Triaxial} class and additional 

305 C{Triaxial} keyword arguments. 

306 ''' 

307 T, kwds = _xkwds_pop2(Triaxial_and_kwds, Triaxial=_MODS.triaxials.Triaxial) 

308 return T(*self.abc3ordered, **_xkwds(kwds, name=self.name)) # 'NN' 

309 

310 def toTriaxial_(self, **Triaxial_and_kwds): # like .Ellipse.toTriaxial_ 

311 '''Return a L{Triaxial_<pygeodesy.Triaxial_>} from this tuple's semi-axes C{abc3}. 

312 

313 @kwarg Triaxial_and_kwds: Optional C{B{Triaxial}=Triaxial_} class and additional 

314 C{Triaxial_} keyword arguments. 

315 ''' 

316 T, kwds = _xkwds_pop2(Triaxial_and_kwds, Triaxial=_MODS.triaxials.Triaxial_) 

317 return T(*self.abc3, **_xkwds(kwds, name=self.name)) # 'NN' 

318 

319 

320class Forward4Tuple(_NamedTuple, _Convergence): 

321 '''4-Tuple C{(easting, northing, gamma, scale)} in C{meter}, C{meter}, meridian 

322 convergence C{gamma} at point in C{degrees} and the C{scale} of projection at 

323 point C{scalar}. 

324 ''' 

325 _Names_ = (_easting_, _northing_, _gamma_, _scale_) 

326 _Units_ = ( Easting, Northing, Degrees, Scalar) 

327 

328 

329class Intersection3Tuple(_NamedTuple): # .css.py, .lcc.py 

330 '''3-Tuple C{(point, outside1, outside2)} of an intersection C{point} and C{outside1}, 

331 the position of the C{point}, C{-1} if before the start, C{+1} if after the end and 

332 C{0} if on or between the start and end point of the first line. 

333 

334 Similarly, C{outside2} is C{-2}, C{+2} or C{0} to indicate the position of the 

335 intersection C{point} on the second line or path. 

336 

337 If a path was specified with an initial bearing instead of an end point, C{outside1} 

338 and/or C{outside2} will be C{0} if the intersection C{point} is on the start point 

339 or C{+1} respectively C{+2} if the intersection C{point} is after the start point, 

340 in the direction of the bearing. 

341 ''' 

342 _Names_ = (_point_, _outside_ + _1_, _outside_ + _2_) 

343 _Units_ = (_Pass, Int, Int) 

344 

345 

346class LatLon2Tuple(_NamedTuple): 

347 '''2-Tuple C{(lat, lon)} in C{degrees90} and C{degrees180}. 

348 ''' 

349 _Names_ = (_lat_, _lon_) 

350 _Units_ = ( Lat, Lon) 

351 

352 def to3Tuple(self, height, **name): 

353 '''Extend this L{LatLon2Tuple} to a L{LatLon3Tuple}. 

354 

355 @arg height: The height to add (C{scalar}). 

356 @kwarg name: Optional C{B{name}=NN} (C{str}), overriding 

357 this name. 

358 

359 @return: A L{LatLon3Tuple}C{(lat, lon, height)}. 

360 

361 @raise ValueError: Invalid B{C{height}}. 

362 ''' 

363 return self._xtend(LatLon3Tuple, height, **name) 

364 

365 def to4Tuple(self, height, datum, **name): 

366 '''Extend this L{LatLon2Tuple} to a L{LatLon4Tuple}. 

367 

368 @arg height: The height to add (C{scalar}). 

369 @arg datum: The datum to add (C{Datum}). 

370 @kwarg name: Optional C{B{name}=NN} (C{str}), overriding 

371 this name. 

372 

373 @return: A L{LatLon4Tuple}C{(lat, lon, height, datum)}. 

374 

375 @raise TypeError: If B{C{datum}} not a C{Datum}. 

376 

377 @raise ValueError: Invalid B{C{height}}. 

378 ''' 

379 return self.to3Tuple(height).to4Tuple(datum, **name) 

380 

381 

382class LatLon3Tuple(_NamedTuple): 

383 '''3-Tuple C{(lat, lon, height)} in C{degrees90}, C{degrees180} 

384 and C{meter}, conventionally. 

385 ''' 

386 _Names_ = (_lat_, _lon_, _height_) 

387 _Units_ = ( Lat, Lon, Height) 

388 

389 def to4Tuple(self, datum, **name): 

390 '''Extend this L{LatLon3Tuple} to a L{LatLon4Tuple}. 

391 

392 @arg datum: The datum to add (C{Datum}). 

393 @kwarg name: Optional C{B{name}=NN} (C{str}), overriding 

394 this name. 

395 

396 @return: A L{LatLon4Tuple}C{(lat, lon, height, datum)}. 

397 

398 @raise TypeError: If B{C{datum}} not a C{Datum}. 

399 ''' 

400 _xinstanceof(_MODS.datums.Datum, datum=datum) 

401 return self._xtend(LatLon4Tuple, datum, **name) 

402 

403 

404class LatLon4Tuple(LatLon3Tuple): # .cartesianBase, .css, .ecef, .lcc 

405 '''4-Tuple C{(lat, lon, height, datum)} in C{degrees90}, 

406 C{degrees180}, C{meter} and L{Datum}. 

407 ''' 

408 _Names_ = (_lat_, _lon_, _height_, _datum_) 

409 _Units_ = ( Lat, Lon, Height, _Pass) 

410 

411 

412def _LL4Tuple(lat, lon, height, datum, LatLon, LatLon_kwds, inst=None, 

413 iteration=None, **name): 

414 '''(INTERNAL) Return a L{LatLon4Tuple} or a B{C{LatLon}} instance. 

415 ''' 

416 if LatLon is None: # ignore LatLon_kwds 

417 r = LatLon4Tuple(lat, lon, height, datum, **name) 

418 else: 

419 kwds = {} if inst is None else _xkwds_not(None, 

420# datum=_xattr(inst, datum=None), 

421 epoch=_xattr(inst, epoch=None), 

422 reframe=_xattr(inst, reframe=None)) # PYCHOK indent 

423 kwds.update(datum=datum, height=height, **name) 

424 if LatLon_kwds: 

425 kwds.update(LatLon_kwds) 

426 r = LatLon(lat, lon, **kwds) 

427 if iteration is not None: # like .named._namedTuple.__new__ 

428 r._iteration = iteration 

429 return r 

430 

431 

432class LatLonDatum3Tuple(_NamedTuple): # .lcc.py, .osgr.py 

433 '''3-Tuple C{(lat, lon, datum)} in C{degrees90}, C{degrees180} 

434 and L{Datum}. 

435 ''' 

436 _Names_ = (_lat_, _lon_, _datum_) 

437 _Units_ = ( Lat, Lon, _Pass) 

438 

439 

440class LatLonDatum5Tuple(LatLonDatum3Tuple, _Convergence): # .ups.py, .utm.py, .utmupsBase.py 

441 '''5-Tuple C{(lat, lon, datum, gamma, scale)} in C{degrees90}, 

442 C{degrees180}, L{Datum}, C{degrees} and C{float}. 

443 ''' 

444 _Names_ = LatLonDatum3Tuple._Names_ + (_gamma_, _scale_) 

445 _Units_ = LatLonDatum3Tuple._Units_ + ( Degrees, Scalar) 

446 

447 

448class LatLonPrec3Tuple(_NamedTuple): # .gars.py, .wgrs.py 

449 '''3-Tuple C{(lat, lon, precision)} in C{degrees}, C{degrees} 

450 and C{int}. 

451 ''' 

452 _Names_ = (_lat_, _lon_, _precision_) 

453 _Units_ = ( Lat, Lon, Precision_) 

454 

455 def to5Tuple(self, height, radius, **name): 

456 '''Extend this L{LatLonPrec3Tuple} to a L{LatLonPrec5Tuple}. 

457 

458 @arg height: The height to add (C{float} or C{None}). 

459 @arg radius: The radius to add (C{float} or C{None}). 

460 @kwarg name: Optional C{B{name}=NN} (C{str}), overriding 

461 this name. 

462 

463 @return: A L{LatLonPrec5Tuple}C{(lat, lon, precision, 

464 height, radius)}. 

465 ''' 

466 return self._xtend(LatLonPrec5Tuple, height, radius, **name) 

467 

468 

469class LatLonPrec5Tuple(LatLonPrec3Tuple): # .wgrs.py 

470 '''5-Tuple C{(lat, lon, precision, height, radius)} in C{degrees}, 

471 C{degrees}, C{int} and C{height} or C{radius} in C{meter} (or 

472 C{None} if missing). 

473 ''' 

474 _Names_ = LatLonPrec3Tuple._Names_ + (_height_, _radius_) 

475 _Units_ = LatLonPrec3Tuple._Units_ + ( Height, Radius) 

476 

477 

478class _NamedTupleTo(_NamedTuple): # in .testNamedTuples 

479 '''(INTERNAL) Base for C{-.toDegrees}, C{-.toRadians}. 

480 ''' 

481 def _Degrees3(self, *xs, **toDMS_kwds): 

482 '''(INTERNAL) Convert C{xs} from C{Radians} to C{Degrees} or C{toDMS}. 

483 ''' 

484 if toDMS_kwds: 

485 toDMS_kwds = _xkwds(toDMS_kwds, ddd=1, pos=NN) 

486 toDMS, s = _MODS.dms.toDMS, None 

487 else: 

488 toDMS, s = None, self 

489 for x in xs: 

490 if not isinstanceof(x, Degrees): 

491 x, s = x.toDegrees(), None 

492 yield toDMS(x, **toDMS_kwds) if toDMS else x 

493 yield s 

494 

495 def _Radians3(self, *xs, **unused): 

496 '''(INTERNAL) Convert C{xs} from C{Degrees} to C{Radians}. 

497 ''' 

498 s = self 

499 for x in xs: 

500 if not isinstanceof(x, Radians): 

501 x, s = x.toRadians(), None 

502 yield x 

503 yield s 

504 

505 

506class NearestOn2Tuple(_NamedTuple): # .ellipsoidalBaseDI 

507 '''2-Tuple C{(closest, fraction)} of the C{closest} point 

508 on and C{fraction} along a line (segment) between two 

509 points. The C{fraction} is C{0} if the closest point 

510 is the first or C{1} the second of the two points. 

511 Negative C{fraction}s indicate the closest point is 

512 C{before} the first point. For C{fraction > 1.0} 

513 the closest point is after the second point. 

514 ''' 

515 _Names_ = (_closest_, _fraction_) 

516 _Units_ = (_Pass, _Pass) 

517 

518 

519class NearestOn3Tuple(_NamedTuple): # .points.py, .sphericalTrigonometry 

520 '''3-Tuple C{(closest, distance, angle)} of the C{closest} 

521 point on the polygon, either a C{LatLon} instance or a 

522 L{LatLon3Tuple}C{(lat, lon, height)} and the C{distance} 

523 and C{angle} to the C{closest} point are in C{meter} 

524 respectively compass C{degrees360}. 

525 ''' 

526 _Names_ = (_closest_, _distance_, _angle_) 

527 _Units_ = (_Pass, Meter, Degrees) 

528 

529 

530# NearestOn4Tuple DEPRECATED, see .deprecated.classes.NearestOn4Tuple 

531 

532 

533class NearestOn5Tuple(_NamedTuple): 

534 '''5-Tuple C{(lat, lon, distance, angle, height)} all in C{degrees}, 

535 except C{height}. The C{distance} is the L{pygeodesy.equirectangular} 

536 distance between the closest and the reference B{C{point}} in C{degrees}. 

537 The C{angle} from the reference B{C{point}} to the closest point is in 

538 compass C{degrees360}, see function L{pygeodesy.compassAngle}. The 

539 C{height} is the (interpolated) height at the closest point in C{meter} 

540 or C{0}. 

541 ''' 

542 _Names_ = (_lat_, _lon_, _distance_, _angle_, _height_) 

543 _Units_ = ( Lat, Lon, Degrees, Degrees, Meter) 

544 

545 

546class NearestOn6Tuple(_NamedTuple): # .latlonBase.py, .vector3d.py 

547 '''6-Tuple C{(closest, distance, fi, j, start, end)} with the C{closest} 

548 point, the C{distance} in C{meter}, conventionally and the C{start} 

549 and C{end} point of the path or polygon edge. Fractional index C{fi} 

550 (an L{FIx} instance) and index C{j} indicate the path or polygon edge 

551 and the fraction along that edge with the C{closest} point. The 

552 C{start} and C{end} points may differ from the given path or polygon 

553 points at indices C{fi} respectively C{j}, when unrolled (C{wrap} is 

554 C{True}). Also, the C{start} and/or C{end} point may be the same 

555 instance as the C{closest} point, for example when the very first 

556 path or polygon point is the nearest. 

557 ''' 

558 _Names_ = (_closest_, _distance_, _fi_, _j_, _start_, _end_) 

559 _Units_ = (_Pass, Meter, FIx, Number_, _Pass , _Pass) 

560 

561 

562class NearestOn8Tuple(_NamedTuple): # .ellipsoidalBaseDI 

563 '''8-Tuple C{(closest, distance, fi, j, start, end, initial, final)}, 

564 like L{NearestOn6Tuple} but extended with the C{initial} and the 

565 C{final} bearing at the reference respectively the C{closest} 

566 point, both in compass C{degrees}. 

567 ''' 

568 _Names_ = NearestOn6Tuple._Names_ + Distance3Tuple._Names_[-2:] 

569 _Units_ = NearestOn6Tuple._Units_ + Distance3Tuple._Units_[-2:] 

570 

571 

572class PhiLam2Tuple(_NamedTuple): # .frechet, .hausdorff, .latlonBase, .points, .vector3d 

573 '''2-Tuple C{(phi, lam)} with latitude C{phi} in C{radians[PI_2]} 

574 and longitude C{lam} in C{radians[PI]}. 

575 

576 @note: Using C{phi/lambda} for lat-/longitude in C{radians} 

577 follows Chris Veness' U{convention 

578 <https://www.Movable-Type.co.UK/scripts/latlong.html>}. 

579 ''' 

580 _Names_ = (_phi_, _lam_) 

581 _Units_ = ( Phi, Lam) 

582 

583 def to3Tuple(self, height, **name): 

584 '''Extend this L{PhiLam2Tuple} to a L{PhiLam3Tuple}. 

585 

586 @arg height: The height to add (C{scalar}). 

587 @kwarg name: Optional C{B{name}=NN} (C{str}), 

588 overriding this name. 

589 

590 @return: A L{PhiLam3Tuple}C{(phi, lam, height)}. 

591 

592 @raise ValueError: Invalid B{C{height}}. 

593 ''' 

594 return self._xtend(PhiLam3Tuple, height, **name) 

595 

596 def to4Tuple(self, height, datum): 

597 '''Extend this L{PhiLam2Tuple} to a L{PhiLam4Tuple}. 

598 

599 @arg height: The height to add (C{scalar}). 

600 @arg datum: The datum to add (C{Datum}). 

601 

602 @return: A L{PhiLam4Tuple}C{(phi, lam, height, datum)}. 

603 

604 @raise TypeError: If B{C{datum}} not a C{Datum}. 

605 

606 @raise ValueError: Invalid B{C{height}}. 

607 ''' 

608 return self.to3Tuple(height).to4Tuple(datum) 

609 

610 

611class PhiLam3Tuple(_NamedTuple): # .nvector.py, extends -2Tuple 

612 '''3-Tuple C{(phi, lam, height)} with latitude C{phi} in 

613 C{radians[PI_2]}, longitude C{lam} in C{radians[PI]} and 

614 C{height} in C{meter}. 

615 

616 @note: Using C{phi/lambda} for lat-/longitude in C{radians} 

617 follows Chris Veness' U{convention 

618 <https://www.Movable-Type.co.UK/scripts/latlong.html>}. 

619 ''' 

620 _Names_ = (_phi_, _lam_, _height_) 

621 _Units_ = ( Phi, Lam, Height) 

622 

623 def to4Tuple(self, datum, **name): 

624 '''Extend this L{PhiLam3Tuple} to a L{PhiLam4Tuple}. 

625 

626 @arg datum: The datum to add (C{Datum}). 

627 @kwarg name: Optional C{B{name}=NN} (C{str}), 

628 overriding this name. 

629 

630 @return: A L{PhiLam4Tuple}C{(phi, lam, height, datum)}. 

631 

632 @raise TypeError: If B{C{datum}} not a C{Datum}. 

633 ''' 

634 _xinstanceof(_MODS.datums.Datum, datum=datum) 

635 return self._xtend(PhiLam4Tuple, datum, **name) 

636 

637 

638class PhiLam4Tuple(_NamedTuple): # extends -3Tuple 

639 '''4-Tuple C{(phi, lam, height, datum)} with latitude C{phi} in 

640 C{radians[PI_2]}, longitude C{lam} in C{radians[PI]}, C{height} 

641 in C{meter} and L{Datum}. 

642 

643 @note: Using C{phi/lambda} for lat-/longitude in C{radians} 

644 follows Chris Veness' U{convention 

645 <https://www.Movable-Type.co.UK/scripts/latlong.html>}. 

646 ''' 

647 _Names_ = (_phi_, _lam_, _height_, _datum_) 

648 _Units_ = ( Phi, Lam, Height, _Pass) 

649 

650 

651class Point3Tuple(_NamedTuple): 

652 '''3-Tuple C{(x, y, ll)} in C{meter}, C{meter} and C{LatLon}. 

653 ''' 

654 _Names_ = (_x_, _y_, _elel_) 

655 _Units_ = ( Meter, Meter, _Pass) 

656 

657 

658class Points2Tuple(_NamedTuple): # .formy, .latlonBase 

659 '''2-Tuple C{(number, points)} with the C{number} of points 

660 and -possible reduced- C{list} or C{tuple} of C{points}. 

661 ''' 

662 _Names_ = (_number_, _points_) 

663 _Units_ = ( Number_, _Pass) 

664 

665 

666class RD4Tuple(_NamedTuple): # .ltp, pyrdnap 

667 '''4-Tuple C{(minRDx, minRDy, maxRDx, maxRDy)} with the region bounds' 

668 lower-left C{(minRDx, minRDy)} and upper-right C{(maxRDx, maxRDy)} 

669 corner in C{meter}, conventionaly. 

670 ''' 

671 _Names_ = ('minRDx', 'minRDy', 'maxRDx', 'maxRDy') 

672 _Units_ = ( Meter, Meter, Meter, Meter) 

673 

674 def isinside(self, RDx, RDy, eps=0): 

675 '''Are B{C{RDx}} and B{C{RDy}} both inside these C{RD} bounds? 

676 

677 @arg RDx: X coordinate (C{meter}). 

678 @arg RDy: Y coordinate (C{meter}). 

679 @kwarg eps: Over-/undersize the C{RD} bounds (C{meter}). 

680 

681 @return: C{False} if B{C{RDx}} or B{C{RDy}} is outsize 

682 these C{RD} bounds or C{NAN}, C{True} otherwise. 

683 ''' 

684 z = Meter(eps=eps) if eps else 0 

685 return _isinside(Meter(RDx=RDx), Meter(RDy=RDy), z, self) 

686 

687 def resize(self, eps): 

688 '''Get these bounds, over- or undersize by C{B{eps}}. 

689 

690 @arg eps: In- or decrease (C{degrees}). 

691 

692 @return: A L{RD4Tuple}C{(minRDx, minRDy, maxRDx, maxRDy)} 

693 with all 4 bounds resized. 

694 ''' 

695 return _resize4(self, Meter(eps=eps)) 

696 

697 

698class Reverse4Tuple(_NamedTuple, _Convergence): 

699 '''4-Tuple C{(lat, lon, gamma, scale)} with C{lat}- and 

700 C{lon}gitude in C{degrees}, meridian convergence C{gamma} 

701 at point in C{degrees} and the C{scale} of projection at 

702 point C{scalar}. 

703 ''' 

704 _Names_ = (_lat_, _lon_, _gamma_, _scale_) 

705 _Units_ = ( Lat, Lon, Degrees, Scalar) 

706 

707 

708class Triangle7Tuple(_NamedTuple): 

709 '''7-Tuple C{(A, a, B, b, C, c, area)} with interior angles C{A}, 

710 C{B} and C{C} in C{degrees}, spherical sides C{a}, C{b} and C{c} 

711 in C{meter} conventionally and the C{area} of a (spherical) 

712 triangle in I{square} C{meter} conventionally. 

713 ''' 

714 _Names_ = (_A_, _a_, _B_, _b_, _C_, _c_, _area_) 

715 _Units_ = ( Degrees, Meter, Degrees, Meter, Degrees, Meter, Meter2) 

716 

717 

718class Triangle8Tuple(_NamedTuple): 

719 '''8-Tuple C{(A, a, B, b, C, c, D, E)} with interior angles C{A}, 

720 C{B} and C{C}, spherical sides C{a}, C{b} and C{c}, the I{spherical 

721 deficit} C{D} and the I{spherical excess} C{E} of a (spherical) 

722 triangle, all in C{radians}. 

723 ''' 

724 _Names_ = (_A_, _a_, _B_, _b_, _C_, _c_, _D_, _E_) 

725 _Units_ = ( Radians, Radians, Radians, Radians, Radians, Radians, Radians, Radians) 

726 

727 

728class Trilaterate5Tuple(_NamedTuple): # .latlonBase, .nvector 

729 '''5-Tuple C{(min, minPoint, max, maxPoint, n)} with C{min} and C{max} 

730 in C{meter}, the corresponding trilaterated C{minPoint} and C{maxPoint} 

731 as C{LatLon} and the number C{n}. For area overlap, C{min} and C{max} 

732 are the smallest respectively largest overlap found. For perimeter 

733 intersection, C{min} and C{max} represent the closest respectively 

734 farthest intersection margin. Count C{n} is the total number of 

735 trilaterated overlaps or intersections found, C{0, 1, 2...6} with 

736 C{0} meaning concentric. 

737 

738 @see: The C{ellipsoidalKarney-}, C{ellipsoidalVincenty-} and 

739 C{sphericalTrigonometry.LatLon.trilaterate5} method for further 

740 details on corner cases, like concentric or single trilaterated 

741 results. 

742 ''' 

743 _Names_ = (min.__name__, 'minPoint', max.__name__, 'maxPoint', _n_) 

744 _Units_ = (Meter, _Pass, Meter, _Pass, Number_) 

745 

746 

747class UtmUps2Tuple(_NamedTuple): # .epsg.py 

748 '''2-Tuple C{(zone, hemipole)} as C{int} and C{str}, where 

749 C{zone} is C{1..60} for UTM or C{0} for UPS and C{hemipole} 

750 C{'N'|'S'} is the UTM hemisphere or the UPS pole. 

751 ''' 

752 _Names_ = (_zone_, _hemipole_) 

753 _Units_ = ( Number_, Str) 

754 

755 

756class UtmUps5Tuple(_NamedTuple): # .mgrs.py, .ups.py, .utm.py, .utmups.py 

757 '''5-Tuple C{(zone, hemipole, easting, northing, band)} as C{int}, 

758 C{str}, C{meter}, C{meter} and C{band} letter, where C{zone} is 

759 C{1..60} for UTM or C{0} for UPS, C{hemipole} C{'N'|'S'} is the UTM 

760 hemisphere or the UPS pole and C{band} is C{""} or the I{longitudinal} 

761 UTM band C{'C'|'D'|..|'W'|'X'} or I{polar} UPS band C{'A'|'B'|'Y'|'Z'}. 

762 ''' 

763 _Names_ = (_zone_, _hemipole_, _easting_, _northing_, _band_) 

764 _Units_ = ( Number_, Str, Easting, Northing, Band) 

765 

766 def __new__(cls, z, h, e, n, B, Error=None, **name): 

767 if Error is not None: 

768 e = Easting( e, Error=Error) 

769 n = Northing(n, Error=Error) 

770 return _NamedTuple.__new__(cls, z, h, e, n, B, **name) 

771 

772 

773class UtmUps8Tuple(_NamedTuple, _Convergence): # .ups, .utm, .utmups 

774 '''8-Tuple C{(zone, hemipole, easting, northing, band, datum, 

775 gamma, scale)} as C{int}, C{str}, C{meter}, C{meter}, C{band} 

776 letter, C{Datum}, C{degrees} and C{scalar}, where C{zone} is 

777 C{1..60} for UTM or C{0} for UPS, C{hemipole} C{'N'|'S'} is 

778 the UTM hemisphere or the UPS pole and C{band} is C{""} or 

779 the I{longitudinal} UTM band C{'C'|'D'|..|'W'|'X'} or 

780 I{polar} UPS band C{'A'|'B'|'Y'|'Z'}. 

781 ''' 

782 _Names_ = (_zone_, _hemipole_, _easting_, _northing_, 

783 _band_, _datum_, _gamma_, _scale_) 

784 _Units_ = ( Number_, Str, Easting, Northing, 

785 Band, _Pass, Degrees, Scalar) 

786 

787 def __new__(cls, z, h, e, n, B, d, g, s, Error=None, **name): # PYCHOK 11 args 

788 if Error is not None: 

789 e = Easting( e, Error=Error) 

790 n = Northing(n, Error=Error) 

791 g = Degrees(gamma=g, Error=Error) 

792 s = Scalar(scale=s, Error=Error) 

793 return _NamedTuple.__new__(cls, z, h, e, n, B, d, g, s, **name) 

794 

795 

796class UtmUpsLatLon5Tuple(_NamedTuple): # .ups.py, .utm.py, .utmups.py 

797 '''5-Tuple C{(zone, band, hemipole, lat, lon)} as C{int}, 

798 C{str}, C{str}, C{degrees90} and C{degrees180}, where 

799 C{zone} is C{1..60} for UTM or C{0} for UPS, C{band} is 

800 C{""} or the I{longitudinal} UTM band C{'C'|'D'|..|'W'|'X'} 

801 or I{polar} UPS band C{'A'|'B'|'Y'|'Z'} and C{hemipole} 

802 C{'N'|'S'} is the UTM hemisphere or the UPS pole. 

803 ''' 

804 _Names_ = (_zone_, _band_, _hemipole_, _lat_, _lon_) 

805 _Units_ = ( Number_, Band, Str, Lat, Lon) 

806 

807 def __new__(cls, z, B, h, lat, lon, Error=None, **name): 

808 if Error is not None: 

809 lat = Lat(lat, Error=Error) 

810 lon = Lon(lon, Error=Error) 

811 return _NamedTuple.__new__(cls, z, B, h, lat, lon, **name) 

812 

813 

814class Vector2Tuple(_NamedTuple): 

815 '''2-Tuple C{(x, y)} of (geocentric) components, each in 

816 C{meter} or the same C{units}. 

817 ''' 

818 _Names_ = (_x_, _y_) 

819 _Units_ = ( Scalar, Scalar) 

820 

821 def toCartesian(self, Cartesian, **Cartesian_kwds): 

822 '''Return this C{Vector2Tuple} as a C{Cartesian}. 

823 

824 @arg Cartesian: The C{Cartesian} class to use. 

825 @kwarg Cartesian_kwds: Optional, additional C{Cartesian} 

826 keyword arguments. 

827 

828 @return: The C{B{Cartesian}} instance with C{z=0}. 

829 ''' 

830 return _v2Cls(self.xyz, Cartesian, Cartesian_kwds) 

831 

832 def to3Tuple(self, z=INT0, **name): 

833 '''Extend this L{Vector2Tuple} to a L{Vector3Tuple}. 

834 

835 @kwarg z: The Z component add (C{scalar}). 

836 @kwarg name: Optional C{B{name}=NN} (C{str}), 

837 overriding this name. 

838 

839 @return: A L{Vector3Tuple}C{(x, y, z)}. 

840 

841 @raise ValueError: Invalid B{C{z}}. 

842 ''' 

843 return self._xtend(Vector3Tuple, z, **name) 

844 

845 @property_RO 

846 def xyz(self): 

847 '''Get X, Y and Z=0 components (C{Vector3Tuple}). 

848 ''' 

849 return Vector3Tuple(*self.xyz3) 

850 

851 @property_RO 

852 def xyz3(self): 

853 '''Get X, Y and Z=0 components as C{3-tuple}. 

854 ''' 

855 return self.x, self.y, INT0 

856 

857 

858class Vector3Tuple(_NamedTuple): 

859 '''3-Tuple C{(x, y, z)} of (geocentric) components, all in 

860 C{meter} or the same C{units}. 

861 ''' 

862 _Names_ = (_x_, _y_, _z_) 

863 _Units_ = ( Scalar, Scalar, Scalar) 

864 

865 def toCartesian(self, Cartesian, **Cartesian_kwds): 

866 '''Return this C{Vector3Tuple} as a C{Cartesian}. 

867 

868 @arg Cartesian: The C{Cartesian} class to use. 

869 @kwarg Cartesian_kwds: Optional, additional C{Cartesian} 

870 keyword arguments. 

871 

872 @return: The C{B{Cartesian}} instance. 

873 ''' 

874 return _v2Cls(self, Cartesian, Cartesian_kwds) 

875 

876 def to4Tuple(self, h=INT0, **name): 

877 '''Extend this L{Vector3Tuple} to a L{Vector4Tuple}. 

878 

879 @arg h: The height to add (C{scalar}). 

880 @kwarg name: Optional C{B{name}=NN} (C{str}), 

881 overriding this name. 

882 

883 @return: A L{Vector4Tuple}C{(x, y, z, h)}. 

884 

885 @raise ValueError: Invalid B{C{h}}. 

886 ''' 

887 return self._xtend(Vector4Tuple, h, **name) 

888 

889 @property_RO 

890 def xyz(self): 

891 '''Get X, Y and Z components (C{Vector3Tuple}). 

892 ''' 

893 return self 

894 

895 @property_RO 

896 def xyz3(self): 

897 '''Get X, Y and Z components as C{3-tuple}. 

898 ''' 

899 return tuple(self) 

900 

901 

902class Vector4Tuple(_NamedTuple): # .nvector.py 

903 '''4-Tuple C{(x, y, z, h)} of (geocentric) components, all 

904 in C{meter} or the same C{units}. 

905 ''' 

906 _Names_ = (_x_, _y_, _z_, _h_) 

907 _Units_ = ( Scalar, Scalar, Scalar, Height) 

908 

909 def toCartesian(self, Cartesian, **Cartesian_kwds): 

910 '''Return this C{Vector4Tuple} as a C{Cartesian}. 

911 

912 @arg Cartesian: The C{Cartesian} class to use. 

913 @kwarg Cartesian_kwds: Optional, additional C{Cartesian} 

914 keyword arguments. 

915 

916 @return: The C{B{Cartesian}} instance. 

917 ''' 

918 return _v2Cls(self, Cartesian, Cartesian_kwds) 

919 

920 def to3Tuple(self): 

921 '''Reduce this L{Vector4Tuple} to a L{Vector3Tuple}. 

922 

923 @return: A L{Vector3Tuple}C{(x, y, z)}. 

924 ''' 

925 return self.xyz 

926 

927 @property_RO 

928 def xyz(self): 

929 '''Get X, Y and Z components (L{Vector3Tuple}). 

930 ''' 

931 return Vector3Tuple(*self.xyz) 

932 

933 @property_RO 

934 def xyz3(self): 

935 '''Get X, Y and Z components as C{3-tuple}. 

936 ''' 

937 return tuple(self[:3]) 

938 

939 

940def _isinside(x, y, eps, bounds4): # in pybelbg.__pygeodesy, pybelbg.belbgs 

941 '''(INTERNAL) Is C{x} and C{y} inside a bounds 4-tuple? 

942 

943 @return: C{False} if C{lat} or C{lon} outside or NAN, 

944 C{True} otherwise. 

945 ''' 

946 # _xinstanceof(Bounds4Tuple, LB4Tuple, RD4Tuple, bounds4=bound4) 

947 L, B, R, T = bounds4 

948 return ((L - x) <= eps and (x - R) <= eps and 

949 (B - y) <= eps and (y - T) <= eps) if eps else \ 

950 (L <= x <= R and B <= y <= T) 

951 

952 

953def _resize4(bounds4, eps): # in pybelbg.__pygeodesy 

954 '''(INTERNAL) Resize a bounds 4-tuple. 

955 ''' 

956 # _xinstanceof(Bounds4Tuple, LB4Tuple, RD4Tuple, bounds4=bounds4) 

957 L, B, R, T = bounds4 

958 if eps: 

959 L -= eps 

960 B -= eps 

961 R += eps 

962 T += eps 

963 if L > R: 

964 L = R = (L + R) * _0_5 

965 if B > T: 

966 B = T = (B + T) * _0_5 

967 return bounds4.classof(L, B, R, T, name=typename(bounds4.resize)) 

968 

969 

970def _v2Cls(v, Cls, Cartesian_kwds): # in .vector3d 

971 if issubclassof(Cls, _MODS.cartesianBase.CartesianBase): # _MODS.vector3d.Vector3d) 

972 return Cls(v, **Cartesian_kwds) 

973 raise _TypeError(Cartesian=Cls, **Cartesian_kwds) 

974 

975# **) MIT License 

976# 

977# Copyright (C) 2016-2026 -- mrJean1 at Gmail -- All Rights Reserved. 

978# 

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

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

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

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

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

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

985# 

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

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

988# 

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

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

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

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

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

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

995# OTHER DEALINGS IN THE SOFTWARE.