5 from nose.tools import * # for assert functions
 
   7 @given(u'the ([0-9.]+ )?grid')
 
   8 def define_node_grid(context, grid_step):
 
  10     Define a grid of node positions.
 
  12     if grid_step is not None:
 
  13         grid_step = float(grid_step.strip())
 
  17     context.osm.clear_grid()
 
  20     for h in context.table.headings:
 
  22             context.osm.add_grid_node(int(h), 0, i)
 
  26     for r in context.table:
 
  30                 context.osm.add_grid_node(int(h), x, y)
 
  35 @when(u'loading osm data')
 
  36 def load_osm_file(context):
 
  38     Load the given data into a freshly created test data using osm2pgsql.
 
  39     No further indexing is done.
 
  41     The data is expected as attached text in OPL format.
 
  43     # create a OSM file in /tmp and import it
 
  44     with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.opl', delete=False) as fd:
 
  46         for line in context.text.splitlines():
 
  47             if line.startswith('n') and line.find(' x') < 0:
 
  48                 coord = context.osm.grid_node(int(line[1:].split(' ')[0]))
 
  50                     coord = (random.random() * 360 - 180,
 
  51                              random.random() * 180 - 90)
 
  52                 line += " x%f y%f" % coord
 
  53             fd.write(line.encode('utf-8'))
 
  56     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)""")
 
  70 @when(u'updating osm data')
 
  71 def update_from_osm_file(context):
 
  73     Update a database previously populated with 'loading osm data'.
 
  74     Needs to run indexing on the existing data first to yield the correct result.
 
  76     The data is expected as attached text in OPL format.
 
  78     context.nominatim.run_setup_script('create-functions', 'create-partition-functions')
 
  80     cur = context.db.cursor()
 
  81     cur.execute("""insert into placex (osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry)
 
  82                      select            osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry from place""")
 
  84         """insert into location_property_osmline (osm_id, address, linegeo)
 
  85              SELECT osm_id, address, geometry from place
 
  86               WHERE class='place' and type='houses' and osm_type='W'
 
  87                     and ST_GeometryType(geometry) = 'ST_LineString'""")
 
  89     context.nominatim.run_setup_script('index', 'index-noanalyse')
 
  90     context.nominatim.run_setup_script('create-functions', 'create-partition-functions',
 
  91                                        'enable-diff-updates')
 
  93     # create a OSM file in /tmp and import it
 
  94     with tempfile.NamedTemporaryFile(dir='/tmp', suffix='.opl', delete=False) as fd:
 
  96         for line in context.text.splitlines():
 
  97             if line.startswith('n') and line.find(' x') < 0:
 
  98                     line += " x%d y%d" % (random.random() * 360 - 180,
 
  99                                           random.random() * 180 - 90)
 
 100             fd.write(line.encode('utf-8'))
 
 103     context.nominatim.run_update_script(import_diff=fname)