]> git.openstreetmap.org Git - nominatim.git/blob - test/bdd/steps/osm_data.py
c1043baaed69c365e8b4742427d4ac8f9b5732fb
[nominatim.git] / test / bdd / steps / osm_data.py
1 import subprocess
2 import tempfile
3 import random
4 import os
5
6 @given(u'the ([0-9.]+ )?grid')
7 def define_node_grid(context, grid_step):
8     """
9     Define a grid of node positions.
10     Use a table to define the grid. The nodes must be integer ids. Optionally
11     you can give the grid distance. The default is 0.00001 degrees.
12     """
13     if grid_step is not None:
14         grid_step = float(grid_step.strip())
15     else:
16         grid_step = 0.00001
17
18     context.osm.set_grid([context.table.headings] + [list(h) for h in context.table],
19                           grid_step)
20
21
22 @when(u'loading osm data')
23 def load_osm_file(context):
24     """
25     Load the given data into a freshly created test data using osm2pgsql.
26     No further indexing is done.
27
28     The data is expected as attached text in OPL format.
29     """
30     # create a OSM file in /tmp and import it
31     with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.opl', delete=False) as fd:
32         fname = fd.name
33         for line in context.text.splitlines():
34             if line.startswith('n') and line.find(' x') < 0:
35                 coord = context.osm.grid_node(int(line[1:].split(' ')[0]))
36                 if coord is None:
37                     coord = (random.random() * 360 - 180,
38                              random.random() * 180 - 90)
39                 line += " x%f y%f" % coord
40             fd.write(line.encode('utf-8'))
41             fd.write(b'\n')
42
43     context.nominatim.run_setup_script('import-data', osm_file=fname,
44                                        osm2pgsql_cache=300)
45
46     ### reintroduce the triggers/indexes we've lost by having osm2pgsql set up place again
47     cur = context.db.cursor()
48     cur.execute("""CREATE TRIGGER place_before_delete BEFORE DELETE ON place
49                     FOR EACH ROW EXECUTE PROCEDURE place_delete()""")
50     cur.execute("""CREATE TRIGGER place_before_insert BEFORE INSERT ON place
51                    FOR EACH ROW EXECUTE PROCEDURE place_insert()""")
52     cur.execute("""CREATE UNIQUE INDEX idx_place_osm_unique on place using btree(osm_id,osm_type,class,type)""")
53     context.db.commit()
54
55     os.remove(fname)
56
57 @when(u'updating osm data')
58 def update_from_osm_file(context):
59     """
60     Update a database previously populated with 'loading osm data'.
61     Needs to run indexing on the existing data first to yield the correct result.
62
63     The data is expected as attached text in OPL format.
64     """
65     context.nominatim.run_setup_script('create-functions', 'create-partition-functions')
66
67     cur = context.db.cursor()
68     cur.execute("""insert into placex (osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry)
69                      select            osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry from place""")
70     cur.execute(
71         """insert into location_property_osmline (osm_id, address, linegeo)
72              SELECT osm_id, address, geometry from place
73               WHERE class='place' and type='houses' and osm_type='W'
74                     and ST_GeometryType(geometry) = 'ST_LineString'""")
75     context.db.commit()
76     context.nominatim.run_setup_script('index', 'index-noanalyse')
77     context.nominatim.run_setup_script('create-functions', 'create-partition-functions',
78                                        'enable-diff-updates')
79
80     # create a OSM file in /tmp and import it
81     with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.opl', delete=False) as fd:
82         fname = fd.name
83         for line in context.text.splitlines():
84             if line.startswith('n') and line.find(' x') < 0:
85                     line += " x%d y%d" % (random.random() * 360 - 180,
86                                           random.random() * 180 - 90)
87             fd.write(line.encode('utf-8'))
88             fd.write(b'\n')
89
90     context.nominatim.run_update_script(import_diff=fname)
91     os.remove(fname)