Search in sources :

Example 1 with Point

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

the class GeoShapeFieldMapper method parse.

@Override
public Mapper parse(ParseContext context) throws IOException {
    try {
        Shape shape = context.parseExternalValue(Shape.class);
        if (shape == null) {
            ShapeBuilder shapeBuilder = ShapeBuilder.parse(context.parser(), this);
            if (shapeBuilder == null) {
                return null;
            }
            shape = shapeBuilder.build();
        }
        if (fieldType().pointsOnly() && !(shape instanceof Point)) {
            throw new MapperParsingException("[{" + fieldType().name() + "}] is configured for points only but a " + ((shape instanceof JtsGeometry) ? ((JtsGeometry) shape).getGeom().getGeometryType() : shape.getClass()) + " was found");
        }
        Field[] fields = fieldType().defaultStrategy().createIndexableFields(shape);
        if (fields == null || fields.length == 0) {
            return null;
        }
        for (Field field : fields) {
            if (!customBoost() && fieldType.boost() != 1f && Version.indexCreated(context.indexSettings()).before(Version.V_5_0_0_alpha1)) {
                field.setBoost(fieldType().boost());
            }
            context.doc().add(field);
        }
    } catch (Exception e) {
        throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
    }
    return null;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Shape(org.locationtech.spatial4j.shape.Shape) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) Point(org.locationtech.spatial4j.shape.Point) QueryShardException(org.elasticsearch.index.query.QueryShardException) IOException(java.io.IOException)

Example 2 with Point

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

the class MultiPointBuilder method build.

@Override
public Shape build() {
    //Could wrap JtsGeometry but probably slower due to conversions to/from JTS in relate()
    //MultiPoint geometry = FACTORY.createMultiPoint(points.toArray(new Coordinate[points.size()]));
    List<Point> shapes = new ArrayList<>(coordinates.size());
    for (Coordinate coord : coordinates) {
        shapes.add(SPATIAL_CONTEXT.makePoint(coord.x, coord.y));
    }
    XShapeCollection<Point> multiPoints = new XShapeCollection<>(shapes, SPATIAL_CONTEXT);
    multiPoints.setPointsOnly(true);
    return multiPoints;
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) XShapeCollection(org.elasticsearch.common.geo.XShapeCollection) ArrayList(java.util.ArrayList) Point(org.locationtech.spatial4j.shape.Point)

Example 3 with Point

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

the class RandomShapeGenerator method xRandomPointIn.

protected static Point xRandomPointIn(Random rand, Rectangle r) {
    double[] pt = new double[2];
    randomPointIn(rand, r.getMinX(), r.getMinY(), r.getMaxX(), r.getMaxY(), pt);
    Point p = ctx.makePoint(pt[0], pt[1]);
    Assert.assertEquals(CONTAINS, r.relate(p));
    return p;
}
Also used : Point(org.locationtech.spatial4j.shape.Point)

Example 4 with Point

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

the class ExternalMapper method parse.

@Override
public Mapper parse(ParseContext context) throws IOException {
    byte[] bytes = "Hello world".getBytes(Charset.defaultCharset());
    binMapper.parse(context.createExternalValueContext(bytes));
    boolMapper.parse(context.createExternalValueContext(true));
    // Let's add a Dummy Point
    Double lat = 42.0;
    Double lng = 51.0;
    GeoPoint point = new GeoPoint(lat, lng);
    pointMapper.parse(context.createExternalValueContext(point));
    // Let's add a Dummy Shape
    Point shape = ShapeBuilders.newPoint(-100, 45).build();
    shapeMapper.parse(context.createExternalValueContext(shape));
    context = context.createExternalValueContext(generatedValue);
    // Let's add a Original String
    stringMapper.parse(context);
    multiFields.parse(this, context);
    return null;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Point(org.locationtech.spatial4j.shape.Point) GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 5 with Point

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

the class RandomSpatialOpFuzzyPrefixTreeTest method gridSnap.

//  private Rectangle inset(Rectangle r) {
//    //typically inset by 1 (whole numbers are easy to read)
//    double d = Math.min(1.0, grid.getDistanceForLevel(grid.getMaxLevels()) / 4);
//    return ctx.makeRectangle(r.getMinX() + d, r.getMaxX() - d, r.getMinY() + d, r.getMaxY() - d);
//  }
protected Shape gridSnap(Shape snapMe) {
    if (snapMe == null)
        return null;
    if (snapMe instanceof ShapePair) {
        ShapePair me = (ShapePair) snapMe;
        return new ShapePair(gridSnap(me.shape1), gridSnap(me.shape2), me.biasContainsThenWithin);
    }
    if (snapMe instanceof Point) {
        snapMe = snapMe.getBoundingBox();
    }
    //The next 4 lines mimic PrefixTreeStrategy.createIndexableFields()
    double distErrPct = ((PrefixTreeStrategy) strategy).getDistErrPct();
    double distErr = SpatialArgs.calcDistanceFromErrPct(snapMe, distErrPct, ctx);
    int detailLevel = grid.getLevelForDistance(distErr);
    CellIterator cells = grid.getTreeCellIterator(snapMe, detailLevel);
    //calc bounding box of cells.
    List<Shape> cellShapes = new ArrayList<>(1024);
    while (cells.hasNext()) {
        Cell cell = cells.next();
        if (!cell.isLeaf())
            continue;
        cellShapes.add(cell.getShape());
    }
    return new ShapeCollection<>(cellShapes, ctx).getBoundingBox();
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) ArrayList(java.util.ArrayList) CellIterator(org.apache.lucene.spatial.prefix.tree.CellIterator) Point(org.locationtech.spatial4j.shape.Point) Cell(org.apache.lucene.spatial.prefix.tree.Cell) Point(org.locationtech.spatial4j.shape.Point)

Aggregations

Point (org.locationtech.spatial4j.shape.Point)71 Test (org.junit.Test)21 Shape (org.locationtech.spatial4j.shape.Shape)15 Query (org.apache.lucene.search.Query)9 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)9 Rectangle (org.locationtech.spatial4j.shape.Rectangle)9 ArrayList (java.util.ArrayList)7 Field (org.apache.lucene.document.Field)6 PointImpl (org.locationtech.spatial4j.shape.impl.PointImpl)6 BooleanQuery (org.apache.lucene.search.BooleanQuery)5 TopDocs (org.apache.lucene.search.TopDocs)5 IOException (java.io.IOException)4 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)4 Document (org.apache.lucene.document.Document)3 StoredField (org.apache.lucene.document.StoredField)3 IndexReader (org.apache.lucene.index.IndexReader)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 Cell (org.apache.lucene.spatial.prefix.tree.Cell)3 CellIterator (org.apache.lucene.spatial.prefix.tree.CellIterator)3