Search in sources :

Example 6 with GeoEntryQueryException

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());
}
Also used : 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 7 with GeoEntryQueryException

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());
}
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) 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 8 with GeoEntryQueryException

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());
}
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 9 with GeoEntryQueryException

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();
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) LoggerFactory(org.slf4j.LoggerFactory) Validate.notNull(org.apache.commons.lang.Validate.notNull) JSONParser(net.minidev.json.parser.JSONParser) MediaType(javax.ws.rs.core.MediaType) Locale(java.util.Locale) ParseException(net.minidev.json.parser.ParseException) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryQueryable(org.codice.ddf.spatial.geocoding.GeoEntryQueryable) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) JtsSpatialContextFactory(org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory) Logger(org.slf4j.Logger) WebClient(org.apache.cxf.jaxrs.client.WebClient) MissingResourceException(java.util.MissingResourceException) Shape(org.locationtech.spatial4j.shape.Shape) StandardCharsets(java.nio.charset.StandardCharsets) Suggestion(org.codice.ddf.spatial.geocoding.Suggestion) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) Collectors.toList(java.util.stream.Collectors.toList) URLEncoder(java.net.URLEncoder) List(java.util.List) Point(org.locationtech.spatial4j.shape.Point) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Optional(java.util.Optional) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject)

Example 10 with GeoEntryQueryException

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;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) QueryResponse(ddf.catalog.operation.QueryResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) Result(ddf.catalog.data.Result)

Aggregations

GeoEntryQueryException (org.codice.ddf.spatial.geocoding.GeoEntryQueryException)20 IOException (java.io.IOException)9 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)7 FederationException (ddf.catalog.federation.FederationException)5 QueryResponse (ddf.catalog.operation.QueryResponse)5 QueryImpl (ddf.catalog.operation.impl.QueryImpl)5 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)5 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)5 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)5 List (java.util.List)5 SolrQuery (org.apache.solr.client.solrj.SolrQuery)5 SolrServerException (org.apache.solr.client.solrj.SolrServerException)5 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)5 NearbyLocation (org.codice.ddf.spatial.geocoding.context.NearbyLocation)5 Query (ddf.catalog.operation.Query)4 QueryRequest (ddf.catalog.operation.QueryRequest)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 GeoEntryQueryable (org.codice.ddf.spatial.geocoding.GeoEntryQueryable)4 Suggestion (org.codice.ddf.spatial.geocoding.Suggestion)4