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 from geometry_alias import ALIASES
16 def get_osm2pgsql_options(nominatim_env, fname, append):
17 return dict(import_file=fname,
18 osm2pgsql=str(nominatim_env.build_dir / 'osm2pgsql' / 'osm2pgsql'),
20 osm2pgsql_style=str(nominatim_env.src_dir / 'settings' / 'import-extratags.style'),
22 dsn=nominatim_env.get_libpq_dsn(),
24 tablespaces=dict(slim_data='', slim_index='',
25 main_data='', main_index=''),
30 def write_opl_file(opl, grid):
31 """ Create a temporary OSM file from OPL and return the file name. It is
32 the responsibility of the caller to delete the file again.
34 Node with missing coordinates, can retrieve their coordinates from
35 a supplied grid. Failing that a random coordinate is assigned.
37 with tempfile.NamedTemporaryFile(suffix='.opl', delete=False) as fd:
38 for line in opl.splitlines():
39 if line.startswith('n') and line.find(' x') < 0:
40 coord = grid.grid_node(int(line[1:].split(' ')[0]))
42 coord = (random.random() * 360 - 180,
43 random.random() * 180 - 90)
44 line += " x%f y%f" % coord
45 fd.write(line.encode('utf-8'))
50 @given(u'the scene (?P<scene>.+)')
51 def set_default_scene(context, scene):
54 @given(u'the ([0-9.]+ )?grid(?: with origin (?P<origin>.*))?')
55 def define_node_grid(context, grid_step, origin):
57 Define a grid of node positions.
58 Use a table to define the grid. The nodes must be integer ids. Optionally
59 you can give the grid distance. The default is 0.00001 degrees.
61 if grid_step is not None:
62 grid_step = float(grid_step.strip())
69 coords = origin.split(',')
71 raise RuntimeError('Grid origin expects orgin with x,y coordinates.')
72 origin = (float(coords[0]), float(coords[1]))
73 elif origin in ALIASES:
74 origin = ALIASES[origin]
76 raise RuntimeError('Grid origin must be either coordinate or alias.')
80 context.osm.set_grid([context.table.headings] + [list(h) for h in context.table],
84 @when(u'loading osm data')
85 def load_osm_file(context):
87 Load the given data into a freshly created test data using osm2pgsql.
88 No further indexing is done.
90 The data is expected as attached text in OPL format.
92 # create an OSM file and import it
93 fname = write_opl_file(context.text, context.osm)
95 run_osm2pgsql(get_osm2pgsql_options(context.nominatim, fname, append=False))
99 ### reintroduce the triggers/indexes we've lost by having osm2pgsql set up place again
100 cur = context.db.cursor()
101 cur.execute("""CREATE TRIGGER place_before_delete BEFORE DELETE ON place
102 FOR EACH ROW EXECUTE PROCEDURE place_delete()""")
103 cur.execute("""CREATE TRIGGER place_before_insert BEFORE INSERT ON place
104 FOR EACH ROW EXECUTE PROCEDURE place_insert()""")
105 cur.execute("""CREATE UNIQUE INDEX idx_place_osm_unique on place using btree(osm_id,osm_type,class,type)""")
109 @when(u'updating osm data')
110 def update_from_osm_file(context):
112 Update a database previously populated with 'loading osm data'.
113 Needs to run indexing on the existing data first to yield the correct result.
115 The data is expected as attached text in OPL format.
117 context.nominatim.copy_from_place(context.db)
118 context.nominatim.run_nominatim('index')
119 context.nominatim.run_nominatim('refresh', '--functions')
121 # create an OSM file and import it
122 fname = write_opl_file(context.text, context.osm)
124 run_osm2pgsql(get_osm2pgsql_options(context.nominatim, fname, append=True))