1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Functions for computation of centroids.
10 from typing import Tuple, Any
11 from collections.abc import Collection
14 """ Centroid computation from single points using an online algorithm.
15 More points may be added at any time.
17 Coordinates are internally treated as a 7-digit fixed-point float
21 def __init__(self) -> None:
26 def centroid(self) -> Tuple[float, float]:
27 """ Return the centroid of all points collected so far.
30 raise ValueError("No points available for centroid.")
32 return (float(self.sum_x/self.count)/10000000,
33 float(self.sum_y/self.count)/10000000)
36 def __len__(self) -> int:
40 def __iadd__(self, other: Any) -> 'PointsCentroid':
41 if isinstance(other, Collection) and len(other) == 2:
42 if all(isinstance(p, (float, int)) for p in other):
44 self.sum_x += int(x * 10000000)
45 self.sum_y += int(y * 10000000)
49 raise ValueError("Can only add 2-element tuples to centroid.")