Search in sources :

Example 36 with PolygonBuilder

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

the class GeoQueryTests method testMultiPolygonSpanningDateline.

public void testMultiPolygonSpanningDateline() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-169 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-179 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(171 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    MultiPolygonBuilder multiPolygon = new MultiPolygonBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-167, 10).coordinate(-171, 10).coordinate(171, 5).coordinate(-167, 5).coordinate(-167, 10))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-177, 10).coordinate(177, 10).coordinate(177, 5).coordinate(-177, 5).coordinate(-177, 10)));
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", multiPolygon.buildGeometry());
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertHitCount(response, 2);
    SearchHits searchHits = response.getHits();
    assertThat(searchHits.getAt(0).getId(), not(equalTo("3")));
    assertThat(searchHits.getAt(1).getId(), not(equalTo("3")));
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) 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 37 with PolygonBuilder

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

Example 38 with PolygonBuilder

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

the class GeoShapeQueryTests method testShapeFilterWithDefinedGeoCollection.

public void testShapeFilterWithDefinedGeoCollection() throws Exception {
    createIndex("shapes");
    client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape,tree=quadtree").get();
    XContentBuilder docSource = jsonBuilder().startObject().startObject("geo").field("type", "geometrycollection").startArray("geometries").startObject().field("type", "point").startArray("coordinates").value(100.0).value(0.0).endArray().endObject().startObject().field("type", "linestring").startArray("coordinates").startArray().value(101.0).value(0.0).endArray().startArray().value(102.0).value(1.0).endArray().endArray().endObject().endArray().endObject().endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(99.0, -1.0).coordinate(99.0, 3.0).coordinate(103.0, 3.0).coordinate(103.0, -1.0).coordinate(99.0, -1.0)))).relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(199.0, -11.0).coordinate(199.0, 13.0).coordinate(193.0, 13.0).coordinate(193.0, -11.0).coordinate(199.0, -11.0)))).relation(ShapeRelation.INTERSECTS);
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 0);
    filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(99.0, -1.0).coordinate(99.0, 3.0).coordinate(103.0, 3.0).coordinate(103.0, -1.0).coordinate(99.0, -1.0))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(199.0, -11.0).coordinate(199.0, 13.0).coordinate(193.0, 13.0).coordinate(193.0, -11.0).coordinate(199.0, -11.0)))).relation(ShapeRelation.INTERSECTS);
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    // no shape
    filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder());
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 0);
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 39 with PolygonBuilder

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

the class RandomShapeGenerator method createShape.

/**
 * Creates a random shape useful for randomized testing, NOTE: exercise caution when using this to build random GeometryCollections
 * as creating a large random number of random shapes can result in massive resource consumption
 * see: {@link GeoShapeQueryTests#testQueryRandomGeoCollection()}
 *
 * The following options are included
 * @param nearPoint Create a shape near a provided point
 * @param within Create a shape within the provided rectangle (note: if not null this will override the provided point)
 * @param st Create a random shape of the provided type
 * @return the ShapeBuilder for a random shape
 */
private static ShapeBuilder createShape(Random r, Point nearPoint, Rectangle within, ShapeType st, boolean validate) throws InvalidShapeException {
    if (st == null) {
        st = ShapeType.randomType(r);
    }
    if (within == null) {
        within = xRandomRectangle(r, nearPoint);
    }
    // inside non overlapping bounding rectangles
    switch(st) {
        case POINT:
            Point p = xRandomPointIn(r, within);
            PointBuilder pb = new PointBuilder().coordinate(new Coordinate(p.getX(), p.getY(), Double.NaN));
            return pb;
        case MULTIPOINT:
        case LINESTRING:
            // for random testing having a maximum number of 10 points for a line string is more than sufficient
            // if this number gets out of hand, the number of self intersections for a linestring can become
            // (n^2-n)/2 and computing the relation intersection matrix will become NP-Hard
            int numPoints = RandomNumbers.randomIntBetween(r, 3, 10);
            CoordinatesBuilder coordinatesBuilder = new CoordinatesBuilder();
            for (int i = 0; i < numPoints; ++i) {
                p = xRandomPointIn(r, within);
                coordinatesBuilder.coordinate(p.getX(), p.getY());
            }
            ShapeBuilder pcb = (st == ShapeType.MULTIPOINT) ? new MultiPointBuilder(coordinatesBuilder.build()) : new LineStringBuilder(coordinatesBuilder);
            return pcb;
        case MULTILINESTRING:
            MultiLineStringBuilder mlsb = new MultiLineStringBuilder();
            for (int i = 0; i < RandomNumbers.randomIntBetween(r, 1, 10); ++i) {
                mlsb.linestring((LineStringBuilder) createShape(r, nearPoint, within, ShapeType.LINESTRING, false));
            }
            return mlsb;
        case POLYGON:
            numPoints = RandomNumbers.randomIntBetween(r, 5, 25);
            Coordinate[] coordinates = new Coordinate[numPoints];
            for (int i = 0; i < numPoints; ++i) {
                p = (Point) createShape(r, nearPoint, within, ShapeType.POINT, false).buildS4J();
                coordinates[i] = new Coordinate(p.getX(), p.getY());
            }
            // random point order or random linestrings can lead to invalid self-crossing polygons,
            // compute the convex hull for a set of points to ensure polygon does not self cross
            Geometry shell = new ConvexHull(coordinates, ctx.getGeometryFactory()).getConvexHull();
            Coordinate[] shellCoords = shell.getCoordinates();
            // when all else fails, use the bounding box as the polygon
            if (shellCoords.length < 3) {
                shellCoords = new Coordinate[4];
                shellCoords[0] = new Coordinate(within.getMinX(), within.getMinY());
                shellCoords[1] = new Coordinate(within.getMinX(), within.getMaxY());
                shellCoords[2] = new Coordinate(within.getMaxX(), within.getMaxY());
                shellCoords[3] = new Coordinate(within.getMaxX(), within.getMinY());
            }
            PolygonBuilder pgb = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoords).close());
            if (validate) {
                // The validate flag will check for these possibilities and bail if an incorrect geometry is created
                try {
                    pgb.buildS4J();
                } catch (AssertionError | InvalidShapeException e) {
                    // or InvalidShapeException
                    return null;
                }
            }
            return pgb;
        default:
            throw new OpenSearchException("Unable to create shape of type [" + st + "]");
    }
}
Also used : ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) ConvexHull(org.locationtech.jts.algorithm.ConvexHull) Point(org.locationtech.spatial4j.shape.Point) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) Point(org.locationtech.spatial4j.shape.Point) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) Geometry(org.locationtech.jts.geom.Geometry) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) OpenSearchException(org.opensearch.OpenSearchException) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder)

Aggregations

PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)39 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)37 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)17 Coordinate (org.locationtech.jts.geom.Coordinate)14 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)14 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)13 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)11 SearchResponse (org.opensearch.action.search.SearchResponse)8 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)8 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)7 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)7 Polygon (org.locationtech.jts.geom.Polygon)6 ArrayList (java.util.ArrayList)5 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)5 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)5 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)5 LineString (org.locationtech.jts.geom.LineString)4 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)4 Settings (org.opensearch.common.settings.Settings)4 OpenSearchGeoAssertions.assertMultiLineString (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiLineString)4