Search in sources :

Example 6 with LineStringBuilder

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

the class GeoWKTShapeParserTests method testParsePolygonWithHole.

public void testParsePolygonWithHole() throws IOException, ParseException {
    // add 3d point to test ISSUE #10501
    List<Coordinate> shellCoordinates = new ArrayList<>();
    shellCoordinates.add(new Coordinate(100, 0));
    shellCoordinates.add(new Coordinate(101, 0));
    shellCoordinates.add(new Coordinate(101, 1));
    shellCoordinates.add(new Coordinate(100, 1));
    shellCoordinates.add(new Coordinate(100, 0));
    List<Coordinate> holeCoordinates = new ArrayList<>();
    holeCoordinates.add(new Coordinate(100.2, 0.2));
    holeCoordinates.add(new Coordinate(100.8, 0.2));
    holeCoordinates.add(new Coordinate(100.8, 0.8));
    holeCoordinates.add(new Coordinate(100.2, 0.8));
    holeCoordinates.add(new Coordinate(100.2, 0.2));
    PolygonBuilder polygonWithHole = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoordinates));
    polygonWithHole.hole(new LineStringBuilder(holeCoordinates));
    LinearRing shell = GEOMETRY_FACTORY.createLinearRing(shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]));
    LinearRing[] holes = new LinearRing[1];
    holes[0] = GEOMETRY_FACTORY.createLinearRing(holeCoordinates.toArray(new Coordinate[holeCoordinates.size()]));
    Polygon expected = GEOMETRY_FACTORY.createPolygon(shell, holes);
    assertExpected(jtsGeom(expected), polygonWithHole, true);
    org.opensearch.geometry.LinearRing hole = new org.opensearch.geometry.LinearRing(new double[] { 100.2d, 100.8d, 100.8d, 100.2d, 100.2d }, new double[] { 0.8d, 0.8d, 0.2d, 0.2d, 0.8d });
    org.opensearch.geometry.Polygon p = new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(new double[] { 101d, 101d, 100d, 100d, 101d }, new double[] { 0d, 1d, 1d, 0d, 0d }), Collections.singletonList(hole));
    assertExpected(p, polygonWithHole, false);
    assertMalformed(polygonWithHole);
}
Also used : ArrayList(java.util.ArrayList) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder)

Example 7 with LineStringBuilder

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

the class GeoWKTShapeParserTests method testParseMixedDimensionPolyWithHoleStoredZ.

public void testParseMixedDimensionPolyWithHoleStoredZ() throws IOException {
    List<Coordinate> shellCoordinates = new ArrayList<>();
    shellCoordinates.add(new Coordinate(100, 0));
    shellCoordinates.add(new Coordinate(101, 0));
    shellCoordinates.add(new Coordinate(101, 1));
    shellCoordinates.add(new Coordinate(100, 1));
    shellCoordinates.add(new Coordinate(100, 0));
    // add 3d point to test ISSUE #10501
    List<Coordinate> holeCoordinates = new ArrayList<>();
    holeCoordinates.add(new Coordinate(100.2, 0.2, 15.0));
    holeCoordinates.add(new Coordinate(100.8, 0.2));
    holeCoordinates.add(new Coordinate(100.8, 0.8));
    holeCoordinates.add(new Coordinate(100.2, 0.8, 10.0));
    holeCoordinates.add(new Coordinate(100.2, 0.2));
    PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoordinates));
    builder.hole(new LineStringBuilder(holeCoordinates));
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().value(builder.toWKT());
    XContentParser parser = createParser(xContentBuilder);
    parser.nextToken();
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    Mapper.BuilderContext mockBuilderContext = new Mapper.BuilderContext(indexSettings, new ContentPath());
    final LegacyGeoShapeFieldMapper mapperBuilder = (LegacyGeoShapeFieldMapper) (new LegacyGeoShapeFieldMapper.Builder("test").ignoreZValue(true).build(mockBuilderContext));
    // test store z disabled
    OpenSearchException e = expectThrows(OpenSearchException.class, () -> ShapeParser.parse(parser, mapperBuilder));
    assertThat(e, hasToString(containsString("unable to add coordinate to CoordinateBuilder: coordinate dimensions do not match")));
}
Also used : MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ArrayList(java.util.ArrayList) ContentPath(org.opensearch.index.mapper.ContentPath) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) GeoShapeFieldMapper(org.opensearch.index.mapper.GeoShapeFieldMapper) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) Coordinate(org.locationtech.jts.geom.Coordinate) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) OpenSearchException(org.opensearch.OpenSearchException) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) Settings(org.opensearch.common.settings.Settings)

Example 8 with LineStringBuilder

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

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

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

Aggregations

LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)23 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)21 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)20 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)17 Coordinate (org.locationtech.jts.geom.Coordinate)7 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)6 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)5 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)5 ArrayList (java.util.ArrayList)4 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)4 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)4 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)4 OpenSearchException (org.opensearch.OpenSearchException)3 OpenSearchParseException (org.opensearch.OpenSearchParseException)3 MultiLine (org.opensearch.geometry.MultiLine)3 LineString (org.locationtech.jts.geom.LineString)2 LinearRing (org.locationtech.jts.geom.LinearRing)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 Point (org.locationtech.jts.geom.Point)2 Polygon (org.locationtech.jts.geom.Polygon)2