Clusters the elements of objectlist if they are within tolerance.
(self, objectlist: Sequence[Shape])
| 330 | raise NotImplementedError |
| 331 | |
| 332 | def cluster(self, objectlist: Sequence[Shape]) -> List[List[Shape]]: |
| 333 | """ |
| 334 | Clusters the elements of objectlist if they are within tolerance. |
| 335 | """ |
| 336 | key_and_obj = [] |
| 337 | for obj in objectlist: |
| 338 | # Need to handle value errors, such as what occurs when you try to |
| 339 | # access the radius of a straight line |
| 340 | try: |
| 341 | key = self.key(obj) |
| 342 | except ValueError: |
| 343 | # forget about this element and continue |
| 344 | continue |
| 345 | key_and_obj.append((key, obj)) |
| 346 | |
| 347 | key_and_obj.sort(key=lambda x: x[0]) |
| 348 | clustered = [[]] # type: List[List[Shape]] |
| 349 | start = key_and_obj[0][0] |
| 350 | for key, obj in key_and_obj: |
| 351 | if abs(key - start) <= self.tolerance: |
| 352 | clustered[-1].append(obj) |
| 353 | else: |
| 354 | clustered.append([obj]) |
| 355 | start = key |
| 356 | return clustered |
| 357 | |
| 358 | |
| 359 | class RadiusNthSelector(_NthSelector): |