Search in sources :

Example 6 with NamedEnvelope

use of org.geotoolkit.index.tree.manager.NamedEnvelope in project geotoolkit by Geomatys.

the class LuceneEnvelopeOnlyTest method addBoundingBox.

/**
 * Add a boundingBox geometry to the specified Document.
 *
 * @param doc  The document to add the geometry
 * @param minx the minimun X coordinate of the bounding box.
 * @param maxx the maximum X coordinate of the bounding box.
 * @param miny the minimun Y coordinate of the bounding box.
 * @param maxy the maximum Y coordinate of the bounding box.
 * @param crsName The coordinate reference system in witch the coordinates are expressed.
 */
private static NamedEnvelope addBoundingBox(final Document doc, final double minx, final double maxx, final double miny, final double maxy, final CoordinateReferenceSystem crs) throws FactoryException, TransformException {
    final Geometry poly = LuceneUtils.getPolygon(minx, maxx, miny, maxy, crs);
    final String id = doc.get("id");
    final NamedEnvelope namedBound = LuceneUtils.getNamedEnvelope(id, poly, CommonCRS.defaultGeographic());
    doc.add(new StoredField(LuceneOGCSpatialQuery.GEOMETRY_FIELD_NAME, WKBUtils.toWKBwithSRID(poly)));
    return namedBound;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) StoredField(org.apache.lucene.document.StoredField) NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope)

Example 7 with NamedEnvelope

use of org.geotoolkit.index.tree.manager.NamedEnvelope in project geotoolkit by Geomatys.

the class LuceneSearcherEnvelopeOnlyTest method QueryAndSpatialFilterAfterRemoveTest.

/**
 * Test the combination of a String query and/or spatial filter.
 * @throws java.lang.Exception
 */
@Test
public void QueryAndSpatialFilterAfterRemoveTest() throws Exception {
    org.opengis.filter.Filter bboxFilter = FF.bbox(GEOMETRY_PROPERTY, -20, -20, 20, 20, "CRS:84");
    SpatialQuery bboxQuery = new SpatialQuery(wrap(bboxFilter));
    Set<String> results = searcher.doSearch(bboxQuery);
    int nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 3);
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
    /*
         * we remove a document
         */
    final Analyzer analyzer = new StandardAnalyzer();
    DocumentIndexer indexer = new DocumentIndexer(directory, null, analyzer);
    indexer.removeDocument("box 2 projected");
    indexer.destroy();
    searcher.destroy();
    searcher = new LuceneIndexSearcher(directory, null, new ClassicAnalyzer(), true);
    // we perform a lucene query
    results = searcher.doSearch(bboxQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 2);
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    // re-add the document
    final CoordinateReferenceSystem CRS3395 = CRS.forCode("EPSG:3395");
    Document doc = new Document();
    doc.add(new StringField("id", "box 2 projected", Field.Store.YES));
    doc.add(new StringField("docid", 66 + "", Field.Store.YES));
    doc.add(new StringField("metafile", "doc", Field.Store.YES));
    NamedEnvelope env = addBoundingBox(doc, 556597.4539663679, 1113194.9079327357, 1111475.1028522244, 1678147.5163917788, CRS3395);
    // attention !! reprojeté
    envelopes.put("box 2 projected", env);
    indexer = new DocumentIndexer(directory, null, analyzer);
    indexer.indexDocument(new DocumentEnvelope(doc, env));
    indexer.destroy();
    searcher.destroy();
    searcher = new LuceneIndexSearcher(directory, null, new StandardAnalyzer(), true);
    // we perform a lucene query
    results = searcher.doSearch(bboxQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 3);
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
}
Also used : NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) SpatialQuery(org.geotoolkit.lucene.filter.SpatialQuery) LuceneOGCSpatialQuery(org.geotoolkit.lucene.filter.LuceneOGCSpatialQuery) LuceneIndexSearcher(org.geotoolkit.lucene.index.LuceneIndexSearcher) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) ClassicAnalyzer(org.geotoolkit.lucene.analysis.standard.ClassicAnalyzer) Document(org.apache.lucene.document.Document) ClassicAnalyzer(org.geotoolkit.lucene.analysis.standard.ClassicAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) StringField(org.apache.lucene.document.StringField) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) DocumentEnvelope(org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope)

Example 8 with NamedEnvelope

use of org.geotoolkit.index.tree.manager.NamedEnvelope in project geotoolkit by Geomatys.

the class LuceneSearcherEnvelopeOnlyTest method rTreeBBOXTest.

/**
 * Test the rTree.
 * @throws java.lang.Exception
 */
@Test
public void rTreeBBOXTest() throws Exception {
    final Tree rTree = SQLRtreeManager.get(searcher.getFileDirectory(), this);
    /*
         * first bbox
         */
    GeneralEnvelope env = new GeneralEnvelope(CommonCRS.defaultGeographic());
    env.setEnvelope(-20, -20, 20, 20);
    // reproject to tree CRS
    env = (GeneralEnvelope) Envelopes.transform(env, treeCrs);
    // we perform a retree query
    List<Envelope> docs = new ArrayList<>();
    final TreeElementMapper<NamedEnvelope> tem = rTree.getTreeElementMapper();
    int[] resultsID = rTree.searchID(env);
    getresultsfromID(resultsID, tem, docs);
    int nbResults = docs.size();
    LOGGER.log(Level.FINER, "BBOX:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    List<String> results = new ArrayList<>();
    for (int i = 0; i < nbResults; i++) {
        NamedEnvelope doc = (NamedEnvelope) docs.get(i);
        String id = doc.getId();
        results.add(id);
        LOGGER.log(Level.FINER, "\tid: {0}", id);
    }
    // we verify that we obtain the correct results
    assertEquals(nbResults, 3);
    assertTrue(results.contains("box 2"));
    // depends on test order
    assertTrue(results.contains("box 2 projected"));
    assertTrue(results.contains("box 4"));
    /*
         * The same box in a diferent crs
         */
    env = new GeneralEnvelope(CRS.forCode("EPSG:3395"));
    env.setEnvelope(-2226389.8158654715, -2258423.6490963786, 2226389.8158654715, 2258423.6490963805);
    // reproject to tree CRS
    env = (GeneralEnvelope) Envelopes.transform(env, treeCrs);
    // we perform a retree query
    docs.clear();
    resultsID = rTree.searchID(env);
    getresultsfromID(resultsID, tem, docs);
    nbResults = docs.size();
    LOGGER.log(Level.FINER, "BBOX:BBOX 1 CRS= 3395: nb Results: {0}", nbResults);
    results = new ArrayList<>();
    for (int i = 0; i < nbResults; i++) {
        NamedEnvelope doc = (NamedEnvelope) docs.get(i);
        String name = doc.getId();
        results.add(name);
        LOGGER.log(Level.FINER, "\tid: {0}", name);
    }
    // we verify that we obtain the correct results
    assertEquals(nbResults, 3);
    assertTrue(results.contains("box 2"));
    // depends on test order
    assertTrue(results.contains("box 2 projected"));
    assertTrue(results.contains("box 4"));
    /*
         * second bbox
         */
    env = new GeneralEnvelope(CommonCRS.defaultGeographic());
    env.setEnvelope(-5, -5, 60, 60);
    // reproject to tree CRS
    env = (GeneralEnvelope) Envelopes.transform(env, treeCrs);
    // we perform a retree query
    docs.clear();
    resultsID = rTree.searchID(env);
    getresultsfromID(resultsID, tem, docs);
    nbResults = docs.size();
    LOGGER.log(Level.FINER, "BBOX:BBOX 2 CRS= 4326: nb Results: {0}", nbResults);
    results = new ArrayList<>();
    for (int i = 0; i < nbResults; i++) {
        NamedEnvelope doc = (NamedEnvelope) docs.get(i);
        String name = doc.getId();
        results.add(name);
        LOGGER.log(Level.FINER, "\tid: {0}", name);
    }
    // we verify that we obtain the correct results
    assertEquals(nbResults, 4);
    assertTrue(results.contains("box 3"));
    assertTrue(results.contains("box 2"));
    // depends on test order
    assertTrue(results.contains("box 2 projected"));
    assertTrue(results.contains("box 5"));
    /*
         * third bbox
         */
    env = new GeneralEnvelope(CommonCRS.defaultGeographic());
    env.setEnvelope(40, -9, 50, -5);
    // reproject to tree CRS
    env = (GeneralEnvelope) Envelopes.transform(env, treeCrs);
    // we perform a retree query
    docs.clear();
    resultsID = rTree.searchID(env);
    getresultsfromID(resultsID, tem, docs);
    nbResults = docs.size();
    LOGGER.log(Level.FINER, "BBOX:BBOX 3 CRS= 4326: nb Results: {0}", nbResults);
    results = new ArrayList<>();
    for (int i = 0; i < nbResults; i++) {
        NamedEnvelope doc = (NamedEnvelope) docs.get(i);
        String name = doc.getId();
        results.add(name);
        LOGGER.log(Level.FINER, "\tid: {0}", name);
    }
    // we verify that we obtain the correct results
    assertEquals(nbResults, 1);
    assertTrue(results.contains("box 5"));
}
Also used : NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) Tree(org.geotoolkit.index.tree.Tree) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) DocumentEnvelope(org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope)

Example 9 with NamedEnvelope

use of org.geotoolkit.index.tree.manager.NamedEnvelope in project geotoolkit by Geomatys.

the class LuceneOGCWeight method scorer.

@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
    // final SortedDocValues values = context.reader().getSortedDocValues(IDENTIFIER_FIELD_NAME);
    final LeafReader reader = context.reader();
    boolean treeSearch = false;
    boolean reverse = false;
    boolean distanceFilter = false;
    final Set<String> treeMatching = new HashSet<>();
    if (tree != null) {
        /*
             * For distance buffer filter no envelope only mode
             */
        List<Expression<Object, ?>> expressions = filter.getExpressions();
        Expression e2 = (expressions.size() >= 2) ? expressions.get(1) : null;
        if (filter instanceof DistanceOperator) {
            distanceFilter = true;
            reverse = filter.getOperatorType() == DistanceOperatorName.BEYOND;
            final DistanceOperator sp = (DistanceOperator) filter;
            if (e2 instanceof Literal) {
                try {
                    final Literal lit = (Literal) e2;
                    Quantity distance = sp.getDistance();
                    final GeneralEnvelope bound = getExtendedReprojectedEnvelope(lit.getValue(), tree.getCrs(), distance.getUnit().toString(), distance.getValue().doubleValue());
                    final int[] resultID = tree.searchID(bound);
                    Arrays.sort(resultID);
                    treeMatching.clear();
                    TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                    for (int id : resultID) {
                        final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                        if (env != null) {
                            treeMatching.add(env.getId());
                        }
                    }
                    treeSearch = true;
                } catch (FactoryException ex) {
                    throw new IOException(ex);
                } catch (StoreIndexException ex) {
                    Throwable cause = ex.getCause();
                    if (cause instanceof IOException) {
                        throw (IOException) cause;
                    } else {
                        throw new IOException(ex);
                    }
                }
            } else {
                LOGGER.log(Level.WARNING, "Not a literal for spatial filter:{0}", e2);
            }
        } else if (filter instanceof BinarySpatialOperator) {
            final BinarySpatialOperator sp = (BinarySpatialOperator) filter;
            if (e2 instanceof Literal) {
                final Literal lit = (Literal) e2;
                final Envelope boundFilter = getReprojectedEnvelope(lit.getValue(), tree.getCrs());
                try {
                    if (filterType == SpatialFilterType.CROSSES || !envelopeOnly) {
                        if (filterType == SpatialFilterType.DISJOINT) {
                            reverse = true;
                        }
                        final int[] resultID = tree.searchID(boundFilter);
                        Arrays.sort(resultID);
                        final TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                        treeMatching.clear();
                        for (int id : resultID) {
                            final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                            if (env != null) {
                                treeMatching.add(env.getId());
                            }
                        }
                        treeSearch = true;
                        envelopeOnly = false;
                    } else {
                        final int[] resultID = TreeX.search(tree, boundFilter, filterType);
                        Arrays.sort(resultID);
                        final TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                        treeMatching.clear();
                        for (int id : resultID) {
                            final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                            if (env != null) {
                                treeMatching.add(env.getId());
                            }
                        }
                        treeSearch = true;
                    }
                } catch (StoreIndexException ex) {
                    Throwable cause = ex.getCause();
                    if (cause instanceof IOException) {
                        throw (IOException) cause;
                    } else {
                        throw new IOException(ex);
                    }
                }
            } else {
                LOGGER.log(Level.WARNING, "Not a literal for spatial filter:{0}", e2);
            }
        } else {
            LOGGER.log(Level.WARNING, "not a spatial operator:{0}", filter.getClass().getName());
        }
    } else {
        LOGGER.finer("Null R-tree in spatial search");
    }
    final BitSet set = new FixedBitSet(reader.maxDoc());
    Bits b = reader.getLiveDocs();
    if (b == null) {
        b = new Bits.MatchAllBits(reader.maxDoc());
    }
    for (int i = 0; i < b.length(); i++) {
        if (b.get(i)) {
            final int docId = i;
            final Document doc = reader.document(docId, ID_FIELDS);
            final String id = doc.get(IDENTIFIER_FIELD_NAME);
            final boolean match = treeMatching.contains(id);
            if (treeSearch && reverse && !match) {
                set.set(docId);
            } else if (!treeSearch || match) {
                if (envelopeOnly && !distanceFilter) {
                    set.set(docId);
                } else {
                    final Document geoDoc = reader.document(docId, GEOMETRY_FIELDS);
                    if (filter.test(geoDoc)) {
                        set.set(docId);
                    }
                }
            }
        }
    }
    return new ConstantScoreScorer(this, boost, scoreMode, new BitSetIterator(set, 5));
}
Also used : NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) FactoryException(org.opengis.util.FactoryException) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) LuceneUtils.getReprojectedEnvelope(org.geotoolkit.lucene.LuceneUtils.getReprojectedEnvelope) LuceneUtils.getExtendedReprojectedEnvelope(org.geotoolkit.lucene.LuceneUtils.getExtendedReprojectedEnvelope) NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) Document(org.apache.lucene.document.Document) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) FixedBitSet(org.apache.lucene.util.FixedBitSet) Literal(org.opengis.filter.Literal) HashSet(java.util.HashSet) BinarySpatialOperator(org.opengis.filter.BinarySpatialOperator) StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) BitSetIterator(org.apache.lucene.util.BitSetIterator) LeafReader(org.apache.lucene.index.LeafReader) DistanceOperator(org.opengis.filter.DistanceOperator) FixedBitSet(org.apache.lucene.util.FixedBitSet) BitSet(org.apache.lucene.util.BitSet) Quantity(javax.measure.Quantity) IOException(java.io.IOException) Expression(org.opengis.filter.Expression) TreeElementMapper(org.geotoolkit.index.tree.TreeElementMapper) Bits(org.apache.lucene.util.Bits) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope)

Example 10 with NamedEnvelope

use of org.geotoolkit.index.tree.manager.NamedEnvelope in project geotoolkit by Geomatys.

the class AbstractIndexer method addGeometry.

/**
 * Add a geometric field with a JTS geometry in the specified lucene document.
 * @param doc The lucene document currently building.
 * @param geom A JTS geometry
 */
public NamedEnvelope addGeometry(final Document doc, final Geometry geom, final CoordinateReferenceSystem crs) {
    NamedEnvelope namedBound = null;
    try {
        final String id = doc.get("id");
        namedBound = LuceneUtils.getNamedEnvelope(id, geom, crs);
        rTree.insert(namedBound);
        rTree.getTreeElementMapper().flush();
        rTree.flush();
    } catch (TransformException | FactoryException | MismatchedReferenceSystemException | StoreIndexException | IOException ex) {
        LOGGER.log(Level.WARNING, "Unable to insert envelope in R-Tree.", ex);
    }
    doc.add(new StoredField(LuceneOGCSpatialQuery.GEOMETRY_FIELD_NAME, WKBUtils.toWKBwithSRID(geom)));
    return namedBound;
}
Also used : MismatchedReferenceSystemException(org.opengis.geometry.MismatchedReferenceSystemException) StoreIndexException(org.geotoolkit.index.tree.StoreIndexException) StoredField(org.apache.lucene.document.StoredField) NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) FactoryException(org.opengis.util.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException)

Aggregations

NamedEnvelope (org.geotoolkit.index.tree.manager.NamedEnvelope)22 StoredField (org.apache.lucene.document.StoredField)11 DocumentEnvelope (org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope)7 Document (org.apache.lucene.document.Document)6 IOException (java.io.IOException)5 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)5 LineString (org.locationtech.jts.geom.LineString)5 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)5 StringField (org.apache.lucene.document.StringField)4 Geometry (org.locationtech.jts.geom.Geometry)4 StoreIndexException (org.geotoolkit.index.tree.StoreIndexException)3 Point (org.locationtech.jts.geom.Point)3 Envelope (org.opengis.geometry.Envelope)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Tree (org.geotoolkit.index.tree.Tree)2