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"));
}
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));
}
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()));
}
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);
}
}
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"));
}
Aggregations