Search in sources :

Example 1 with Coordinates

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);
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) Coordinates(org.opennms.features.geocoder.Coordinates) PaintTarget(com.vaadin.server.PaintTarget) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with Coordinates

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);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StatusLine(org.apache.http.StatusLine) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Coordinates(org.opennms.features.geocoder.Coordinates) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) GeocoderException(org.opennms.features.geocoder.GeocoderException) ElementTree(net.simon04.jelementtree.ElementTree)

Aggregations

Coordinates (org.opennms.features.geocoder.Coordinates)2 PaintTarget (com.vaadin.server.PaintTarget)1 InputStream (java.io.InputStream)1 ElementTree (net.simon04.jelementtree.ElementTree)1 StatusLine (org.apache.http.StatusLine)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 GeocoderException (org.opennms.features.geocoder.GeocoderException)1 OnmsAssetRecord (org.opennms.netmgt.model.OnmsAssetRecord)1 OnmsGeolocation (org.opennms.netmgt.model.OnmsGeolocation)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1