Search in sources :

Example 1 with SpatialRelation

use of org.locationtech.spatial4j.shape.SpatialRelation in project lucene-solr by apache.

the class WithinPrefixTreeQuery method getDocIdSet.

@Override
protected DocIdSet getDocIdSet(LeafReaderContext context) throws IOException {
    return new VisitorTemplate(context) {

        private FixedBitSet inside;

        private FixedBitSet outside;

        @Override
        protected void start() {
            inside = new FixedBitSet(maxDoc);
            outside = new FixedBitSet(maxDoc);
        }

        @Override
        protected DocIdSet finish() {
            inside.andNot(outside);
            return new BitDocIdSet(inside);
        }

        @Override
        protected CellIterator findSubCellsToVisit(Cell cell) {
            //use buffered query shape instead of orig.  Works with null too.
            return cell.getNextLevelCells(bufferedQueryShape);
        }

        @Override
        protected boolean visitPrefix(Cell cell) throws IOException {
            //cell.relate is based on the bufferedQueryShape; we need to examine what
            // the relation is against the queryShape
            SpatialRelation visitRelation = cell.getShape().relate(queryShape);
            if (cell.getLevel() == detailLevel) {
                collectDocs(visitRelation.intersects() ? inside : outside);
                return false;
            } else if (visitRelation == SpatialRelation.WITHIN) {
                collectDocs(inside);
                return false;
            } else if (visitRelation == SpatialRelation.DISJOINT) {
                collectDocs(outside);
                return false;
            }
            return true;
        }

        @Override
        protected void visitLeaf(Cell cell) throws IOException {
            if (allCellsIntersectQuery(cell))
                collectDocs(inside);
            else
                collectDocs(outside);
        }

        /** Returns true if the provided cell, and all its sub-cells down to
       * detailLevel all intersect the queryShape.
       */
        private boolean allCellsIntersectQuery(Cell cell) {
            SpatialRelation relate = cell.getShape().relate(queryShape);
            if (cell.getLevel() == detailLevel)
                return relate.intersects();
            if (relate == SpatialRelation.WITHIN)
                return true;
            if (relate == SpatialRelation.DISJOINT)
                return false;
            // Note: Generating all these cells just to determine intersection is not ideal.
            // The real solution is LUCENE-4869.
            CellIterator subCells = cell.getNextLevelCells(null);
            while (subCells.hasNext()) {
                Cell subCell = subCells.next();
                if (//recursion
                !allCellsIntersectQuery(subCell))
                    return false;
            }
            return true;
        }

        @Override
        protected void visitScanned(Cell cell) throws IOException {
            //collects as we want, even if not a leaf
            visitLeaf(cell);
        //        if (cell.isLeaf()) {
        //          visitLeaf(cell);
        //        } else {
        //          visitPrefix(cell);
        //        }
        }
    }.getDocIdSet();
}
Also used : BitDocIdSet(org.apache.lucene.util.BitDocIdSet) FixedBitSet(org.apache.lucene.util.FixedBitSet) DocIdSet(org.apache.lucene.search.DocIdSet) BitDocIdSet(org.apache.lucene.util.BitDocIdSet) CellIterator(org.apache.lucene.spatial.prefix.tree.CellIterator) IOException(java.io.IOException) Cell(org.apache.lucene.spatial.prefix.tree.Cell) SpatialRelation(org.locationtech.spatial4j.shape.SpatialRelation)

Example 2 with SpatialRelation

use of org.locationtech.spatial4j.shape.SpatialRelation in project lucene-solr by apache.

the class QuadPrefixTree method checkBattenberg.

protected void checkBattenberg(char c, double cx, double cy, int level, List<Cell> matches, BytesRef str, Shape shape, int maxLevel) {
    assert str.length == level;
    assert str.offset == 0;
    double w = levelW[level] / 2;
    double h = levelH[level] / 2;
    int strlen = str.length;
    Rectangle rectangle = ctx.makeRectangle(cx - w, cx + w, cy - h, cy + h);
    SpatialRelation v = shape.relate(rectangle);
    if (SpatialRelation.CONTAINS == v) {
        //append
        str.bytes[str.length++] = (byte) c;
        //str.append(SpatialPrefixGrid.COVER);
        matches.add(new QuadCell(BytesRef.deepCopyOf(str), v.transpose()));
    } else if (SpatialRelation.DISJOINT == v) {
    // nothing
    } else {
        // SpatialRelation.WITHIN, SpatialRelation.INTERSECTS
        //append
        str.bytes[str.length++] = (byte) c;
        int nextLevel = level + 1;
        if (nextLevel >= maxLevel) {
            //str.append(SpatialPrefixGrid.INTERSECTS);
            matches.add(new QuadCell(BytesRef.deepCopyOf(str), v.transpose()));
        } else {
            build(cx, cy, nextLevel, matches, str, shape, maxLevel);
        }
    }
    str.length = strlen;
}
Also used : Rectangle(org.locationtech.spatial4j.shape.Rectangle) Point(org.locationtech.spatial4j.shape.Point) SpatialRelation(org.locationtech.spatial4j.shape.SpatialRelation)

Example 3 with SpatialRelation

use of org.locationtech.spatial4j.shape.SpatialRelation in project lucene-solr by apache.

the class RandomizedShapeTestCase method _assertIntersect.

private void _assertIntersect(String msg, SpatialRelation expected, Shape a, Shape b) {
    SpatialRelation sect = a.relate(b);
    if (sect == expected)
        return;
    msg = ((msg == null) ? "" : msg + "\r") + a + " intersect " + b;
    if (expected == WITHIN || expected == CONTAINS) {
        if (// they are the same shape type
        a.getClass().equals(b.getClass()))
            assertEquals(msg, a, b);
        else {
            //they are effectively points or lines that are the same location
            assertTrue(msg, !a.hasArea());
            assertTrue(msg, !b.hasArea());
            Rectangle aBBox = a.getBoundingBox();
            Rectangle bBBox = b.getBoundingBox();
            if (aBBox.getHeight() == 0 && bBBox.getHeight() == 0 && (aBBox.getMaxY() == 90 && bBBox.getMaxY() == 90 || aBBox.getMinY() == -90 && bBBox.getMinY() == -90))
                //== a point at the pole
                ;
            else
                assertEquals(msg, aBBox, bBBox);
        }
    } else {
        //always fails
        assertEquals(msg, expected, sect);
    }
}
Also used : Rectangle(org.locationtech.spatial4j.shape.Rectangle) SpatialRelation(org.locationtech.spatial4j.shape.SpatialRelation)

Example 4 with SpatialRelation

use of org.locationtech.spatial4j.shape.SpatialRelation in project lucene-solr by apache.

the class PackedQuadPrefixTree method checkBattenberg.

protected void checkBattenberg(byte quad, double cx, double cy, int level, List<Cell> matches, long term, Shape shape, int maxLevel) {
    // short-circuit if we find a match for the point (no need to continue recursion)
    if (shape instanceof Point && !matches.isEmpty())
        return;
    double w = levelW[level] / 2;
    double h = levelH[level] / 2;
    SpatialRelation v = shape.relate(ctx.makeRectangle(cx - w, cx + w, cy - h, cy + h));
    if (SpatialRelation.DISJOINT == v) {
        return;
    }
    // set bits for next level
    term |= (((long) (quad)) << (64 - (++level << 1)));
    // increment level
    term = ((term >>> 1) + 1) << 1;
    if (SpatialRelation.CONTAINS == v || (level >= maxLevel)) {
        matches.add(new PackedQuadCell(term, v.transpose()));
    } else {
        // SpatialRelation.WITHIN, SpatialRelation.INTERSECTS
        build(cx, cy, level, matches, term, shape, maxLevel);
    }
}
Also used : Point(org.locationtech.spatial4j.shape.Point) SpatialRelation(org.locationtech.spatial4j.shape.SpatialRelation)

Aggregations

SpatialRelation (org.locationtech.spatial4j.shape.SpatialRelation)4 Point (org.locationtech.spatial4j.shape.Point)2 Rectangle (org.locationtech.spatial4j.shape.Rectangle)2 IOException (java.io.IOException)1 DocIdSet (org.apache.lucene.search.DocIdSet)1 Cell (org.apache.lucene.spatial.prefix.tree.Cell)1 CellIterator (org.apache.lucene.spatial.prefix.tree.CellIterator)1 BitDocIdSet (org.apache.lucene.util.BitDocIdSet)1 FixedBitSet (org.apache.lucene.util.FixedBitSet)1