Search in sources :

Example 21 with NearbyLocation

use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.

the class GazetteerGeoCoderTest method testGetNearbyCities.

@Test
public void testGetNearbyCities() throws ParseException, GeoEntryQueryException {
    NearbyLocation mockNearbyLocation = mock(NearbyLocation.class);
    when(mockNearbyLocation.getCardinalDirection()).thenReturn("W");
    when(mockNearbyLocation.getDistance()).thenReturn(10.24);
    when(mockNearbyLocation.getName()).thenReturn("The City");
    List<NearbyLocation> nearbyLocations = mock(List.class);
    when(nearbyLocations.size()).thenReturn(1);
    when(nearbyLocations.get(0)).thenReturn(mockNearbyLocation);
    when(geoEntryQueryable.getNearestCities("POINT(1.0 20)", 50, 1)).thenReturn(nearbyLocations);
    NearbyLocation returnedNearbyLocation = gazetteerGeoCoder.getNearbyCity("POINT(1.0 20)");
    assertThat(returnedNearbyLocation, equalTo(mockNearbyLocation));
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Test(org.junit.Test)

Example 22 with NearbyLocation

use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.

the class GeoNamesWebService method getNearestCities.

@Override
public List<NearbyLocation> getNearestCities(String locationWkt, int radiusInKm, int maxResults) throws java.text.ParseException, GeoEntryQueryException {
    notNull(locationWkt, "argument locationWkt may not be null");
    Point wktCenterPoint = createPointFromWkt(locationWkt);
    String urlStr = String.format("%s://%s/findNearbyPlaceNameJSON?lat=%f&lng=%f&maxRows=%d&radius=%d&username=%s&cities=cities5000", GEONAMES_PROTOCOL, GEONAMES_API_ADDRESS, wktCenterPoint.getY(), wktCenterPoint.getX(), limitMaxRows(maxResults), radiusInKm, USERNAME);
    Object result = webQuery(urlStr);
    if (result instanceof JSONObject) {
        JSONObject jsonResult = (JSONObject) result;
        JSONArray geoNames = (JSONArray) jsonResult.get(GEONAMES_KEY);
        if (geoNames != null) {
            return geoNames.stream().map(JSONObject.class::cast).map(obj -> {
                double lat = Double.parseDouble((String) obj.get(LAT_KEY));
                double lon = Double.parseDouble((String) obj.get(LON_KEY));
                String cityName = (String) obj.get(PLACENAME_KEY);
                Point cityPoint = new PointImpl(lon, lat, SpatialContext.GEO);
                return new NearbyLocationImpl(wktCenterPoint, cityPoint, cityName);
            }).collect(toList());
        }
    }
    return Collections.emptyList();
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) LoggerFactory(org.slf4j.LoggerFactory) Validate.notNull(org.apache.commons.lang.Validate.notNull) JSONParser(net.minidev.json.parser.JSONParser) MediaType(javax.ws.rs.core.MediaType) Locale(java.util.Locale) ParseException(net.minidev.json.parser.ParseException) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryQueryable(org.codice.ddf.spatial.geocoding.GeoEntryQueryable) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) JtsSpatialContextFactory(org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory) Logger(org.slf4j.Logger) WebClient(org.apache.cxf.jaxrs.client.WebClient) MissingResourceException(java.util.MissingResourceException) Shape(org.locationtech.spatial4j.shape.Shape) StandardCharsets(java.nio.charset.StandardCharsets) Suggestion(org.codice.ddf.spatial.geocoding.Suggestion) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) Collectors.toList(java.util.stream.Collectors.toList) URLEncoder(java.net.URLEncoder) List(java.util.List) Point(org.locationtech.spatial4j.shape.Point) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Optional(java.util.Optional) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 23 with NearbyLocation

use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.

the class GeoNamesWebServiceTest method testGetNearbyCity.

@Test
public void testGetNearbyCity() throws ParseException, GeoEntryQueryException {
    prepareWebClient(NEARBY_CITY_QUERY_TEST_RESPONSE);
    NearbyLocation nearbyLocation = webServiceSpy.getNearestCities(QUERY_TEST_LOCATION, 0, 1).get(0);
    assertThat(nearbyLocation.getCardinalDirection(), equalTo("W"));
    assertThat(nearbyLocation.getDistance(), greaterThan(14.0));
    assertThat(nearbyLocation.getDistance(), lessThan(15.0));
    assertThat(nearbyLocation.getName(), equalTo("Qaryat Wādī ‘Abbādī 2"));
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Test(org.junit.Test)

Example 24 with NearbyLocation

use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.

the class GazetteerQueryCatalogTest method testGetNearestCitiesMissingLocation.

@Test
public void testGetNearestCitiesMissingLocation() throws Exception {
    Metacard metacard = generateGeoNamesMetacard();
    metacard.setAttribute(new AttributeImpl(Core.LOCATION, ""));
    QueryResponse queryResponse = generateQueryResponseFromMetacard(metacard);
    when(catalogFramework.query(any(QueryRequest.class))).thenReturn(queryResponse);
    List<NearbyLocation> nearbyLocations = queryCatalog.getNearestCities(NEAR_BOSTON_WKT, RADIUS_IN_KM, MAX_RESULTS);
    assertThat(nearbyLocations.size(), is(0));
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) QueryResponse(ddf.catalog.operation.QueryResponse) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Test(org.junit.Test)

Aggregations

NearbyLocation (org.codice.ddf.spatial.geocoding.context.NearbyLocation)24 Test (org.junit.Test)18 GeoEntryQueryException (org.codice.ddf.spatial.geocoding.GeoEntryQueryException)5 ParseException (java.text.ParseException)4 Shape (org.locationtech.spatial4j.shape.Shape)4 Matchers.anyString (org.mockito.Matchers.anyString)4 Metacard (ddf.catalog.data.Metacard)3 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)3 QueryRequest (ddf.catalog.operation.QueryRequest)3 QueryResponse (ddf.catalog.operation.QueryResponse)3 IOException (java.io.IOException)3 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 COLLECTION_NAME (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COLLECTION_NAME)2 COUNTRY_CODE (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COUNTRY_CODE)2 FEATURE_CODE (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.FEATURE_CODE)2 GAZETTEER_REQUEST_HANDLER (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.GAZETTEER_REQUEST_HANDLER)2 ID (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.ID)2