]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/refresh.py
1fcb1577302d1fcca188683426ce6a1dfa48efc1
[nominatim.git] / nominatim / tools / refresh.py
1 """
2 Functions for bringing auxiliary data in the database up-to-date.
3 """
4 import json
5 import re
6
7 from psycopg2.extras import execute_values
8
9 from ..db.utils import execute_file
10
11 def update_postcodes(conn, sql_dir):
12     """ Recalculate postcode centroids and add, remove and update entries in the
13         location_postcode table. `conn` is an opne connection to the database.
14     """
15     execute_file(conn, sql_dir / 'update-postcodes.sql')
16
17
18 def recompute_word_counts(conn, sql_dir):
19     """ Compute the frequency of full-word search terms.
20     """
21     execute_file(conn, sql_dir / 'words_from_search_name.sql')
22
23
24 def _add_address_level_rows_from_entry(rows, entry):
25     """ Converts a single entry from the JSON format for address rank
26         descriptions into a flat format suitable for inserting into a
27         PostgreSQL table and adds these lines to `rows`.
28     """
29     countries = entry.get('countries') or (None, )
30     for key, values in entry['tags'].items():
31         for value, ranks in values.items():
32             if isinstance(ranks, list):
33                 rank_search, rank_address = ranks
34             else:
35                 rank_search = rank_address = ranks
36             if not value:
37                 value = None
38             for country in countries:
39                 rows.append((country, key, value, rank_search, rank_address))
40
41 def load_address_levels(conn, table, levels):
42     """ Replace the `address_levels` table with the contents of `levels'.
43
44         A new table is created any previously existing table is dropped.
45         The table has the following columns:
46             country, class, type, rank_search, rank_address
47     """
48     rows = []
49     for entry in levels:
50         _add_address_level_rows_from_entry(rows, entry)
51
52     with conn.cursor() as cur:
53         cur.execute('DROP TABLE IF EXISTS {}'.format(table))
54
55         cur.execute("""CREATE TABLE {} (country_code varchar(2),
56                                         class TEXT,
57                                         type TEXT,
58                                         rank_search SMALLINT,
59                                         rank_address SMALLINT)""".format(table))
60
61         execute_values(cur, "INSERT INTO {} VALUES %s".format(table), rows)
62
63         cur.execute('CREATE UNIQUE INDEX ON {} (country_code, class, type)'.format(table))
64
65     conn.commit()
66
67 def load_address_levels_from_file(conn, config_file):
68     """ Replace the `address_levels` table with the contents of the config
69         file.
70     """
71     with config_file.open('r') as fdesc:
72         load_address_levels(conn, 'address_levels', json.load(fdesc))
73
74 PLPGSQL_BASE_MODULES = (
75     'utils.sql',
76     'normalization.sql',
77     'ranking.sql',
78     'importance.sql',
79     'address_lookup.sql',
80     'interpolation.sql'
81 )
82
83 PLPGSQL_TABLE_MODULES = (
84     ('place', 'place_triggers.sql'),
85     ('placex', 'placex_triggers.sql'),
86     ('location_postcode', 'postcode_triggers.sql')
87 )
88
89 def _get_standard_function_sql(conn, config, sql_dir, enable_diff_updates, enable_debug):
90     """ Read all applicable SQLs containing PL/pgSQL functions, replace
91         placefolders and execute them.
92     """
93     sql_func_dir = sql_dir / 'functions'
94     sql = ''
95
96     # Get the basic set of functions that is always imported.
97     for sql_file in PLPGSQL_BASE_MODULES:
98         with (sql_func_dir / sql_file).open('r') as fdesc:
99             sql += fdesc.read()
100
101     # Some files require the presence of a certain table
102     for table, fname in PLPGSQL_TABLE_MODULES:
103         if conn.table_exists(table):
104             with (sql_func_dir / fname).open('r') as fdesc:
105                 sql += fdesc.read()
106
107     # Replace placeholders.
108     sql = sql.replace('{modulepath}',
109                       config.DATABASE_MODULE_PATH or str((config.project_dir / 'module').resolve()))
110
111     if enable_diff_updates:
112         sql = sql.replace('RETURN NEW; -- %DIFFUPDATES%', '--')
113
114     if enable_debug:
115         sql = sql.replace('--DEBUG:', '')
116
117     if config.get_bool('LIMIT_REINDEXING'):
118         sql = sql.replace('--LIMIT INDEXING:', '')
119
120     if not config.get_bool('USE_US_TIGER_DATA'):
121         sql = sql.replace('-- %NOTIGERDATA% ', '')
122
123     if not config.get_bool('USE_AUX_LOCATION_DATA'):
124         sql = sql.replace('-- %NOAUXDATA% ', '')
125
126     reverse_only = 'false' if conn.table_exists('search_name') else 'true'
127
128     return sql.replace('%REVERSE-ONLY%', reverse_only)
129
130
131 def replace_partition_string(sql, partitions):
132     """ Replace a partition template with the actual partition code.
133     """
134     for match in re.findall('^-- start(.*?)^-- end', sql, re.M | re.S):
135         repl = ''
136         for part in partitions:
137             repl += match.replace('-partition-', str(part))
138         sql = sql.replace(match, repl)
139
140     return sql
141
142 def _get_partition_function_sql(conn, sql_dir):
143     """ Create functions that work on partition tables.
144     """
145     with conn.cursor() as cur:
146         cur.execute('SELECT distinct partition FROM country_name')
147         partitions = set([0])
148         for row in cur:
149             partitions.add(row[0])
150
151     with (sql_dir / 'partition-functions.src.sql').open('r') as fdesc:
152         sql = fdesc.read()
153
154     return replace_partition_string(sql, sorted(partitions))
155
156 def create_functions(conn, config, sql_dir,
157                      enable_diff_updates=True, enable_debug=False):
158     """ (Re)create the PL/pgSQL functions.
159     """
160     sql = _get_standard_function_sql(conn, config, sql_dir,
161                                      enable_diff_updates, enable_debug)
162     sql += _get_partition_function_sql(conn, sql_dir)
163
164     with conn.cursor() as cur:
165         cur.execute(sql)
166
167     conn.commit()