]> git.openstreetmap.org Git - nominatim.git/blob - test/bdd/utils/grid.py
release 5.1.0.post5
[nominatim.git] / test / bdd / utils / grid.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 A grid describing node placement in an area.
9 Useful for visually describing geometries.
10 """
11
12
13 class Grid:
14
15     def __init__(self, table, step, origin):
16         if step is None:
17             step = 0.00001
18         if origin is None:
19             origin = (0.0, 0.0)
20         self.grid = {}
21
22         y = origin[1]
23         for line in table:
24             x = origin[0]
25             for pt_id in line:
26                 if pt_id:
27                     self.grid[pt_id] = (x, y)
28                 x += step
29             y += step
30
31     def get(self, nodeid):
32         """ Get the coordinates for the given grid node.
33         """
34         return self.grid.get(nodeid)
35
36     def parse_point(self, value):
37         """ Get the coordinates for either a grid node or a full coordinate.
38         """
39         value = value.strip()
40         if ' ' in value:
41             return [float(v) for v in value.split(' ', 1)]
42
43         return self.grid.get(value)
44
45     def parse_line(self, value):
46         return [self.parse_point(p) for p in value.split(',')]