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"));
}
use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.
the class TestGeoNamesQueryLuceneIndex method testNearestCitiesWithMaxResults.
@Test
public void testNearestCitiesWithMaxResults() throws java.text.ParseException, GeoEntryQueryException {
String testPoint = "POINT (56.78 1)";
final int requestedMaxResults = 2;
final List<NearbyLocation> nearestCities = directoryIndex.getNearestCities(testPoint, 50, requestedMaxResults);
assertThat(nearestCities.size(), is(requestedMaxResults));
/* These distances values were obtained from
http://www.movable-type.co.uk/scripts/latlong.html
Phoenix is first because it has a higher population.
Additionally, "Phoenix Airport" (GEO_ENTRY_2) is within 50 km of (56.78, 1), 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("S"));
final double firstDistance = first.getDistance();
assertThat(String.format("%.2f", firstDistance), is("26.02"));
assertThat(first.getName(), is("Phoenix"));
final NearbyLocation second = nearestCities.get(1);
assertThat(second.getCardinalDirection(), is("W"));
final double secondDistance = second.getDistance();
assertThat(String.format("%.2f", secondDistance), is("24.46"));
assertThat(second.getName(), is("Glendale"));
}
use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.
the class TestGeoNamesLocalIndex method testGetNearbyCitiesNoResult.
@Test
public void testGetNearbyCitiesNoResult() 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(0);
when(geoEntryQueryable.getNearestCities("POINT(1.0 20)", 50, 1)).thenReturn(nearbyLocations);
NearbyLocation returnedNearbyLocation = geoNamesLocalIndex.getNearbyCity("POINT(1.0 20)");
assertThat(returnedNearbyLocation, nullValue());
}
use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.
the class TestGeoNamesLocalIndex 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 = geoNamesLocalIndex.getNearbyCity("POINT(1.0 20)");
assertThat(returnedNearbyLocation, equalTo(mockNearbyLocation));
}
use of org.codice.ddf.spatial.geocoding.context.NearbyLocation in project ddf by codice.
the class GeoCoderEndpoint method getNearbyCities.
@GET
@Path("nearby/cities/{wkt}")
public Response getNearbyCities(@PathParam("wkt") String wkt) {
GeoCoder geoCoder = geoCoderFactory.getService();
try {
NearbyLocation nearbyLocation = geoCoder.getNearbyCity(wkt);
if (nearbyLocation != null) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("direction", nearbyLocation.getCardinalDirection());
jsonObject.put("distance", nearbyLocation.getDistance());
jsonObject.put("name", nearbyLocation.getName());
return Response.ok(jsonObject.toJSONString()).build();
} else {
return Response.status(Response.Status.NO_CONTENT).build();
}
} catch (GeoEntryQueryException e) {
LOGGER.debug("Error querying GeoNames resource with wkt:{}", wkt, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
Aggregations