Search in sources :

Example 61 with Shape

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

the class SpatialArgsTest method calcDistanceFromErrPct.

@Test
public void calcDistanceFromErrPct() {
    final SpatialContext ctx = SpatialContext.GEO;
    //distErrPct
    final double DEP = 0.5;
    //the result is the diagonal distance from the center to the closest corner,
    // times distErrPct
    Shape superwide = ctx.makeRectangle(-180, 180, 0, 0);
    //0 distErrPct means 0 distance always
    assertEquals(0, SpatialArgs.calcDistanceFromErrPct(superwide, 0, ctx), 0);
    assertEquals(180 * DEP, SpatialArgs.calcDistanceFromErrPct(superwide, DEP, ctx), 0);
    Shape supertall = ctx.makeRectangle(0, 0, -90, 90);
    assertEquals(90 * DEP, SpatialArgs.calcDistanceFromErrPct(supertall, DEP, ctx), 0);
    Shape upperhalf = ctx.makeRectangle(-180, 180, 0, 90);
    assertEquals(45 * DEP, SpatialArgs.calcDistanceFromErrPct(upperhalf, DEP, ctx), 0.0001);
    Shape midCircle = ctx.makeCircle(0, 0, 45);
    assertEquals(60 * DEP, SpatialArgs.calcDistanceFromErrPct(midCircle, DEP, ctx), 0.0001);
}
Also used : SpatialContext(org.locationtech.spatial4j.context.SpatialContext) Shape(org.locationtech.spatial4j.shape.Shape) Test(org.junit.Test)

Example 62 with Shape

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

the class SpatialExample method newSampleDocument.

private Document newSampleDocument(int id, Shape... shapes) {
    Document doc = new Document();
    doc.add(new StoredField("id", id));
    doc.add(new NumericDocValuesField("id", id));
    // strategies; see the javadocs of the SpatialStrategy impl to see.
    for (Shape shape : shapes) {
        for (Field f : strategy.createIndexableFields(shape)) {
            doc.add(f);
        }
        //store it too; the format is up to you
        //  (assume point in this example)
        Point pt = (Point) shape;
        doc.add(new StoredField(strategy.getFieldName(), pt.getX() + " " + pt.getY()));
    }
    return doc;
}
Also used : StoredField(org.apache.lucene.document.StoredField) SortField(org.apache.lucene.search.SortField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) StoredField(org.apache.lucene.document.StoredField) Shape(org.locationtech.spatial4j.shape.Shape) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Point(org.locationtech.spatial4j.shape.Point) Document(org.apache.lucene.document.Document)

Example 63 with Shape

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

the class DistanceToShapeValueSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues shapeValues = shapeValueSource.getValues(context, readerContext);
    return new DoubleDocValues(this) {

        @Override
        public double doubleVal(int doc) throws IOException {
            Shape shape = (Shape) shapeValues.objectVal(doc);
            if (shape == null || shape.isEmpty())
                return nullValue;
            Point pt = shape.getCenter();
            return distCalc.distance(queryPoint, pt) * multiplier;
        }

        @Override
        public Explanation explain(int doc) throws IOException {
            Explanation exp = super.explain(doc);
            List<Explanation> details = new ArrayList<>(Arrays.asList(exp.getDetails()));
            details.add(shapeValues.explain(doc));
            return Explanation.match(exp.getValue(), exp.getDescription(), details);
        }
    };
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Explanation(org.apache.lucene.search.Explanation) DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) ArrayList(java.util.ArrayList) FunctionValues(org.apache.lucene.queries.function.FunctionValues) Point(org.locationtech.spatial4j.shape.Point)

Example 64 with Shape

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

the class ShapePredicateValueSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues shapeValues = shapeValuesource.getValues(context, readerContext);
    return new BoolDocValues(this) {

        @Override
        public boolean boolVal(int doc) throws IOException {
            Shape indexedShape = (Shape) shapeValues.objectVal(doc);
            if (indexedShape == null)
                return false;
            return op.evaluate(indexedShape, queryShape);
        }

        @Override
        public Explanation explain(int doc) throws IOException {
            Explanation exp = super.explain(doc);
            List<Explanation> details = new ArrayList<>(Arrays.asList(exp.getDetails()));
            details.add(shapeValues.explain(doc));
            return Explanation.match(exp.getValue(), exp.getDescription(), details);
        }
    };
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Explanation(org.apache.lucene.search.Explanation) BoolDocValues(org.apache.lucene.queries.function.docvalues.BoolDocValues) ArrayList(java.util.ArrayList) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 65 with Shape

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

the class DistanceStrategyTest method testRecipScore.

@Test
public void testRecipScore() throws IOException {
    Point p100 = ctx.makePoint(2.02, 0.98);
    adoc("100", p100);
    Point p101 = ctx.makePoint(-1.001, 4.001);
    adoc("101", p101);
    //test score for nothing
    adoc("103", (Shape) null);
    //test deleted
    adoc("999", ctx.makePoint(2, 1));
    commit();
    deleteDoc("999");
    commit();
    double dist = ctx.getDistCalc().distance(p100, p101);
    Shape queryShape = ctx.makeCircle(2.01, 0.99, dist);
    checkValueSource(strategy.makeRecipDistanceValueSource(queryShape), new float[] { 1.00f, 0.10f, 0f }, 0.09f);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

Aggregations

Shape (org.locationtech.spatial4j.shape.Shape)81 Test (org.junit.Test)18 Point (org.locationtech.spatial4j.shape.Point)16 ArrayList (java.util.ArrayList)14 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)13 CoordinatesBuilder (org.elasticsearch.common.geo.builders.CoordinatesBuilder)13 PolygonBuilder (org.elasticsearch.common.geo.builders.PolygonBuilder)11 Document (org.apache.lucene.document.Document)10 Field (org.apache.lucene.document.Field)9 Query (org.apache.lucene.search.Query)8 Rectangle (org.locationtech.spatial4j.shape.Rectangle)8 StoredField (org.apache.lucene.document.StoredField)7 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)7 LineStringBuilder (org.elasticsearch.common.geo.builders.LineStringBuilder)7 UnitNRShape (org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape)6 StringField (org.apache.lucene.document.StringField)5 TextField (org.apache.lucene.document.TextField)4 IndexableField (org.apache.lucene.index.IndexableField)4 UnsupportedSpatialOperation (org.apache.lucene.spatial.query.UnsupportedSpatialOperation)4 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)4