use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GazetteerQueryOfflineSolr method query.
@Override
public List<GeoEntry> query(String queryString, int maxResults) throws GeoEntryQueryException {
SolrQuery solrQuery = new SolrQuery(String.format("%s:\"%s\"", NAME, ClientUtils.escapeQueryChars(queryString)));
solrQuery.setRows(Math.min(maxResults, GazetteerQueryOfflineSolr.MAX_RESULTS));
QueryResponse response = null;
try {
response = client.query(solrQuery, METHOD.POST);
} catch (SolrServerException | IOException e) {
throw new GeoEntryQueryException("Error while querying", e);
}
return response.getResults().stream().map(this::transformMetacardToGeoEntry).collect(Collectors.toList());
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GazetteerQueryOfflineSolr method getSuggestedNames.
@Override
public List<Suggestion> getSuggestedNames(String queryString, int maxResults) throws GeoEntryQueryException {
SolrQuery solrQuery = new SolrQuery();
solrQuery.setRequestHandler(GAZETTEER_REQUEST_HANDLER);
solrQuery.setParam(SUGGEST_Q_KEY, ClientUtils.escapeQueryChars(queryString));
solrQuery.setParam(SUGGEST_DICT_KEY, SUGGEST_DICT);
solrQuery.setParam(SUGGEST_COUNT_KEY, Integer.toString(Math.min(maxResults, MAX_RESULTS)));
QueryResponse response;
try {
response = client.query(solrQuery);
} catch (SolrServerException | IOException e) {
throw new GeoEntryQueryException("Error while querying", e);
}
return Optional.ofNullable(response).map(QueryResponse::getSuggesterResponse).map(SuggesterResponse::getSuggestions).map(suggestionsPerDict -> suggestionsPerDict.get(SUGGEST_DICT)).orElse(Collections.emptyList()).stream().map(suggestion -> new SuggestionImpl(suggestion.getPayload(), suggestion.getTerm())).collect(Collectors.toList());
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GazetteerQueryOfflineSolr method getNearestCities.
@Override
public List<NearbyLocation> getNearestCities(String location, int radiusInKm, int maxResults) throws ParseException, GeoEntryQueryException {
Geometry geometry;
try {
geometry = WKT_READER_THREAD_LOCAL.get().read(location);
} catch (org.locationtech.jts.io.ParseException e) {
throw new GeoEntryQueryException("Could not parse location");
}
final Geometry originalGeometry = geometry;
Geometry bufferedGeo = originalGeometry.buffer(convertKilometerToDegree(radiusInKm), 14);
String wkt = WKT_WRITER_THREAD_LOCAL.get().write(bufferedGeo);
String q = String.format("%s_index:\"Intersects( %s ) AND %s\"", LOCATION, ClientUtils.escapeQueryChars(wkt), CITY_SOLR_QUERY);
SolrQuery solrQuery = new SolrQuery(q);
solrQuery.setRows(Math.min(maxResults, MAX_RESULTS));
QueryResponse response;
try {
response = client.query(solrQuery, METHOD.POST);
} catch (SolrServerException | IOException e) {
throw new GeoEntryQueryException("Error executing query for nearest cities", e);
}
return response.getResults().stream().map(result -> convert(result, originalGeometry)).collect(Collectors.toList());
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GeoNamesWebService method query.
@Override
public List<GeoEntry> query(String queryString, int maxResults) throws GeoEntryQueryException {
String location = getUrlEncodedLocation(queryString);
String urlStr = String.format("%s://%s/searchJSON?q=%s&username=%s&maxRows=%d", GEONAMES_PROTOCOL, GEONAMES_API_ADDRESS, location, USERNAME, limitMaxRows(maxResults));
Object result = webQuery(urlStr);
if (result != null) {
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 -> new GeoEntry.Builder().name((String) obj.get(PLACENAME_KEY)).population((Long) obj.get(POPULATION_KEY)).featureCode((String) obj.get(ADMIN_CODE_KEY)).latitude(Double.valueOf((String) obj.get(LAT_KEY))).longitude(Double.valueOf((String) obj.get(LON_KEY))).build()).collect(toList());
}
}
}
return Collections.emptyList();
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GazetteerQueryCatalog method queryById.
@Override
public GeoEntry queryById(String id) throws GeoEntryQueryException {
if (StringUtils.isBlank(id)) {
throw new IllegalArgumentException("id cannot be blank or null");
}
Filter idFilter = filterBuilder.attribute(Core.ID).is().text(id);
Filter queryFilter = filterBuilder.allOf(tagFilter, idFilter);
QueryResponse queryResponse;
try {
queryResponse = catalogFramework.query(new QueryRequestImpl(new QueryImpl(queryFilter)));
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
throw new GeoEntryQueryException(ERROR_MESSAGE, e);
}
if (!queryResponse.getResults().isEmpty()) {
Result result = queryResponse.getResults().get(0);
return transformMetacardToGeoEntry(result.getMetacard());
}
return null;
}
Aggregations