Skip to content

Commit 63b0552

Browse files
authored
Merge pull request #3714 from lonvia/postcode-update-without-project-dir
Change postcode update function to work without a project directory
2 parents b80e691 + 97d9e3c commit 63b0552

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

src/nominatim_db/tools/postcodes.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# This file is part of Nominatim. (https://nominatim.org)
44
#
5-
# Copyright (C) 2024 by the Nominatim developer community.
5+
# Copyright (C) 2025 by the Nominatim developer community.
66
# For a full list of authors see the git log.
77
"""
88
Functions for importing, updating and otherwise maintaining the table
@@ -64,11 +64,15 @@ def add(self, postcode: str, x: float, y: float) -> None:
6464
if normalized:
6565
self.collected[normalized] += (x, y)
6666

67-
def commit(self, conn: Connection, analyzer: AbstractAnalyzer, project_dir: Path) -> None:
68-
""" Update postcodes for the country from the postcodes selected so far
69-
as well as any externally supplied postcodes.
67+
def commit(self, conn: Connection, analyzer: AbstractAnalyzer,
68+
project_dir: Optional[Path]) -> None:
69+
""" Update postcodes for the country from the postcodes selected so far.
70+
71+
When 'project_dir' is set, then any postcode files found in this
72+
directory are taken into account as well.
7073
"""
71-
self._update_from_external(analyzer, project_dir)
74+
if project_dir is not None:
75+
self._update_from_external(analyzer, project_dir)
7276
to_add, to_delete, to_update = self._compute_changes(conn)
7377

7478
LOG.info("Processing country '%s' (%s added, %s deleted, %s updated).",
@@ -170,7 +174,7 @@ def _open_external(self, project_dir: Path) -> Optional[TextIO]:
170174
return None
171175

172176

173-
def update_postcodes(dsn: str, project_dir: Path, tokenizer: AbstractTokenizer) -> None:
177+
def update_postcodes(dsn: str, project_dir: Optional[Path], tokenizer: AbstractTokenizer) -> None:
174178
""" Update the table of artificial postcodes.
175179
176180
Computes artificial postcode centroids from the placex table,

test/bdd/test_db.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"""
1212
import asyncio
1313
import re
14-
from pathlib import Path
1514

1615
import psycopg
1716

@@ -130,7 +129,7 @@ def do_import(db_conn, def_config):
130129
create_table_triggers(db_conn, def_config)
131130
asyncio.run(load_data(def_config.get_libpq_dsn(), 1))
132131
tokenizer = tokenizer_factory.get_tokenizer_for_db(def_config)
133-
update_postcodes(def_config.get_libpq_dsn(), Path('/xxxx'), tokenizer)
132+
update_postcodes(def_config.get_libpq_dsn(), None, tokenizer)
134133
cli.nominatim(['index', '-q'], def_config.environ)
135134

136135
return _collect_place_ids(db_conn)

test/python/tools/test_postcodes.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,17 @@ def _insert_implicit_postcode(osm_id, country, geometry, address):
8585
return _insert_implicit_postcode
8686

8787

88-
def test_postcodes_empty(dsn, postcode_table, place_table,
89-
tmp_path, tokenizer):
90-
postcodes.update_postcodes(dsn, tmp_path, tokenizer)
88+
def test_postcodes_empty(dsn, postcode_table, place_table, tokenizer):
89+
postcodes.update_postcodes(dsn, None, tokenizer)
9190

9291
assert not postcode_table.row_set
9392

9493

95-
def test_postcodes_add_new(dsn, postcode_table, tmp_path,
96-
insert_implicit_postcode, tokenizer):
94+
def test_postcodes_add_new(dsn, postcode_table, insert_implicit_postcode, tokenizer):
9795
insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='9486'))
9896
postcode_table.add('yy', '9486', 99, 34)
9997

100-
postcodes.update_postcodes(dsn, tmp_path, tokenizer)
98+
postcodes.update_postcodes(dsn, None, tokenizer)
10199

102100
assert postcode_table.row_set == {('xx', '9486', 10, 12), }
103101

@@ -112,49 +110,48 @@ def test_postcodes_replace_coordinates(dsn, postcode_table, tmp_path,
112110
assert postcode_table.row_set == {('xx', 'AB 4511', 10, 12)}
113111

114112

115-
def test_postcodes_replace_coordinates_close(dsn, postcode_table, tmp_path,
113+
def test_postcodes_replace_coordinates_close(dsn, postcode_table,
116114
insert_implicit_postcode, tokenizer):
117115
insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='AB 4511'))
118116
postcode_table.add('xx', 'AB 4511', 10, 11.99999)
119117

120-
postcodes.update_postcodes(dsn, tmp_path, tokenizer)
118+
postcodes.update_postcodes(dsn, None, tokenizer)
121119

122120
assert postcode_table.row_set == {('xx', 'AB 4511', 10, 11.99999)}
123121

124122

125-
def test_postcodes_remove(dsn, postcode_table, tmp_path,
123+
def test_postcodes_remove(dsn, postcode_table,
126124
insert_implicit_postcode, tokenizer):
127125
insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='AB 4511'))
128126
postcode_table.add('xx', 'badname', 10, 12)
129127

130-
postcodes.update_postcodes(dsn, tmp_path, tokenizer)
128+
postcodes.update_postcodes(dsn, None, tokenizer)
131129

132130
assert postcode_table.row_set == {('xx', 'AB 4511', 10, 12)}
133131

134132

135-
def test_postcodes_ignore_empty_country(dsn, postcode_table, tmp_path,
133+
def test_postcodes_ignore_empty_country(dsn, postcode_table,
136134
insert_implicit_postcode, tokenizer):
137135
insert_implicit_postcode(1, None, 'POINT(10 12)', dict(postcode='AB 4511'))
138-
postcodes.update_postcodes(dsn, tmp_path, tokenizer)
136+
postcodes.update_postcodes(dsn, None, tokenizer)
139137
assert not postcode_table.row_set
140138

141139

142-
def test_postcodes_remove_all(dsn, postcode_table, place_table,
143-
tmp_path, tokenizer):
140+
def test_postcodes_remove_all(dsn, postcode_table, place_table, tokenizer):
144141
postcode_table.add('ch', '5613', 10, 12)
145-
postcodes.update_postcodes(dsn, tmp_path, tokenizer)
142+
postcodes.update_postcodes(dsn, None, tokenizer)
146143

147144
assert not postcode_table.row_set
148145

149146

150-
def test_postcodes_multi_country(dsn, postcode_table, tmp_path,
147+
def test_postcodes_multi_country(dsn, postcode_table,
151148
insert_implicit_postcode, tokenizer):
152149
insert_implicit_postcode(1, 'de', 'POINT(10 12)', dict(postcode='54451'))
153150
insert_implicit_postcode(2, 'cc', 'POINT(100 56)', dict(postcode='DD23 T'))
154151
insert_implicit_postcode(3, 'de', 'POINT(10.3 11.0)', dict(postcode='54452'))
155152
insert_implicit_postcode(4, 'cc', 'POINT(10.3 11.0)', dict(postcode='54452'))
156153

157-
postcodes.update_postcodes(dsn, tmp_path, tokenizer)
154+
postcodes.update_postcodes(dsn, None, tokenizer)
158155

159156
assert postcode_table.row_set == {('de', '54451', 10, 12),
160157
('de', '54452', 10.3, 11.0),
@@ -211,7 +208,7 @@ def test_can_compute(dsn, table_factory):
211208
assert postcodes.can_compute(dsn)
212209

213210

214-
def test_no_placex_entry(dsn, tmp_path, temp_db_cursor, place_row, postcode_table, tokenizer):
211+
def test_no_placex_entry(dsn, temp_db_cursor, place_row, postcode_table, tokenizer):
215212
# Rewrite the get_country_code function to verify its execution.
216213
temp_db_cursor.execute("""
217214
CREATE OR REPLACE FUNCTION get_country_code(place geometry)
@@ -220,12 +217,12 @@ def test_no_placex_entry(dsn, tmp_path, temp_db_cursor, place_row, postcode_tabl
220217
END; $$ LANGUAGE plpgsql;
221218
""")
222219
place_row(geom='SRID=4326;POINT(10 12)', address=dict(postcode='AB 4511'))
223-
postcodes.update_postcodes(dsn, tmp_path, tokenizer)
220+
postcodes.update_postcodes(dsn, None, tokenizer)
224221

225222
assert postcode_table.row_set == {('yy', 'AB 4511', 10, 12)}
226223

227224

228-
def test_discard_badly_formatted_postcodes(dsn, tmp_path, temp_db_cursor, place_row,
225+
def test_discard_badly_formatted_postcodes(dsn, temp_db_cursor, place_row,
229226
postcode_table, tokenizer):
230227
# Rewrite the get_country_code function to verify its execution.
231228
temp_db_cursor.execute("""
@@ -235,6 +232,6 @@ def test_discard_badly_formatted_postcodes(dsn, tmp_path, temp_db_cursor, place_
235232
END; $$ LANGUAGE plpgsql;
236233
""")
237234
place_row(geom='SRID=4326;POINT(10 12)', address=dict(postcode='AB 4511'))
238-
postcodes.update_postcodes(dsn, tmp_path, tokenizer)
235+
postcodes.update_postcodes(dsn, None, tokenizer)
239236

240237
assert not postcode_table.row_set

0 commit comments

Comments
 (0)