Search in sources :

Example 11 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException 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 12 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException 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 13 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException 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 14 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.

the class GeoNamesLocalIndex method getLocation.

@Override
public GeoResult getLocation(final String location) {
    try {
        final List<GeoEntry> topResults = geoEntryQueryable.query(location, 1);
        if (topResults.size() > 0) {
            final GeoEntry topResult = topResults.get(0);
            final String name = topResult.getName();
            final double latitude = topResult.getLatitude();
            final double longitude = topResult.getLongitude();
            final String featureCode = topResult.getFeatureCode();
            final long population = topResult.getPopulation();
            return GeoResultCreator.createGeoResult(name, latitude, longitude, featureCode, population);
        }
    } catch (GeoEntryQueryException e) {
        LOGGER.debug("Error querying the local GeoNames index", e);
    }
    return null;
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException)

Example 15 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.

the class TestGeoNamesLocalIndex method testGetCountryCodeGeoEntryQueryException.

@Test
public void testGetCountryCodeGeoEntryQueryException() throws ParseException, GeoEntryQueryException {
    when(geoEntryQueryable.getCountryCode(TEST_POINT, 50)).thenThrow(new GeoEntryQueryException(""));
    countryCode = geoNamesLocalIndex.getCountryCode(TEST_POINT, 50);
    assertThat(countryCode.isPresent(), is(false));
}
Also used : GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) Test(org.junit.Test)

Aggregations

GeoEntryQueryException (org.codice.ddf.spatial.geocoding.GeoEntryQueryException)20 IOException (java.io.IOException)9 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)7 FederationException (ddf.catalog.federation.FederationException)5 QueryResponse (ddf.catalog.operation.QueryResponse)5 QueryImpl (ddf.catalog.operation.impl.QueryImpl)5 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)5 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)5 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)5 List (java.util.List)5 SolrQuery (org.apache.solr.client.solrj.SolrQuery)5 SolrServerException (org.apache.solr.client.solrj.SolrServerException)5 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)5 NearbyLocation (org.codice.ddf.spatial.geocoding.context.NearbyLocation)5 Query (ddf.catalog.operation.Query)4 QueryRequest (ddf.catalog.operation.QueryRequest)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 GeoEntryQueryable (org.codice.ddf.spatial.geocoding.GeoEntryQueryable)4 Suggestion (org.codice.ddf.spatial.geocoding.Suggestion)4