Search in sources :

Example 16 with GeoEntryQueryException

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"));
}
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 17 with GeoEntryQueryException

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));
}
Also used : GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Point(org.locationtech.jts.geom.Point) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 18 with GeoEntryQueryException

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));
}
Also used : GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) Test(org.junit.Test)

Example 19 with GeoEntryQueryException

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();
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) HashMap(java.util.HashMap) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) SuggestionImpl(org.codice.ddf.spatial.geocoding.context.impl.SuggestionImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 20 with GeoEntryQueryException

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();
}
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) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

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