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 Helper classes for filling the place table.
14 """ Helper class to collect contents from a behave table row and
15 insert it into the place table.
17 def __init__(self, context):
18 self.columns = {'admin_level' : 15}
19 self.context = context
22 def add_row(self, row, force_name):
23 """ Parse the content from the given behave row as place column data.
25 for name, value in zip(row.headings, row.cells):
26 self._add(name, value)
28 assert 'osm_type' in self.columns, "osm column missing"
30 if force_name and 'name' not in self.columns:
31 self._add_hstore('name', 'name',
32 ''.join(random.choice(string.printable)
33 for _ in range(int(random.random()*30))))
37 def _add(self, key, value):
38 if hasattr(self, '_set_key_' + key):
39 getattr(self, '_set_key_' + key)(value)
40 elif key.startswith('name+'):
41 self._add_hstore('name', key[5:], value)
42 elif key.startswith('extra+'):
43 self._add_hstore('extratags', key[6:], value)
44 elif key.startswith('addr+'):
45 self._add_hstore('address', key[5:], value)
46 elif key in ('name', 'address', 'extratags'):
47 self.columns[key] = eval('{' + value + '}')
49 assert key in ('class', 'type'), "Unknown column '{}'.".format(key)
50 self.columns[key] = None if value == '' else value
52 def _set_key_name(self, value):
53 self._add_hstore('name', 'name', value)
55 def _set_key_osm(self, value):
56 assert value[0] in 'NRW' and value[1:].isdigit(), \
57 "OSM id needs to be of format <NRW><id>."
59 self.columns['osm_type'] = value[0]
60 self.columns['osm_id'] = int(value[1:])
62 def _set_key_admin(self, value):
63 self.columns['admin_level'] = int(value)
65 def _set_key_housenr(self, value):
67 self._add_hstore('address', 'housenumber', value)
69 def _set_key_postcode(self, value):
71 self._add_hstore('address', 'postcode', value)
73 def _set_key_street(self, value):
75 self._add_hstore('address', 'street', value)
77 def _set_key_addr_place(self, value):
79 self._add_hstore('address', 'place', value)
81 def _set_key_country(self, value):
83 self._add_hstore('address', 'country', value)
85 def _set_key_geometry(self, value):
86 self.geometry = self.context.osm.parse_geometry(value)
87 assert self.geometry is not None, "Bad geometry: {}".format(value)
89 def _add_hstore(self, column, key, value):
90 if column in self.columns:
91 self.columns[column][key] = value
93 self.columns[column] = {key: value}
95 def db_insert(self, cursor):
96 """ Insert the collected data into the database.
98 if self.columns['osm_type'] == 'N' and self.geometry is None:
99 pt = self.context.osm.grid_node(self.columns['osm_id'])
101 pt = (random.random()*360 - 180, random.random()*180 - 90)
103 self.geometry = "ST_SetSRID(ST_Point(%f, %f), 4326)" % pt
105 assert self.geometry is not None, "Geometry missing"
107 query = 'INSERT INTO place ({}, geometry) values({}, {})'.format(
108 ','.join(self.columns.keys()),
109 ','.join(['%s' for x in range(len(self.columns))]),
111 cursor.execute(query, list(self.columns.values()))