5 from nose.tools import * # for assert functions
10 def __init__(self, context, force_name):
12 self.force_name = force_name
13 self.context = context
16 def add(self, key, value):
17 if hasattr(self, 'set_key_' + key):
18 getattr(self, 'set_key_' + key)(value)
19 elif key.startswith('name+'):
20 self.add_hstore('name', key[5:], value)
21 elif key.startswith('extra+'):
22 self.add_hstore('extratags', key[6:], value)
24 assert_in(key, ('class', 'type', 'street', 'addr_place',
26 self.columns[key] = value
28 def set_key_name(self, value):
29 self.add_hstore('name', 'name', value)
31 def set_key_osm(self, value):
32 assert_in(value[0], 'NRW')
33 ok_(value[1:].isdigit())
35 self.columns['osm_type'] = value[0]
36 self.columns['osm_id'] = int(value[1:])
38 def set_key_admin(self, value):
39 self.columns['admin_level'] = int(value)
41 def set_key_housenr(self, value):
42 self.columns['housenumber'] = value
44 def set_key_cc(self, value):
46 self.columns['country_code'] = value
48 def set_key_geometry(self, value):
49 geom, precision = self.context.osm.parse_geometry(value, self.context.scene)
50 assert_is_not_none(geom)
51 self.geometry = "ST_SetSRID('%s'::geometry, 4326)" % geom
53 def add_hstore(self, column, key, value):
54 if column in self.columns:
55 self.columns[column][key] = value
57 self.columns[column] = { key : value }
59 def db_insert(self, cursor):
60 assert_in('osm_type', self.columns)
61 if self.force_name and 'name' not in self.columns:
62 self.add_hstore('name', 'name', ''.join(random.choice(string.printable)
63 for _ in range(int(random.random()*30))))
65 if self.columns['osm_type'] == 'N' and self.geometry is None:
66 self.geometry = "ST_SetSRID(ST_Point(%f, %f), 4326)" % (
67 random.random()*360 - 180, random.random()*180 - 90)
69 query = 'INSERT INTO place (%s, geometry) values(%s, %s)' % (
70 ','.join(self.columns.keys()),
71 ','.join(['%s' for x in range(len(self.columns))]),
73 cursor.execute(query, list(self.columns.values()))
76 """ Splits a unique identifier for places into its components.
77 As place_ids cannot be used for testing, we use a unique
78 identifier instead that is of the form <osmtype><osmid>[:<class>].
81 id_regex = re.compile(r"(?P<tp>[NRW])(?P<id>\d+)(?P<cls>:\w+)?")
83 def __init__(self, oid):
84 self.typ = self.oid = self.cls = None
87 m = self.id_regex.fullmatch(oid)
88 assert_is_not_none(m, "ID '%s' not of form <osmtype><osmid>[:<class>]" % oid)
90 self.typ = m.group('tp')
91 self.oid = m.group('id')
92 self.cls = m.group('cls')
94 def table_select(self):
95 """ Return where clause and parameter list to select the object
96 from a Nominatim table.
98 where = 'osm_type = %s and osm_id = %s'
99 params = [self.typ, self. oid]
101 if self.cls is not None:
102 where += ' class = %s'
103 params.append(self.cls)
108 @given("the (?P<named>named )?places")
109 def add_data_to_place_table(context, named):
110 cur = context.db.cursor()
111 cur.execute('ALTER TABLE place DISABLE TRIGGER place_before_insert')
112 for r in context.table:
113 col = PlaceColumn(context, named is not None)
119 cur.execute('ALTER TABLE place ENABLE TRIGGER place_before_insert')
125 def import_and_index_data_from_place_table(context):
126 context.nominatim.run_setup_script('create-functions', 'create-partition-functions')
127 cur = context.db.cursor()
129 """insert into placex (osm_type, osm_id, class, type, name, admin_level,
130 housenumber, street, addr_place, isin, postcode, country_code, extratags,
132 select * from place where not (class='place' and type='houses' and osm_type='W')""")
134 """select insert_osmline (osm_id, housenumber, street, addr_place,
135 postcode, country_code, geometry)
136 from place where class='place' and type='houses' and osm_type='W'""")
138 context.nominatim.run_setup_script('index', 'index-noanalyse')
141 @then("placex contains(?P<exact> exactly)?")
142 def check_placex_contents(context, exact):
143 cur = context.db.cursor(cursor_factory=psycopg2.extras.DictCursor)
145 cur.execute('SELECT osm_type, osm_id, class from placex')
146 to_match = [(r[0], r[1], r[2]) for r in cur]
148 for row in context.table:
149 nid = NominatimID(row['object'])
150 where, params = nid.table_select()
151 cur.execute("""SELECT *, ST_AsText(geometry) as geomtxt,
152 ST_X(centroid) as cx, ST_Y(centroid) as cy
153 FROM placex where %s""" % where,
157 for h in row.headings:
160 elif h.startswith('name'):
161 name = h[5:] if h.startswith('name+') else 'name'
162 assert_in(name, res['name'])
163 eq_(res['name'][name], row[h])
164 elif h.startswith('extratags+'):
165 eq_(res['extratags'][h[10:]], row[h])
166 elif h.startswith('centroid'):
167 fac = float(h[9:]) if h.startswith('centroid*') else 1.0
168 x, y = row[h].split(' ')
169 assert_almost_equal(float(x) * fac, res['cx'])
170 assert_almost_equal(float(y) * fac, res['cy'])
172 eq_(row[h], str(res[h]))