Search in sources :

Example 21 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class RandomShapeGenerator method createGeometryCollection.

protected static GeometryCollectionBuilder createGeometryCollection(Random r, Point nearPoint, Rectangle bounds, int numGeometries) throws InvalidShapeException {
    if (numGeometries <= 0) {
        // cap geometry collection at 4 shapes (to save test time)
        numGeometries = RandomNumbers.randomIntBetween(r, 2, 4);
    }
    if (nearPoint == null) {
        nearPoint = xRandomPoint(r);
    }
    if (bounds == null) {
        bounds = xRandomRectangle(r, nearPoint);
    }
    GeometryCollectionBuilder gcb = new GeometryCollectionBuilder();
    for (int i = 0; i < numGeometries; ) {
        ShapeBuilder builder = createShapeWithin(r, bounds);
        // Not the most efficient but its the lesser of the evil alternatives
        if (builder != null) {
            gcb.shape(builder);
            ++i;
        }
    }
    return gcb;
}
Also used : GeometryCollectionBuilder(org.elasticsearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Point(org.locationtech.spatial4j.shape.Point)

Example 22 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project crate by crate.

the class GeoJsonParser method parseGeometries.

/**
 * Parse the geometries array of a GeometryCollection
 *
 * @param parser Parser that will be read from
 * @return Geometry[] geometries of the GeometryCollection
 * @throws IOException Thrown if an error occurs while reading from the XContentParser
 */
static GeometryCollectionBuilder parseGeometries(XContentParser parser, GeoShapeFieldMapper mapper) throws IOException {
    if (parser.currentToken() != XContentParser.Token.START_ARRAY) {
        throw new ElasticsearchParseException("geometries must be an array of geojson objects");
    }
    XContentParser.Token token = parser.nextToken();
    GeometryCollectionBuilder geometryCollection = new GeometryCollectionBuilder();
    while (token != XContentParser.Token.END_ARRAY) {
        ShapeBuilder shapeBuilder = ShapeParser.parse(parser);
        geometryCollection.shape(shapeBuilder);
        token = parser.nextToken();
    }
    return geometryCollection;
}
Also used : GeometryCollectionBuilder(org.elasticsearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 23 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project crate by crate.

the class GeoWKTParser method parseExpectedType.

/**
 * throws an exception if the parsed geometry type does not match the expected shape type
 */
public static ShapeBuilder parseExpectedType(XContentParser parser, final GeoShapeType shapeType, final GeoShapeFieldMapper shapeMapper) throws IOException, ElasticsearchParseException {
    StringReader reader = new StringReader(parser.text());
    try {
        boolean ignoreZValue = true;
        // setup the tokenizer; configured to read words w/o numbers
        StreamTokenizer tokenizer = new StreamTokenizer(reader);
        tokenizer.resetSyntax();
        tokenizer.wordChars('a', 'z');
        tokenizer.wordChars('A', 'Z');
        tokenizer.wordChars(128 + 32, 255);
        tokenizer.wordChars('0', '9');
        tokenizer.wordChars('-', '-');
        tokenizer.wordChars('+', '+');
        tokenizer.wordChars('.', '.');
        tokenizer.whitespaceChars(0, ' ');
        tokenizer.commentChar('#');
        ShapeBuilder builder = parseGeometry(tokenizer, shapeType, ignoreZValue);
        checkEOF(tokenizer);
        return builder;
    } finally {
        reader.close();
    }
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) StringReader(java.io.StringReader) StreamTokenizer(java.io.StreamTokenizer)

Aggregations

ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)23 Coordinate (com.vividsolutions.jts.geom.Coordinate)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)6 GeometryCollectionBuilder (org.elasticsearch.common.geo.builders.GeometryCollectionBuilder)5 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 XContentParser (org.elasticsearch.common.xcontent.XContentParser)3 IOException (java.io.IOException)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Query (org.apache.lucene.search.Query)2 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)2 SpatialStrategy (org.elasticsearch.common.geo.SpatialStrategy)2 GeoShapeQueryBuilder (org.elasticsearch.index.query.GeoShapeQueryBuilder)2 ShapeType (org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType)2 Point (org.locationtech.spatial4j.shape.Point)2 JtsGeometry (org.locationtech.spatial4j.shape.jts.JtsGeometry)2 StreamTokenizer (java.io.StreamTokenizer)1