use of org.opennms.features.geocoder.Coordinates in project opennms by OpenNMS.
the class OpenlayersWidgetCompontentTest method testGeolocation.
@Test
@Ignore
public void testGeolocation() throws Exception {
final OnmsNode node = new OnmsNode();
final OnmsAssetRecord asset = new OnmsAssetRecord();
final OnmsGeolocation geo = new OnmsGeolocation();
node.setId(1);
node.setAssetRecord(asset);
asset.setGeolocation(geo);
geo.setAddress1("220 Chatham Business Dr.");
geo.setCity("Pittsboro");
geo.setState("NC");
geo.setZip("27312");
assertEquals("220 Chatham Business Dr., Pittsboro, NC 27312", geo.asAddressString());
EasyMock.expect(m_geocoder.getCoordinates(geo.asAddressString())).andReturn(new Coordinates(-1.0f, 1.0f)).times(1);
final PaintTarget target = EasyMock.createMock(PaintTarget.class);
m_assetDao.saveOrUpdate(EasyMock.isA(OnmsAssetRecord.class));
target.startTag(EasyMock.eq("1"));
target.addAttribute(EasyMock.eq("longitude"), EasyMock.eq("-1.0"));
target.addAttribute(EasyMock.eq("latitude"), EasyMock.eq("1.0"));
target.endTag(EasyMock.eq("1"));
EasyMock.replay(m_nodeDao, m_assetDao, m_geocoder, target);
// m_component.paintNode(target, node);
EasyMock.verify(m_nodeDao, m_assetDao, m_geocoder, target);
}
use of org.opennms.features.geocoder.Coordinates 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