Search in sources :

Example 6 with ShapeBuilder

use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.

the class GeoShapeQueryTests method testPointsOnly.

public void testPointsOnly() throws Exception {
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_shape").field("tree", randomBoolean() ? "quadtree" : "geohash").field("tree_levels", "6").field("distance_error_pct", "0.01").field("points_only", true).endObject().endObject().endObject().endObject());
    client().admin().indices().prepareCreate("geo_points_only").addMapping("type1", mapping, XContentType.JSON).get();
    ensureGreen();
    ShapeBuilder shape = RandomShapeGenerator.createShape(random());
    try {
        client().prepareIndex("geo_points_only").setId("1").setSource(jsonBuilder().startObject().field("location", shape).endObject()).setRefreshPolicy(IMMEDIATE).get();
    } catch (MapperParsingException e) {
        // RandomShapeGenerator created something other than a POINT type, verify the correct exception is thrown
        assertThat(e.getCause().getMessage(), containsString("is configured for points only"));
        return;
    }
    // test that point was inserted
    SearchResponse response = client().prepareSearch("geo_points_only").setQuery(geoIntersectionQuery("location", shape)).get();
    assertHitCount(response, 1);
}
Also used : ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 7 with ShapeBuilder

use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.

the class RandomShapeGenerator method createShape.

private static ShapeBuilder createShape(Random r, Point nearPoint, Rectangle within, ShapeType st) throws InvalidShapeException {
    ShapeBuilder shape;
    short i = 0;
    do {
        shape = createShape(r, nearPoint, within, st, ST_VALIDATE);
        if (shape != null) {
            return shape;
        }
    } while (++i != 100);
    throw new InvalidShapeException("Unable to create a valid random shape with provided seed");
}
Also used : ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException)

Example 8 with ShapeBuilder

use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.

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.opensearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) Point(org.locationtech.spatial4j.shape.Point)

Example 9 with ShapeBuilder

use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.

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, AbstractShapeGeometryFieldMapper mapper) throws IOException {
    if (parser.currentToken() != XContentParser.Token.START_ARRAY) {
        throw new OpenSearchParseException("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.opensearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 10 with ShapeBuilder

use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.

the class GeoShapeQueryTests method testQueryRandomGeoCollection.

public void testQueryRandomGeoCollection() throws Exception {
    // Create a random geometry collection.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
    org.apache.lucene.geo.Polygon randomPoly = GeoTestUtil.nextPolygon();
    CoordinatesBuilder cb = new CoordinatesBuilder();
    for (int i = 0; i < randomPoly.numPoints(); ++i) {
        cb.coordinate(randomPoly.getPolyLon(i), randomPoly.getPolyLat(i));
    }
    gcb.shape(new PolygonBuilder(cb));
    XContentBuilder builder = createRandomMapping();
    client().admin().indices().prepareCreate("test").addMapping("type", builder).get();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = (gcb.getShapeAt(gcb.numShapes() - 1));
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", filterShape);
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertSearchResponse(result);
    assumeTrue("Skipping the check for the polygon with a degenerated dimension until " + " https://issues.apache.org/jira/browse/LUCENE-8634 is fixed", randomPoly.maxLat - randomPoly.minLat > 8.4e-8 && randomPoly.maxLon - randomPoly.minLon > 8.4e-8);
    assertHitCount(result, 1);
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) RandomShapeGenerator.xRandomPoint(org.opensearch.test.geo.RandomShapeGenerator.xRandomPoint) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)13 SearchResponse (org.opensearch.action.search.SearchResponse)6 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)6 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)6 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)6 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Coordinate (org.locationtech.jts.geom.Coordinate)4 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)4 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)4 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)4 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)4 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)3 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)3 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)2 Point (org.locationtech.spatial4j.shape.Point)2 OpenSearchParseException (org.opensearch.OpenSearchParseException)2 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)2