2 Functions for importing and managing static country information.
6 from nominatim.db import utils as db_utils
7 from nominatim.db.connection import connect
9 def setup_country_tables(dsn, sql_dir, ignore_partitions=False):
10 """ Create and populate the tables with basic static data that provides
11 the background for geocoding. Data is assumed to not yet exist.
13 db_utils.execute_file(dsn, sql_dir / 'country_name.sql')
14 db_utils.execute_file(dsn, sql_dir / 'country_osm_grid.sql.gz')
17 with connect(dsn) as conn:
18 with conn.cursor() as cur:
19 cur.execute('UPDATE country_name SET partition = 0')
23 def create_country_names(conn, tokenizer, languages=None):
24 """ Add default country names to search index. `languages` is a comma-
25 separated list of language codes as used in OSM. If `languages` is not
26 empty then only name translations for the given languages are added
30 languages = languages.split(',')
32 def _include_key(key):
33 return key == 'name' or \
34 (key.startswith('name:') and (not languages or key[5:] in languages))
36 with conn.cursor() as cur:
37 psycopg2.extras.register_hstore(cur)
38 cur.execute("""SELECT country_code, name FROM country_name
39 WHERE country_code is not null""")
41 with tokenizer.name_analyzer() as analyzer:
42 for code, name in cur:
43 names = {'countrycode': code}
45 names['short_name'] = 'UK'
47 names['short_name'] = 'United States'
49 # country names (only in languages as provided)
51 names.update(((k, v) for k, v in name.items() if _include_key(k)))
53 analyzer.add_country_names(code, names)