Coverage for pygeodesy/iters.py: 97%

203 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-05-15 16:36 -0400

1 

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

3 

4u'''Iterators with options. 

5 

6Iterator classes L{LatLon2PsxyIter} and L{PointsIter} to iterate 

7over iterables, lists, sets, tuples, etc. with optional loop-back to 

8the initial items, skipping of duplicate items and copying of the 

9iterated items. 

10''' 

11 

12from pygeodesy.basics import islistuple, issubclassof, len2, \ 

13 map2, _passarg 

14# from pygeodesy.constants import _1_0 # from .utily 

15from pygeodesy.errors import _IndexError, LenError, PointsError, \ 

16 _TypeError, _ValueError 

17# from pygeodesy.internals import _passarg # from .basics 

18from pygeodesy.interns import NN, _0_, _composite_, _few_, \ 

19 _latlon_, _points_, _too_ 

20from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS 

21from pygeodesy.named import Fmt, _Named, property_RO 

22from pygeodesy.namedTuples import Point3Tuple, Points2Tuple 

23# from pygeodesy.props import property_RO # from .named 

24# from pygeodesy.streprs import Fmt # from .named 

25from pygeodesy.units import Int, Radius 

26from pygeodesy.utily import degrees2m, _Wrap, _1_0 

27 

28__all__ = _ALL_LAZY.iters 

29__version__ = '23.12.14' 

30 

31_items_ = 'items' 

32_iterNumpy2len = 1 # adjustable for testing purposes 

33_NOTHING = object() # unique 

34 

35 

36class _BaseIter(_Named): 

37 '''(INTERNAL) Iterator over items with loop-back and de-duplication. 

38 

39 @see: Luciano Ramalho, "Fluent Python", O'Reilly, 2016 p. 418+, 2022 p. 600+ 

40 ''' 

41 _closed = True 

42 _copies = () 

43 _dedup = False 

44 _Error = LenError 

45 _items = None 

46 _len = 0 

47 _loop = () 

48 _looped = False 

49 _name = _items_ 

50 _prev = _NOTHING 

51 _wrap = False 

52 

53 def __init__(self, items, loop=0, dedup=False, Error=None, name=NN): 

54 '''New iterator over an iterable of B{C{items}}. 

55 

56 @arg items: Iterable (any C{type}, except composites). 

57 @kwarg loop: Number of loop-back items, also initial enumerate and 

58 iterate index (non-negative C{int}). 

59 @kwarg dedup: Skip duplicate items (C{bool}). 

60 @kwarg Error: Error to raise (L{LenError}). 

61 @kwarg name: Optional name (C{str}). 

62 

63 @raise Error: Invalid B{C{items}} or sufficient number of B{C{items}}. 

64 

65 @raise TypeError: Composite B{C{items}}. 

66 ''' 

67 if dedup: 

68 self._dedup = True 

69 if issubclassof(Error, Exception): 

70 self._Error = Error 

71 if name: 

72 self.rename(name) 

73 

74 if islistuple(items): # range in Python 2 

75 self._items = items 

76 elif _MODS.booleans.isBoolean(items): 

77 raise _TypeError(points=_composite_) 

78# XXX if hasattr(items, 'next') or hasattr(items, '__length_hint__'): 

79# XXX # handle reversed, iter, etc. items types 

80 self._iter = iter(items) 

81 self._indx = -1 

82 if Int(loop) > 0: 

83 try: 

84 self._loop = tuple(self.next for _ in range(loop)) 

85 if self.loop != loop: 

86 raise RuntimeError # force Error 

87 except (RuntimeError, StopIteration): 

88 raise self._Error(self.name, self.loop, txt=_too_(_few_)) 

89 

90 @property_RO 

91 def copies(self): 

92 '''Get the saved copies, if any (C{tuple} or C{list}) and only I{once}. 

93 ''' 

94 cs = self._copies 

95 if cs: 

96 self._copies = () 

97 return cs 

98 

99 @property_RO 

100 def dedup(self): 

101 '''Get the de-duplication setting (C{bool}). 

102 ''' 

103 return self._dedup 

104 

105 def enumerate(self, closed=False, copies=False, dedup=False): 

106 '''Yield all items, each as a 2-tuple C{(index, item)}. 

107 

108 @kwarg closed: Loop back to the first B{C{point(s)}}. 

109 @kwarg copies: Make a copy of all B{C{items}} (C{bool}). 

110 @kwarg dedup: Set de-duplication in loop-back (C{bool}). 

111 ''' 

112 for item in self.iterate(closed=closed, copies=copies, dedup=dedup): 

113 yield self._indx, item 

114 

115 def __getitem__(self, index): 

116 '''Get the item(s) at the given B{C{index}} or C{slice}. 

117 

118 @raise IndexError: Invalid B{C{index}}, beyond B{C{loop}}. 

119 ''' 

120 t = self._items or self._copies or self._loop 

121 try: # Luciano Ramalho, "Fluent Python", O'Reilly, 2016 p. 293+, 2022 p. 408+ 

122 if isinstance(index, slice): 

123 return t[index.start:index.stop:index.step] 

124 else: 

125 return t[index] 

126 except IndexError as x: 

127 t = Fmt.SQUARE(self.name, index) 

128 raise _IndexError(str(x), txt=t, cause=x) 

129 

130 def __iter__(self): # PYCHOK no cover 

131 '''Make this iterator C{iterable}. 

132 ''' 

133 # Luciano Ramalho, "Fluent Python", O'Reilly, 2016 p. 421, 2022 p. 604+ 

134 return self.iterate() # XXX or self? 

135 

136 def iterate(self, closed=False, copies=False, dedup=False): 

137 '''Yield all items, each as C{item}. 

138 

139 @kwarg closed: Loop back to the first B{C{point(s)}}. 

140 @kwarg copies: Make a copy of all B{C{items}} (C{bool}). 

141 @kwarg dedup: Set de-duplication in loop-back (C{bool}). 

142 

143 @raise Error: Using C{B{closed}=True} without B{C{loop}}-back. 

144 ''' 

145 if closed and not self.loop: 

146 raise self._Error(closed=closed, loop=self.loop) 

147 

148 if copies: 

149 if self._items: 

150 self._copies = self._items 

151 self._items = _copy = None 

152 else: 

153 self._copies = list(self._loop) 

154 _copy = self._copies.append 

155 else: # del B{C{items}} reference 

156 self._items = _copy = None 

157 

158 self._closed = closed 

159 self._looped = False 

160 if self._iter: 

161 try: 

162 _next_ = self.next_ 

163 if _copy: 

164 while True: 

165 item = _next_(dedup=dedup) 

166 _copy(item) 

167 yield item 

168 else: 

169 while True: 

170 yield _next_(dedup=dedup) 

171 except StopIteration: 

172 self._iter = () # del self._iter, prevent re-iterate 

173 

174 def __len__(self): 

175 '''Get the number of items seen so far. 

176 ''' 

177 return self._len 

178 

179 @property_RO 

180 def loop(self): 

181 '''Get the B{C{loop}} setting (C{int}), C{0} for non-loop-back. 

182 ''' 

183 return len(self._loop) 

184 

185 @property_RO 

186 def looped(self): 

187 '''In this C{Iter}ator in loop-back? (C{bool}). 

188 ''' 

189 return self._looped 

190 

191 @property_RO 

192 def next(self): 

193 '''Get the next item. 

194 ''' 

195 return self._next_dedup() if self._dedup else self._next(False) 

196 

197# __next__ # NO __next__ AND __iter__ ... see Luciano Ramalho, 

198# # "Fluent Python", O'Reilly, 2016 p. 426, 2022 p. 610 

199 

200 def next_(self, dedup=False): 

201 '''Return the next item. 

202 

203 @kwarg dedup: Set de-duplication for loop-back (C{bool}). 

204 ''' 

205 return self._next_dedup() if self._dedup else self._next(dedup) 

206 

207 def _next(self, dedup): 

208 '''Return the next item, regardless. 

209 

210 @arg dedup: Set de-duplication for loop-back (C{bool}). 

211 ''' 

212 try: 

213 self._indx += 1 

214 self._len = self._indx # max(_len, _indx) 

215 self._prev = item = next(self._iter) 

216 return item 

217 except StopIteration: 

218 pass 

219 if self._closed and self._loop: # loop back 

220 self._dedup = bool(dedup or self._dedup) 

221 self._indx = 0 

222 self._iter = iter(self._loop) 

223 self._loop = () 

224 self._looped = True 

225 return next(self._iter) 

226 

227 def _next_dedup(self): 

228 '''Return the next item, different from the previous one. 

229 ''' 

230 prev = self._prev 

231 item = self._next(True) 

232 if prev is not _NOTHING: 

233 while item == prev: 

234 item = self._next(True) 

235 return item 

236 

237 

238class PointsIter(_BaseIter): 

239 '''Iterator for C{points} with optional loop-back and copies. 

240 ''' 

241 _base = None 

242 _Error = PointsError 

243 

244 def __init__(self, points, loop=0, base=None, dedup=False, wrap=False, name=NN): 

245 '''New L{PointsIter} iterator. 

246 

247 @arg points: C{Iterable} or C{list}, C{sequence}, C{set}, C{tuple}, 

248 etc. (C{point}s). 

249 @kwarg loop: Number of loop-back points, also initial C{enumerate} and 

250 C{iterate} index (non-negative C{int}). 

251 @kwarg base: Optional B{C{points}} instance for type checking (C{any}). 

252 @kwarg dedup: Skip duplicate points (C{bool}). 

253 @kwarg wrap: If C{True}, wrap or I{normalize} the enum-/iterated 

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

255 @kwarg name: Optional name (C{str}). 

256 

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

258 

259 @raise TypeError: Some B{C{points}} are not B{C{base}}. 

260 ''' 

261 _BaseIter.__init__(self, points, loop=loop, dedup=dedup, name=name or _points_) 

262 

263 if base and not (isNumpy2(points) or isTuple2(points)): 

264 self._base = base 

265 if wrap: 

266 self._wrap = True 

267 

268 def enumerate(self, closed=False, copies=False): # PYCHOK signature 

269 '''Iterate and yield each point as a 2-tuple C{(index, point)}. 

270 

271 @kwarg closed: Loop back to the first B{C{point(s)}}, de-dup'ed (C{bool}). 

272 @kwarg copies: Save a copy of all B{C{points}} (C{bool}). 

273 

274 @raise PointsError: Insufficient number of B{C{points}} or using 

275 C{B{closed}=True} without B{C{loop}}-back. 

276 

277 @raise TypeError: Some B{C{points}} are not B{C{base}}-compatible. 

278 ''' 

279 for p in self.iterate(closed=closed, copies=copies): 

280 yield self._indx, p 

281 

282 def iterate(self, closed=False, copies=False): # PYCHOK signature 

283 '''Iterate through all B{C{points}} starting at index C{loop}. 

284 

285 @kwarg closed: Loop back to the first B{C{point(s)}}, de-dup'ed (C{bool}). 

286 @kwarg copies: Save a copy of all B{C{points}} (C{bool}). 

287 

288 @raise PointsError: Insufficient number of B{C{points}} or using 

289 C{B{closed}=True} without B{C{loop}}-back. 

290 

291 @raise TypeError: Some B{C{points}} are not B{C{base}}-compatible. 

292 ''' 

293 if self._base: 

294 _oth = self._base.others 

295 _fmt = Fmt.SQUARE(points=0).replace 

296 else: 

297 _oth = _fmt = None 

298 

299 n = self.loop if self._iter else 0 

300 _p = _Wrap.point if self._wrap else _passarg # and _Wrap.normal is not None 

301 for p in _BaseIter.iterate(self, closed=closed, copies=copies, dedup=closed): 

302 if _oth: 

303 _oth(p, name=_fmt(_0_, str(self._indx)), up=2) 

304 yield _p(p) 

305 n += 1 

306 if n < (4 if closed else 2): 

307 raise self._Error(self.name, n, txt=_too_(_few_)) 

308 

309 

310class LatLon2PsxyIter(PointsIter): 

311 '''Iterate and convert for C{points} with optional loop-back and copies. 

312 ''' 

313 _deg2m = None 

314 _radius = None # keep degrees 

315 _wrap = True 

316 

317 def __init__(self, points, loop=0, base=None, wrap=True, radius=None, 

318 dedup=False, name=_latlon_): 

319 '''New L{LatLon2PsxyIter} iterator. 

320 

321 @note: The C{LatLon} latitude is considered the I{pseudo-y} and 

322 longitude the I{pseudo-x} coordinate, like L{LatLon2psxy}. 

323 

324 @arg points: C{Iterable} or C{list}, C{sequence}, C{set}, C{tuple}, 

325 etc. (C{LatLon}[]). 

326 @kwarg loop: Number of loop-back points, also initial C{enumerate} and 

327 C{iterate} index (non-negative C{int}). 

328 @kwarg base: Optional B{C{points}} instance for type checking (C{any}). 

329 @kwarg wrap: If C{True}, wrap or I{normalize} the enum-/iterated 

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

331 @kwarg radius: Mean earth radius (C{meter}) for conversion from 

332 C{degrees} to C{meter} (or C{radians} if C{B{radius}=1}). 

333 @kwarg dedup: Skip duplicate points (C{bool}). 

334 @kwarg name: Optional name (C{str}). 

335 

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

337 

338 @raise TypeError: Some B{C{points}} are not B{C{base}}-compatible. 

339 ''' 

340 PointsIter.__init__(self, points, loop=loop, base=base, dedup=dedup, name=name) 

341 if not wrap: 

342 self._wrap = False 

343 if radius: 

344 self._radius = r = Radius(radius) 

345 self._deg2m = degrees2m(_1_0, r) 

346 

347 def __getitem__(self, index): 

348 '''Get the point(s) at the given B{C{index}} or C{slice}. 

349 

350 @raise IndexError: Invalid B{C{index}}, beyond B{C{loop}}. 

351 ''' 

352 ll = PointsIter.__getitem__(self, index) 

353 if isinstance(index, slice): 

354 return map2(self._point3Tuple, ll) 

355 else: 

356 return self._point3Tuple(ll) 

357 

358 def enumerate(self, closed=False, copies=False): # PYCHOK signature 

359 '''Iterate and yield each point as a 2-tuple C{(index, L{Point3Tuple})}. 

360 

361 @kwarg closed: Loop back to the first B{C{point(s)}}, de-dup'ed (C{bool}). 

362 @kwarg copies: Save a copy of all B{C{points}} (C{bool}). 

363 

364 @raise PointsError: Insufficient number of B{C{points}} or using 

365 C{B{closed}=True} without B{C{loop}}-back. 

366 

367 @raise TypeError: Some B{C{points}} are not B{C{base}}-compatible. 

368 ''' 

369 return PointsIter.enumerate(self, closed=closed, copies=copies) 

370 

371 def iterate(self, closed=False, copies=False): # PYCHOK signature 

372 '''Iterate the B{C{points}} starting at index B{C{loop}} and 

373 yield each as a L{Point3Tuple}C{(x, y, ll)}. 

374 

375 @kwarg closed: Loop back to the first B{C{point(s)}}, de-dup'ed (C{bool}). 

376 @kwarg copies: Save a copy of all B{C{points}} (C{bool}). 

377 

378 @raise PointsError: Insufficient number of B{C{points}} or using 

379 C{B{closed}=True} without B{C{loop}}-back. 

380 

381 @raise TypeError: Some B{C{points}} are not B{C{base}}-compatible. 

382 ''' 

383 if self._deg2m not in (None, _1_0): 

384 _p3 = self._point3Tuple 

385 else: 

386 def _p3(ll): # PYCHOK redef 

387 return Point3Tuple(ll.lon, ll.lat, ll) 

388 

389 for ll in PointsIter.iterate(self, closed=closed, copies=copies): 

390 yield _p3(ll) 

391 

392 def _point3Tuple(self, ll): 

393 '''(INTERNAL) Create a L{Point3Tuple} for point B{C{ll}}. 

394 ''' 

395 x, y = ll.lon, ll.lat # note, x, y = lon, lat 

396 d = self._deg2m 

397 if d: # convert degrees 

398 x *= d 

399 y *= d 

400 return Point3Tuple(x, y, ll) 

401 

402 

403def _imdex2(closed, n): # PYCHOK by .clipy 

404 '''(INTERNAL) Return first and second index of C{range(B{n})}. 

405 ''' 

406 return (n-1, 0) if closed else (0, 1) 

407 

408 

409def isNumpy2(obj): 

410 '''Check for a B{C{Numpy2LatLon}} points wrapper. 

411 

412 @arg obj: The object (any C{type}). 

413 

414 @return: C{True} if B{C{obj}} is a B{C{Numpy2LatLon}} 

415 instance, C{False} otherwise. 

416 ''' 

417 # isinstance(self, (Numpy2LatLon, ...)) 

418 return getattr(obj, isNumpy2.__name__, False) 

419 

420 

421def isPoints2(obj): 

422 '''Check for a B{C{LatLon2psxy}} points wrapper. 

423 

424 @arg obj: The object (any C{type}). 

425 

426 @return: C{True} if B{C{obj}} is a B{C{LatLon2psxy}} 

427 instance, C{False} otherwise. 

428 ''' 

429 # isinstance(self, (LatLon2psxy, ...)) 

430 return getattr(obj, isPoints2.__name__, False) 

431 

432 

433def isTuple2(obj): 

434 '''Check for a B{C{Tuple2LatLon}} points wrapper. 

435 

436 @arg obj: The object (any). 

437 

438 @return: C{True} if B{C{obj}} is a B{C{Tuple2LatLon}} 

439 instance, C{False} otherwise. 

440 ''' 

441 # isinstance(self, (Tuple2LatLon, ...)) 

442 return getattr(obj, isTuple2.__name__, False) 

443 

444 

445def iterNumpy2(obj): 

446 '''Iterate over Numpy2 wrappers or other sequences exceeding 

447 the threshold. 

448 

449 @arg obj: Points array, list, sequence, set, etc. (any). 

450 

451 @return: C{True} do, C{False} don't iterate. 

452 ''' 

453 try: 

454 return isNumpy2(obj) or len(obj) > _iterNumpy2len 

455 except TypeError: 

456 return False 

457 

458 

459def iterNumpy2over(n=None): 

460 '''Get or set the L{iterNumpy2} threshold. 

461 

462 @kwarg n: Optional, new threshold (C{int}). 

463 

464 @return: Previous threshold (C{int}). 

465 

466 @raise ValueError: Invalid B{C{n}}. 

467 ''' 

468 global _iterNumpy2len 

469 p = _iterNumpy2len 

470 if n is not None: 

471 try: 

472 i = int(n) 

473 if i > 0: 

474 _iterNumpy2len = i 

475 else: 

476 raise ValueError 

477 except (TypeError, ValueError): 

478 raise _ValueError(n=n) 

479 return p 

480 

481 

482def points2(points, closed=True, base=None, Error=PointsError): 

483 '''Check a path or polygon represented by points. 

484 

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

486 @kwarg closed: Optionally, consider the polygon closed, 

487 ignoring any duplicate or closing final 

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

489 @kwarg base: Optionally, check all B{C{points}} against 

490 this base class, if C{None} don't check. 

491 @kwarg Error: Exception to raise (C{ValueError}). 

492 

493 @return: A L{Points2Tuple}C{(number, points)} with the number 

494 of points and the points C{list} or C{tuple}. 

495 

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

497 

498 @raise TypeError: Some B{C{points}} are not B{C{base}} 

499 compatible or composite B{C{points}}. 

500 ''' 

501 if _MODS.booleans.isBoolean(points): 

502 raise Error(points=points, txt=_composite_) 

503 

504 n, points = len2(points) 

505 

506 if closed: 

507 # remove duplicate or closing final points 

508 while n > 1 and points[n-1] in (points[0], points[n-2]): 

509 n -= 1 

510 # XXX following line is unneeded if points 

511 # are always indexed as ... i in range(n) 

512 points = points[:n] # XXX numpy.array slice is a view! 

513 

514 if n < (3 if closed else 1): 

515 raise Error(points=n, txt=_too_(_few_)) 

516 

517 if base and not (isNumpy2(points) or isTuple2(points)): 

518 for i in range(n): 

519 base.others(points[i], name=Fmt.SQUARE(points=i)) 

520 

521 return Points2Tuple(n, points) 

522 

523 

524__all__ += _ALL_DOCS(_BaseIter) 

525 

526# **) MIT License 

527# 

528# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved. 

529# 

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

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

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

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

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

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

536# 

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

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

539# 

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

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

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

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

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

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

546# OTHER DEALINGS IN THE SOFTWARE.