2 from nose.tools import *
8 from haversine import haversine
9 from shapely.wkt import loads as wkt_load
10 from shapely.ops import linemerge
12 logger = logging.getLogger(__name__)
14 class NominatimConfig:
18 loglevel = getattr(logging, os.environ.get('LOGLEVEL','info').upper())
19 if 'LOGFILE' in os.environ:
20 logging.basicConfig(filename=os.environ.get('LOGFILE','run.log'),
23 logging.basicConfig(level=loglevel)
24 # Nominatim test setup
25 self.base_url = os.environ.get('NOMINATIM_SERVER', 'http://localhost/nominatim')
26 self.source_dir = os.path.abspath(os.environ.get('NOMINATIM_DIR', '../build'))
27 self.template_db = os.environ.get('TEMPLATE_DB', 'test_template_nominatim')
28 self.test_db = os.environ.get('TEST_DB', 'test_nominatim')
29 self.local_settings_file = os.environ.get('NOMINATIM_SETTINGS', '/tmp/nominatim_settings.php')
30 self.reuse_template = 'NOMINATIM_REMOVE_TEMPLATE' not in os.environ
31 self.keep_scenario_db = 'NOMINATIM_KEEP_SCENARIO_DB' in os.environ
32 os.environ['NOMINATIM_SETTINGS'] = '/tmp/nominatim_settings.php'
34 scriptpath = os.path.dirname(os.path.abspath(__file__))
35 self.scene_path = os.environ.get('SCENE_PATH',
36 os.path.join(scriptpath, '..', 'scenes', 'data'))
40 return 'Server URL: %s\nSource dir: %s\n' % (self.base_url, self.source_dir)
42 world.config = NominatimConfig()
45 def write_nominatim_config(dbname):
46 f = open(world.config.local_settings_file, 'w')
47 f.write("<?php\n @define('CONST_Database_DSN', 'pgsql://@/%s');\n" % dbname)
52 def run_nominatim_script(script, *args):
53 cmd = [os.path.join(world.config.source_dir, 'utils', '%s.php' % script)]
54 cmd.extend(['--%s' % x for x in args])
55 proc = subprocess.Popen(cmd, cwd=world.config.source_dir,
56 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
57 (outp, outerr) = proc.communicate()
58 assert (proc.returncode == 0), "Script '%s' failed:\n%s\n%s\n" % (script, outp, outerr)
62 return eval('{' + inp + '}')
66 """ Splits a unique identifier for places into its components.
67 As place_ids cannot be used for testing, we use a unique
68 identifier instead that is of the form <osmtype><osmid>[:class].
72 return None, None, None
74 assert_in(osmtype, ('R','N','W'))
76 osmid, cls = oid[1:].split(':')
77 return (osmtype, int(osmid), cls)
79 return (osmtype, int(oid[1:]), None)
83 """ Tries to retrive the place_id for a unique identifier. """
87 osmtype, osmid, cls = world.split_id(oid)
90 cur = world.conn.cursor()
92 q = 'SELECT place_id FROM placex where osm_type = %s and osm_id = %s'
93 params = (osmtype, osmid)
95 q = 'SELECT place_id FROM placex where osm_type = %s and osm_id = %s and class = %s'
96 params = (osmtype, osmid, cls)
97 cur.execute(q, params)
98 assert_equals(cur.rowcount, 1, "%d rows found for place %s" % (cur.rowcount, oid))
99 return cur.fetchone()[0]
103 def match_geometry(coord, matchstring):
104 m = re.match(r'([-0-9.]+),\s*([-0-9.]+)\s*(?:\+-([0-9.]+)([a-z]+)?)?', matchstring)
105 assert_is_not_none(m, "Invalid match string")
107 logger.debug("Distmatch: %s/%s %s %s" % (m.group(1), m.group(2), m.group(3), m.group(4) ))
108 dist = haversine(coord, (float(m.group(1)), float(m.group(2))))
110 if m.group(3) is not None:
111 expdist = float(m.group(3))
112 if m.group(4) is not None:
113 if m.group(4) == 'm':
114 expdist = expdist/1000
115 elif m.group(4) == 'km':
118 raise Exception("Unknown unit '%s' in geometry match" % (m.group(4), ))
122 logger.debug("Distances expected: %f, got: %f" % (expdist, dist))
123 assert dist <= expdist, "Geometry too far away, expected: %f, got: %f" % (expdist, dist)
128 def db_dump_table(table):
129 cur = world.conn.cursor()
130 cur.execute('SELECT * FROM %s' % table)
131 print '<<<<<<< BEGIN OF TABLE DUMP %s' % table
134 print '<<<<<<< END OF TABLE DUMP %s' % table
137 def db_drop_database(name):
138 conn = psycopg2.connect(database='postgres')
139 conn.set_isolation_level(0)
141 cur.execute('DROP DATABASE IF EXISTS %s' % (name, ))
145 world.is_template_set_up = False
148 def db_template_setup():
149 """ Set up a template database, containing all tables
150 but not yet any functions.
152 if world.is_template_set_up:
155 world.is_template_set_up = True
156 world.write_nominatim_config(world.config.template_db)
157 if world.config.reuse_template:
158 # check that the template is there
159 conn = psycopg2.connect(database='postgres')
161 cur.execute('select count(*) from pg_database where datname = %s',
162 (world.config.template_db,))
163 if cur.fetchone()[0] == 1:
166 # just in case... make sure a previous table has been dropped
167 world.db_drop_database(world.config.template_db)
168 # call the first part of database setup
169 world.run_nominatim_script('setup', 'create-db', 'setup-db')
170 # remove external data to speed up indexing for tests
171 conn = psycopg2.connect(database=world.config.template_db)
172 psycopg2.extras.register_hstore(conn, globally=False, unicode=True)
174 for table in ('gb_postcode', 'us_postcode', 'us_state', 'us_statecounty'):
175 cur.execute('TRUNCATE TABLE %s' % (table,))
178 # execute osm2pgsql on an empty file to get the right tables
179 osm2pgsql = os.path.join(world.config.source_dir, 'osm2pgsql', 'osm2pgsql')
180 proc = subprocess.Popen([osm2pgsql, '-lsc', '-r', 'xml', '-O', 'gazetteer', '-d', world.config.template_db, '-'],
181 cwd=world.config.source_dir, stdin=subprocess.PIPE,
182 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
183 [outstr, errstr] = proc.communicate(input='<osm version="0.6"></osm>')
184 world.run_nominatim_script('setup', 'create-functions', 'create-tables', 'create-partition-tables', 'create-partition-functions', 'load-data', 'create-search-indices')
187 # Leave the table around so it can be reused again after a non-reuse test round.
189 def db_template_teardown(total):
190 """ Set up a template database, containing all tables
191 but not yet any functions.
193 if world.is_template_set_up:
195 if not world.config.reuse_template:
196 world.db_drop_database(world.config.template_db)
198 os.remove(world.config.local_settings_file)
200 pass # ignore missing file
203 ##########################################################################
205 # Data scene handling
209 world.current_scene = None
212 def load_scene(name):
213 if name in world.scenes:
214 world.current_scene = world.scenes[name]
216 with open(os.path.join(world.config.scene_path, "%s.wkt" % name), 'r') as fd:
220 obj, wkt = line.split('|', 2)
222 scene[obj.strip()] = wkt_load(wkt)
223 world.scenes[name] = scene
224 world.current_scene = scene
227 def get_scene_geometry(name):
229 # Not a scene description
233 for obj in name.split('+'):
235 if oname.startswith(':'):
236 geoms.append(world.current_scene[oname[1:]])
238 scene, obj = oname.split(':', 2)
239 oldscene = world.current_scene
240 world.load_scene(scene)
241 wkt = world.current_scene[obj]
242 world.current_scene = oldscene
248 return linemerge(geoms)