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