Search in sources :

Example 1 with SpatialQuery

use of org.geotoolkit.lucene.filter.SpatialQuery in project geotoolkit by Geomatys.

the class LuceneSearcherTest method QueryAndSpatialFilterAfterRemoveTest.

/**
 * Test the combination of a String query and/or spatial filter.
 * @throws java.lang.Exception
 */
@Test
public void QueryAndSpatialFilterAfterRemoveTest() throws Exception {
    // 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(), false);
    /*
         * case 1: a normal spatial request BBOX
         */
    double[] min1 = { -20, -20 };
    double[] max1 = { 20, 20 };
    GeneralEnvelope bbox = new GeneralEnvelope(min1, max1);
    bbox.setCoordinateReferenceSystem(CommonCRS.defaultGeographic());
    org.opengis.filter.Filter bboxFilter = FF.bbox(GEOMETRY_PROPERTY, -20, -20, 20, 20, "CRS:84");
    SpatialQuery bboxQuery = new SpatialQuery(wrap(bboxFilter));
    // we perform a lucene query
    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, 9);
    assertTrue(results.contains("point 1"));
    assertTrue(results.contains("point 1 projected"));
    assertTrue(results.contains("point 2"));
    assertTrue(results.contains("point 3"));
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("line 2"));
    assertTrue(results.contains("line 1"));
    assertTrue(results.contains("line 1 projected"));
    // re-add the document
    final CoordinateReferenceSystem CRS3395 = CRS.forCode("EPSG:3395");
    Document docu = new Document();
    docu.add(new StringField("id", "box 2 projected", Field.Store.YES));
    docu.add(new StringField("docid", 66 + "", Field.Store.YES));
    docu.add(new StringField("metafile", "doc", Field.Store.YES));
    // attention !! reprojeté
    NamedEnvelope env = addBoundingBox(docu, 556597.4539663679, 1113194.9079327357, 1111475.1028522244, 1678147.5163917788, CRS3395);
    indexer = new DocumentIndexer(directory, null, analyzer);
    indexer.indexDocument(new DocumentIndexer.DocumentEnvelope(docu, env));
    indexer.destroy();
    searcher.destroy();
    searcher = new LuceneIndexSearcher(directory, null, new ClassicAnalyzer(), false);
    // 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, 10);
    assertTrue(results.contains("point 1"));
    assertTrue(results.contains("point 1 projected"));
    assertTrue(results.contains("point 2"));
    assertTrue(results.contains("point 3"));
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
    assertTrue(results.contains("line 2"));
    assertTrue(results.contains("line 1"));
    assertTrue(results.contains("line 1 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) LineString(org.locationtech.jts.geom.LineString) 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) Point(org.locationtech.jts.geom.Point) DocumentEnvelope(org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope) 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) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope)

Example 2 with SpatialQuery

use of org.geotoolkit.lucene.filter.SpatialQuery in project geotoolkit by Geomatys.

the class LuceneSearcherTest method mulitpleFilterTest.

/**
 * Test the combinated spatial filter.
 */
@Test
public void mulitpleFilterTest() throws Exception {
    /*
         * case 1: a BBOX TOUCHES filter OR a BBOX filter
         *
         */
    double[] min1 = { 25, -10 };
    double[] max1 = { 60, 50 };
    GeneralEnvelope bbox = new GeneralEnvelope(min1, max1);
    bbox.setCoordinateReferenceSystem(CommonCRS.defaultGeographic());
    org.opengis.filter.Filter filter1 = FF.touches(GEOMETRY_PROPERTY, FF.literal(bbox));
    org.opengis.filter.Filter filter2 = FF.bbox(GEOMETRY_PROPERTY, 25, -10, 60, 50, "CRS:84");
    SpatialQuery spatialQuery1 = new SpatialQuery(wrap(filter1));
    SpatialQuery spatialQuery2 = new SpatialQuery(wrap(filter2));
    BooleanQuery serialQuery = new BooleanQuery.Builder().add(spatialQuery1.getQuery(), BooleanClause.Occur.SHOULD).add(spatialQuery2.getQuery(), BooleanClause.Occur.SHOULD).build();
    SpatialQuery sQuery = new SpatialQuery("", serialQuery, LogicalFilterType.AND);
    // we perform a lucene query
    Set<String> results = searcher.doSearch(sQuery);
    int nbResults = results.size();
    LOGGER.log(Level.FINER, "TO || BBOX: BBox 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 5);
    assertTrue(results.contains("point 4"));
    assertTrue(results.contains("box 3"));
    assertTrue(results.contains("line 1"));
    assertTrue(results.contains("line 1 projected"));
    assertTrue(results.contains("box 5"));
    // TODO add precision
    // assertTrue(results.contains("line 1 projected"));
    /*
         * case 2: same test with AND instead of OR
         *
         */
    serialQuery = new BooleanQuery.Builder().add(spatialQuery1.getQuery(), BooleanClause.Occur.MUST).add(spatialQuery2.getQuery(), BooleanClause.Occur.MUST).build();
    sQuery = new SpatialQuery("", serialQuery, LogicalFilterType.AND);
    // we perform a lucene query
    results = searcher.doSearch(sQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "TO && BBOX: BBox 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 1);
    assertTrue(results.contains("line 1"));
    /*
         * case 3: NOT INTERSECT line1
         *
         */
    geom = GF.createLineString(new Coordinate[] { new Coordinate(7, 40), new Coordinate(6, -40) });
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.intersects(GEOMETRY_PROPERTY, FF.literal(geom));
    SpatialQuery spatialQuery = new SpatialQuery(wrap(filter));
    serialQuery = new BooleanQuery.Builder().add(spatialQuery.getQuery(), BooleanClause.Occur.MUST_NOT).add(simpleQuery, BooleanClause.Occur.MUST).build();
    sQuery = new SpatialQuery("", serialQuery, LogicalFilterType.AND);
    // we perform a lucene query
    results = searcher.doSearch(sQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "NOT INTER:Line 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 11);
    assertTrue(results.contains("point 1"));
    assertTrue(results.contains("point 1 projected"));
    assertTrue(results.contains("point 2"));
    assertTrue(results.contains("point 3"));
    assertTrue(results.contains("point 4"));
    assertTrue(results.contains("point 5"));
    assertTrue(results.contains("box 1"));
    assertTrue(results.contains("box 3"));
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 5"));
    assertTrue(results.contains("line 2"));
    /*
         * case 4: INTERSECT line AND BBOX
         *
         */
    double[] min2 = { -12, -17 };
    double[] max2 = { 15, 50 };
    GeneralEnvelope bbox2 = new GeneralEnvelope(min2, max2);
    bbox2.setCoordinateReferenceSystem(CommonCRS.defaultGeographic());
    org.opengis.filter.Filter bfilter = FF.bbox(GEOMETRY_PROPERTY, -12, -17, 15, 50, "CRS:84");
    SpatialQuery bboxQuery = new SpatialQuery(wrap(bfilter));
    serialQuery = new BooleanQuery.Builder().add(spatialQuery.getQuery(), BooleanClause.Occur.MUST).add(bboxQuery.getQuery(), BooleanClause.Occur.MUST).build();
    sQuery = new SpatialQuery("", serialQuery, LogicalFilterType.AND);
    // we perform a lucene query
    results = searcher.doSearch(sQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "NOT INTER:Line 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 4);
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
    assertTrue(results.contains("line 1"));
    assertTrue(results.contains("line 1 projected"));
    /*
         * case 5: INTERSECT line AND NOT BBOX
         *
         */
    serialQuery = new BooleanQuery.Builder().add(spatialQuery.getQuery(), BooleanClause.Occur.MUST).add(bboxQuery.getQuery(), BooleanClause.Occur.MUST_NOT).build();
    sQuery = new SpatialQuery("", serialQuery, LogicalFilterType.AND);
    // we perform a lucene query
    results = searcher.doSearch(sQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "NOT INTER:Line 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 0);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpatialQuery(org.geotoolkit.lucene.filter.SpatialQuery) LuceneOGCSpatialQuery(org.geotoolkit.lucene.filter.LuceneOGCSpatialQuery) LineString(org.locationtech.jts.geom.LineString) Point(org.locationtech.jts.geom.Point) Coordinate(org.locationtech.jts.geom.Coordinate) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope)

Example 3 with SpatialQuery

use of org.geotoolkit.lucene.filter.SpatialQuery in project geotoolkit by Geomatys.

the class LuceneSearcherTest method touchesTest.

/**
 * Test the spatial filter TOUCHES.
 */
@Test
public void touchesTest() throws Exception {
    /*
         * case 1: point (equals to point 3)
         *
         */
    geom = GF.createPoint(new Coordinate(0, 0));
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.touches(GEOMETRY_PROPERTY, FF.literal(geom));
    SpatialQuery spatialQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    Set<String> results = searcher.doSearch(spatialQuery);
    int nbResults = results.size();
    LOGGER.log(Level.FINER, "TO:Point 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 3);
    // assertTrue(results.contains("point 3")); //it overlaps
    assertTrue(results.contains("line 1"));
    // match because precision errors have been corrected
    assertTrue(results.contains("line 1 projected"));
    assertTrue(results.contains("line 2"));
    /*
         * case 2: another point
         *
         */
    geom = GF.createPoint(new Coordinate(-30, 5));
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.touches(GEOMETRY_PROPERTY, FF.literal(geom));
    spatialQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(spatialQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "TO:Point 2 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 1);
    assertTrue(results.contains("box 4"));
    /*
         * case 3: another point
         *
         */
    geom = GF.createPoint(new Coordinate(-25, -50));
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.touches(GEOMETRY_PROPERTY, FF.literal(geom));
    spatialQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(spatialQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "TO:Point 3 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 1);
    assertTrue(results.contains("box 1"));
    /*
         * case 4: another point
         *
         */
    geom = GF.createPoint(new Coordinate(0, -10));
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.touches(GEOMETRY_PROPERTY, FF.literal(geom));
    spatialQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(spatialQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "TO:Point 4 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 0);
    // assertTrue(results.contains("line 2")); //point intersect or in within, it is not consider "touches" in jts
    /*
         * case 5: another point
         *
         */
    geom = GF.createPoint(new Coordinate(40, 20));
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.touches(GEOMETRY_PROPERTY, FF.literal(geom));
    spatialQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(spatialQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "TO:Point 5 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 0);
    // assertTrue(results.contains("point 4")); //same point intersect,within,overlaps but not consider "touches"
    /*
         * case 6: a line
         *
         */
    geom = GF.createLineString(new Coordinate[] { new Coordinate(7, 30), new Coordinate(7, 0) });
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.touches(GEOMETRY_PROPERTY, FF.literal(geom));
    spatialQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(spatialQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "TO:Line 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 2);
    assertTrue(results.contains("line 1"));
    // match because precision errors have been corrected
    assertTrue(results.contains("line 1 projected"));
    /*
         * case 7: another line
         *
         */
    geom = GF.createLineString(new Coordinate[] { new Coordinate(-15, 3), new Coordinate(30, 4) });
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.touches(GEOMETRY_PROPERTY, FF.literal(geom));
    spatialQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(spatialQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "TO:Line 2 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 2);
    assertTrue(results.contains("box 3"));
    assertTrue(results.contains("box 4"));
    /*
         * case 8: another line
         *
         */
    geom = GF.createLineString(new Coordinate[] { new Coordinate(0, 0), new Coordinate(-40, -40) });
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.touches(GEOMETRY_PROPERTY, FF.literal(geom));
    spatialQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(spatialQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "TO:Line 2 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 5);
    assertTrue(results.contains("point 3"));
    assertTrue(results.contains("box 1"));
    assertTrue(results.contains("line 1"));
    // match because precision errors have been corrected
    assertTrue(results.contains("line 1 projected"));
    assertTrue(results.contains("line 2"));
    /*
         * case 9: a BBOX
         *
         */
    double[] min1 = { -15, 0 };
    double[] max1 = { 30, 50 };
    GeneralEnvelope bbox = new GeneralEnvelope(min1, max1);
    bbox.setCoordinateReferenceSystem(CommonCRS.defaultGeographic());
    filter = FF.touches(GEOMETRY_PROPERTY, FF.literal(bbox));
    spatialQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(spatialQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "TO:BBox 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 7);
    assertTrue(results.contains("point 2"));
    assertTrue(results.contains("point 3"));
    assertTrue(results.contains("box 3"));
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("line 1"));
    // match because precision errors have been corrected
    assertTrue(results.contains("line 1 projected"));
    assertTrue(results.contains("line 2"));
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) SpatialQuery(org.geotoolkit.lucene.filter.SpatialQuery) LuceneOGCSpatialQuery(org.geotoolkit.lucene.filter.LuceneOGCSpatialQuery) LineString(org.locationtech.jts.geom.LineString) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Point(org.locationtech.jts.geom.Point)

Example 4 with SpatialQuery

use of org.geotoolkit.lucene.filter.SpatialQuery in project geotoolkit by Geomatys.

the class LuceneSearcherTest method containsTest.

/**
 * Test the spatial filter CONTAINS.
 */
@Test
public void containsTest() throws Exception {
    /*
         * case 1: BOX/BOX.
         */
    double[] min1 = { -30, -47 };
    double[] max1 = { -26, -42 };
    GeneralEnvelope bbox = new GeneralEnvelope(min1, max1);
    bbox.setCoordinateReferenceSystem(CommonCRS.defaultGeographic());
    filter = FF.contains(GEOMETRY_PROPERTY, FF.literal(bbox));
    SpatialQuery bboxQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    Set<String> results = searcher.doSearch(bboxQuery);
    int nbResults = results.size();
    LOGGER.log(Level.FINER, "CT:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 1);
    assertTrue(results.contains("box 1"));
    /*
         * case 2: BOX/Line
         */
    geom = GF.createLineString(new Coordinate[] { new Coordinate(-25, 5), new Coordinate(-15, 5) });
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.contains(GEOMETRY_PROPERTY, FF.literal(geom));
    bboxQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(bboxQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "CT:Line 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 1);
    assertTrue(results.contains("box 4"));
    /*
         * case 3: BOX/point
         */
    geom = GF.createPoint(new Coordinate(-25, 5));
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.contains(GEOMETRY_PROPERTY, FF.literal(geom));
    bboxQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(bboxQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "CT:Point 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 1);
    assertTrue(results.contains("box 4"));
    /*
         * case 4: Line/point
         */
    geom = GF.createPoint(new Coordinate(20, 0));
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.dwithin(GEOMETRY_PROPERTY, FF.literal(geom), 0.00001, "m");
    bboxQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(bboxQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "CT:Point 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 2);
    assertTrue(results.contains("line 1"));
    assertTrue(results.contains("line 1 projected"));
    /*
         * case 5: Line/Line
         */
    geom = GF.createLineString(new Coordinate[] { new Coordinate(20, 0), new Coordinate(15, 0) });
    JTS.setCRS(geom, CommonCRS.defaultGeographic());
    filter = FF.dwithin(GEOMETRY_PROPERTY, FF.literal(geom), TOLERANCE, "m");
    bboxQuery = new SpatialQuery(wrap(filter));
    // we perform a lucene query
    results = searcher.doSearch(bboxQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "CT:Line 2 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results.
    assertEquals(nbResults, 2);
    assertTrue(results.contains("line 1"));
    assertTrue(results.contains("line 1 projected"));
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) SpatialQuery(org.geotoolkit.lucene.filter.SpatialQuery) LuceneOGCSpatialQuery(org.geotoolkit.lucene.filter.LuceneOGCSpatialQuery) LineString(org.locationtech.jts.geom.LineString) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Point(org.locationtech.jts.geom.Point)

Example 5 with SpatialQuery

use of org.geotoolkit.lucene.filter.SpatialQuery in project geotoolkit by Geomatys.

the class LuceneSearcherTest method QueryAndSpatialFilterTest.

/**
 * Test the combination of a String query and/or spatial filter.
 */
@Test
public void QueryAndSpatialFilterTest() throws Exception {
    /*
         * case 1: a normal spatial request BBOX
         */
    double[] min1 = { -20, -20 };
    double[] max1 = { 20, 20 };
    GeneralEnvelope bbox = new GeneralEnvelope(min1, max1);
    bbox.setCoordinateReferenceSystem(CommonCRS.defaultGeographic());
    org.opengis.filter.Filter bboxFilter = FF.bbox(GEOMETRY_PROPERTY, -20, -20, 20, 20, "CRS:84");
    SpatialQuery sQuery = new SpatialQuery(wrap(bboxFilter));
    // we perform a lucene query
    Set<String> results = searcher.doSearch(sQuery);
    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, 10);
    assertTrue(results.contains("point 1"));
    assertTrue(results.contains("point 1 projected"));
    assertTrue(results.contains("point 2"));
    assertTrue(results.contains("point 3"));
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
    assertTrue(results.contains("line 2"));
    assertTrue(results.contains("line 1"));
    assertTrue(results.contains("line 1 projected"));
    /*
         *  case 1.2: NOT a normal spatial request BBOX
         */
    sQuery = new SpatialQuery(null, wrap(bboxFilter), LogicalFilterType.NOT);
    // we perform a lucene query
    results = searcher.doSearch(sQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "NOT QnS:BBOX 1 CRS=4326: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 5);
    assertTrue(results.contains("point 4"));
    assertTrue(results.contains("point 5"));
    assertTrue(results.contains("box 1"));
    assertTrue(results.contains("box 3"));
    assertTrue(results.contains("box 5"));
    /*
         *  case 2: same filter with a StringQuery
         */
    // we perform a lucene query
    sQuery = new SpatialQuery("id:point*", wrap(bboxFilter), LogicalFilterType.AND);
    results = searcher.doSearch(sQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS: title like point* AND BBOX 1: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 4);
    assertTrue(results.contains("point 1"));
    assertTrue(results.contains("point 1 projected"));
    assertTrue(results.contains("point 2"));
    assertTrue(results.contains("point 3"));
    /*
         *  case 2.1: NOT same filter with a StringQuery
         */
    // we perform a lucene query
    sQuery = new SpatialQuery(null, LogicalFilterType.NOT);
    sQuery.addSubQuery(new SpatialQuery("id:point*", wrap(bboxFilter), LogicalFilterType.AND));
    results = searcher.doSearch(sQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS: title like point* AND BBOX 1: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 11);
    assertTrue(results.contains("point 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("line 1"));
    assertTrue(results.contains("line 1 projected"));
    assertTrue(results.contains("line 2"));
    assertTrue(results.contains("box 3"));
    assertTrue(results.contains("point 5"));
    assertTrue(results.contains("box 1"));
    assertTrue(results.contains("box 5"));
    /*
         *  case 3: same filter same query but with an OR
         */
    // we perform two lucene query
    sQuery = new SpatialQuery("id:point*", wrap(bboxFilter), LogicalFilterType.OR);
    results = searcher.doSearch(sQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS: name like point* OR BBOX 1: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 12);
    assertTrue(results.contains("point 1"));
    assertTrue(results.contains("point 1 projected"));
    assertTrue(results.contains("point 2"));
    assertTrue(results.contains("point 3"));
    assertTrue(results.contains("point 4"));
    assertTrue(results.contains("point 5"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
    assertTrue(results.contains("line 2"));
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("line 1 projected"));
    assertTrue(results.contains("line 1"));
    /*
         *  case 3.1: NOT same filter same query but with an OR
         */
    // we perform two lucene query
    sQuery = new SpatialQuery(null, LogicalFilterType.NOT);
    sQuery.addSubQuery(new SpatialQuery("id:point*", wrap(bboxFilter), LogicalFilterType.OR));
    results = searcher.doSearch(sQuery);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS: name like point* OR BBOX 1: nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 3);
    assertTrue(results.contains("box 3"));
    assertTrue(results.contains("box 1"));
    assertTrue(results.contains("box 5"));
    /*
         *  case 4: two filter two query with an OR in the middle
         *          (BBOX and name like point*) OR (INTERSECT line1 and name like box*)
         */
    // we perform two lucene query
    Geometry geom1 = GF.createLineString(new Coordinate[] { new Coordinate(40, 30), new Coordinate(40, -30) });
    JTS.setCRS(geom1, CommonCRS.defaultGeographic());
    filter = FF.intersects(GEOMETRY_PROPERTY, FF.literal(geom1));
    SpatialQuery interQuery = new SpatialQuery(wrap(filter));
    SpatialQuery query1 = new SpatialQuery("id:point*", wrap(bboxFilter), LogicalFilterType.AND);
    SpatialQuery query2 = new SpatialQuery("id:box*", interQuery.getQuery(), LogicalFilterType.AND);
    SpatialQuery query3 = new SpatialQuery(null, LogicalFilterType.OR);
    query3.addSubQuery(query1);
    query3.addSubQuery(query2);
    results = searcher.doSearch(query3);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS: (name like point* AND BBOX 1) OR (name like box* AND INTERSECT line 1): nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 5);
    assertTrue(results.contains("point 1"));
    assertTrue(results.contains("point 1 projected"));
    assertTrue(results.contains("point 2"));
    assertTrue(results.contains("point 3"));
    assertTrue(results.contains("box 3"));
    /*
         *  case 4.1: NOT (two filter two query with an OR in the middle
         *          (BBOX and name like point*) OR (INTERSECT line1 and name like box*))
         */
    query3 = new SpatialQuery(null, LogicalFilterType.NOT);
    query3.addSubQuery(query1);
    query3.addSubQuery(query2);
    results = searcher.doSearch(query3);
    nbResults = results.size();
    LOGGER.log(Level.FINER, "QnS: (name like point* AND BBOX 1) OR (name like box* AND INTERSECT line 1): nb Results: {0}", nbResults);
    // we verify that we obtain the correct results
    assertEquals(nbResults, 10);
    assertTrue(results.contains("point 4"));
    assertTrue(results.contains("box 2"));
    assertTrue(results.contains("box 2 projected"));
    assertTrue(results.contains("box 4"));
    assertTrue(results.contains("line 1"));
    assertTrue(results.contains("line 1 projected"));
    assertTrue(results.contains("line 2"));
    assertTrue(results.contains("point 5"));
    assertTrue(results.contains("box 1"));
    assertTrue(results.contains("box 5"));
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) SpatialQuery(org.geotoolkit.lucene.filter.SpatialQuery) LuceneOGCSpatialQuery(org.geotoolkit.lucene.filter.LuceneOGCSpatialQuery) LineString(org.locationtech.jts.geom.LineString) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Point(org.locationtech.jts.geom.Point)

Aggregations

SpatialQuery (org.geotoolkit.lucene.filter.SpatialQuery)78 LuceneOGCSpatialQuery (org.geotoolkit.lucene.filter.LuceneOGCSpatialQuery)62 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)52 Coordinate (org.locationtech.jts.geom.Coordinate)33 Document (org.apache.lucene.document.Document)30 ArrayList (java.util.ArrayList)28 Test (org.junit.Test)26 LinkedHashSet (java.util.LinkedHashSet)22 BooleanQuery (org.apache.lucene.search.BooleanQuery)17 TopDocs (org.apache.lucene.search.TopDocs)14 TotalHits (org.apache.lucene.search.TotalHits)14 LineString (org.locationtech.jts.geom.LineString)14 Point (org.locationtech.jts.geom.Point)14 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)7 Analyzer (org.apache.lucene.analysis.Analyzer)6 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)6 ClassicAnalyzer (org.geotoolkit.lucene.analysis.standard.ClassicAnalyzer)6 StringField (org.apache.lucene.document.StringField)4 DocumentEnvelope (org.geotoolkit.lucene.DocumentIndexer.DocumentEnvelope)4 Geometry (org.locationtech.jts.geom.Geometry)3