]> git.openstreetmap.org Git - nominatim.git/blob - test/bdd/steps/osm_data.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / bdd / steps / osm_data.py
1 import subprocess
2 import tempfile
3 import random
4 import os
5 from nose.tools import * # for assert functions
6
7 @when(u'loading osm data')
8 def load_osm_file(context):
9
10     # create a OSM file in /tmp and import it
11     with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.opl', delete=False) as fd:
12         fname = fd.name
13         for line in context.text.splitlines():
14             if line.startswith('n') and line.find(' x') < 0:
15                     line += " x%d y%d" % (random.random() * 360 - 180,
16                                           random.random() * 180 - 90)
17             fd.write(line.encode('utf-8'))
18             fd.write(b'\n')
19
20     context.nominatim.run_setup_script('import-data', osm_file=fname,
21                                        osm2pgsql_cache=300)
22
23     ### reintroduce the triggers/indexes we've lost by having osm2pgsql set up place again
24     cur = context.db.cursor()
25     cur.execute("""CREATE TRIGGER place_before_delete BEFORE DELETE ON place
26                     FOR EACH ROW EXECUTE PROCEDURE place_delete()""")
27     cur.execute("""CREATE TRIGGER place_before_insert BEFORE INSERT ON place
28                    FOR EACH ROW EXECUTE PROCEDURE place_insert()""")
29     cur.execute("""CREATE UNIQUE INDEX idx_place_osm_unique on place using btree(osm_id,osm_type,class,type)""")
30     context.db.commit()
31
32     os.remove(fname)
33
34 @when(u'updating osm data')
35 def update_from_osm_file(context):
36     context.nominatim.run_setup_script('create-functions', 'create-partition-functions')
37
38     cur = context.db.cursor()
39     cur.execute("""insert into placex (osm_type, osm_id, class, type, name,
40                    admin_level,  housenumber, street, addr_place, isin, postcode,
41                    country_code, extratags, geometry) select * from place""")
42     context.db.commit()
43     context.nominatim.run_setup_script('index', 'index-noanalyse')
44     context.nominatim.run_setup_script('create-functions', 'create-partition-functions',
45                                        'enable-diff-updates')
46
47     # create a OSM file in /tmp and import it
48     with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.opl', delete=False) as fd:
49         fname = fd.name
50         for line in context.text.splitlines():
51             if line.startswith('n') and line.find(' x') < 0:
52                     line += " x%d y%d" % (random.random() * 360 - 180,
53                                           random.random() * 180 - 90)
54             fd.write(line.encode('utf-8'))
55             fd.write(b'\n')
56
57     context.nominatim.run_update_script(import_diff=fname)
58     os.remove(fname)