Search in sources :

Example 16 with RecursivePrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy 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 RecursivePrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy 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)

Example 18 with RecursivePrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy in project titan by thinkaurelius.

the class LuceneExample method getSpatialStrategy.

private SpatialStrategy getSpatialStrategy(String key) {
    SpatialStrategy strategy = spatial.get(key);
    if (strategy == null) {
        final int maxLevels = 11;
        SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
        strategy = new RecursivePrefixTreeStrategy(grid, key);
        spatial.put(key, strategy);
    }
    return strategy;
}
Also used : 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)

Aggregations

RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)18 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)12 GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)11 SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)6 QuadPrefixTree (org.apache.lucene.spatial.prefix.tree.QuadPrefixTree)4 ArrayList (java.util.ArrayList)3 TermQueryPrefixTreeStrategy (org.apache.lucene.spatial.prefix.TermQueryPrefixTreeStrategy)3 PackedQuadPrefixTree (org.apache.lucene.spatial.prefix.tree.PackedQuadPrefixTree)3 SerializedDVStrategy (org.apache.lucene.spatial.serialized.SerializedDVStrategy)3 ParametersFactory (com.carrotsearch.randomizedtesting.annotations.ParametersFactory)2 CompositeSpatialStrategy (org.apache.lucene.spatial.composite.CompositeSpatialStrategy)2 PrefixTreeStrategy (org.apache.lucene.spatial.prefix.PrefixTreeStrategy)2 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)2 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)2 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)2 Point (org.locationtech.spatial4j.shape.Point)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