1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Functions for computation of centroids.
10 from collections.abc import Collection
13 """ Centroid computation from single points using an online algorithm.
14 More points may be added at any time.
16 Coordinates are internally treated as a 7-digit fixed-point float
26 """ Return the centroid of all points collected so far.
29 raise ValueError("No points available for centroid.")
31 return (float(self.sum_x/self.count)/10000000,
32 float(self.sum_y/self.count)/10000000)
39 def __iadd__(self, other):
40 if isinstance(other, Collection) and len(other) == 2:
41 if all(isinstance(p, (float, int)) for p in other):
43 self.sum_x += int(x * 10000000)
44 self.sum_y += int(y * 10000000)
48 raise ValueError("Can only add 2-element tuples to centroid.")