1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
10 from pathlib import Path
12 from nominatim.tools.exec_utils import run_osm2pgsql
14 def get_osm2pgsql_options(nominatim_env, fname, append):
15 return dict(import_file=fname,
16 osm2pgsql=str(nominatim_env.build_dir / 'osm2pgsql' / 'osm2pgsql'),
18 osm2pgsql_style=str(nominatim_env.src_dir / 'settings' / 'import-extratags.style'),
20 dsn=nominatim_env.get_libpq_dsn(),
22 tablespaces=dict(slim_data='', slim_index='',
23 main_data='', main_index=''),
28 def write_opl_file(opl, grid):
29 """ Create a temporary OSM file from OPL and return the file name. It is
30 the responsibility of the caller to delete the file again.
32 Node with missing coordinates, can retrieve their coordinates from
33 a supplied grid. Failing that a random coordinate is assigned.
35 with tempfile.NamedTemporaryFile(suffix='.opl', delete=False) as fd:
36 for line in opl.splitlines():
37 if line.startswith('n') and line.find(' x') < 0:
38 coord = grid.grid_node(int(line[1:].split(' ')[0]))
40 coord = (random.random() * 360 - 180,
41 random.random() * 180 - 90)
42 line += " x%f y%f" % coord
43 fd.write(line.encode('utf-8'))
48 @given(u'the scene (?P<scene>.+)')
49 def set_default_scene(context, scene):
52 @given(u'the ([0-9.]+ )?grid')
53 def define_node_grid(context, grid_step):
55 Define a grid of node positions.
56 Use a table to define the grid. The nodes must be integer ids. Optionally
57 you can give the grid distance. The default is 0.00001 degrees.
59 if grid_step is not None:
60 grid_step = float(grid_step.strip())
64 context.osm.set_grid([context.table.headings] + [list(h) for h in context.table],
68 @when(u'loading osm data')
69 def load_osm_file(context):
71 Load the given data into a freshly created test data using osm2pgsql.
72 No further indexing is done.
74 The data is expected as attached text in OPL format.
76 # create an OSM file and import it
77 fname = write_opl_file(context.text, context.osm)
79 run_osm2pgsql(get_osm2pgsql_options(context.nominatim, fname, append=False))
83 ### reintroduce the triggers/indexes we've lost by having osm2pgsql set up place again
84 cur = context.db.cursor()
85 cur.execute("""CREATE TRIGGER place_before_delete BEFORE DELETE ON place
86 FOR EACH ROW EXECUTE PROCEDURE place_delete()""")
87 cur.execute("""CREATE TRIGGER place_before_insert BEFORE INSERT ON place
88 FOR EACH ROW EXECUTE PROCEDURE place_insert()""")
89 cur.execute("""CREATE UNIQUE INDEX idx_place_osm_unique on place using btree(osm_id,osm_type,class,type)""")
93 @when(u'updating osm data')
94 def update_from_osm_file(context):
96 Update a database previously populated with 'loading osm data'.
97 Needs to run indexing on the existing data first to yield the correct result.
99 The data is expected as attached text in OPL format.
101 context.nominatim.copy_from_place(context.db)
102 context.nominatim.run_nominatim('index')
103 context.nominatim.run_nominatim('refresh', '--functions')
105 # create an OSM file and import it
106 fname = write_opl_file(context.text, context.osm)
108 run_osm2pgsql(get_osm2pgsql_options(context.nominatim, fname, append=True))