]> git.openstreetmap.org Git - nominatim.git/blob - test/python/utils/test_centroid.py
Merge pull request #2757 from lonvia/filter-postcodes
[nominatim.git] / test / python / utils / test_centroid.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for centroid computation.
9 """
10 import pytest
11
12 from nominatim.utils.centroid import PointsCentroid
13
14 def test_empty_set():
15     c = PointsCentroid()
16
17     with pytest.raises(ValueError, match='No points'):
18         c.centroid()
19
20
21 @pytest.mark.parametrize("centroid", [(0,0), (-1, 3), [0.0000032, 88.4938]])
22 def test_one_point_centroid(centroid):
23     c = PointsCentroid()
24
25     c += centroid
26
27     assert len(c.centroid()) == 2
28     assert c.centroid() == (pytest.approx(centroid[0]), pytest.approx(centroid[1]))
29
30
31 def test_multipoint_centroid():
32     c = PointsCentroid()
33
34     c += (20.0, -10.0)
35     assert c.centroid() == (pytest.approx(20.0), pytest.approx(-10.0))
36     c += (20.2, -9.0)
37     assert c.centroid() == (pytest.approx(20.1), pytest.approx(-9.5))
38     c += (20.2, -9.0)
39     assert c.centroid() == (pytest.approx(20.13333), pytest.approx(-9.333333))
40
41
42 def test_manypoint_centroid():
43     c = PointsCentroid()
44
45     for _ in range(10000):
46         c += (4.564732, -0.000034)
47
48     assert c.centroid() == (pytest.approx(4.564732), pytest.approx(-0.000034))
49
50
51 @pytest.mark.parametrize("param", ["aa", None, 5, [1, 2, 3], (3, None), ("a", 3.9)])
52 def test_add_non_tuple(param):
53     c = PointsCentroid()
54
55     with pytest.raises(ValueError, match='2-element tuples'):
56         c += param