Search in sources :

Example 86 with QueryResponse

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());
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) Filter(org.opengis.filter.Filter) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) Point(org.locationtech.jts.geom.Point)

Example 87 with QueryResponse

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();
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) Point(org.locationtech.jts.geom.Point) FederationException(ddf.catalog.federation.FederationException) Point(org.locationtech.jts.geom.Point) Result(ddf.catalog.data.Result) Geometry(org.locationtech.jts.geom.Geometry) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse)

Example 88 with QueryResponse

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());
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) HashMap(java.util.HashMap) SortBy(org.opengis.filter.sort.SortBy) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) SortByImpl(ddf.catalog.filter.impl.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse)

Example 89 with QueryResponse

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);
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) QueryResponse(ddf.catalog.operation.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework) Before(org.junit.Before)

Example 90 with QueryResponse

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());
}
Also used : Metacard(ddf.catalog.data.Metacard) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) QueryRequest(ddf.catalog.operation.QueryRequest) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Test(org.junit.Test)

Aggregations

QueryResponse (ddf.catalog.operation.QueryResponse)187 QueryRequest (ddf.catalog.operation.QueryRequest)130 Test (org.junit.Test)113 Metacard (ddf.catalog.data.Metacard)91 Result (ddf.catalog.data.Result)85 ArrayList (java.util.ArrayList)73 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)70 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)55 QueryImpl (ddf.catalog.operation.impl.QueryImpl)48 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)43 FederationException (ddf.catalog.federation.FederationException)40 ResultImpl (ddf.catalog.data.impl.ResultImpl)39 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)39 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)33 Filter (org.opengis.filter.Filter)33 HashMap (java.util.HashMap)31 Serializable (java.io.Serializable)30 Source (ddf.catalog.source.Source)28 HashSet (java.util.HashSet)28 CatalogFramework (ddf.catalog.CatalogFramework)26