Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take postcodes from postcode relations into account #870

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/main/java/de/komoot/photon/PhotonDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ public PhotonDoc address(Map<String, String> address) {
extractAddress(address, AddressType.COUNTY, "county");
extractAddress(address, AddressType.STATE, "state");

String addressPostCode = address.get("postcode");
if (addressPostCode != null && !addressPostCode.equals(postcode)) {
LOGGER.debug("Replacing postcode {} with {} for osmId #{}", postcode, addressPostCode, osmId);
postcode = addressPostCode;
}
postcode = address.getOrDefault("postcode", postcode);
}
return this;
}
Expand Down Expand Up @@ -211,9 +207,6 @@ public boolean isUsefulForIndex() {
*
* @param addressType The type of address field to fill.
* @param addressFieldName The name of the address tag to use (without the 'addr:' prefix).
*
* @return 'existingField' potentially with the name field replaced. If existingField was null and
* the address field could be found, then a new map with the address as single entry is returned.
*/
private void extractAddress(Map<String, String> address, AddressType addressType, String addressFieldName) {
String field = address.get(addressFieldName);
Expand Down Expand Up @@ -258,13 +251,17 @@ public boolean setAddressPartIfNew(AddressType addressType, Map<String, String>
public void completePlace(List<AddressRow> addresses) {
final AddressType doctype = getAddressType();
for (AddressRow address : addresses) {
final AddressType atype = address.getAddressType();

if (atype != null
&& (atype == doctype || !setAddressPartIfNew(atype, address.getName()))
&& address.isUsefulForContext()) {
// no specifically handled item, check if useful for context
getContext().add(address.getName());
if (address.isPostcode()) {
this.postcode = address.getName().getOrDefault("ref", this.postcode);
} else {
final AddressType atype = address.getAddressType();

if (atype != null
&& (atype == doctype || !setAddressPartIfNew(atype, address.getName()))
&& address.isUsefulForContext()) {
// no specifically handled item, check if useful for context
getContext().add(address.getName());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public AddressType getAddressType() {
return AddressType.fromRank(rankAddress);
}

private boolean isPostcode() {
public boolean isPostcode() {
if ("place".equals(osmKey) && "postcode".equals(osmValue)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,60 @@ void testNoCountry() {
assertNull(importer.get(place).getCountryCode());
}

@Test
void testUsePostcodeFromPlacex() {
PlacexTestRow parent = PlacexTestRow.make_street("Main St").add(jdbc);
PlacexTestRow place = new PlacexTestRow("building", "yes")
.addr("housenumber", "34")
.postcode("AA 44XH")
.parent(parent).add(jdbc);

readEntireDatabase();

PhotonDoc result = importer.get(place);

assertEquals("AA 44XH", result.getPostcode());
}

@Test
void testPreferPostcodeFromPostcodeRelations() {
PlacexTestRow parent = PlacexTestRow.make_street("Main St").add(jdbc);
PlacexTestRow place = new PlacexTestRow("building", "yes")
.addr("housenumber", "34")
.postcode("XXX")
.parent(parent).add(jdbc);
PlacexTestRow postcode = new PlacexTestRow("boundary", "postal_code")
.name("ref", "1234XZ").ranks(11).add(jdbc);

parent.addAddresslines(jdbc, postcode);

readEntireDatabase();

PhotonDoc result = importer.get(place);

assertEquals("1234XZ", result.getPostcode());
}

@Test
void testPreferPostcodeFromAddress() {
PlacexTestRow parent = PlacexTestRow.make_street("Main St").add(jdbc);
PlacexTestRow place = new PlacexTestRow("building", "yes")
.addr("housenumber", "34")
.addr("postcode", "45-234")
.postcode("XXX")
.parent(parent).add(jdbc);
PlacexTestRow postcode = new PlacexTestRow("boundary", "postal_code")
.name("ref", "1234XZ").ranks(11).add(jdbc);

parent.addAddresslines(jdbc, postcode);

readEntireDatabase();

PhotonDoc result = importer.get(place);

assertEquals("45-234", result.getPostcode());
}

@Test
void testGetImportDate() {
Date importDate = connector.getLastImportDate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class PlacexTestRow {
private Integer rankAddress = 30;
private Integer rankSearch = 30;
private String centroid;
private String postcode;
private String countryCode = "us";
private Double importance = null;

Expand Down Expand Up @@ -115,12 +116,17 @@ public PlacexTestRow parent(PlacexTestRow row) {
return this;
}

public PlacexTestRow postcode(String postcode) {
this.postcode = postcode;
return this;
}

public PlacexTestRow add(JdbcTemplate jdbc) {
jdbc.update("INSERT INTO placex (place_id, parent_place_id, osm_type, osm_id, class, type, rank_search, rank_address,"
+ " centroid, name, country_code, importance, address, indexed_status)"
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ? FORMAT JSON, ?, ?, ? FORMAT JSON, 0)",
+ " centroid, name, country_code, importance, address, postcode, indexed_status)"
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ? FORMAT JSON, ?, ?, ? FORMAT JSON, ?, 0)",
placeId, parentPlaceId, osmType, osmId, key, value, rankSearch, rankAddress, centroid,
asJson(names), countryCode, importance, asJson(address));
asJson(names), countryCode, importance, asJson(address), postcode);

return this;
}
Expand Down