Search in sources :

Example 1 with GeometryCollectionBuilder

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

the class GeoWKTShapeParserTests method testParseGeometryCollection.

@Override
public void testParseGeometryCollection() throws IOException, ParseException {
    if (rarely()) {
        // assert empty shape collection
        GeometryCollectionBuilder builder = new GeometryCollectionBuilder();
        Shape[] expected = new Shape[0];
        if (randomBoolean()) {
            assertEquals(shapeCollection(expected).isEmpty(), builder.buildS4J().isEmpty());
        } else {
            assertEquals(shapeCollection(expected).isEmpty(), builder.buildGeometry().size() == 0);
        }
    } else {
        GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
        assertExpected(gcb.buildS4J(), gcb, true);
        assertExpected(new GeoShapeIndexer(true, "name").prepareForIndexing(gcb.buildGeometry()), gcb, false);
    }
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) Shape(org.locationtech.spatial4j.shape.Shape) GeoShapeIndexer(org.opensearch.index.mapper.GeoShapeIndexer)

Example 2 with GeometryCollectionBuilder

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

the class GeoQueryTests method testIndexPointsPolygon.

public void testIndexPointsPolygon() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate(defaultIndexName).addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-30 -30)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-45 -50)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    CoordinatesBuilder cb = new CoordinatesBuilder();
    cb.coordinate(new Coordinate(-35, -35)).coordinate(new Coordinate(-35, -25)).coordinate(new Coordinate(-25, -25)).coordinate(new Coordinate(-25, -35)).coordinate(new Coordinate(-35, -35));
    PolygonBuilder shape = new PolygonBuilder(cb);
    GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(shape);
    Geometry geometry = builder.buildGeometry();
    SearchResponse searchResponse = client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry).relation(ShapeRelation.INTERSECTS)).get();
    assertSearchResponse(searchResponse);
    assertHitCount(searchResponse, 1);
    SearchHits searchHits = searchResponse.getHits();
    assertThat(searchHits.getAt(0).getId(), equalTo("1"));
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) Geometry(org.opensearch.geometry.Geometry) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) SearchHits(org.opensearch.search.SearchHits) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with GeometryCollectionBuilder

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

the class GeoShapeQueryTests method testRandomGeoCollectionQuery.

public void testRandomGeoCollectionQuery() throws Exception {
    // Create a random geometry collection to index.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
    org.apache.lucene.geo.Polygon randomPoly = GeoTestUtil.nextPolygon();
    assumeTrue("Skipping the check for the polygon with a degenerated dimension", randomPoly.maxLat - randomPoly.minLat > 8.4e-8 && randomPoly.maxLon - randomPoly.minLon > 8.4e-8);
    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 mapping = createRandomMapping();
    Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
    client().admin().indices().prepareCreate("test").addMapping("_doc", mapping).setSettings(settings).get();
    ensureGreen();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    // Create a random geometry collection to query
    GeometryCollectionBuilder queryCollection = RandomShapeGenerator.createGeometryCollection(random());
    queryCollection.shape(new PolygonBuilder(cb));
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", queryCollection);
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertSearchResponse(result);
    assertThat(result.getHits().getHits().length, greaterThan(0));
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) 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) Settings(org.opensearch.common.settings.Settings) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder 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 5 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder 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)

Aggregations

GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)15 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)11 SearchResponse (org.opensearch.action.search.SearchResponse)10 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)10 Coordinate (org.locationtech.jts.geom.Coordinate)6 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)5 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)5 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)5 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)4 Geometry (org.opensearch.geometry.Geometry)4 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)3 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)3 RandomShapeGenerator.xRandomPoint (org.opensearch.test.geo.RandomShapeGenerator.xRandomPoint)3 OpenSearchParseException (org.opensearch.OpenSearchParseException)2 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)2 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)2 XContentParser (org.opensearch.common.xcontent.XContentParser)2 Point (org.locationtech.spatial4j.shape.Point)1 Rectangle (org.locationtech.spatial4j.shape.Rectangle)1 Shape (org.locationtech.spatial4j.shape.Shape)1