adapt BDD tests for new postcode table structure

This commit is contained in:
Sarah Hoffmann
2025-12-23 22:26:43 +01:00
parent 354aa07cad
commit 3bcd1aa721
11 changed files with 167 additions and 230 deletions

View File

@@ -8,6 +8,7 @@
A grid describing node placement in an area.
Useful for visually describing geometries.
"""
import re
class Grid:
@@ -44,3 +45,28 @@ class Grid:
def parse_line(self, value):
return [self.parse_point(p) for p in value.split(',')]
def geometry_to_wkt(self, value):
""" Parses the given value into a geometry and returns the WKT.
The value can either be a WKT already or a geometry shortcut
with coordinates or grid points.
"""
if re.fullmatch(r'([A-Z]+)\((.*)\)', value) is not None:
return value # already a WKT
# points
if ',' not in value:
x, y = self.parse_point(value)
return f"POINT({x} {y})"
# linestring
if '(' not in value:
coords = ','.join(' '.join(f"{p:.7f}" for p in pt)
for pt in self.parse_line(value))
return f"LINESTRING({coords})"
# simple polygons
coords = ','.join(' '.join(f"{p:.7f}" for p in pt)
for pt in self.parse_line(value[1:-1]))
return f"POLYGON(({coords}))"

View File

@@ -11,6 +11,7 @@ import random
import string
from .geometry_alias import ALIASES
from .grid import Grid
class PlaceColumn:
@@ -19,7 +20,7 @@ class PlaceColumn:
"""
def __init__(self, grid=None):
self.columns = {'admin_level': 15}
self.grid = grid
self.grid = grid or Grid()
self.geometry = None
def add_row(self, headings, row, force_name):
@@ -91,26 +92,9 @@ class PlaceColumn:
if value.startswith('country:'):
ccode = value[8:].upper()
self.geometry = "ST_SetSRID(ST_Point({}, {}), 4326)".format(*ALIASES[ccode])
elif ',' not in value:
if self.grid:
pt = self.grid.parse_point(value)
else:
pt = value.split(' ')
self.geometry = f"ST_SetSRID(ST_Point({pt[0]}, {pt[1]}), 4326)"
elif '(' not in value:
if self.grid:
coords = ','.join(' '.join(f"{p:.7f}" for p in pt)
for pt in self.grid.parse_line(value))
else:
coords = value
self.geometry = f"'srid=4326;LINESTRING({coords})'::geometry"
else:
if self.grid:
coords = ','.join(' '.join(f"{p:.7f}" for p in pt)
for pt in self.grid.parse_line(value[1:-1]))
else:
coords = value[1:-1]
self.geometry = f"'srid=4326;POLYGON(({coords}))'::geometry"
wkt = self.grid.geometry_to_wkt(value)
self.geometry = f"'srid=4326;{wkt}'::geometry"
def _add_hstore(self, column, key, value):
if column in self.columns:
@@ -120,7 +104,7 @@ class PlaceColumn:
def get_wkt(self):
if self.columns['osm_type'] == 'N' and self.geometry is None:
pt = self.grid.get(str(self.columns['osm_id'])) if self.grid else None
pt = self.grid.get(str(self.columns['osm_id']))
if pt is None:
pt = (random.uniform(-180, 180), random.uniform(-90, 90))