1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Functions for importing and managing static country information.
10 import psycopg2.extras
12 from nominatim.db import utils as db_utils
13 from nominatim.db.connection import connect
17 """ Caches country-specific properties from the configuration file.
22 self._key_prefix = 'name'
24 def load(self, config):
25 """ Load the country properties from the configuration files,
26 if they are not loaded yet.
29 self._info = config.load_sub_configuration('country_settings.yaml')
30 # Convert languages into a list for simpler handling.
31 for prop in self._info.values():
32 if 'languages' not in prop:
33 prop['languages'] = []
34 elif not isinstance(prop['languages'], list):
35 prop['languages'] = [x.strip()
36 for x in prop['languages'].split(',')]
37 if 'names' not in prop or prop['names'] is None:
38 prop['names'] = {self._key_prefix: {}}
41 """ Return tuples of (country_code, property dict) as iterable.
43 return self._info.items()
46 """ Return the prefix that will be attached to the keys of the country
47 names values when storing them in the database
49 return self._key_prefix
52 _COUNTRY_INFO = _CountryInfo()
55 def setup_country_config(config):
56 """ Load country properties from the configuration file.
57 Needs to be called before using any other functions in this
60 _COUNTRY_INFO.load(config)
64 """ Iterate over country code and properties.
66 return _COUNTRY_INFO.items()
69 def setup_country_tables(dsn, sql_dir, ignore_partitions=False):
70 """ Create and populate the tables with basic static data that provides
71 the background for geocoding. Data is assumed to not yet exist.
73 db_utils.execute_file(dsn, sql_dir / 'country_osm_grid.sql.gz')
75 def add_prefix_to_keys(names, prefix):
76 return {prefix+':'+k: v for k, v in names.items()}
79 for ccode, props in _COUNTRY_INFO.items():
80 if ccode is not None and props is not None:
84 partition = props.get('partition')
85 lang = props['languages'][0] if len(
86 props['languages']) == 1 else None
87 name = add_prefix_to_keys(props.get('names').get(
88 _COUNTRY_INFO.key_prefix()), _COUNTRY_INFO.key_prefix())
89 params.append((ccode, name, lang, partition))
90 with connect(dsn) as conn:
91 with conn.cursor() as cur:
92 psycopg2.extras.register_hstore(cur)
94 """ CREATE TABLE public.country_name (
95 country_code character varying(2),
97 derived_name public.hstore,
98 country_default_language_code text,
102 """ INSERT INTO public.country_name
103 (country_code, name, country_default_language_code, partition) VALUES %s
108 def create_country_names(conn, tokenizer, languages=None):
109 """ Add default country names to search index. `languages` is a comma-
110 separated list of language codes as used in OSM. If `languages` is not
111 empty then only name translations for the given languages are added
115 languages = languages.split(',')
117 def _include_key(key):
118 return key == _COUNTRY_INFO.key_prefix() or \
119 (key.startswith(_COUNTRY_INFO.key_prefix()+':') and
120 (not languages or key[len(_COUNTRY_INFO.key_prefix())+1:] in languages))
122 with conn.cursor() as cur:
123 psycopg2.extras.register_hstore(cur)
124 cur.execute("""SELECT country_code, name FROM country_name
125 WHERE country_code is not null""")
127 with tokenizer.name_analyzer() as analyzer:
128 for code, name in cur:
129 names = {'countrycode': code}
131 names['short_name'] = 'UK'
133 names['short_name'] = 'United States'
135 # country names (only in languages as provided)
138 for k, v in name.items() if _include_key(k)))
140 analyzer.add_country_names(code, names)