Search in sources :

Example 26 with Geometry

use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.

the class GeoQueryTests method testIndexPointsFilterRectangle.

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

Example 27 with Geometry

use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.

the class GeoQueryTests method testIndexPointsMultiPolygon.

public void testIndexPointsMultiPolygon() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate(defaultIndexName).addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultGeoFieldName, "POINT(-30 -30)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultGeoFieldName, "POINT(-40 -40)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field("name", "Document 3").field(defaultGeoFieldName, "POINT(-50 -50)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    CoordinatesBuilder encloseDocument1Cb = new CoordinatesBuilder();
    encloseDocument1Cb.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 encloseDocument1Shape = new PolygonBuilder(encloseDocument1Cb);
    CoordinatesBuilder encloseDocument2Cb = new CoordinatesBuilder();
    encloseDocument2Cb.coordinate(new Coordinate(-55, -55)).coordinate(new Coordinate(-55, -45)).coordinate(new Coordinate(-45, -45)).coordinate(new Coordinate(-45, -55)).coordinate(new Coordinate(-55, -55));
    PolygonBuilder encloseDocument2Shape = new PolygonBuilder(encloseDocument2Cb);
    MultiPolygonBuilder mp = new MultiPolygonBuilder();
    mp.polygon(encloseDocument1Shape).polygon(encloseDocument2Shape);
    GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(mp);
    Geometry geometry = builder.buildGeometry();
    SearchResponse searchResponse = client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry).relation(ShapeRelation.INTERSECTS)).get();
    assertSearchResponse(searchResponse);
    assertHitCount(searchResponse, 2);
    assertThat(searchResponse.getHits().getAt(0).getId(), not(equalTo("2")));
    assertThat(searchResponse.getHits().getAt(1).getId(), not(equalTo("2")));
}
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) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) 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 28 with Geometry

use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.

the class GeometryTestUtils method randomGeometryCollection.

private static GeometryCollection<Geometry> randomGeometryCollection(int level, boolean hasAlt) {
    int size = OpenSearchTestCase.randomIntBetween(1, 10);
    List<Geometry> shapes = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        shapes.add(randomGeometry(level, hasAlt));
    }
    return new GeometryCollection<>(shapes);
}
Also used : Geometry(org.opensearch.geometry.Geometry) GeometryCollection(org.opensearch.geometry.GeometryCollection) ArrayList(java.util.ArrayList) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint)

Aggregations

Geometry (org.opensearch.geometry.Geometry)28 MultiPoint (org.opensearch.geometry.MultiPoint)10 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 Point (org.opensearch.geometry.Point)9 Coordinate (org.locationtech.jts.geom.Coordinate)7 GeometryCollection (org.opensearch.geometry.GeometryCollection)7 Line (org.opensearch.geometry.Line)7 MultiLine (org.opensearch.geometry.MultiLine)7 ArrayList (java.util.ArrayList)6 XContentParser (org.opensearch.common.xcontent.XContentParser)6 IOException (java.io.IOException)5 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)5 ParseException (java.text.ParseException)4 OpenSearchParseException (org.opensearch.OpenSearchParseException)4 Point (org.locationtech.jts.geom.Point)3 Shape (org.locationtech.spatial4j.shape.Shape)3 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)3