Search in sources :

Example 1 with Rectangle

use of org.locationtech.spatial4j.shape.Rectangle in project elasticsearch by elastic.

the class EnvelopeBuilderTests method createRandomShape.

static EnvelopeBuilder createRandomShape() {
    Rectangle box = RandomShapeGenerator.xRandomRectangle(random(), RandomShapeGenerator.xRandomPoint(random()));
    EnvelopeBuilder envelope = new EnvelopeBuilder(new Coordinate(box.getMinX(), box.getMaxY()), new Coordinate(box.getMaxX(), box.getMinY()));
    return envelope;
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) Rectangle(org.locationtech.spatial4j.shape.Rectangle)

Example 2 with Rectangle

use of org.locationtech.spatial4j.shape.Rectangle in project elasticsearch by elastic.

the class ShapeBuilderTests method testNewRectangle.

public void testNewRectangle() {
    Rectangle rectangle = ShapeBuilders.newEnvelope(new Coordinate(-45, 30), new Coordinate(45, -30)).build();
    assertEquals(-45D, rectangle.getMinX(), 0.0d);
    assertEquals(-30D, rectangle.getMinY(), 0.0d);
    assertEquals(45D, rectangle.getMaxX(), 0.0d);
    assertEquals(30D, rectangle.getMaxY(), 0.0d);
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) Rectangle(org.locationtech.spatial4j.shape.Rectangle)

Example 3 with Rectangle

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

the class BBoxValueSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    LeafReader reader = readerContext.reader();
    final NumericDocValues minX = DocValues.getNumeric(reader, strategy.field_minX);
    final NumericDocValues minY = DocValues.getNumeric(reader, strategy.field_minY);
    final NumericDocValues maxX = DocValues.getNumeric(reader, strategy.field_maxX);
    final NumericDocValues maxY = DocValues.getNumeric(reader, strategy.field_maxY);
    //reused
    final Rectangle rect = strategy.getSpatialContext().makeRectangle(0, 0, 0, 0);
    return new FunctionValues() {

        private int lastDocID = -1;

        private double getDocValue(NumericDocValues values, int doc) throws IOException {
            int curDocID = values.docID();
            if (doc > curDocID) {
                curDocID = values.advance(doc);
            }
            if (doc == curDocID) {
                return Double.longBitsToDouble(values.longValue());
            } else {
                return 0.0;
            }
        }

        @Override
        public Object objectVal(int doc) throws IOException {
            if (doc < lastDocID) {
                throw new AssertionError("docs were sent out-of-order: lastDocID=" + lastDocID + " vs doc=" + doc);
            }
            lastDocID = doc;
            double minXValue = getDocValue(minX, doc);
            if (minX.docID() != doc) {
                return null;
            } else {
                double minYValue = getDocValue(minY, doc);
                double maxXValue = getDocValue(maxX, doc);
                double maxYValue = getDocValue(maxY, doc);
                rect.reset(minXValue, maxXValue, minYValue, maxYValue);
                return rect;
            }
        }

        @Override
        public String strVal(int doc) throws IOException {
            //TODO support WKT output once Spatial4j does
            Object v = objectVal(doc);
            return v == null ? null : v.toString();
        }

        @Override
        public boolean exists(int doc) throws IOException {
            getDocValue(minX, doc);
            return minX.docID() == doc;
        }

        @Override
        public Explanation explain(int doc) throws IOException {
            return Explanation.match(Float.NaN, toString(doc));
        }

        @Override
        public String toString(int doc) throws IOException {
            return description() + '=' + strVal(doc);
        }
    };
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) Rectangle(org.locationtech.spatial4j.shape.Rectangle) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 4 with Rectangle

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

the class TestTestFramework method testQueries.

@Test
public void testQueries() throws IOException {
    String name = StrategyTestCase.QTEST_Cities_Intersects_BBox;
    InputStream in = getClass().getClassLoader().getResourceAsStream(name);
    SpatialContext ctx = SpatialContext.GEO;
    Iterator<SpatialTestQuery> iter = SpatialTestQuery.getTestQueries(new SpatialArgsParser(), ctx, name, //closes the InputStream
    in);
    List<SpatialTestQuery> tests = new ArrayList<>();
    while (iter.hasNext()) {
        tests.add(iter.next());
    }
    Assert.assertEquals(3, tests.size());
    SpatialTestQuery sf = tests.get(0);
    // assert
    assertEquals(1, sf.ids.size());
    Assert.assertTrue(sf.ids.get(0).equals("G5391959"));
    Assert.assertTrue(sf.args.getShape() instanceof Rectangle);
    assertEquals(SpatialOperation.Intersects, sf.args.getOperation());
}
Also used : SpatialContext(org.locationtech.spatial4j.context.SpatialContext) SpatialArgsParser(org.apache.lucene.spatial.query.SpatialArgsParser) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Rectangle(org.locationtech.spatial4j.shape.Rectangle) Test(org.junit.Test)

Example 5 with Rectangle

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

the class TestBBoxStrategy method testOverlapRatio.

public void testOverlapRatio() throws IOException {
    setupNeedsDocValuesOnly();
    //Simply assert null shape results in 0
    adoc("999", (Shape) null);
    commit();
    BBoxStrategy bboxStrategy = (BBoxStrategy) strategy;
    checkValueSource(bboxStrategy.makeOverlapRatioValueSource(randomRectangle(), 0.0), new float[] { 0f }, 0f);
    //we test raw BBoxOverlapRatioValueSource without actual indexing
    for (int SHIFT = 0; SHIFT < 360; SHIFT += 10) {
        //40x40, 1600 area
        Rectangle queryBox = shiftedRect(0, 40, -20, 20, SHIFT);
        final boolean MSL = random().nextBoolean();
        final double minSideLength = MSL ? 0.1 : 0.0;
        BBoxOverlapRatioValueSource sim = new BBoxOverlapRatioValueSource(null, true, queryBox, 0.5, minSideLength);
        //-10, 0, or 10.  Keep 0 on first round.
        int nudge = SHIFT == 0 ? 0 : random().nextInt(3) * 10 - 10;
        final double EPS = 0.0000001;
        assertEquals("within", (200d / 1600d * 0.5) + (0.5), sim.score(shiftedRect(10, 30, 0, 10, SHIFT + nudge), null), EPS);
        assertEquals("in25%", 0.25, sim.score(shiftedRect(30, 70, -20, 20, SHIFT), null), EPS);
        assertEquals("wrap", 0.2794117, sim.score(shiftedRect(30, 10, -20, 20, SHIFT + nudge), null), EPS);
        assertEquals("no intersection H", 0.0, sim.score(shiftedRect(-10, -10, -20, 20, SHIFT), null), EPS);
        assertEquals("no intersection V", 0.0, sim.score(shiftedRect(0, 20, -30, -30, SHIFT), null), EPS);
        assertEquals("point", 0.5 + (MSL ? (0.1 * 0.1 / 1600.0 / 2.0) : 0), sim.score(shiftedRect(0, 0, 0, 0, SHIFT), null), EPS);
        assertEquals("line 25% intersection", 0.25 / 2 + (MSL ? (10.0 * 0.1 / 1600.0 / 2.0) : 0.0), sim.score(shiftedRect(-30, 10, 0, 0, SHIFT), null), EPS);
        //test with point query
        sim = new BBoxOverlapRatioValueSource(null, true, shiftedRect(0, 0, 0, 0, SHIFT), 0.5, minSideLength);
        assertEquals("same", 1.0, sim.score(shiftedRect(0, 0, 0, 0, SHIFT), null), EPS);
        assertEquals("contains", 0.5 + (MSL ? (0.1 * 0.1 / (30 * 10) / 2.0) : 0.0), sim.score(shiftedRect(0, 30, 0, 10, SHIFT), null), EPS);
        //test with line query (vertical this time)
        sim = new BBoxOverlapRatioValueSource(null, true, shiftedRect(0, 0, 20, 40, SHIFT), 0.5, minSideLength);
        assertEquals("line 50%", 0.5, sim.score(shiftedRect(0, 0, 10, 30, SHIFT), null), EPS);
        assertEquals("point", 0.5 + (MSL ? (0.1 * 0.1 / (20 * 0.1) / 2.0) : 0.0), sim.score(shiftedRect(0, 0, 30, 30, SHIFT), null), EPS);
    }
}
Also used : Rectangle(org.locationtech.spatial4j.shape.Rectangle)

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