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.
9 Node with missing coordinates, can retrieve their coordinates from
10 a supplied grid. Failing that a random coordinate is assigned.
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]))
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'))
25 @given(u'the scene (?P<scene>.+)')
26 def set_default_scene(context, scene):
29 @given(u'the ([0-9.]+ )?grid')
30 def define_node_grid(context, grid_step):
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.
36 if grid_step is not None:
37 grid_step = float(grid_step.strip())
41 context.osm.set_grid([context.table.headings] + [list(h) for h in context.table],
45 @when(u'loading osm data')
46 def load_osm_file(context):
48 Load the given data into a freshly created test data using osm2pgsql.
49 No further indexing is done.
51 The data is expected as attached text in OPL format.
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,
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)""")
69 @when(u'updating osm data')
70 def update_from_osm_file(context):
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.
75 The data is expected as attached text in OPL format.
77 context.nominatim.copy_from_place(context.db)
78 context.nominatim.run_nominatim('index')
79 context.nominatim.run_nominatim('refresh', '--functions')
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)