1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 A grid describing node placement in an area.
9 Useful for visually describing geometries.
16 def __init__(self, table, step, origin):
28 self.grid[pt_id] = (x, y)
32 def get(self, nodeid):
33 """ Get the coordinates for the given grid node.
35 return self.grid.get(nodeid)
37 def parse_point(self, value):
38 """ Get the coordinates for either a grid node or a full coordinate.
42 return [float(v) for v in value.split(' ', 1)]
44 return self.grid.get(value)
46 def parse_line(self, value):
47 return [self.parse_point(p) for p in value.split(',')]
49 def geometry_to_wkt(self, value):
50 """ Parses the given value into a geometry and returns the WKT.
52 The value can either be a WKT already or a geometry shortcut
53 with coordinates or grid points.
55 if re.fullmatch(r'([A-Z]+)\((.*)\)', value) is not None:
56 return value # already a WKT
60 x, y = self.parse_point(value)
61 return f"POINT({x} {y})"
65 coords = ','.join(' '.join(f"{p:.7f}" for p in pt)
66 for pt in self.parse_line(value))
67 return f"LINESTRING({coords})"
70 coords = ','.join(' '.join(f"{p:.7f}" for p in pt)
71 for pt in self.parse_line(value[1:-1]))
72 return f"POLYGON(({coords}))"