Search in sources :

Example 16 with GeohashPrefixTree

use of org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree in project ddf by codice.

the class GeoNamesQueryLuceneIndex method createSpatialQuery.

private Query createSpatialQuery(Point shapeCenter, int radiusInKm) {
    final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
    final SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
    // Create a spatial filter that will select the documents that are in the specified
    // search radius around the metacard's center.
    final double searchRadiusDegrees = radiusInKm * DistanceUtils.KM_TO_DEG;
    final SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, SPATIAL_CONTEXT.getShapeFactory().circle(shapeCenter, searchRadiusDegrees));
    return strategy.makeQuery(args);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Example 17 with GeohashPrefixTree

use of org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree in project ddf by codice.

the class GeoNamesLuceneIndexer method updateIndex.

@Override
public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoEntryIndexingException, GeoEntryExtractionException, GeoNamesRemoteDownloadException {
    Directory directory;
    try {
        directory = FSDirectory.open(Paths.get(indexLocation));
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Couldn't open the directory for the index, " + indexLocation, e);
    }
    // Try-with-resources to ensure the IndexWriter always gets closed.
    try (final IndexWriter indexWriter = createIndexWriter(create, directory)) {
        final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
        final SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
        final ExtractionCallback extractionCallback = new ExtractionCallback() {

            @Override
            public void extracted(final GeoEntry newEntry) throws GeoEntryIndexingException {
                try {
                    addDocument(indexWriter, newEntry, strategy);
                } catch (IOException e) {
                    throw new GeoEntryIndexingException("Error writing to the index.", e);
                }
            }

            @Override
            public void updateProgress(final int progress) {
                if (progressCallback != null) {
                    progressCallback.updateProgress(progress);
                }
            }
        };
        try {
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, extractionCallback);
        } catch (GeoEntryExtractionException e) {
            // Need to roll back here before the IndexWriter is closed at the end of the try
            // block.
            indexWriter.rollback();
            throw e;
        }
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Error writing to the index.", e);
    }
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) IndexWriter(org.apache.lucene.index.IndexWriter) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) IOException(java.io.IOException) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Aggregations

GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)17 RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)11 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)10 SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)5 QuadPrefixTree (org.apache.lucene.spatial.prefix.tree.QuadPrefixTree)4 ArrayList (java.util.ArrayList)3 TermQueryPrefixTreeStrategy (org.apache.lucene.spatial.prefix.TermQueryPrefixTreeStrategy)3 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)3 ParametersFactory (com.carrotsearch.randomizedtesting.annotations.ParametersFactory)2 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)2 SerializedDVStrategy (org.apache.lucene.spatial.serialized.SerializedDVStrategy)2 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 CompositeSpatialStrategy (org.apache.lucene.spatial.composite.CompositeSpatialStrategy)1 Cell (org.apache.lucene.spatial.prefix.tree.Cell)1 PackedQuadPrefixTree (org.apache.lucene.spatial.prefix.tree.PackedQuadPrefixTree)1