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
10 """ Caches country-specific properties from the configuration file.
16 def load(self, config):
17 """ Load the country properties from the configuration files,
18 if they are not loaded yet.
21 self._info = config.load_sub_configuration('country_settings.yaml')
24 """ Return tuples of (country_code, property dict) as iterable.
26 return self._info.items()
29 _COUNTRY_INFO = _CountryInfo()
31 def setup_country_config(config):
32 """ Load country properties from the configuration file.
33 Needs to be called before using any other functions in this
36 _COUNTRY_INFO.load(config)
39 def setup_country_tables(dsn, sql_dir, ignore_partitions=False):
40 """ Create and populate the tables with basic static data that provides
41 the background for geocoding. Data is assumed to not yet exist.
43 db_utils.execute_file(dsn, sql_dir / 'country_name.sql')
44 db_utils.execute_file(dsn, sql_dir / 'country_osm_grid.sql.gz')
47 for ccode, props in _COUNTRY_INFO.items():
48 if ccode is not None and props is not None:
52 partition = props.get('partition')
53 if ',' in (props.get('languages', ',') or ','):
56 lang = props['languages']
57 params.append((ccode, partition, lang))
59 with connect(dsn) as conn:
60 with conn.cursor() as cur:
62 """ UPDATE country_name
63 SET partition = part, country_default_language_code = lang
64 FROM (VALUES %s) AS v (cc, part, lang)
65 WHERE country_code = v.cc""", params)
69 def create_country_names(conn, tokenizer, languages=None):
70 """ Add default country names to search index. `languages` is a comma-
71 separated list of language codes as used in OSM. If `languages` is not
72 empty then only name translations for the given languages are added
76 languages = languages.split(',')
78 def _include_key(key):
79 return key == 'name' or \
80 (key.startswith('name:') and (not languages or key[5:] in languages))
82 with conn.cursor() as cur:
83 psycopg2.extras.register_hstore(cur)
84 cur.execute("""SELECT country_code, name FROM country_name
85 WHERE country_code is not null""")
87 with tokenizer.name_analyzer() as analyzer:
88 for code, name in cur:
89 names = {'countrycode': code}
91 names['short_name'] = 'UK'
93 names['short_name'] = 'United States'
95 # country names (only in languages as provided)
97 names.update(((k, v) for k, v in name.items() if _include_key(k)))
99 analyzer.add_country_names(code, names)