Search in sources :

Example 16 with CoordinatesBuilder

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

the class GeoShapeQueryTests method testReusableBuilder.

public void testReusableBuilder() throws IOException {
    PolygonBuilder polygon = 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()));
    assertUnmodified(polygon);
    LineStringBuilder linestring = new LineStringBuilder(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close());
    assertUnmodified(linestring);
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder)

Example 17 with CoordinatesBuilder

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

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

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

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

the class GeoWKTShapeParserTests method testParseSelfCrossingPolygon.

public void testParseSelfCrossingPolygon() throws IOException {
    // test self crossing ccw poly not crossing dateline
    List<Coordinate> shellCoordinates = new ArrayList<>();
    shellCoordinates.add(new Coordinate(176, 15));
    shellCoordinates.add(new Coordinate(-177, 10));
    shellCoordinates.add(new Coordinate(-177, -10));
    shellCoordinates.add(new Coordinate(176, -15));
    shellCoordinates.add(new Coordinate(-177, 15));
    shellCoordinates.add(new Coordinate(172, 0));
    shellCoordinates.add(new Coordinate(176, 15));
    PolygonBuilder poly = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoordinates));
    XContentBuilder builder = XContentFactory.jsonBuilder().value(poly.toWKT());
    assertValidException(builder, InvalidShapeException.class);
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) ArrayList(java.util.ArrayList) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)42 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)37 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)21 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)18 Coordinate (org.locationtech.jts.geom.Coordinate)12 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)12 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)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 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 Polygon (org.locationtech.jts.geom.Polygon)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