Search in sources :

Example 1 with MultiPointBuilder

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

the class GeoWKTShapeParserTests method testParseMultiPoint.

@Override
public void testParseMultiPoint() throws IOException, ParseException {
    int numPoints = randomIntBetween(0, 100);
    List<Coordinate> coordinates = new ArrayList<>(numPoints);
    for (int i = 0; i < numPoints; ++i) {
        coordinates.add(new Coordinate(GeoTestUtil.nextLongitude(), GeoTestUtil.nextLatitude()));
    }
    List<org.opensearch.geometry.Point> points = new ArrayList<>(numPoints);
    for (int i = 0; i < numPoints; ++i) {
        Coordinate c = coordinates.get(i);
        points.add(new org.opensearch.geometry.Point(c.x, c.y));
    }
    Geometry expectedGeom;
    MultiPointBuilder actual;
    if (numPoints == 0) {
        expectedGeom = MultiPoint.EMPTY;
        actual = new MultiPointBuilder();
    } else if (numPoints == 1) {
        expectedGeom = points.get(0);
        actual = new MultiPointBuilder(coordinates);
    } else {
        expectedGeom = new MultiPoint(points);
        actual = new MultiPointBuilder(coordinates);
    }
    assertExpected(expectedGeom, actual, false);
    assertMalformed(actual);
    assumeTrue("JTS test path cannot handle empty multipoints", numPoints > 1);
    Shape[] shapes = new Shape[numPoints];
    for (int i = 0; i < numPoints; ++i) {
        Coordinate c = coordinates.get(i);
        shapes[i] = SPATIAL_CONTEXT.makePoint(c.x, c.y);
    }
    ShapeCollection<?> expected = shapeCollection(shapes);
    assertExpected(expected, new MultiPointBuilder(coordinates), true);
}
Also used : MultiPoint(org.opensearch.geometry.MultiPoint) Shape(org.locationtech.spatial4j.shape.Shape) ArrayList(java.util.ArrayList) MultiPoint(org.opensearch.geometry.MultiPoint) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.jts.geom.Point) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) MultiPoint(org.opensearch.geometry.MultiPoint) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.jts.geom.Point) Geometry(org.opensearch.geometry.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate)

Example 2 with MultiPointBuilder

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

the class GeoPointShapeQueryTests method testQueryMultiPoint.

public void testQueryMultiPoint() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    MultiPointBuilder mpb = new MultiPointBuilder().coordinate(-35, -25).coordinate(-15, -5);
    MultiPoint multiPoint = mpb.buildGeometry();
    try {
        client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiPoint)).get();
    } catch (Exception e) {
        assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.MULTIPOINT + " queries"));
    }
}
Also used : MultiPoint(org.opensearch.geometry.MultiPoint) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException)

Example 3 with MultiPointBuilder

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

the class GeoShapeQueryTests method testContainsShapeQuery.

public void testContainsShapeQuery() throws Exception {
    // Create a random geometry collection.
    Rectangle mbr = xRandomRectangle(random(), xRandomPoint(random()), true);
    boolean usePrefixTrees = randomBoolean();
    GeometryCollectionBuilder gcb;
    if (usePrefixTrees) {
        gcb = createGeometryCollectionWithin(random(), mbr);
    } else {
        // vector strategy does not yet support multipoint queries
        gcb = new GeometryCollectionBuilder();
        int numShapes = RandomNumbers.randomIntBetween(random(), 1, 4);
        for (int i = 0; i < numShapes; ++i) {
            ShapeBuilder shape;
            do {
                shape = RandomShapeGenerator.createShapeWithin(random(), mbr);
            } while (shape instanceof MultiPointBuilder);
            gcb.shape(shape);
        }
    }
    if (usePrefixTrees) {
        client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape,tree=quadtree").execute().actionGet();
    } else {
        client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape").execute().actionGet();
    }
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    // index the mbr of the collection
    EnvelopeBuilder env = new EnvelopeBuilder(new Coordinate(mbr.getMinX(), mbr.getMaxY()), new Coordinate(mbr.getMaxX(), mbr.getMinY()));
    docSource = env.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("2").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1)));
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("geo", filterShape).relation(ShapeRelation.CONTAINS);
    SearchResponse response = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getHits().length, greaterThan(0));
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) Rectangle(org.locationtech.spatial4j.shape.Rectangle) RandomShapeGenerator.xRandomRectangle(org.opensearch.test.geo.RandomShapeGenerator.xRandomRectangle) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) RandomShapeGenerator.xRandomPoint(org.opensearch.test.geo.RandomShapeGenerator.xRandomPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with MultiPointBuilder

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

MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)4 Coordinate (org.locationtech.jts.geom.Coordinate)3 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 MultiPoint (org.opensearch.geometry.MultiPoint)2 ArrayList (java.util.ArrayList)1 ConvexHull (org.locationtech.jts.algorithm.ConvexHull)1 Geometry (org.locationtech.jts.geom.Geometry)1 Point (org.locationtech.jts.geom.Point)1 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)1 Point (org.locationtech.spatial4j.shape.Point)1 Rectangle (org.locationtech.spatial4j.shape.Rectangle)1 Shape (org.locationtech.spatial4j.shape.Shape)1 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)1 OpenSearchException (org.opensearch.OpenSearchException)1 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)1 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)1 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)1