use of ddf.catalog.solr.offlinegazetteer.GazetteerConstants.LOCATION in project ddf by codice.
the class GazetteerQueryOfflineSolr method convert.
private NearbyLocation convert(SolrDocument doc, Geometry originalLocation) {
String location = getField(doc, LOCATION, String.class);
String title = Optional.ofNullable(getField(doc, NAME, String.class)).filter(Objects::nonNull).filter(s -> !s.isEmpty()).orElse("NO TITLE");
String cardinalDirection = "";
double distance = 0;
try {
Geometry geo = WKT_READER_THREAD_LOCAL.get().read(location);
cardinalDirection = bearingToCardinalDirection(getBearing(originalLocation.getCentroid(), geo.getCentroid()));
distance = convertDegreeToKilometer(originalLocation.distance(geo.getCentroid()));
} catch (org.locationtech.jts.io.ParseException e) {
LOGGER.debug("Could not parse location for item (object: {})", doc, e);
}
return new NearbyLocationImpl(title, cardinalDirection, distance);
}
use of ddf.catalog.solr.offlinegazetteer.GazetteerConstants.LOCATION 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 ddf.catalog.solr.offlinegazetteer.GazetteerConstants.LOCATION in project ddf by codice.
the class CatalogGazetteerForwardingPlugin method convert.
protected static SolrInputDocument convert(Metacard metacard) {
SolrInputDocument solrDoc = new SolrInputDocument();
Consumer<String> getAttrAndAdd = (attributeName) -> Optional.ofNullable(getStringAttribute(metacard, attributeName)).ifPresent(attributeValue -> solrDoc.addField(GAZETTEER_TO_CATALOG.inverse().get(attributeName), attributeValue));
getAttrAndAdd.accept(GAZETTEER_TO_CATALOG.get(DESCRIPTION));
getAttrAndAdd.accept(GAZETTEER_TO_CATALOG.get(FEATURE_CODE));
getAttrAndAdd.accept(GAZETTEER_TO_CATALOG.get(NAME));
getAttrAndAdd.accept(GAZETTEER_TO_CATALOG.get(ID));
getAttrAndAdd.accept(GAZETTEER_TO_CATALOG.get(COUNTRY_CODE));
Optional.of(metacard).map(m -> getStringAttribute(m, GAZETTEER_TO_CATALOG.get(LOCATION))).ifPresent(v -> solrDoc.addField(LOCATION, v));
Optional.of(metacard).map(m -> m.getAttribute(GAZETTEER_TO_CATALOG.get(POPULATION))).map(Attribute::getValue).filter(Long.class::isInstance).map(Long.class::cast).ifPresent(v -> solrDoc.addField(POPULATION, v));
Optional.of(metacard).map(m -> m.getAttribute(GAZETTEER_TO_CATALOG.get(SORT_VALUE))).map(Attribute::getValue).filter(Integer.class::isInstance).map(Integer.class::cast).ifPresent(v -> solrDoc.addField(SORT_VALUE, v));
return solrDoc;
}
Aggregations