Search in sources :

Example 6 with PolygonBuilder

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

the class ShapeBuilderTests method testDatelineOGC.

public void testDatelineOGC() {
    // tests that the following shape (defined in counterclockwise OGC order)
    // https://gist.github.com/anonymous/7f1bb6d7e9cd72f5977c crosses the dateline
    // expected results: 3 polygons, 1 with a hole
    // a giant c shape
    PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(174, 0).coordinate(-176, 0).coordinate(-176, 3).coordinate(177, 3).coordinate(177, 5).coordinate(-176, 5).coordinate(-176, 8).coordinate(174, 8).coordinate(174, 0));
    // 3/4 of an embedded 'c', crossing dateline once
    builder.hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(175, 1).coordinate(175, 7).coordinate(-178, 7).coordinate(-178, 6).coordinate(176, 6).coordinate(176, 2).coordinate(179, 2).coordinate(179, 1).coordinate(175, 1)));
    // embedded hole right of the dateline
    builder.hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-179, 1).coordinate(-179, 2).coordinate(-177, 2).coordinate(-177, 1).coordinate(-179, 1)));
    assertMultiPolygon(builder.close().buildS4J(), true);
    assertMultiPolygon(buildGeometry(builder.close()), false);
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder)

Example 7 with PolygonBuilder

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

the class ShapeBuilderTests method testBoundaryShapeWithInvalidTangentialHole.

public void testBoundaryShapeWithInvalidTangentialHole() {
    // test shape with two tangential (shared) vertices (should throw exception)
    PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(-177, 10).coordinate(176, 15).coordinate(172, 0).coordinate(176, -15).coordinate(-177, -10).coordinate(-177, 10));
    builder.hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-177, 10).coordinate(172, 0).coordinate(180, -5).coordinate(176, -10).coordinate(-177, 10)));
    Exception e;
    e = expectThrows(InvalidShapeException.class, () -> builder.close().buildS4J());
    assertThat(e.getMessage(), containsString("interior cannot share more than one point with the exterior"));
    e = expectThrows(IllegalArgumentException.class, () -> buildGeometry(builder.close()));
    assertThat(e.getMessage(), containsString("interior cannot share more than one point with the exterior"));
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException)

Example 8 with PolygonBuilder

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

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

the class GeoWKTShapeParserTests method testParsePolyWithStoredZ.

public void testParsePolyWithStoredZ() throws IOException {
    List<Coordinate> shellCoordinates = new ArrayList<>();
    shellCoordinates.add(new Coordinate(100, 0, 0));
    shellCoordinates.add(new Coordinate(101, 0, 0));
    shellCoordinates.add(new Coordinate(101, 1, 0));
    shellCoordinates.add(new Coordinate(100, 1, 5));
    shellCoordinates.add(new Coordinate(100, 0, 5));
    PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoordinates));
    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));
    ShapeBuilder<?, ?, ?> shapeBuilder = ShapeParser.parse(parser, mapperBuilder);
    assertEquals(shapeBuilder.numDimensions(), 3);
}
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) 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) 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 10 with PolygonBuilder

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

the class GeoWKTShapeParserTests method testParseMixedDimensionPolyWithHole.

public void testParseMixedDimensionPolyWithHole() throws IOException, ParseException {
    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 GeoShapeFieldMapper mapperBuilder = (GeoShapeFieldMapper) (new GeoShapeFieldMapper.Builder("test").ignoreZValue(false).build(mockBuilderContext));
    // test store z disabled
    OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> ShapeParser.parse(parser, mapperBuilder));
    assertThat(e, hasToString(containsString("but [ignore_z_value] parameter is [false]")));
}
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) OpenSearchParseException(org.opensearch.OpenSearchParseException) Coordinate(org.locationtech.jts.geom.Coordinate) GeoShapeFieldMapper(org.opensearch.index.mapper.GeoShapeFieldMapper) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) 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)

Aggregations

PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)39 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)37 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)17 Coordinate (org.locationtech.jts.geom.Coordinate)14 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)14 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)13 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)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 Polygon (org.locationtech.jts.geom.Polygon)6 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 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)4 Settings (org.opensearch.common.settings.Settings)4 OpenSearchGeoAssertions.assertMultiLineString (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiLineString)4