Search in sources :

Example 1 with GWTLatLng

use of org.opennms.features.poller.remote.gwt.client.GWTLatLng in project opennms by OpenNMS.

the class TestGoogleMapsGeocoder method testGoogleLookupSuccess.

@Test
public void testGoogleLookupSuccess() throws Exception {
    if (shouldRun()) {
        Geocoder geocoder = new GoogleMapsGeocoder();
        final GWTLatLng remote = geocoder.geocode("220 Chatham Business Dr, Pittsboro, NC 27312");
        final GWTLatLng local = new GWTLatLng(35.717372, -79.161857);
        assertEquals(local.hashCode(), remote.hashCode());
        assertEquals(local, remote);
    }
}
Also used : GWTLatLng(org.opennms.features.poller.remote.gwt.client.GWTLatLng) Test(org.junit.Test)

Example 2 with GWTLatLng

use of org.opennms.features.poller.remote.gwt.client.GWTLatLng in project opennms by OpenNMS.

the class TestNominatimGeocoder method testLookupSuccess.

@Test
public void testLookupSuccess() throws Exception {
    if (shouldRun()) {
        Geocoder geocoder = new NominatimGeocoder("opennms@opennms.org");
        final GWTLatLng remote = geocoder.geocode("220 Chatham Business Dr, Pittsboro, NC 27312");
        final GWTLatLng local = new GWTLatLng(35.7182403203186, -79.1621859463074);
        assertEquals(local.hashCode(), remote.hashCode());
        assertEquals(local, remote);
    }
}
Also used : GWTLatLng(org.opennms.features.poller.remote.gwt.client.GWTLatLng) Test(org.junit.Test)

Example 3 with GWTLatLng

use of org.opennms.features.poller.remote.gwt.client.GWTLatLng in project opennms by OpenNMS.

the class DefaultLocationDataService method getLatLng.

/**
 * {@inheritDoc}
 */
@Transactional
@Override
public GWTLatLng getLatLng(final OnmsMonitoringLocation def, boolean x) {
    GWTLatLng latLng = null;
    final String coordinateMatchString = "^\\s*[\\-\\d\\.]+\\s*,\\s*[\\-\\d\\.]+\\s*$";
    // first, see if we already have coordinates
    if (def.getLatitude() != null && def.getLongitude() != null) {
        latLng = new GWTLatLng(def.getLatitude(), def.getLongitude());
    }
    // if not, see if geolocation is coordinates
    if (latLng == null) {
        LOG.debug("using geolocation: {}", def.getGeolocation());
        if (def.getGeolocation() != null && def.getGeolocation().matches(coordinateMatchString)) {
            final String[] coordinates = def.getGeolocation().split(",");
            latLng = new GWTLatLng(Double.valueOf(coordinates[0]), Double.valueOf(coordinates[1]));
        }
    }
    // otherwise, try to geocode it
    if (latLng == null && def.getGeolocation() != null && !def.getGeolocation().equals("")) {
        try {
            latLng = m_geocoder.geocode(def.getGeolocation());
            LOG.debug("got coordinates {} for geolocation {}", latLng.getCoordinates(), def.getGeolocation());
        } catch (GeocoderException e) {
            LOG.warn("unable to geocode {}", def.getGeolocation(), e);
        }
    }
    return latLng;
}
Also used : GWTLatLng(org.opennms.features.poller.remote.gwt.client.GWTLatLng) GeocoderException(org.opennms.features.poller.remote.gwt.server.geocoding.GeocoderException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with GWTLatLng

use of org.opennms.features.poller.remote.gwt.client.GWTLatLng 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);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) GWTLatLng(org.opennms.features.poller.remote.gwt.client.GWTLatLng) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) ElementTree(net.simon04.jelementtree.ElementTree)

Example 5 with GWTLatLng

use of org.opennms.features.poller.remote.gwt.client.GWTLatLng 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);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) GWTLatLng(org.opennms.features.poller.remote.gwt.client.GWTLatLng) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ElementTree(net.simon04.jelementtree.ElementTree)

Aggregations

GWTLatLng (org.opennms.features.poller.remote.gwt.client.GWTLatLng)10 Test (org.junit.Test)4 InputStream (java.io.InputStream)2 ElementTree (net.simon04.jelementtree.ElementTree)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2 Date (java.util.Date)1 TreeSet (java.util.TreeSet)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 GWTBounds (org.opennms.features.poller.remote.gwt.client.GWTBounds)1 GWTMarkerState (org.opennms.features.poller.remote.gwt.client.GWTMarkerState)1 LocationInfo (org.opennms.features.poller.remote.gwt.client.location.LocationInfo)1 GeocodingUpdatingRemoteEvent (org.opennms.features.poller.remote.gwt.client.remoteevents.GeocodingUpdatingRemoteEvent)1 BoundsBuilder (org.opennms.features.poller.remote.gwt.client.utils.BoundsBuilder)1 GeocoderException (org.opennms.features.poller.remote.gwt.server.geocoding.GeocoderException)1 OnmsMonitoringLocation (org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation)1 Transactional (org.springframework.transaction.annotation.Transactional)1