use of net.simon04.jelementtree.ElementTree in project opennms by OpenNMS.
the class MapquestGeocoder method geocode.
/**
* {@inheritDoc}
*/
@Override
public GWTLatLng geocode(final String geolocation) throws GeocoderException {
final HttpUriRequest method = new HttpGet(getUrl(geolocation));
method.addHeader("User-Agent", "OpenNMS-MapQuestGeocoder/1.0");
method.addHeader("Referer", m_referer);
try {
InputStream responseStream = m_httpClient.execute(method).getEntity().getContent();
final ElementTree tree = ElementTree.fromStream(responseStream);
if (tree == null) {
throw new GeocoderException("an error occurred connecting to the MapQuest geocoding service (no XML tree was found)");
}
final ElementTree statusCode = tree.find("//statusCode");
if (statusCode == null || !statusCode.getText().equals("0")) {
final String code = (statusCode == null ? "unknown" : statusCode.getText());
final ElementTree messageTree = tree.find("//message");
final String message = (messageTree == null ? "unknown" : messageTree.getText());
throw new GeocoderException("an error occurred when querying MapQuest (statusCode=" + code + ", message=" + message + ")");
}
final List<ElementTree> locations = tree.findAll("//location");
if (locations.size() > 1) {
LOG.warn("more than one location returned for query: {}", geolocation);
} else if (locations.size() == 0) {
throw new GeocoderException("MapQuest returned an OK status code, but no locations");
}
final ElementTree location = locations.get(0);
// first, check the quality
if (m_minimumQuality != null) {
final Quality geocodeQuality = Quality.valueOf(location.find("//geocodeQuality").getText().toUpperCase());
if (geocodeQuality.compareTo(m_minimumQuality) < 0) {
throw new GeocoderException("response did not meet minimum quality requirement (" + geocodeQuality + " is less specific than " + m_minimumQuality + ")");
}
}
// then, extract the lat/lng
final ElementTree latLng = location.find("//latLng");
Double latitude = Double.valueOf(latLng.find("//lat").getText());
Double longitude = Double.valueOf(latLng.find("//lng").getText());
return new GWTLatLng(latitude, longitude);
} catch (GeocoderException e) {
throw e;
} catch (Throwable e) {
throw new GeocoderException("unable to get lat/lng from MapQuest", e);
}
}
use of net.simon04.jelementtree.ElementTree in project opennms by OpenNMS.
the class NominatimGeocoder method geocode.
/**
* {@inheritDoc}
*/
@Override
public GWTLatLng geocode(final String geolocation) throws GeocoderException {
final HttpUriRequest method = new HttpGet(getUrl(geolocation));
method.addHeader("User-Agent", "OpenNMS-MapquestGeocoder/1.0");
if (m_referer != null) {
method.addHeader("Referer", m_referer);
}
CloseableHttpResponse response = null;
try {
response = m_clientWrapper.execute(method);
InputStream responseStream = response.getEntity().getContent();
final ElementTree tree = ElementTree.fromStream(responseStream);
if (tree == null) {
throw new GeocoderException("an error occurred connecting to the Nominatim geocoding service (no XML tree was found)");
}
final List<ElementTree> places = tree.findAll("//place");
if (places.size() > 1) {
LOG.warn("more than one location returned for query: {}", geolocation);
} else if (places.size() == 0) {
throw new GeocoderException("Nominatim returned an OK status code, but no places");
}
final ElementTree place = places.get(0);
Double latitude = Double.valueOf(place.getAttribute("lat"));
Double longitude = Double.valueOf(place.getAttribute("lon"));
return new GWTLatLng(latitude, longitude);
} catch (GeocoderException e) {
throw e;
} catch (Throwable e) {
throw new GeocoderException("unable to get lat/lng from Nominatim", e);
} finally {
m_clientWrapper.close(response);
}
}
use of net.simon04.jelementtree.ElementTree in project opennms by OpenNMS.
the class NominatimGeocoderService method getCoordinates.
@Override
public Coordinates getCoordinates(final String address) throws GeocoderException {
final HttpUriRequest method = new HttpGet(getUrl(address));
method.addHeader("User-Agent", "OpenNMS-NominatimGeocoderService/1.0");
if (m_referer != null && !"".equals(m_referer)) {
method.addHeader("Referer", m_referer);
}
InputStream responseStream = null;
CloseableHttpResponse response = null;
try {
response = m_clientWrapper.execute(method);
final StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() != 200) {
throw new GeocoderException("Nominatim returned a non-OK response code: " + statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
}
responseStream = response.getEntity().getContent();
final ElementTree tree = ElementTree.fromStream(responseStream);
if (tree == null) {
throw new GeocoderException("an error occurred connecting to the Nominatim geocoding service (no XML tree was found)");
}
final List<ElementTree> places = tree.findAll("//place");
if (places.size() > 1) {
m_log.warn("More than one location returned for query: {}", address);
} else if (places.size() == 0) {
throw new GeocoderException("Nominatim returned an OK status code, but no places");
}
final ElementTree place = places.get(0);
final Float longitude = Float.valueOf(place.getAttribute("lon"));
final Float latitude = Float.valueOf(place.getAttribute("lat"));
return new Coordinates(longitude, latitude);
} catch (final GeocoderException e) {
throw e;
} catch (final Throwable e) {
throw new GeocoderException("unable to get lon/lat from Nominatim", e);
} finally {
IOUtils.closeQuietly(responseStream);
m_clientWrapper.close(response);
}
}
Aggregations