Search in sources :

Example 1 with LOCATION

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);
}
Also used : COLLECTION_NAME(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COLLECTION_NAME) WKTReader(org.locationtech.jts.io.WKTReader) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) SolrClient(org.codice.solr.client.solrj.SolrClient) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) SolrServerException(org.apache.solr.client.solrj.SolrServerException) WKTWriter(org.locationtech.jts.io.WKTWriter) Map(java.util.Map) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) COUNTRY_CODE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COUNTRY_CODE) ParseException(java.text.ParseException) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) GAZETTEER_REQUEST_HANDLER(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.GAZETTEER_REQUEST_HANDLER) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ImmutableMap(com.google.common.collect.ImmutableMap) Point(org.locationtech.jts.geom.Point) GeoCodingConstants(org.codice.ddf.spatial.geocoding.GeoCodingConstants) Shape(org.locationtech.spatial4j.shape.Shape) Collectors(java.util.stream.Collectors) Suggestion(org.codice.ddf.spatial.geocoding.Suggestion) Objects(java.util.Objects) List(java.util.List) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) FEATURE_CODE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.FEATURE_CODE) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) LOCATION(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.LOCATION) ClientUtils(org.apache.solr.client.solrj.util.ClientUtils) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) SUGGEST_DICT(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT) ValidationRule(org.locationtech.spatial4j.context.jts.ValidationRule) SolrClientFactory(org.codice.solr.factory.SolrClientFactory) SUGGEST_DICT_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT_KEY) METHOD(org.apache.solr.client.solrj.SolrRequest.METHOD) SUGGEST_Q_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_Q_KEY) GeoEntryQueryable(org.codice.ddf.spatial.geocoding.GeoEntryQueryable) SuggesterResponse(org.apache.solr.client.solrj.response.SuggesterResponse) JtsSpatialContextFactory(org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory) Logger(org.slf4j.Logger) NAME(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.NAME) SORT_VALUE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SORT_VALUE) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) IOException(java.io.IOException) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) SolrDocument(org.apache.solr.common.SolrDocument) SUGGEST_COUNT_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_COUNT_KEY) POPULATION(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.POPULATION) ID(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.ID) Collections(java.util.Collections) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) Geometry(org.locationtech.jts.geom.Geometry) Objects(java.util.Objects)

Example 2 with LOCATION

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());
}
Also used : JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) Geometry(org.locationtech.jts.geom.Geometry) COLLECTION_NAME(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COLLECTION_NAME) WKTReader(org.locationtech.jts.io.WKTReader) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) SolrClient(org.codice.solr.client.solrj.SolrClient) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) SolrServerException(org.apache.solr.client.solrj.SolrServerException) WKTWriter(org.locationtech.jts.io.WKTWriter) Map(java.util.Map) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) COUNTRY_CODE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COUNTRY_CODE) ParseException(java.text.ParseException) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) GAZETTEER_REQUEST_HANDLER(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.GAZETTEER_REQUEST_HANDLER) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ImmutableMap(com.google.common.collect.ImmutableMap) Point(org.locationtech.jts.geom.Point) GeoCodingConstants(org.codice.ddf.spatial.geocoding.GeoCodingConstants) Shape(org.locationtech.spatial4j.shape.Shape) Collectors(java.util.stream.Collectors) Suggestion(org.codice.ddf.spatial.geocoding.Suggestion) Objects(java.util.Objects) List(java.util.List) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) FEATURE_CODE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.FEATURE_CODE) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) LOCATION(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.LOCATION) ClientUtils(org.apache.solr.client.solrj.util.ClientUtils) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) SUGGEST_DICT(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT) ValidationRule(org.locationtech.spatial4j.context.jts.ValidationRule) SolrClientFactory(org.codice.solr.factory.SolrClientFactory) SUGGEST_DICT_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT_KEY) METHOD(org.apache.solr.client.solrj.SolrRequest.METHOD) SUGGEST_Q_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_Q_KEY) GeoEntryQueryable(org.codice.ddf.spatial.geocoding.GeoEntryQueryable) SuggesterResponse(org.apache.solr.client.solrj.response.SuggesterResponse) JtsSpatialContextFactory(org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory) Logger(org.slf4j.Logger) NAME(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.NAME) SORT_VALUE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SORT_VALUE) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) IOException(java.io.IOException) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) SolrDocument(org.apache.solr.common.SolrDocument) SUGGEST_COUNT_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_COUNT_KEY) POPULATION(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.POPULATION) ID(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.ID) Collections(java.util.Collections) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 3 with LOCATION

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;
}
Also used : COLLECTION_NAME(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COLLECTION_NAME) PreQueryPlugin(ddf.catalog.plugin.PreQueryPlugin) SolrClient(org.codice.solr.client.solrj.SolrClient) SolrClientFactory(org.codice.solr.factory.SolrClientFactory) LoggerFactory(org.slf4j.LoggerFactory) DESCRIPTION(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.DESCRIPTION) DeleteResponse(ddf.catalog.operation.DeleteResponse) SUGGEST_DICT_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT_KEY) Update(ddf.catalog.operation.Update) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) PostIngestPlugin(ddf.catalog.plugin.PostIngestPlugin) SolrServerException(org.apache.solr.client.solrj.SolrServerException) CreateResponse(ddf.catalog.operation.CreateResponse) Metacard(ddf.catalog.data.Metacard) SUGGEST_Q_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_Q_KEY) QueryRequest(ddf.catalog.operation.QueryRequest) COUNTRY_CODE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COUNTRY_CODE) Nullable(javax.annotation.Nullable) GAZETTEER_REQUEST_HANDLER(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.GAZETTEER_REQUEST_HANDLER) GAZETTEER_METACARD_TAG(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.GAZETTEER_METACARD_TAG) Logger(org.slf4j.Logger) NAME(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.NAME) SORT_VALUE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SORT_VALUE) SUGGEST_BUILD_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_BUILD_KEY) StopProcessingException(ddf.catalog.plugin.StopProcessingException) IOException(java.io.IOException) GAZETTEER_TO_CATALOG(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.GAZETTEER_TO_CATALOG) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Consumer(java.util.function.Consumer) SUGGESTION_BUILD_KEY(ddf.catalog.Constants.SUGGESTION_BUILD_KEY) List(java.util.List) Attribute(ddf.catalog.data.Attribute) FEATURE_CODE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.FEATURE_CODE) SolrQuery(org.apache.solr.client.solrj.SolrQuery) POPULATION(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.POPULATION) Optional(java.util.Optional) LOCATION(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.LOCATION) ID(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.ID) UpdateResponse(ddf.catalog.operation.UpdateResponse) SUGGEST_DICT(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputDocument(org.apache.solr.common.SolrInputDocument)

Aggregations

COLLECTION_NAME (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COLLECTION_NAME)3 COUNTRY_CODE (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COUNTRY_CODE)3 FEATURE_CODE (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.FEATURE_CODE)3 GAZETTEER_REQUEST_HANDLER (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.GAZETTEER_REQUEST_HANDLER)3 ID (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.ID)3 LOCATION (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.LOCATION)3 NAME (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.NAME)3 POPULATION (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.POPULATION)3 SORT_VALUE (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SORT_VALUE)3 SUGGEST_DICT (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT)3 SUGGEST_DICT_KEY (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT_KEY)3 SUGGEST_Q_KEY (ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_Q_KEY)3 IOException (java.io.IOException)3 List (java.util.List)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 SolrQuery (org.apache.solr.client.solrj.SolrQuery)3 SolrServerException (org.apache.solr.client.solrj.SolrServerException)3 SolrClient (org.codice.solr.client.solrj.SolrClient)3 SolrClientFactory (org.codice.solr.factory.SolrClientFactory)3