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 ([0-9.]+ )?grid')
26 def define_node_grid(context, grid_step):
28 Define a grid of node positions.
29 Use a table to define the grid. The nodes must be integer ids. Optionally
30 you can give the grid distance. The default is 0.00001 degrees.
32 if grid_step is not None:
33 grid_step = float(grid_step.strip())
37 context.osm.set_grid([context.table.headings] + [list(h) for h in context.table],
41 @when(u'loading osm data')
42 def load_osm_file(context):
44 Load the given data into a freshly created test data using osm2pgsql.
45 No further indexing is done.
47 The data is expected as attached text in OPL format.
49 # create an OSM file and import it
50 fname = write_opl_file(context.text, context.osm)
51 context.nominatim.run_setup_script('import-data', osm_file=fname,
55 ### reintroduce the triggers/indexes we've lost by having osm2pgsql set up place again
56 cur = context.db.cursor()
57 cur.execute("""CREATE TRIGGER place_before_delete BEFORE DELETE ON place
58 FOR EACH ROW EXECUTE PROCEDURE place_delete()""")
59 cur.execute("""CREATE TRIGGER place_before_insert BEFORE INSERT ON place
60 FOR EACH ROW EXECUTE PROCEDURE place_insert()""")
61 cur.execute("""CREATE UNIQUE INDEX idx_place_osm_unique on place using btree(osm_id,osm_type,class,type)""")
65 @when(u'updating osm data')
66 def update_from_osm_file(context):
68 Update a database previously populated with 'loading osm data'.
69 Needs to run indexing on the existing data first to yield the correct result.
71 The data is expected as attached text in OPL format.
73 context.nominatim.run_setup_script('create-functions', 'create-partition-functions')
75 cur = context.db.cursor()
76 cur.execute("""insert into placex (osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry)
77 select osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry from place""")
79 """insert into location_property_osmline (osm_id, address, linegeo)
80 SELECT osm_id, address, geometry from place
81 WHERE class='place' and type='houses' and osm_type='W'
82 and ST_GeometryType(geometry) = 'ST_LineString'""")
84 context.nominatim.run_setup_script('index', 'index-noanalyse')
85 context.nominatim.run_setup_script('create-functions', 'create-partition-functions',
86 'enable-diff-updates')
88 # create an OSM file and import it
89 fname = write_opl_file(context.text, context.osm)
90 context.nominatim.run_update_script(import_diff=fname)