Skip to content

Commit

Permalink
When building a reverse search URL, try both OSM types "W" and "N"
Browse files Browse the repository at this point in the history
Constructing a Nominatim reverse search URL requires both, OSM Id
and OSM type. The first try is done using OSM Type "W" (way), the
second try with OSM Type "N" (node).

References #14490
  • Loading branch information
omessner committed Mar 8, 2016
1 parent 292ecbb commit 8ad51b1
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,18 @@ public List<Address> getAddressesWherePlaceIsIn(Long placeId) {
@Override
public Address getAddressByOsmId(long osmId) {

String suburbUrl = nominatimUrlBuilder.buildOsmUrl(osmId);
List<Address> foundAddresses = nominatimResponder.getAddressesFromOSMId(suburbUrl);
// Note that both, OSM Id and OSM Type are required for the Nominatim reverse search
// first try using OSM Type WAY
String url = nominatimUrlBuilder.buildOsmUrl(osmId, OsmType.WAY);
List<Address> addresses = nominatimResponder.getAddressesFromOSMId(url);

if (addresses.get(0) == null || addresses.get(0).getOsmId() != osmId) {
// second try using OSM Type NODE
url = nominatimUrlBuilder.buildOsmUrl(osmId, OsmType.NODE);
addresses = nominatimResponder.getAddressesFromOSMId(url);
}

return foundAddresses.get(0);
return addresses.get(0);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class NominatimUrlBuilder {
private static final String ADDRESS_DETAILS = "&addressdetails=1";
private static final String COUNTRY = "&countrycodes=";
private static final String WHITESPACE = " ";
private static final String OSMTYPE_W = "&osm_type=W";
private static final String OSMID = "osm_id=";
private static final String REVERSE_URL = "reverse?";
private static final String LOG_TWO_PLACEHOLDERS = "{}{}";
Expand Down Expand Up @@ -107,15 +106,18 @@ String buildSuburbUrl(long placeId, String suburbType) {


/**
* Build the osm url within the osm id.
* Builds an URL for a Nominatim reverse search. Note that a reverse search requires both, an OSM Id and OSM Type.
*
* @param osmId to build osm url
* @param osmId the OSM Id
* @param osmType the OSM Type
*
* @return The URL for requesting ONE Address by osmId. The osm Type used is "N" for Node by default.
* @return a reverse search URL
*
* @see OsmType
*/
String buildOsmUrl(long osmId) {
String buildOsmUrl(long osmId, OsmType osmType) {

String osmUrl = baseUrl + REVERSE_URL + OSMID + osmId + OSMTYPE_W + FORMAT;
String osmUrl = baseUrl + REVERSE_URL + OSMID + osmId + "&osm_type=" + osmType.getKey() + FORMAT;
LOG.debug(LOG_TWO_PLACEHOLDERS, LOG_BUILT_URL, osmUrl);

return osmUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.contargo.iris.address.nominatim.service;

/**
* @author Oliver Messner - [email protected]
*/
enum OsmType {

WAY {

@Override
String getKey() {

return "W";
}
},

NODE {

@Override
String getKey() {

return "N";
}
};

abstract String getKey();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import org.junit.runner.RunWith;

import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;

import org.mockito.runners.MockitoJUnitRunner;

Expand Down Expand Up @@ -41,6 +43,7 @@
import static org.mockito.Matchers.anyString;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import static java.util.Arrays.asList;
Expand Down Expand Up @@ -186,17 +189,40 @@ public void reverseLookupAddress() {


@Test
public void testGetAddressByOsmId() {
public void getAddressByOsmIdWithOsmTypeWAY() {

Address expectedAddress = new Address();
expectedAddress.setDisplayName("Alleinstellungsmerkmal für die expectedAddress");
expectedAddress.setOsmId(OSM_ID);

when(nominatimUrlBuilderMock.buildOsmUrl(OSM_ID)).thenReturn(DUMMY_URL);
when(nominatimUrlBuilderMock.buildOsmUrl(OSM_ID, OsmType.WAY)).thenReturn(DUMMY_URL);
when(nominatimResponderMock.getAddressesFromOSMId(DUMMY_URL)).thenReturn(singletonList(expectedAddress));

Address actualAddress = sut.getAddressByOsmId(OSM_ID);
assertThat(sut.getAddressByOsmId(OSM_ID), is(expectedAddress));

assertThat(actualAddress, is(expectedAddress));
verify(nominatimUrlBuilderMock).buildOsmUrl(OSM_ID, OsmType.WAY);
verifyNoMoreInteractions(nominatimUrlBuilderMock);
}


@Test
public void getAddressByOsmIdWithOsmTypeNODE() {

Address addressWithoutExpectedOsmId = new Address();
addressWithoutExpectedOsmId.setOsmId(0);

Address expectedAddress = new Address();
expectedAddress.setOsmId(OSM_ID);

when(nominatimUrlBuilderMock.buildOsmUrl(OSM_ID, OsmType.WAY)).thenReturn(null);
when(nominatimUrlBuilderMock.buildOsmUrl(OSM_ID, OsmType.NODE)).thenReturn(DUMMY_URL);
when(nominatimResponderMock.getAddressesFromOSMId(null)).thenReturn(singletonList(addressWithoutExpectedOsmId));
when(nominatimResponderMock.getAddressesFromOSMId(DUMMY_URL)).thenReturn(singletonList(expectedAddress));

assertThat(sut.getAddressByOsmId(OSM_ID), is(expectedAddress));

InOrder inOrder = Mockito.inOrder(nominatimUrlBuilderMock);
inOrder.verify(nominatimUrlBuilderMock).buildOsmUrl(OSM_ID, OsmType.WAY);
inOrder.verify(nominatimUrlBuilderMock).buildOsmUrl(OSM_ID, OsmType.NODE);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,17 @@ public void testBuildSuburbUrlForTypeVillage() {


@Test
public void testBuildOsmUrl() {
public void buildOsmUrl() {

String url = sut.buildOsmUrl(OSM_ID);
assertThat(url,
// using OsmType.WAY
assertThat(sut.buildOsmUrl(OSM_ID, OsmType.WAY),
containsString("http://maps.contargo.net/nominatim/reverse?osm_id=" + OSM_ID
+ "&osm_type=W&format=json"));

// using OsmType.NODE
assertThat(sut.buildOsmUrl(OSM_ID, OsmType.NODE),
containsString("http://maps.contargo.net/nominatim/reverse?osm_id=" + OSM_ID
+ "&osm_type=N&format=json"));
}


Expand Down

0 comments on commit 8ad51b1

Please sign in to comment.