use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class GazetteerQueryCatalog method getNearestCities.
@Override
public List<NearbyLocation> getNearestCities(String location, int radiusInKm, int maxResults) throws ParseException, GeoEntryQueryException {
Filter featureCodeFilter = filterBuilder.anyOf(featureCodeFilters);
int radiusInMeters = radiusInKm * KM_TO_M;
Filter textFilter = filterBuilder.attribute(Core.LOCATION).withinBuffer().wkt(location, radiusInMeters);
Filter queryFilter = filterBuilder.allOf(featureCodeFilter, tagFilter, textFilter);
Query query = new QueryImpl(queryFilter, 1, maxResults, SortBy.NATURAL_ORDER, false, TIMEOUT);
QueryRequest queryRequest = new QueryRequestImpl(query);
QueryResponse queryResponse;
try {
queryResponse = catalogFramework.query(queryRequest);
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
throw new GeoEntryQueryException(ERROR_MESSAGE, e);
}
return queryResponse.getResults().stream().map(Result::getMetacard).map(metacard -> transformMetacardToNearbyLocation(location, metacard)).filter(Objects::nonNull).collect(Collectors.toList());
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class GazetteerQueryCatalog method getCountryCode.
@Override
public Optional<String> getCountryCode(String wktLocation, int radius) throws GeoEntryQueryException, ParseException {
String wkt;
try {
Point center = WKT_READER_THREAD_LOCAL.get().read(wktLocation).getCentroid();
Geometry geometry = GEOMETRY_FACTORY.createPoint(center.getCoordinate());
wkt = WKT_WRITER_THREAD_LOCAL.get().write(geometry);
} catch (org.locationtech.jts.io.ParseException e) {
return Optional.empty();
}
int radiusInMeters = KM_TO_M * radius;
Filter filter = filterBuilder.attribute(Core.LOCATION).withinBuffer().wkt(wkt, radiusInMeters);
Filter queryFilter = filterBuilder.allOf(tagFilter, filter);
Query query = new QueryImpl(queryFilter);
QueryRequest queryRequest = new QueryRequestImpl(query);
QueryResponse queryResponse;
try {
queryResponse = catalogFramework.query(queryRequest);
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
throw new GeoEntryQueryException(ERROR_MESSAGE, e);
}
List<Result> results = queryResponse.getResults();
if (CollectionUtils.isNotEmpty(results)) {
Result firstResult = results.get(0);
Metacard metacard = firstResult.getMetacard();
String countryCode = getStringAttributeFromMetacard(metacard, Location.COUNTRY_CODE);
return Optional.ofNullable(countryCode);
}
return Optional.empty();
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class GazetteerQueryCatalog method query.
@Override
public List<GeoEntry> query(String queryString, int maxResults) throws GeoEntryQueryException {
Filter textFilter = filterBuilder.attribute(Core.TITLE).is().like().text(queryString);
Filter queryFilter = filterBuilder.allOf(tagFilter, textFilter);
Map<String, Serializable> properties = new HashMap<>();
SortBy featureCodeSortBy = new SortByImpl(GeoEntryAttributes.FEATURE_CODE_ATTRIBUTE_NAME, SortOrder.ASCENDING);
SortBy populationSortBy = new SortByImpl(GeoEntryAttributes.POPULATION_ATTRIBUTE_NAME, SortOrder.DESCENDING);
SortBy[] sortbys = { populationSortBy };
properties.put(ADDITIONAL_SORT_BYS, sortbys);
Query query = new QueryImpl(queryFilter, 1, maxResults, featureCodeSortBy, false, TIMEOUT);
QueryRequest queryRequest = new QueryRequestImpl(query, properties);
QueryResponse queryResponse;
try {
queryResponse = catalogFramework.query(queryRequest);
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
throw new GeoEntryQueryException(ERROR_MESSAGE, e);
}
return queryResponse.getResults().stream().map(Result::getMetacard).map(this::transformMetacardToGeoEntry).filter(Objects::nonNull).collect(Collectors.toList());
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class GazetteerQueryCatalogTest method setUp.
@Before
public void setUp() throws Exception {
catalogFramework = mock(CatalogFramework.class);
QueryResponse queryResponse = generateQueryResponseFromMetacard(generateGeoNamesMetacard());
when(catalogFramework.query(any(QueryRequest.class))).thenReturn(queryResponse);
queryCatalog = new GazetteerQueryCatalog(catalogFramework, FILTER_BUILDER);
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class GazetteerQueryCatalogTest method testQueryNoLocationOnMetacard.
@Test
public void testQueryNoLocationOnMetacard() throws Exception {
Metacard metacard = generateGeoNamesMetacard();
metacard.setAttribute(new AttributeImpl(Core.LOCATION, ""));
QueryResponse queryResponse = generateQueryResponseFromMetacard(metacard);
when(catalogFramework.query(any(QueryRequest.class))).thenReturn(queryResponse);
List<GeoEntry> geoEntryList = queryCatalog.query(QUERY_STRING, 1);
assertThat(geoEntryList.size(), is(1));
GeoEntry geoEntry = geoEntryList.get(0);
assertThat(geoEntry.getLatitude(), nullValue());
assertThat(geoEntry.getLongitude(), nullValue());
}
Aggregations