1

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

3

4 u'''Iterators with options.

5

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

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

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

9 iterated items.

10'''

11

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

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

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

15 _TypeError, _ValueError

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

17 _latlon_, _points_, _too_

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

19 from pygeodesy.named import Fmt, _Named, property_RO

20 from pygeodesy.namedTuples import Point3Tuple, Points2Tuple

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

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

23 from pygeodesy.units import Int, Radius

24 from pygeodesy.utily import degrees2m, _Wrap, _1_0

25

26__all__ = _ALL_LAZY.iters

27__version__ = '23.05.04'

28

29_items_ = 'items'

30_iterNumpy2len = 1 # adjustable for testing purposes

31_NOTHING = object() # unique

32

33

34 class _BaseIter(_Named):

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

36

37 @see: Luciano Ramalho, "Fluent Python", page 418+, O'Reilly, 2016.

38 '''

39 _closed = True

40 _copies = ()

41 _dedup = False

42 _Error = LenError

43 _items = None

44 _len = 0

45 _loop = ()

46 _looped = False

47 _name = _items_

48 _prev = _NOTHING

49 _wrap = False

50

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

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

53

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

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

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

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

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

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

60

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

62

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

64 '''

65 if dedup:

66 self._dedup = True

67 if issubclassof(Error, Exception):

68 self._Error = Error

69 if name:

70 self.rename(name)

71

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

73 self._items = items

74 elif _MODS.booleans.isBoolean(items):

75 raise _TypeError(points=_composite_)

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

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

78 self._iter = iter(items)

79 self._indx = -1

80 if Int(loop) > 0:

81 try:

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

83 if self.loop != loop:

84 raise RuntimeError # force Error

85 except (RuntimeError, StopIteration):

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

87

88 @property_RO

89 def copies(self):

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

91 '''

92 cs = self._copies

93 if cs:

94 self._copies = ()

95 return cs

96

97 @property_RO

98 def dedup(self):

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

100 '''

101 return self._dedup

102

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

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

105

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

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

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

109 '''

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

111 yield self._indx, item

112

113 def __getitem__(self, index):

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

115

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

117 '''

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

119 try: # Luciano Ramalho, "Fluent Python", page 293+, O'Reilly, 2016.

120 if isinstance(index, slice):

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

122 else:

123 return t[index]

124 except IndexError as x:

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

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

127

128 def __iter__(self): # PYCHOK no cover

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

130 '''

131 # Luciano Ramalho, "Fluent Python", page 421, O'Reilly, 2016.

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

133

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

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

136

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

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

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

140

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

142 '''

143 if closed and not self.loop:

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

145

146 if copies:

147 if self._items:

148 self._copies = self._items

149 self._items = _copy = None

150 else:

151 self._copies = list(self._loop)

152 _copy = self._copies.append

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

154 self._items = _copy = None

155

156 self._closed = closed

157 self._looped = False

158 if self._iter:

159 try:

160 _next_ = self.next_

161 if _copy:

162 while True:

163 item = _next_(dedup=dedup)

164 _copy(item)

165 yield item

166 else:

167 while True:

168 yield _next_(dedup=dedup)

169 except StopIteration:

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

171

172 def __len__(self):

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

174 '''

175 return self._len

176

177 @property_RO

178 def loop(self):

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

180 '''

181 return len(self._loop)

182

183 @property_RO

184 def looped(self):

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

186 '''

187 return self._looped

188

189 @property_RO

190 def next(self):

191 '''Get the next item.

192 '''

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

194

195# __next__ # NO __next__ AND __iter__ ... see Ramalho, page 426

196

197 def next_(self, dedup=False):

198 '''Return the next item.

199

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

201 '''

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

203

204 def _next(self, dedup):

205 '''Return the next item, regardless.

206

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

208 '''

209 try:

210 self._indx += 1

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

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

213 return item

214 except StopIteration:

215 pass

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

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

218 self._indx = 0

219 self._iter = iter(self._loop)

220 self._loop = ()

221 self._looped = True

222 return next(self._iter)

223

224 def _next_dedup(self):

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

226 '''

227 prev = self._prev

228 item = self._next(True)

229 if prev is not _NOTHING:

230 while item == prev:

231 item = self._next(True)

232 return item

233

234

235 class PointsIter(_BaseIter):

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

237 '''

238 _base = None

239 _Error = PointsError

240

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

242 '''New L{PointsIter} iterator.

243

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

245 etc. (C{point}s).

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

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

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

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

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

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

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

253

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

255

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

257 '''

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

259

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

261 self._base = base

262 if wrap:

263 self._wrap = True

264

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

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

267

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

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

270

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

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

273

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

275 '''

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

277 yield self._indx, p

278

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

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

281

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

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

284

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

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

287

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

289 '''

290 if self._base:

291 _oth = self._base.others

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

293 else:

294 _oth = _fmt = None

295

296 w = self._wrap # and _Wrap.normal is not None

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

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

299 if _oth:

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

301 yield _Wrap.point(p) if w else p

302 n += 1

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

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

305

306

307 class LatLon2PsxyIter(PointsIter):

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

309 '''

310 _deg2m = None

311 _radius = None # keep degrees

312 _wrap = True

313

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

315 dedup=False, name=_latlon_):

316 '''New L{LatLon2PsxyIter} iterator.

317

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

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

320

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

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

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

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

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

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

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

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

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

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

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

332

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

334

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

336 '''

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

338 if not wrap:

339 self._wrap = False

340 if radius:

341 self._radius = r = Radius(radius)

342 self._deg2m = degrees2m(_1_0, r)

343

344 def __getitem__(self, index):

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

346

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

348 '''

349 ll = PointsIter.__getitem__(self, index)

350 if isinstance(index, slice):

351 return map2(self._point3Tuple, ll)

352 else:

353 return self._point3Tuple(ll)

354

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

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

357

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

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

360

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

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

363

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

365 '''

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

367

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

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

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

371

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

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

374

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

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

377

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

379 '''

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

381 _p3 = self._point3Tuple

382 else:

383 def _p3(ll): # PYCHOK redef

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

385

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

387 yield _p3(ll)

388

389 def _point3Tuple(self, ll):

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

391 '''

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

393 d = self._deg2m

394 if d: # convert degrees

395 x *= d

396 y *= d

397 return Point3Tuple(x, y, ll)

398

399

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

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

402 '''

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

404

405

406 def isNumpy2(obj):

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

408

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

410

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

412 instance, C{False} otherwise.

413 '''

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

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

416

417

418 def isPoints2(obj):

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

420

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

422

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

424 instance, C{False} otherwise.

425 '''

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

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

428

429

430 def isTuple2(obj):

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

432

433 @arg obj: The object (any).

434

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

436 instance, C{False} otherwise.

437 '''

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

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

440

441

442 def iterNumpy2(obj):

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

444 the threshold.

445

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

447

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

449 '''

450 try:

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

452 except TypeError:

453 return False

454

455

456 def iterNumpy2over(n=None):

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

458

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

460

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

462

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

464 '''

465 global _iterNumpy2len

466 p = _iterNumpy2len

467 if n is not None:

468 try:

469 i = int(n)

470 if i > 0:

471 _iterNumpy2len = i

472 else:

473 raise ValueError

474 except (TypeError, ValueError):

475 raise _ValueError(n=n)

476 return p

477

478

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

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

481

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

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

484 ignoring any duplicate or closing final

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

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

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

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

489

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

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

492

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

494

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

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

497 '''

498 if _MODS.booleans.isBoolean(points):

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

500

501 n, points = len2(points)

502

503 if closed:

504 # remove duplicate or closing final points

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

506 n -= 1

507 # XXX following line is unneeded if points

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

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

510

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

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

513

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

515 for i in range(n):

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

517

518 return Points2Tuple(n, points)

519

520

521__all__ += _ALL_DOCS(_BaseIter)

522

523# **) MIT License

524#

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

526#

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

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

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

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

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

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

533#

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

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

536#

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

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

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

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

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

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

543# OTHER DEALINGS IN THE SOFTWARE.