Search in sources :

Example 1 with MultiPolygonBuilder

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

the class GeoWKTShapeParserTests method testParseMultiPolygon.

@Override
public void testParseMultiPolygon() throws IOException, ParseException {
    int numPolys = randomIntBetween(0, 8);
    MultiPolygonBuilder builder = new MultiPolygonBuilder();
    PolygonBuilder pb;
    Coordinate[] coordinates;
    Polygon[] shapes = new Polygon[numPolys];
    LinearRing shell;
    for (int i = 0; i < numPolys; ++i) {
        pb = PolygonBuilder.class.cast(RandomShapeGenerator.createShape(random(), RandomShapeGenerator.ShapeType.POLYGON));
        builder.polygon(pb);
        coordinates = pb.coordinates()[0][0];
        shell = GEOMETRY_FACTORY.createLinearRing(coordinates);
        shapes[i] = GEOMETRY_FACTORY.createPolygon(shell, null);
    }
    assumeTrue("JTS test path cannot handle empty multipolygon", numPolys > 1);
    Shape expected = shapeCollection(shapes);
    assertExpected(expected, builder, true);
    assertMalformed(builder);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Coordinate(org.locationtech.jts.geom.Coordinate) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) Polygon(org.locationtech.jts.geom.Polygon) LinearRing(org.locationtech.jts.geom.LinearRing) MultiPoint(org.opensearch.geometry.MultiPoint) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.jts.geom.Point) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder)

Example 2 with MultiPolygonBuilder

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

the class GeoFilterIT method testShapeRelations.

public void testShapeRelations() throws Exception {
    assertTrue("Intersect relation is not supported", intersectSupport);
    assertTrue("Disjoint relation is not supported", disjointSupport);
    assertTrue("within relation is not supported", withinSupport);
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("polygon").startObject("properties").startObject("area").field("type", "geo_shape").field("tree", "geohash").endObject().endObject().endObject().endObject());
    CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("shapes").addMapping("polygon", mapping, XContentType.JSON);
    mappingRequest.get();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().get();
    // Create a multipolygon with two polygons. The first is an rectangle of size 10x10
    // with a hole of size 5x5 equidistant from all sides. This hole in turn contains
    // the second polygon of size 4x4 equidistant from all sites
    MultiPolygonBuilder polygon = new MultiPolygonBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
    BytesReference data = BytesReference.bytes(jsonBuilder().startObject().field("area", polygon).endObject());
    client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
    client().admin().indices().prepareRefresh().get();
    // Point in polygon
    SearchResponse result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(3, 3))).get();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("1"));
    // Point in polygon hole
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(4.5, 4.5))).get();
    assertHitCount(result, 0);
    // by definition the border of a polygon belongs to the inner
    // so the border of a polygons hole also belongs to the inner
    // of the polygon NOT the hole
    // Point on polygon border
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(10.0, 5.0))).get();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("1"));
    // Point on hole border
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(5.0, 2.0))).get();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("1"));
    if (disjointSupport) {
        // Point not in polygon
        result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", new PointBuilder(3, 3))).get();
        assertHitCount(result, 0);
        // Point in polygon hole
        result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", new PointBuilder(4.5, 4.5))).get();
        assertHitCount(result, 1);
        assertFirstHit(result, hasId("1"));
    }
    // Create a polygon that fills the empty area of the polygon defined above
    PolygonBuilder inverse = new PolygonBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
    data = BytesReference.bytes(jsonBuilder().startObject().field("area", inverse).endObject());
    client().prepareIndex("shapes").setId("2").setSource(data, XContentType.JSON).get();
    client().admin().indices().prepareRefresh().get();
    // re-check point on polygon hole
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(4.5, 4.5))).get();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("2"));
    // Create Polygon with hole and common edge
    PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(10, 5).coordinate(10, -5).close()));
    if (withinSupport) {
        // Polygon WithIn Polygon
        builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(-30, -30).coordinate(-30, 30).coordinate(30, 30).coordinate(30, -30).close());
        result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoWithinQuery("area", builder.buildGeometry())).get();
        assertHitCount(result, 2);
    }
    // Create a polygon crossing longitude 180.
    builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close());
    data = BytesReference.bytes(jsonBuilder().startObject().field("area", builder).endObject());
    client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
    client().admin().indices().prepareRefresh().get();
    // Create a polygon crossing longitude 180 with hole.
    builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(175, -5).coordinate(185, -5).coordinate(185, 5).coordinate(175, 5).close()));
    data = BytesReference.bytes(jsonBuilder().startObject().field("area", builder).endObject());
    client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
    client().admin().indices().prepareRefresh().get();
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(174, -4).buildGeometry())).get();
    assertHitCount(result, 1);
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(-174, -4).buildGeometry())).get();
    assertHitCount(result, 1);
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(180, -4).buildGeometry())).get();
    assertHitCount(result, 0);
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(180, -6).buildGeometry())).get();
    assertHitCount(result, 1);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) CreateIndexRequestBuilder(org.opensearch.action.admin.indices.create.CreateIndexRequestBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with MultiPolygonBuilder

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

the class GeoFilterIT method testShapeBuilders.

public void testShapeBuilders() {
    try {
        // self intersection polygon
        new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(10, 10).coordinate(-10, 10).coordinate(10, -10).close()).buildS4J();
        fail("Self intersection not detected");
    } catch (InvalidShapeException e) {
    }
    // polygon with hole
    new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close())).buildS4J();
    try {
        // polygon with overlapping hole
        new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 11).coordinate(5, 11).coordinate(5, -5).close())).buildS4J();
        fail("Self intersection not detected");
    } catch (InvalidShapeException e) {
    }
    try {
        // polygon with intersection holes
        new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close())).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -6).coordinate(5, -6).coordinate(5, -4).coordinate(-5, -4).close())).buildS4J();
        fail("Intersection of holes not detected");
    } catch (InvalidShapeException e) {
    }
    try {
        // Common line in polygon
        new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(-5, 10).coordinate(-5, -5).coordinate(-5, 20).coordinate(10, 20).coordinate(10, -10).close()).buildS4J();
        fail("Self intersection not detected");
    } catch (InvalidShapeException e) {
    }
    // Multipolygon: polygon with hole and polygon within the whole
    new MultiPolygonBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close())).buildS4J();
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder)

Example 4 with MultiPolygonBuilder

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

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

Aggregations

MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)5 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)5 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)4 SearchResponse (org.opensearch.action.search.SearchResponse)3 Coordinate (org.locationtech.jts.geom.Coordinate)2 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)2 LinearRing (org.locationtech.jts.geom.LinearRing)1 Point (org.locationtech.jts.geom.Point)1 Polygon (org.locationtech.jts.geom.Polygon)1 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)1 Shape (org.locationtech.spatial4j.shape.Shape)1 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)1 CreateIndexRequestBuilder (org.opensearch.action.admin.indices.create.CreateIndexRequestBuilder)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)1 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)1 Geometry (org.opensearch.geometry.Geometry)1 MultiPoint (org.opensearch.geometry.MultiPoint)1