Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
Pull request #20 adds support for lookup API with testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kops committed Sep 9, 2015
2 parents 0a83678 + f238d9e commit 223d813
Show file tree
Hide file tree
Showing 8 changed files with 430 additions and 10 deletions.
38 changes: 38 additions & 0 deletions src/main/java/fr/dudie/nominatim/client/JsonNominatimClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import fr.dudie.nominatim.client.request.NominatimLookupRequest;
import fr.dudie.nominatim.client.request.NominatimReverseRequest;
import fr.dudie.nominatim.client.request.NominatimSearchRequest;
import fr.dudie.nominatim.client.request.paramhelper.OsmType;
Expand All @@ -52,6 +53,7 @@
* An implementation of the Nominatim Api Service.
*
* @author Jérémie Huchet
* @author Sunil D S
*/
public final class JsonNominatimClient implements NominatimClient {

Expand All @@ -72,6 +74,9 @@ public final class JsonNominatimClient implements NominatimClient {

/** The url for reverse geocoding. */
private final String reverseUrl;

/** The url for address lookup. */
private final String lookupUrl;

/** The default search options. */
private final NominatimOptions defaults;
Expand All @@ -84,6 +89,9 @@ public final class JsonNominatimClient implements NominatimClient {

/** The default response handler for reverse geocoding requests. */
private NominatimResponseHandler<Address> defaultReverseGeocodingHandler;

/** The default response handler for lookup requests. */
private NominatimResponseHandler<List<Address>> defaultLookupHandler;

/**
* Creates the json nominatim client with the default base URL ({@value #DEFAULT_BASE_URL}.
Expand Down Expand Up @@ -152,6 +160,7 @@ public JsonNominatimClient(final String baseUrl, final HttpClient httpClient, fi
}
this.searchUrl = String.format("%s/search?format=json&email=%s", baseUrl.replaceAll("/$", ""), emailEncoded);
this.reverseUrl = String.format("%s/reverse?format=json&email=%s", baseUrl.replaceAll("/$", ""), emailEncoded);
this.lookupUrl = String.format("%s/lookup?format=json&email=%s", baseUrl.replaceAll("/$", ""), emailEncoded);

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("API search URL: {}", searchUrl);
Expand All @@ -175,6 +184,8 @@ public JsonNominatimClient(final String baseUrl, final HttpClient httpClient, fi
defaultSearchResponseHandler = new NominatimResponseHandler<List<Address>>(gsonInstance, new TypeToken<List<Address>>() {
}.getType());
defaultReverseGeocodingHandler = new NominatimResponseHandler<Address>(gsonInstance, Address.class);
defaultLookupHandler = new NominatimResponseHandler<List<Address>>(gsonInstance, new TypeToken<List<Address>>() {
}.getType());
}

/**
Expand Down Expand Up @@ -205,6 +216,20 @@ public Address getAddress(final NominatimReverseRequest reverse) throws IOExcept
return httpClient.execute(req, defaultReverseGeocodingHandler);
}

/**
* {@inheritDoc}
*
* @see fr.dudie.nominatim.client.NominatimClient#lookupAddress(fr.dudie.nominatim.client.request.NominatimLookupRequest)
*/
@Override
public List<Address> lookupAddress(final NominatimLookupRequest lookup) throws IOException {

final String apiCall = String.format("%s&%s", lookupUrl, lookup.getQueryString());
LOGGER.debug("lookup url: {}", apiCall);
final HttpGet req = new HttpGet(apiCall);
return httpClient.execute(req, defaultLookupHandler);
}

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -270,4 +295,17 @@ public Address getAddress(final String type, final long id) throws IOException {
q.setQuery(OsmType.from(type), id);
return this.getAddress(q);
}

/**
* {@inheritDoc}
*
* @see fr.dudie.nominatim.client.NominatimClient#lookupAddress(java.util.List)
*/
@Override
public List<Address> lookupAddress(final List<String> typeId) throws IOException {

final NominatimLookupRequest q = new NominatimLookupRequest();
q.setQuery(typeId);
return this.lookupAddress(q);
}
}
26 changes: 26 additions & 0 deletions src/main/java/fr/dudie/nominatim/client/NominatimClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.util.List;

import fr.dudie.nominatim.client.request.NominatimLookupRequest;
import fr.dudie.nominatim.client.request.NominatimReverseRequest;
import fr.dudie.nominatim.client.request.NominatimSearchRequest;
import fr.dudie.nominatim.model.Address;
Expand All @@ -33,6 +34,7 @@
* Interface to use the Nominatim Service.
*
* @author Jérémie Huchet
* @author Sunil D S
* @since 1.0
*/
public interface NominatimClient {
Expand Down Expand Up @@ -109,6 +111,18 @@ public interface NominatimClient {
*/
@Deprecated
Address getAddress(String type, long id) throws IOException;

/**
* This method can be used to lookup addresses with an OSM type and ID
*
* @param typeId
* [N|W|R]ID
* @return a list of addresses corresponding to the given OSM type and ID or <code>null</code> if no result found
* @throws IOException
* a communication error occurred
* @since 3.2
*/
List<Address> lookupAddress(List<String> typeId) throws IOException;

/**
* Search for addresses.
Expand All @@ -133,5 +147,17 @@ public interface NominatimClient {
* @since 3.0
*/
Address getAddress(NominatimReverseRequest reverse) throws IOException;

/**
* Address lookup request.
*
* @param lookup
* a lookup request
* @return a list of addresses corresponding to the given OSM type and ID or <code>null</code> if no result found
* @throws IOException
* a communication error occurred
* @since 3.2
*/
List<Address> lookupAddress(NominatimLookupRequest lookup) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import java.lang.reflect.Type;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ResponseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

Expand All @@ -41,10 +41,6 @@
*/
public final class NominatimResponseHandler<T> implements ResponseHandler<T> {

/** The event logger. */
private static final Logger LOGGER = LoggerFactory
.getLogger(NominatimResponseHandler.class);

/** Gson instance for Nominatim API calls. */
private final Gson gsonInstance;

Expand Down Expand Up @@ -77,6 +73,10 @@ public T handleResponse(final HttpResponse response) throws IOException {
final T addresses;

try {
final StatusLine status = response.getStatusLine();
if (status.getStatusCode() >= HttpStatus.SC_BAD_REQUEST) {
throw new IOException(String.format("HTTP error: %s %s", status.getStatusCode(), status.getReasonPhrase()));
}
content = response.getEntity().getContent();
addresses = gsonInstance
.fromJson(new InputStreamReader(content, "utf-8"), responseType);
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/fr/dudie/nominatim/client/request/LookupQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fr.dudie.nominatim.client.request;

/*
* [license]
* Nominatim Java API client
* ~~~~
* Copyright (C) 2010 - 2014 Dudie
* ~~~~
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* [/license]
*/

/**
* Holds parameters for a lookup query.
*
* @author Sunil D S
*/
abstract class LookupQuery extends NominatimRequest {

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package fr.dudie.nominatim.client.request;

/*
* [license]
* Nominatim Java API client
* ~~~~
* Copyright (C) 2010 - 2014 Dudie
* ~~~~
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* [/license]
*/

import java.util.List;

import fr.dudie.nominatim.client.request.paramhelper.BooleanSerializer;
import fr.dudie.nominatim.client.request.paramhelper.QueryParameter;

/**
* Holds request parameters for a lookup request.
* <p>
* Attributes documentation was extracted from <a href="http://wiki.openstreetmap.org/wiki/Nominatim">Nominatim Wiki</a>
* page on September 1st, 2015.
*
* @author Sunil D S
*/
public class NominatimLookupRequest extends NominatimRequest {

/**
* Preferred language order for showing search results, overrides the browser value. Either uses standard rfc2616
* accept-language string or a simple comma separated list of language codes.
*/
@QueryParameter("accept-language=%s")
private String acceptLanguage;

/** Holds the OSM lookup request. */
@QueryParameter
private LookupQuery query;

/** Include a breakdown of the address into elements. */
@QueryParameter(value = "addressdetails=%s", serializer = BooleanSerializer.class)
private Boolean addressDetails;

/**
* Gets the preferred language order for showing search results which overrides the browser value.
*
* @return the accept-language value
*/
public String getAcceptLanguage() {
return acceptLanguage;
}

/**
* Sets the preferred language order for showing search results, overrides the browser value.
*
* @param acceptLanguage
* a standard rfc2616 accept-language string or a simple comma separated list of language codes
*/
public void setAcceptLanguage(final String acceptLanguage) {
this.acceptLanguage = acceptLanguage;
}

/**
* @return the lookup query parameters
*/
public LookupQuery getQuery() {
return query;
}

/**
* @param query
* the lookup query parameters to set
*/
public void setQuery(final LookupQuery query) {
this.query = query;
}

public void setQuery(final List<String> typeId) {
this.query = new OsmTypeAndIdLookupQuery(typeId);
}

/**
* Include a breakdown of the address into elements.
*
* @return the addressDetails
*/
public Boolean getAddressDetails() {
return addressDetails;
}

/**
* Include a breakdown of the address into elements.
*
* @param addressDetails
* the addressDetails to set
*/
public void setAddressDetails(final boolean addressDetails) {
this.addressDetails = addressDetails;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package fr.dudie.nominatim.client.request;

/*
* [license]
* Nominatim Java API client
* ~~~~
* Copyright (C) 2010 - 2014 Dudie
* ~~~~
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* [/license]
*/

import java.util.List;

import fr.dudie.nominatim.client.request.paramhelper.ListSerializer;
import fr.dudie.nominatim.client.request.paramhelper.QueryParameter;

/**
* Holds OSM TYPE and ID of the lookup request.
* <p>
* Attributes documentation was extracted from <a href="http://wiki.openstreetmap.org/wiki/Nominatim">Nominatim Wiki</a>
* page on September 1st, 2015.
*
* @author Sunil D S
*/
public class OsmTypeAndIdLookupQuery extends LookupQuery {

/**
* List of type and id of the elements to lookup.
*/
@QueryParameter(value = "osm_ids=%s", serializer = ListSerializer.class)
private List<String> typeId;

/**
* @param typeId
* list of type and id of the elements to lookup
*/
public OsmTypeAndIdLookupQuery(final List<String> typeId) {
this.typeId = typeId;
}

/**
* @return the typeId
*/
public List<String> getTypeId() {
return typeId;
}

/**
* @param typeId
* list of type and id of the elements to lookup set
*/
public void setTypeId(List<String> typeId) {
this.typeId = typeId;
}
}
Loading

0 comments on commit 223d813

Please sign in to comment.