use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GazetteerQueryOfflineSolr method queryById.
@Override
public GeoEntry queryById(String id) throws GeoEntryQueryException {
SolrQuery solrQuery = new SolrQuery(String.format("%s:\"%s\"", ID, ClientUtils.escapeQueryChars(id)));
solrQuery.setRows(1);
QueryResponse response = null;
try {
response = client.query(solrQuery, METHOD.POST);
} catch (SolrServerException | IOException e) {
throw new GeoEntryQueryException("Error while querying by ID", e);
}
return response.getResults().stream().map(this::transformMetacardToGeoEntry).findFirst().orElseThrow(() -> new GeoEntryQueryException("Could not find id"));
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GazetteerQueryOfflineSolr 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(fixSelfIntersectingGeometry(wktLocation)).getCentroid();
wkt = WKT_WRITER_THREAD_LOCAL.get().write(center.buffer(convertKilometerToDegree(radius)));
} catch (org.locationtech.jts.io.ParseException e) {
LOGGER.debug("Could not parse wkt: {}", wktLocation, e);
throw new GeoEntryQueryException("Could not parse wkt", e);
}
SolrQuery solrQuery = new SolrQuery(String.format("%s_index:\"Intersects( %s )\"", LOCATION, ClientUtils.escapeQueryChars(wkt)));
solrQuery.setRows(1);
QueryResponse response;
try {
response = client.query(solrQuery, METHOD.POST);
} catch (SolrServerException | IOException e) {
LOGGER.debug("Could not query for country code ({})", wktLocation, e);
throw new GeoEntryQueryException("Error encountered when querying", e);
}
return response.getResults().stream().findFirst().map(doc -> getField(doc, COUNTRY_CODE, String.class));
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GazetteerGeoCoderTest method testGetCountryCodeGeoEntryQueryException.
@Test
public void testGetCountryCodeGeoEntryQueryException() throws ParseException, GeoEntryQueryException {
when(geoEntryQueryable.getCountryCode(TEST_POINT, 50)).thenThrow(new GeoEntryQueryException(""));
countryCode = gazetteerGeoCoder.getCountryCode(TEST_POINT, 50);
assertThat(countryCode.isPresent(), is(false));
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GazetteerQueryCatalog method getSuggestedNames.
@Override
public List<Suggestion> getSuggestedNames(String queryString, int maxResults) throws GeoEntryQueryException {
Map<String, Serializable> suggestProps = new HashMap<>();
suggestProps.put(SUGGESTION_QUERY_KEY, queryString);
suggestProps.put(SUGGESTION_CONTEXT_KEY, GAZETTEER_METACARD_TAG);
suggestProps.put(SUGGESTION_DICT_KEY, SUGGEST_PLACE_KEY);
Query suggestionQuery = new QueryImpl(filterBuilder.attribute(Core.TITLE).text(queryString));
QueryRequest suggestionRequest = new QueryRequestImpl(suggestionQuery, suggestProps);
try {
QueryResponse suggestionResponse = catalogFramework.query(suggestionRequest);
if (suggestionResponse.getPropertyValue(SUGGESTION_RESULT_KEY) instanceof List) {
List<Map.Entry<String, String>> suggestions = (List<Map.Entry<String, String>>) suggestionResponse.getPropertyValue(SUGGESTION_RESULT_KEY);
return suggestions.stream().map(suggestion -> new SuggestionImpl(suggestion.getKey(), suggestion.getValue())).limit(maxResults).collect(Collectors.toList());
}
} catch (SourceUnavailableException | FederationException | UnsupportedQueryException e) {
throw new GeoEntryQueryException("Failed to execute suggestion query", e);
}
return Collections.emptyList();
}
use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.
the class GeoNamesWebService method getNearestCities.
@Override
public List<NearbyLocation> getNearestCities(String locationWkt, int radiusInKm, int maxResults) throws java.text.ParseException, GeoEntryQueryException {
notNull(locationWkt, "argument locationWkt may not be null");
Point wktCenterPoint = createPointFromWkt(locationWkt);
String urlStr = String.format("%s://%s/findNearbyPlaceNameJSON?lat=%f&lng=%f&maxRows=%d&radius=%d&username=%s&cities=cities5000", GEONAMES_PROTOCOL, GEONAMES_API_ADDRESS, wktCenterPoint.getY(), wktCenterPoint.getX(), limitMaxRows(maxResults), radiusInKm, USERNAME);
Object result = webQuery(urlStr);
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 -> {
double lat = Double.parseDouble((String) obj.get(LAT_KEY));
double lon = Double.parseDouble((String) obj.get(LON_KEY));
String cityName = (String) obj.get(PLACENAME_KEY);
Point cityPoint = new PointImpl(lon, lat, SpatialContext.GEO);
return new NearbyLocationImpl(wktCenterPoint, cityPoint, cityName);
}).collect(toList());
}
}
return Collections.emptyList();
}
Aggregations