Search in sources :

Example 6 with Rectangle

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

the class TestBBoxStrategy method randomIndexedShape.

@Override
protected Shape randomIndexedShape() {
    Rectangle world = ctx.getWorldBounds();
    if (// increased chance of getting one of these
    random().nextInt(10) == 0)
        return world;
    int worldWidth = (int) Math.round(world.getWidth());
    int deltaLeft = nextIntInclusive(worldWidth);
    int deltaRight = nextIntInclusive(worldWidth - deltaLeft);
    int worldHeight = (int) Math.round(world.getHeight());
    int deltaTop = nextIntInclusive(worldHeight);
    int deltaBottom = nextIntInclusive(worldHeight - deltaTop);
    if (ctx.isGeo() && (deltaLeft != 0 || deltaRight != 0)) {
        //if geo & doesn't world-wrap, we shift randomly to potentially cross dateline
        int shift = nextIntInclusive(360);
        return ctx.makeRectangle(DistanceUtils.normLonDEG(world.getMinX() + deltaLeft + shift), DistanceUtils.normLonDEG(world.getMaxX() - deltaRight + shift), world.getMinY() + deltaBottom, world.getMaxY() - deltaTop);
    } else {
        return ctx.makeRectangle(world.getMinX() + deltaLeft, world.getMaxX() - deltaRight, world.getMinY() + deltaBottom, world.getMaxY() - deltaTop);
    }
}
Also used : Rectangle(org.locationtech.spatial4j.shape.Rectangle)

Example 7 with Rectangle

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

the class CompositeStrategyTest method randomCircle.

//TODO move up
private Shape randomCircle() {
    final Point point = randomPoint();
    //TODO pick using gaussian
    double radius;
    if (ctx.isGeo()) {
        radius = randomDouble() * 100;
    } else {
        //find distance to closest edge
        final Rectangle worldBounds = ctx.getWorldBounds();
        double maxRad = point.getX() - worldBounds.getMinX();
        maxRad = Math.min(maxRad, worldBounds.getMaxX() - point.getX());
        maxRad = Math.min(maxRad, point.getY() - worldBounds.getMinY());
        maxRad = Math.min(maxRad, worldBounds.getMaxY() - point.getY());
        radius = randomDouble() * maxRad;
    }
    return ctx.makeCircle(point, radius);
}
Also used : Rectangle(org.locationtech.spatial4j.shape.Rectangle) Point(org.locationtech.spatial4j.shape.Point)

Example 8 with Rectangle

use of org.locationtech.spatial4j.shape.Rectangle 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 9 with Rectangle

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

the class LegacyPrefixTree method getDistanceForLevel.

public double getDistanceForLevel(int level) {
    if (level < 1 || level > getMaxLevels())
        throw new IllegalArgumentException("Level must be in 1 to maxLevels range");
    //TODO cache for each level
    Cell cell = getCell(ctx.getWorldBounds().getCenter(), level);
    Rectangle bbox = cell.getShape().getBoundingBox();
    double width = bbox.getWidth();
    double height = bbox.getHeight();
    // than the correct one but it's okay to over-estimate.
    return Math.sqrt(width * width + height * height);
}
Also used : Rectangle(org.locationtech.spatial4j.shape.Rectangle)

Example 10 with Rectangle

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

the class BBoxStrategy method makeQuery.

//---------------------------------
// Query Building
//---------------------------------
//  Utility on SpatialStrategy?
//  public Query makeQueryWithValueSource(SpatialArgs args, ValueSource valueSource) {
//    return new CustomScoreQuery(makeQuery(args), new FunctionQuery(valueSource));
//or...
//  return new BooleanQuery.Builder()
//      .add(new FunctionQuery(valueSource), BooleanClause.Occur.MUST)//matches everything and provides score
//      .add(filterQuery, BooleanClause.Occur.FILTER)//filters (score isn't used)
//  .build();
//  }
@Override
public Query makeQuery(SpatialArgs args) {
    Shape shape = args.getShape();
    if (!(shape instanceof Rectangle))
        throw new UnsupportedOperationException("Can only query by Rectangle, not " + shape);
    Rectangle bbox = (Rectangle) shape;
    Query spatial;
    // Useful for understanding Relations:
    // http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm
    SpatialOperation op = args.getOperation();
    if (op == SpatialOperation.BBoxIntersects)
        spatial = makeIntersects(bbox);
    else if (op == SpatialOperation.BBoxWithin)
        spatial = makeWithin(bbox);
    else if (op == SpatialOperation.Contains)
        spatial = makeContains(bbox);
    else if (op == SpatialOperation.Intersects)
        spatial = makeIntersects(bbox);
    else if (op == SpatialOperation.IsEqualTo)
        spatial = makeEquals(bbox);
    else if (op == SpatialOperation.IsDisjointTo)
        spatial = makeDisjoint(bbox);
    else if (op == SpatialOperation.IsWithin)
        spatial = makeWithin(bbox);
    else {
        //no Overlaps support yet
        throw new UnsupportedSpatialOperation(op);
    }
    return new ConstantScoreQuery(spatial);
}
Also used : UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) Shape(org.locationtech.spatial4j.shape.Shape) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Rectangle(org.locationtech.spatial4j.shape.Rectangle) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation)

Aggregations

Rectangle (org.locationtech.spatial4j.shape.Rectangle)36 Point (org.locationtech.spatial4j.shape.Point)10 Test (org.junit.Test)8 Shape (org.locationtech.spatial4j.shape.Shape)7 ArrayList (java.util.ArrayList)5 Query (org.apache.lucene.search.Query)5 Coordinate (com.vividsolutions.jts.geom.Coordinate)3 FunctionValues (org.apache.lucene.queries.function.FunctionValues)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)3 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)3 GeoShape (org.apache.lucene.spatial3d.geom.GeoShape)3 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)3 Map (java.util.Map)2 LeafReader (org.apache.lucene.index.LeafReader)2 NumericDocValues (org.apache.lucene.index.NumericDocValues)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 UnsupportedSpatialOperation (org.apache.lucene.spatial.query.UnsupportedSpatialOperation)2 GeoPoint (org.apache.lucene.spatial3d.geom.GeoPoint)2