Skip to content

Commit 43c5128

Browse files
authored
Merge pull request #917 from lonvia/conflicting-address-tags
Correctly handle addr:street and addr:place appearing together
2 parents 80e2a47 + 593526e commit 43c5128

File tree

3 files changed

+53
-35
lines changed

3 files changed

+53
-35
lines changed

src/main/java/de/komoot/photon/PhotonDocAddressSet.java

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,45 @@ public Iterator<PhotonDoc> iterator() {
3131
}
3232

3333
private void addPlaceAddress(PhotonDoc base, Map<String, String> address, String key) {
34-
Map<AddressType, Map<String, String>> placeAddress = null;
35-
for (String hnr : splitHousenumber(address, key)) {
36-
if (placeAddress == null) {
37-
placeAddress = new EnumMap<>(AddressType.class);
38-
39-
for (var entry : base.getAddressParts().entrySet()) {
40-
if (entry.getKey() != AddressType.STREET) {
41-
placeAddress.put(entry.getKey(), entry.getValue());
42-
}
34+
final String place = address.get("place");
35+
if (place != null && !place.isBlank()) {
36+
Map<AddressType, Map<String, String>> placeAddress = null;
37+
for (String hnr : splitHousenumber(address, key)) {
38+
if (placeAddress == null) {
39+
placeAddress = copyAddressWithStreet(base, place);
4340
}
4441

45-
final String place = address.get("place");
46-
if (place != null && !place.isBlank()) {
47-
placeAddress.put(AddressType.STREET, Map.of("default", place));
48-
}
42+
docs.add(new PhotonDoc(base).replaceAddress(placeAddress).houseNumber(hnr));
4943
}
50-
51-
docs.add(new PhotonDoc(base).replaceAddress(placeAddress).houseNumber(hnr));
5244
}
5345
}
5446

5547
private void addStreetAddress(PhotonDoc base, Map<String, String> address, String key) {
56-
for (String hnr : splitHousenumber(address, key)) {
57-
docs.add(new PhotonDoc(base).houseNumber(hnr));
48+
if (address.containsKey("street")) {
49+
for (String hnr : splitHousenumber(address, key)) {
50+
docs.add(new PhotonDoc(base).houseNumber(hnr));
51+
}
5852
}
5953
}
6054

6155
private void addGenericAddress(PhotonDoc base, Map<String, String> address, String key) {
6256
Map<AddressType, Map<String, String>> placeAddress = null;
6357
for (String hnr : splitHousenumber(address, key)) {
6458
if (placeAddress == null) {
65-
String place = address.get("place");
66-
if (place == null || place.isBlank()) {
67-
place = address.get("block_number");
68-
}
69-
if (place != null && !place.isBlank()) {
70-
placeAddress = new EnumMap<>(AddressType.class);
71-
72-
for (var entry : base.getAddressParts().entrySet()) {
73-
if (entry.getKey() != AddressType.STREET) {
74-
placeAddress.put(entry.getKey(), entry.getValue());
75-
}
76-
}
77-
78-
placeAddress.put(AddressType.STREET, Map.of("default", place));
59+
String block = address.get("block_number");
60+
if (block != null && !block.isBlank()) {
61+
// block numbers replace streets unconditionally.
62+
// We assume that addr:street is a tagging error and ignore it.
63+
placeAddress = copyAddressWithStreet(base, block);
7964
} else {
80-
placeAddress = base.getAddressParts();
65+
String place = address.get("place");
66+
// When addr:place and addr:Street appear together, then assume
67+
// that addr:place is a tagging error and ignore it.
68+
if (place != null && !place.isBlank() && !address.containsKey("street")) {
69+
placeAddress = copyAddressWithStreet(base, place);
70+
} else {
71+
placeAddress = base.getAddressParts();
72+
}
8173
}
8274
}
8375

@@ -98,4 +90,18 @@ private String[] splitHousenumber(Map<String, String> address, String key) {
9890
.toArray(String[]::new);
9991
}
10092

93+
private EnumMap<AddressType, Map<String, String>> copyAddressWithStreet(PhotonDoc base, String streetName) {
94+
final EnumMap<AddressType, Map<String, String>> outmap = new EnumMap<>(AddressType.class);
95+
96+
for (var entry : base.getAddressParts().entrySet()) {
97+
if (entry.getKey() != AddressType.STREET) {
98+
outmap.put(entry.getKey(), entry.getValue());
99+
}
100+
}
101+
102+
outmap.put(AddressType.STREET, Map.of("default", streetName));
103+
104+
return outmap;
105+
}
106+
101107
}

src/test/java/de/komoot/photon/PhotonDocAddressSetTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,30 @@ void testHousenumberList() {
8989
void testPlaceAddress() {
9090
assertThat(new PhotonDocAddressSet(baseDoc, Map.of(
9191
"housenumber", "34;50 b",
92-
"place", "Nowhere",
93-
"street", "irrelevant")))
92+
"place", "Nowhere")))
9493
.satisfiesExactly(
9594
d -> assertDocWithHnrAndStreet(d, "34", "Nowhere"),
9695
d2 -> assertDocWithHnrAndStreet(d2, "50 b", "Nowhere"));
9796
}
9897

98+
@Test
99+
void testPlaceAndStreetAddress() {
100+
assertThat(new PhotonDocAddressSet(baseDoc, Map.of(
101+
"housenumber", "34",
102+
"place", "Nowhere",
103+
"street", "Chaussee")))
104+
.satisfiesExactly(
105+
d -> assertDocWithHnrAndStreet(d, "34", "Chaussee"));
106+
}
107+
99108
@Test
100109
void testConscriptionAddress() {
101110
assertThat(new PhotonDocAddressSet(baseDoc, Map.of(
102111
"housenumber", "34/50",
103112
"conscriptionnumber", "50",
104113
"streetnumber", "34",
105-
"place", "Nowhere")))
114+
"place", "Nowhere",
115+
"street", "Chaussee")))
106116
.satisfiesExactly(
107117
d -> assertDocWithHnrAndStreet(d, "50", "Nowhere"),
108118
d2 -> assertDocWithHnrAndStreet(d2, "34", "Chaussee"));
@@ -118,6 +128,7 @@ void testBlockAddress() {
118128
);
119129
}
120130

131+
121132
@ParameterizedTest
122133
@ValueSource(strings = {
123134
"987987誰も住んでいないスーパーマーケット誰も住んでいないスーパーマーケット誰も住んでいないスーパーマーケット誰も住んでいないスーパーマーケット誰も住んでいないスーパーマーケット誰も住んでいないスーパーマーケット誰も住んでいないスーパーマーケット誰も住んでいないスーパーマーケット誰も住んでいないスーパーマー",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ void testObjectWithConscriptionNumber() {
341341
.addr("streetnumber", "34")
342342
.addr("conscriptionnumber", "99521")
343343
.addr("place", "Village")
344+
.addr("street", "Main St")
344345
.parent(parent).add(jdbc);
345346

346347
place.addAddresslines(jdbc,

0 commit comments

Comments
 (0)