Search in sources :

Example 1 with NearbyLocation

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

the class TestGeoNamesWebService method testGetNearbyCity.

@Test
public void testGetNearbyCity() {
    prepareWebClient(NEARBY_CITY_QUERY_TEST_RESPONSE);
    NearbyLocation nearbyLocation = webServiceSpy.getNearbyCity(QUERY_TEST_LOCATION);
    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 2 with NearbyLocation

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

the class TestGeoNamesQueryLuceneIndex method testNearestCitiesWithNoResults.

@Test
public void testNearestCitiesWithNoResults() throws java.text.ParseException, GeoEntryQueryException {
    String testPoint = "POINT (0 1)";
    final int requestedMaxResults = 2;
    final int actualResults = 0;
    final List<NearbyLocation> nearestCities = directoryIndex.getNearestCities(testPoint, 50, requestedMaxResults);
    assertThat(nearestCities.size(), is(actualResults));
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 3 with NearbyLocation

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

the class TestGeoNamesQueryLuceneIndex method testDoGetNearestCitiesNullDirectory.

@Test
public void testDoGetNearestCitiesNullDirectory() throws IOException, GeoEntryQueryException {
    Shape shape = mock(Shape.class);
    List<NearbyLocation> nearestCities = directoryIndex.doGetNearestCities(shape, 10, 10, null);
    assertThat(nearestCities, is(Collections.emptyList()));
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Test(org.junit.Test)

Example 4 with NearbyLocation

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

the class GeoNamesQueryLuceneIndex method doGetNearestCities.

protected List<NearbyLocation> doGetNearestCities(final Shape shape, final int radiusInKm, final int maxResults, final Directory directory) throws GeoEntryQueryException {
    notNull(shape, "GeoNamesQueryLuceneIndex.doGetNearestCities(): argument 'shape' may not be null.");
    if (radiusInKm <= 0) {
        throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetNearestCities(): radiusInKm must be positive.");
    }
    if (maxResults <= 0) {
        throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetNearestCities(): maxResults must be positive.");
    }
    if (directory == null) {
        return Collections.emptyList();
    }
    try (final IndexReader indexReader = createIndexReader(directory)) {
        final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
        final List<NearbyLocation> closestCities = new ArrayList<>();
        final Point center = shape.getCenter();
        final Query filter = createSpatialQuery(center, radiusInKm);
        // Query for all the documents in the index that are cities, then filter those
        // results for the ones that are in the search area.
        final BooleanQuery booleanQuery = new BooleanQuery.Builder().add(PPL_QUERY, BooleanClause.Occur.MUST).add(filter, BooleanClause.Occur.FILTER).build();
        final TopDocs topDocs = indexSearcher.search(booleanQuery, maxResults, SORT);
        if (topDocs.totalHits > 0) {
            for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                final double lat = Double.parseDouble(indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.LATITUDE_FIELD));
                final double lon = Double.parseDouble(indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.LONGITUDE_FIELD));
                final String name = indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.NAME_FIELD);
                final NearbyLocation city = new NearbyLocationImpl(center, new PointImpl(lon, lat, SPATIAL_CONTEXT), name);
                closestCities.add(city);
            }
        }
        return closestCities;
    } catch (IOException e) {
        throw new GeoEntryQueryException("Error reading the index", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ArrayList(java.util.ArrayList) Point(org.locationtech.spatial4j.shape.Point) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) IndexReader(org.apache.lucene.index.IndexReader) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 5 with NearbyLocation

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

the class TestGeoNamesQueryLuceneIndex method testNearestCitiesWithLessThanMaxResults.

@Test
public void testNearestCitiesWithLessThanMaxResults() throws java.text.ParseException, GeoEntryQueryException {
    String testPoint = "POINT (56.78 1.5)";
    final int requestedMaxResults = 2;
    final int actualResults = 1;
    final List<NearbyLocation> nearestCities = directoryIndex.getNearestCities(testPoint, 50, requestedMaxResults);
    assertThat(nearestCities.size(), is(actualResults));
    /* This distance value was obtained from http://www.movable-type.co.uk/scripts/latlong.html

           Additionally, "Phoenix Airport" (GEO_ENTRY_2) is within 50 km of (56.78, 1.5), but it
           should not be included in the results because its feature code is AIRP (not a city).
        */
    final NearbyLocation first = nearestCities.get(0);
    assertThat(first.getCardinalDirection(), is("N"));
    final double distance = first.getDistance();
    assertThat(String.format("%.2f", distance), is("29.58"));
    assertThat(first.getName(), is("Phoenix"));
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Matchers.anyString(org.mockito.Matchers.anyString) 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