Search in sources :

Example 1 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.

the class GeoNamesQueryLuceneIndex method doGetNearestCities.

protected List<NearbyLocation> doGetNearestCities(final Shape shape, final int radiusInKm, final int maxResults, final Directory directory) throws GeoEntryQueryException {
    notNull(shape, "GeoNamesQueryLuceneIndex.doGetNearestCities(): argument 'shape' may not be null.");
    if (radiusInKm <= 0) {
        throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetNearestCities(): radiusInKm must be positive.");
    }
    if (maxResults <= 0) {
        throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetNearestCities(): maxResults must be positive.");
    }
    if (directory == null) {
        return Collections.emptyList();
    }
    try (final IndexReader indexReader = createIndexReader(directory)) {
        final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
        final List<NearbyLocation> closestCities = new ArrayList<>();
        final Point center = shape.getCenter();
        final Query filter = createSpatialQuery(center, radiusInKm);
        // Query for all the documents in the index that are cities, then filter those
        // results for the ones that are in the search area.
        final BooleanQuery booleanQuery = new BooleanQuery.Builder().add(PPL_QUERY, BooleanClause.Occur.MUST).add(filter, BooleanClause.Occur.FILTER).build();
        final TopDocs topDocs = indexSearcher.search(booleanQuery, maxResults, SORT);
        if (topDocs.totalHits > 0) {
            for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                final double lat = Double.parseDouble(indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.LATITUDE_FIELD));
                final double lon = Double.parseDouble(indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.LONGITUDE_FIELD));
                final String name = indexSearcher.doc(scoreDoc.doc).get(GeoNamesLuceneConstants.NAME_FIELD);
                final NearbyLocation city = new NearbyLocationImpl(center, new PointImpl(lon, lat, SPATIAL_CONTEXT), name);
                closestCities.add(city);
            }
        }
        return closestCities;
    } catch (IOException e) {
        throw new GeoEntryQueryException("Error reading the index", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ArrayList(java.util.ArrayList) Point(org.locationtech.spatial4j.shape.Point) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) IndexReader(org.apache.lucene.index.IndexReader) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 2 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.

the class GeoNamesQueryLuceneIndex method doGetCountryCode.

protected String doGetCountryCode(Shape shape, int radiusInKm, Directory directory) throws GeoEntryQueryException {
    notNull(shape, "GeoNamesQueryLuceneIndex.doGetCountryCode(): argument 'shape' may not be null.");
    notNull(directory, "GeoNamesQueryLuceneIndex.doGetCountryCode(): argument 'directory' may not be null.");
    if (radiusInKm <= 0) {
        throw new IllegalArgumentException("GeoNamesQueryLuceneIndex.doGetCountryCode(): radiusInKm must be positive.");
    }
    try (final IndexReader indexReader = createIndexReader(directory)) {
        final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
        final Point center = shape.getCenter();
        final Query filter = createSpatialQuery(center, radiusInKm);
        final BooleanQuery booleanQuery = new BooleanQuery.Builder().add(filter, BooleanClause.Occur.FILTER).build();
        final TopDocs topDocs = indexSearcher.search(booleanQuery, 1, SORT);
        String countryCode = null;
        if (topDocs.totalHits > 0) {
            countryCode = indexSearcher.doc(topDocs.scoreDocs[0].doc).get(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD);
        }
        return countryCode;
    } catch (IOException e) {
        throw new GeoEntryQueryException("Error reading the index", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) IndexReader(org.apache.lucene.index.IndexReader) Point(org.locationtech.spatial4j.shape.Point) IOException(java.io.IOException)

Example 3 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.

the class GeoNamesQueryLuceneIndex method doQuery.

protected List<GeoEntry> doQuery(final String queryString, final int maxResults, final Directory directory) throws GeoEntryQueryException {
    if (StringUtils.isBlank(queryString)) {
        throw new IllegalArgumentException("The query string cannot be null or empty.");
    }
    if (maxResults < 1) {
        throw new IllegalArgumentException("maxResults must be positive.");
    }
    if (directory == null) {
        return Collections.emptyList();
    }
    try (final IndexReader indexReader = createIndexReader(directory)) {
        final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
        final Query query = createQuery(queryString);
        final TopDocs topDocs = indexSearcher.search(query, maxResults);
        if (topDocs.totalHits > 0) {
            final List<GeoEntry> results = new ArrayList<>();
            for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                final Document document = indexSearcher.doc(scoreDoc.doc);
                // The alternate names aren't being stored (they are only used for queries),
                // so we don't retrieve them here.
                results.add(new GeoEntry.Builder().name(document.get(GeoNamesLuceneConstants.NAME_FIELD)).latitude(Double.parseDouble(document.get(GeoNamesLuceneConstants.LATITUDE_FIELD))).longitude(Double.parseDouble(document.get(GeoNamesLuceneConstants.LONGITUDE_FIELD))).featureCode(document.get(GeoNamesLuceneConstants.FEATURE_CODE_FIELD)).population(Long.parseLong(document.get(GeoNamesLuceneConstants.POPULATION_FIELD))).countryCode(document.get(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD)).build());
            }
            return results;
        } else {
            return Collections.emptyList();
        }
    } catch (IOException e) {
        throw new GeoEntryQueryException("Error reading the index", e);
    } catch (ParseException e) {
        throw new GeoEntryQueryException("Error parsing query", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) IndexReader(org.apache.lucene.index.IndexReader) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 4 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.

the class GeoNamesQueryLuceneDirectoryIndex method openDirectoryAndCheckForIndex.

private Directory openDirectoryAndCheckForIndex() throws GeoEntryQueryException {
    Directory directory;
    try {
        directory = openDirectory();
        if (!indexExists(directory)) {
            directory.close();
            LOGGER.debug("There is no index at {}. Load a Geonames file into the offline gazetteer", indexLocation);
            return null;
        }
        return directory;
    } catch (IOException e) {
        throw new GeoEntryQueryException("Error opening the index directory at " + indexLocation, e);
    }
}
Also used : GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) IOException(java.io.IOException) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 5 with GeoEntryQueryException

use of org.codice.ddf.spatial.geocoding.GeoEntryQueryException in project ddf by codice.

the class GeoCoderEndpoint method getNearbyCities.

@GET
@Path("nearby/cities/{wkt}")
public Response getNearbyCities(@PathParam("wkt") String wkt) {
    GeoCoder geoCoder = geoCoderFactory.getService();
    try {
        NearbyLocation nearbyLocation = geoCoder.getNearbyCity(wkt);
        if (nearbyLocation != null) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("direction", nearbyLocation.getCardinalDirection());
            jsonObject.put("distance", nearbyLocation.getDistance());
            jsonObject.put("name", nearbyLocation.getName());
            return Response.ok(jsonObject.toJSONString()).build();
        } else {
            return Response.status(Response.Status.NO_CONTENT).build();
        }
    } catch (GeoEntryQueryException e) {
        LOGGER.debug("Error querying GeoNames resource with wkt:{}", wkt, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : GeoCoder(org.codice.ddf.spatial.geocoder.GeoCoder) JSONObject(net.minidev.json.JSONObject) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

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