Search in sources :

Example 1 with NamedEnvelope

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

the class LuceneSGBDTreeEltMapper method getObjectFromTreeIdentifier.

@Override
public NamedEnvelope getObjectFromTreeIdentifier(final int treeIdentifier) throws IOException {
    NamedEnvelope result = null;
    try (Connection conn = source.getConnection();
        PreparedStatement stmt = conn.prepareStatement("SELECT * FROM \"" + schemaName + "\".\"records\" WHERE \"id\"=?")) {
        stmt.setInt(1, treeIdentifier);
        try (ResultSet rs = stmt.executeQuery()) {
            if (rs.next()) {
                final String identifier = rs.getString("identifier");
                final int nbEnv = rs.getInt("nbenv");
                final double minx = rs.getDouble("minx");
                final double maxx = rs.getDouble("maxx");
                final double miny = rs.getDouble("miny");
                final double maxy = rs.getDouble("maxy");
                result = new NamedEnvelope(crs, identifier, nbEnv);
                result.setRange(0, minx, maxx);
                result.setRange(1, miny, maxy);
            }
        }
    } catch (SQLException ex) {
        throw new IOException("Error while getting envelope", ex);
    }
    return result;
}
Also used : NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 2 with NamedEnvelope

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

the class LuceneSGBDTreeEltMapper method getFullMap.

@Override
public Map<Integer, NamedEnvelope> getFullMap() throws IOException {
    Map<Integer, NamedEnvelope> result = new HashMap<>();
    try (Connection conn = source.getConnection();
        PreparedStatement stmt = conn.prepareStatement("SELECT * FROM \"" + schemaName + "\".\"records\"");
        ResultSet rs = stmt.executeQuery()) {
        while (rs.next()) {
            final String identifier = rs.getString("identifier");
            final int treeId = rs.getInt("id");
            final int nbEnv = rs.getInt("nbenv");
            final double minx = rs.getDouble("minx");
            final double maxx = rs.getDouble("maxx");
            final double miny = rs.getDouble("miny");
            final double maxy = rs.getDouble("maxy");
            final NamedEnvelope env = new NamedEnvelope(crs, identifier, nbEnv);
            env.setRange(0, minx, maxx);
            env.setRange(1, miny, maxy);
            result.put(treeId, env);
        }
    } catch (SQLException ex) {
        throw new IOException("Error while getting envelope", ex);
    }
    return result;
}
Also used : NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 3 with NamedEnvelope

use of org.geotoolkit.index.tree.manager.NamedEnvelope 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 4 with NamedEnvelope

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

the class LuceneSearcherTest method addPoint.

/**
 *  Add a point geometry to the specified Document.
 *
 * @param doc     The document to add the geometry
 * @param x       The x coordinate of the point.
 * @param y       The y coordinate of the point.
 * @param crsName The coordinate reference system in witch the coordinates are expressed.
 */
private static NamedEnvelope addPoint(final Document doc, final double x, final double y, final CoordinateReferenceSystem crs) throws Exception {
    Point pt = GF.createPoint(new Coordinate(x, y));
    JTS.setCRS(pt, crs);
    final String id = doc.get("id");
    NamedEnvelope namedBound = LuceneUtils.getNamedEnvelope(id, pt, treeCrs);
    doc.add(new StoredField(LuceneOGCSpatialQuery.GEOMETRY_FIELD_NAME, WKBUtils.toWKBwithSRID(pt)));
    return namedBound;
}
Also used : StoredField(org.apache.lucene.document.StoredField) NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) Coordinate(org.locationtech.jts.geom.Coordinate) Point(org.locationtech.jts.geom.Point) LineString(org.locationtech.jts.geom.LineString)

Example 5 with NamedEnvelope

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

the class LuceneSearcherTest method addLine.

/**
 * Add a Line geometry to the specified Document.
 *
 * @param doc The document to add the geometry
 * @param x1  the X coordinate of the first point of the line.
 * @param y1  the Y coordinate of the first point of the line.
 * @param x2  the X coordinate of the second point of the line.
 * @param y2  the Y coordinate of the first point of the line.
 * @param crsName The coordinate reference system in witch the coordinates are expressed.
 */
private static NamedEnvelope addLine(final Document doc, final double x1, final double y1, final double x2, final double y2, final CoordinateReferenceSystem crs) throws Exception {
    LineString line = GF.createLineString(new Coordinate[] { new Coordinate(x1, y1), new Coordinate(x2, y2) });
    JTS.setCRS(line, crs);
    final String id = doc.get("id");
    NamedEnvelope namedBound = LuceneUtils.getNamedEnvelope(id, line, treeCrs);
    doc.add(new StoredField(LuceneOGCSpatialQuery.GEOMETRY_FIELD_NAME, WKBUtils.toWKBwithSRID(line)));
    return namedBound;
}
Also used : StoredField(org.apache.lucene.document.StoredField) NamedEnvelope(org.geotoolkit.index.tree.manager.NamedEnvelope) LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) LineString(org.locationtech.jts.geom.LineString)

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