Skip to content

Commit 1a7fd07

Browse files
authoredFeb 26, 2025··
Merge pull request #870 from lonvia/postcodes-on-relations
Take postcodes from postcode relations into account
2 parents 31c4908 + a3517e2 commit 1a7fd07

File tree

4 files changed

+76
-19
lines changed

4 files changed

+76
-19
lines changed
 

‎src/main/java/de/komoot/photon/PhotonDoc.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ public PhotonDoc address(Map<String, String> address) {
104104
extractAddress(address, AddressType.COUNTY, "county");
105105
extractAddress(address, AddressType.STATE, "state");
106106

107-
String addressPostCode = address.get("postcode");
108-
if (addressPostCode != null && !addressPostCode.equals(postcode)) {
109-
LOGGER.debug("Replacing postcode {} with {} for osmId #{}", postcode, addressPostCode, osmId);
110-
postcode = addressPostCode;
111-
}
107+
postcode = address.getOrDefault("postcode", postcode);
112108
}
113109
return this;
114110
}
@@ -211,9 +207,6 @@ public boolean isUsefulForIndex() {
211207
*
212208
* @param addressType The type of address field to fill.
213209
* @param addressFieldName The name of the address tag to use (without the 'addr:' prefix).
214-
*
215-
* @return 'existingField' potentially with the name field replaced. If existingField was null and
216-
* the address field could be found, then a new map with the address as single entry is returned.
217210
*/
218211
private void extractAddress(Map<String, String> address, AddressType addressType, String addressFieldName) {
219212
String field = address.get(addressFieldName);
@@ -258,13 +251,17 @@ public boolean setAddressPartIfNew(AddressType addressType, Map<String, String>
258251
public void completePlace(List<AddressRow> addresses) {
259252
final AddressType doctype = getAddressType();
260253
for (AddressRow address : addresses) {
261-
final AddressType atype = address.getAddressType();
262-
263-
if (atype != null
264-
&& (atype == doctype || !setAddressPartIfNew(atype, address.getName()))
265-
&& address.isUsefulForContext()) {
266-
// no specifically handled item, check if useful for context
267-
getContext().add(address.getName());
254+
if (address.isPostcode()) {
255+
this.postcode = address.getName().getOrDefault("ref", this.postcode);
256+
} else {
257+
final AddressType atype = address.getAddressType();
258+
259+
if (atype != null
260+
&& (atype == doctype || !setAddressPartIfNew(atype, address.getName()))
261+
&& address.isUsefulForContext()) {
262+
// no specifically handled item, check if useful for context
263+
getContext().add(address.getName());
264+
}
268265
}
269266
}
270267
}

‎src/main/java/de/komoot/photon/nominatim/model/AddressRow.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AddressType getAddressType() {
2222
return AddressType.fromRank(rankAddress);
2323
}
2424

25-
private boolean isPostcode() {
25+
public boolean isPostcode() {
2626
if ("place".equals(osmKey) && "postcode".equals(osmValue)) {
2727
return true;
2828
}

‎src/test/java/de/komoot/photon/nominatim/NominatimConnectorDBTest.java

+54
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,60 @@ void testNoCountry() {
360360
assertNull(importer.get(place).getCountryCode());
361361
}
362362

363+
@Test
364+
void testUsePostcodeFromPlacex() {
365+
PlacexTestRow parent = PlacexTestRow.make_street("Main St").add(jdbc);
366+
PlacexTestRow place = new PlacexTestRow("building", "yes")
367+
.addr("housenumber", "34")
368+
.postcode("AA 44XH")
369+
.parent(parent).add(jdbc);
370+
371+
readEntireDatabase();
372+
373+
PhotonDoc result = importer.get(place);
374+
375+
assertEquals("AA 44XH", result.getPostcode());
376+
}
377+
378+
@Test
379+
void testPreferPostcodeFromPostcodeRelations() {
380+
PlacexTestRow parent = PlacexTestRow.make_street("Main St").add(jdbc);
381+
PlacexTestRow place = new PlacexTestRow("building", "yes")
382+
.addr("housenumber", "34")
383+
.postcode("XXX")
384+
.parent(parent).add(jdbc);
385+
PlacexTestRow postcode = new PlacexTestRow("boundary", "postal_code")
386+
.name("ref", "1234XZ").ranks(11).add(jdbc);
387+
388+
parent.addAddresslines(jdbc, postcode);
389+
390+
readEntireDatabase();
391+
392+
PhotonDoc result = importer.get(place);
393+
394+
assertEquals("1234XZ", result.getPostcode());
395+
}
396+
397+
@Test
398+
void testPreferPostcodeFromAddress() {
399+
PlacexTestRow parent = PlacexTestRow.make_street("Main St").add(jdbc);
400+
PlacexTestRow place = new PlacexTestRow("building", "yes")
401+
.addr("housenumber", "34")
402+
.addr("postcode", "45-234")
403+
.postcode("XXX")
404+
.parent(parent).add(jdbc);
405+
PlacexTestRow postcode = new PlacexTestRow("boundary", "postal_code")
406+
.name("ref", "1234XZ").ranks(11).add(jdbc);
407+
408+
parent.addAddresslines(jdbc, postcode);
409+
410+
readEntireDatabase();
411+
412+
PhotonDoc result = importer.get(place);
413+
414+
assertEquals("45-234", result.getPostcode());
415+
}
416+
363417
@Test
364418
void testGetImportDate() {
365419
Date importDate = connector.getLastImportDate();

‎src/test/java/de/komoot/photon/nominatim/testdb/PlacexTestRow.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class PlacexTestRow {
2323
private Integer rankAddress = 30;
2424
private Integer rankSearch = 30;
2525
private String centroid;
26+
private String postcode;
2627
private String countryCode = "us";
2728
private Double importance = null;
2829

@@ -115,12 +116,17 @@ public PlacexTestRow parent(PlacexTestRow row) {
115116
return this;
116117
}
117118

119+
public PlacexTestRow postcode(String postcode) {
120+
this.postcode = postcode;
121+
return this;
122+
}
123+
118124
public PlacexTestRow add(JdbcTemplate jdbc) {
119125
jdbc.update("INSERT INTO placex (place_id, parent_place_id, osm_type, osm_id, class, type, rank_search, rank_address,"
120-
+ " centroid, name, country_code, importance, address, indexed_status)"
121-
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ? FORMAT JSON, ?, ?, ? FORMAT JSON, 0)",
126+
+ " centroid, name, country_code, importance, address, postcode, indexed_status)"
127+
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ? FORMAT JSON, ?, ?, ? FORMAT JSON, ?, 0)",
122128
placeId, parentPlaceId, osmType, osmId, key, value, rankSearch, rankAddress, centroid,
123-
asJson(names), countryCode, importance, asJson(address));
129+
asJson(names), countryCode, importance, asJson(address), postcode);
124130

125131
return this;
126132
}

0 commit comments

Comments
 (0)
Please sign in to comment.