Search in sources :

Example 1 with JtsGeometry

use of org.locationtech.spatial4j.shape.jts.JtsGeometry 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 JtsGeometry

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

the class GeoPolygonQueryBuilderTests method randomPolygon.

private static List<GeoPoint> randomPolygon() {
    ShapeBuilder shapeBuilder = null;
    // in this case keep trying until we successfully generate one
    while (shapeBuilder == null) {
        shapeBuilder = RandomShapeGenerator.createShapeWithin(random(), null, ShapeType.POLYGON);
    }
    JtsGeometry shape = (JtsGeometry) shapeBuilder.build();
    Coordinate[] coordinates = shape.getGeom().getCoordinates();
    ArrayList<GeoPoint> polygonPoints = new ArrayList<>();
    for (Coordinate coord : coordinates) {
        polygonPoints.add(new GeoPoint(coord.y, coord.x));
    }
    return polygonPoints;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Coordinate(com.vividsolutions.jts.geom.Coordinate) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) ArrayList(java.util.ArrayList)

Aggregations

ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)2 JtsGeometry (org.locationtech.spatial4j.shape.jts.JtsGeometry)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Field (org.apache.lucene.document.Field)1 IndexableField (org.apache.lucene.index.IndexableField)1 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 QueryShardException (org.elasticsearch.index.query.QueryShardException)1 Point (org.locationtech.spatial4j.shape.Point)1 Shape (org.locationtech.spatial4j.shape.Shape)1