-
Notifications
You must be signed in to change notification settings - Fork 292
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
Fix #302: Read address attributes from nominatim #303
Changes from 5 commits
49c2125
04d386e
48c3eff
10dafda
ae5cf03
cc8211a
c4ecf1b
2e44f7a
c662672
5872237
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,9 @@ | |
import com.vividsolutions.jts.geom.Point; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
@@ -17,6 +19,7 @@ | |
*/ | ||
@Getter | ||
@Setter | ||
@Slf4j | ||
public class PhotonDoc { | ||
final private long placeId; | ||
final private String osmType; | ||
|
@@ -25,6 +28,7 @@ public class PhotonDoc { | |
final private String tagValue; | ||
final private Map<String, String> name; | ||
private String postcode; | ||
final private Map<String, String> address; | ||
final private Map<String, String> extratags; | ||
final private Envelope bbox; | ||
final private long parentPlaceId; // 0 if unset | ||
|
@@ -34,14 +38,16 @@ public class PhotonDoc { | |
final private int rankSearch; | ||
|
||
private Map<String, String> street; | ||
private Map<String, String> locality; | ||
private Map<String, String> district; | ||
private Map<String, String> city; | ||
private Set<Map<String, String>> context = new HashSet<Map<String, String>>(); | ||
private Map<String, String> country; | ||
private Map<String, String> state; | ||
private String houseNumber; | ||
private Point centroid; | ||
|
||
public PhotonDoc(long placeId, String osmType, long osmId, String tagKey, String tagValue, Map<String, String> name, String houseNumber, Map<String, String> extratags, Envelope bbox, long parentPlaceId, double importance, CountryCode countryCode, Point centroid, long linkedPlaceId, int rankSearch) { | ||
public PhotonDoc(long placeId, String osmType, long osmId, String tagKey, String tagValue, Map<String, String> name, String houseNumber, Map<String, String> address, Map<String, String> extratags, Envelope bbox, long parentPlaceId, double importance, CountryCode countryCode, Point centroid, long linkedPlaceId, int rankSearch) { | ||
String place = extratags != null ? extratags.get("place") : null; | ||
if (place != null) { | ||
// take more specific extra tag information | ||
|
@@ -56,6 +62,7 @@ public PhotonDoc(long placeId, String osmType, long osmId, String tagKey, String | |
this.tagValue = tagValue; | ||
this.name = name; | ||
this.houseNumber = houseNumber; | ||
this.address = address; | ||
this.extratags = extratags; | ||
this.bbox = bbox; | ||
this.parentPlaceId = parentPlaceId; | ||
|
@@ -75,6 +82,7 @@ public PhotonDoc(PhotonDoc other) { | |
this.name = other.name; | ||
this.houseNumber = other.houseNumber; | ||
this.postcode = other.postcode; | ||
this.address = other.address; | ||
this.extratags = other.extratags; | ||
this.bbox = other.bbox; | ||
this.parentPlaceId = other.parentPlaceId; | ||
|
@@ -84,6 +92,8 @@ public PhotonDoc(PhotonDoc other) { | |
this.linkedPlaceId = other.linkedPlaceId; | ||
this.rankSearch = other.rankSearch; | ||
this.street = other.street; | ||
this.locality = other.locality; | ||
this.district = other.district; | ||
this.city = other.city; | ||
this.context = other.context; | ||
this.country = other.country; | ||
|
@@ -102,7 +112,7 @@ public String getUid() { | |
*/ | ||
public static PhotonDoc create(long placeId, String osmType, long osmId, Map<String, String> nameMap) { | ||
return new PhotonDoc(placeId, osmType, osmId, "", "", nameMap, | ||
"", null, null, 0, 0, null, null, 0, 0); | ||
"", null, null, null, 0, 0, null, null, 0, 0); | ||
} | ||
|
||
public boolean isUsefulForIndex() { | ||
|
@@ -116,4 +126,62 @@ public boolean isUsefulForIndex() { | |
|
||
return true; | ||
} | ||
|
||
/** | ||
* Complete doc from nominatim address information. | ||
*/ | ||
public void completeFromAddress() { | ||
if (address == null) return; | ||
|
||
String addressStreet = address.get("street"); | ||
if (addressStreet != null) { | ||
if (this.street == null) { | ||
this.street = new HashMap<>(); | ||
} | ||
setOrReplace(addressStreet, this.street, "street"); | ||
} | ||
|
||
String addressCity = address != null ? address.get("city") : null; | ||
if (addressCity != null) { | ||
if (this.city == null) { | ||
this.city = new HashMap<>(); | ||
} | ||
setOrReplace(addressCity, this.city, "city"); | ||
} | ||
|
||
String addressDistrict = address != null ? address.get("suburb") : null; | ||
if (addressDistrict != null) { | ||
if (this.district == null) { | ||
this.district = new HashMap<>(); | ||
} | ||
setOrReplace(addressDistrict, this.district, "suburb"); | ||
} | ||
|
||
String addressLocality = address != null ? address.get("neighbourhood") : null; | ||
if (addressLocality != null) { | ||
if (this.locality == null) { | ||
this.locality = new HashMap<>(); | ||
} | ||
setOrReplace(addressLocality, this.locality, "neighbourhood"); | ||
} | ||
|
||
String addressPostCode = address != null ? address.get("postcode") : null; | ||
if (addressPostCode != null && !addressPostCode.equals(this.postcode)) { | ||
if (log.isDebugEnabled()) { | ||
log.debug("Replacing postcode "+this.postcode+" with "+ addressPostCode+ " for osmId #" + osmId); | ||
} | ||
this.postcode = addressPostCode; | ||
} | ||
} | ||
|
||
private void setOrReplace(String name, Map<String, String> namesMap, String field) { | ||
String existingName = namesMap.get("name"); | ||
if (!name.equals(existingName)) { | ||
if (log.isDebugEnabled()) { | ||
log.debug("Replacing "+ field +" name '"+existingName+"' with '"+ name+ "' for osmId #" + osmId); | ||
} | ||
namesMap.put("formerName", existingName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was more thinking of adding it to the context hashset. If you have a look at |
||
namesMap.put("name", name); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,13 @@ public boolean isStreet() { | |
return 26 <= rankAddress && rankAddress < 28; | ||
} | ||
|
||
public boolean isLocality() { | ||
return 22 <= rankAddress && rankAddress < 26; | ||
} | ||
|
||
public boolean isDistrict() { | ||
return 17 <= rankAddress && rankAddress < 22; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the other one, you should now use the rank address and get better results for that: suburb: [17, 21] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, that was misleading. I meant inclusive ranges here. So it should be:
and
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed that one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stylistically you should be returning the condition directly, that is Further I would suggest, as a rule, always adding javadoc to new methods. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, GitHub doesn't update the view of the lines you have commented on. It actually already does what you suggest, @simonpoole. And yes, I can add the javadoc. |
||
public boolean isCity() { | ||
return 13 <= rankAddress && rankAddress <= 16; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package de.komoot.photon; | ||
|
||
import java.util.HashMap; | ||
|
||
import org.hamcrest.core.IsEqual; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class PhotonDocTest { | ||
|
||
@Test | ||
public void testCompleteAddressOverwritesStreet() { | ||
HashMap<String, String> address = new HashMap<>(); | ||
address.put("street", "test street"); | ||
PhotonDoc doc = createPhotonDocWithAddress(address); | ||
|
||
HashMap<String, String> streetNames = new HashMap<>(); | ||
streetNames.put("name", "parent place street"); | ||
doc.setStreet(streetNames); | ||
|
||
doc.completeFromAddress(); | ||
Assert.assertThat(doc.getStreet().get("name"), IsEqual.equalTo("test street")); | ||
} | ||
|
||
@Test | ||
public void testCompleteAddressCreatesStreetIfNonExistantBefore() { | ||
HashMap<String, String> address = new HashMap<>(); | ||
address.put("street", "test street"); | ||
PhotonDoc doc = createPhotonDocWithAddress(address); | ||
|
||
doc.completeFromAddress(); | ||
Assert.assertThat(doc.getStreet().get("name"), IsEqual.equalTo("test street")); | ||
} | ||
|
||
private PhotonDoc createPhotonDocWithAddress(HashMap<String, String> address) { | ||
return new PhotonDoc(1, "W", 2, "highway", "residential", null, "4", address, null, null, 0, 30, null, null, 0, 30); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code duplication can be avoided if you move the lookup into setOrReplace as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed some duplication but not as much as I would have liked as the assignment to the instance fields (
this.locality
...) must happen outsidesetOrReplace
or am I overlooking something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking along the lines of having a function which can be used like this:
Nevermind for now. It is good as is.