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