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