Search in sources :

Example 1 with PolygonBuilder

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

the class ShapeBuilderTests method testNewPolygon_coordinates.

public void testNewPolygon_coordinates() {
    PolygonBuilder pb = new PolygonBuilder(new CoordinatesBuilder().coordinates(new Coordinate(-45, 30), new Coordinate(45, 30), new Coordinate(45, -30), new Coordinate(-45, -30), new Coordinate(-45, 30)));
    Polygon poly = pb.toPolygonS4J();
    LineString exterior = poly.getExteriorRing();
    assertEquals(exterior.getCoordinateN(0), new Coordinate(-45, 30));
    assertEquals(exterior.getCoordinateN(1), new Coordinate(45, 30));
    assertEquals(exterior.getCoordinateN(2), new Coordinate(45, -30));
    assertEquals(exterior.getCoordinateN(3), new Coordinate(-45, -30));
    LinearRing polygon = pb.toPolygonGeometry().getPolygon();
    assertEquals(polygon.getY(0), 30, 0d);
    assertEquals(polygon.getX(0), -45, 0d);
    assertEquals(polygon.getY(1), 30, 0d);
    assertEquals(polygon.getX(1), 45, 0d);
    assertEquals(polygon.getY(2), -30, 0d);
    assertEquals(polygon.getX(2), 45, 0d);
    assertEquals(polygon.getY(3), -30, 0d);
    assertEquals(polygon.getX(3), -45, 0d);
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) OpenSearchGeoAssertions.assertMultiLineString(org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiLineString) LineString(org.locationtech.jts.geom.LineString) OpenSearchGeoAssertions.assertPolygon(org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertPolygon) OpenSearchGeoAssertions.assertMultiPolygon(org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiPolygon) Polygon(org.locationtech.jts.geom.Polygon) LinearRing(org.opensearch.geometry.LinearRing) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder)

Example 2 with PolygonBuilder

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

the class ShapeBuilderTests method testNewPolygon.

public void testNewPolygon() {
    PolygonBuilder pb = new PolygonBuilder(new CoordinatesBuilder().coordinate(-45, 30).coordinate(45, 30).coordinate(45, -30).coordinate(-45, -30).coordinate(-45, 30));
    Polygon poly = pb.toPolygonS4J();
    LineString exterior = poly.getExteriorRing();
    assertEquals(exterior.getCoordinateN(0), new Coordinate(-45, 30));
    assertEquals(exterior.getCoordinateN(1), new Coordinate(45, 30));
    assertEquals(exterior.getCoordinateN(2), new Coordinate(45, -30));
    assertEquals(exterior.getCoordinateN(3), new Coordinate(-45, -30));
    LinearRing polygon = pb.toPolygonGeometry().getPolygon();
    assertEquals(polygon.getY(0), 30, 0d);
    assertEquals(polygon.getX(0), -45, 0d);
    assertEquals(polygon.getY(1), 30, 0d);
    assertEquals(polygon.getX(1), 45, 0d);
    assertEquals(polygon.getY(2), -30, 0d);
    assertEquals(polygon.getX(2), 45, 0d);
    assertEquals(polygon.getY(3), -30, 0d);
    assertEquals(polygon.getX(3), -45, 0d);
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) OpenSearchGeoAssertions.assertMultiLineString(org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiLineString) LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) OpenSearchGeoAssertions.assertPolygon(org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertPolygon) OpenSearchGeoAssertions.assertMultiPolygon(org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiPolygon) Polygon(org.locationtech.jts.geom.Polygon) LinearRing(org.opensearch.geometry.LinearRing) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder)

Example 3 with PolygonBuilder

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

the class ShapeBuilderTests method testPolygon3D.

public void testPolygon3D() {
    String expected = "{\n" + "  \"type\" : \"polygon\",\n" + "  \"orientation\" : \"right\",\n" + "  \"coordinates\" : [\n" + "    [\n" + "      [\n" + "        -45.0,\n" + "        30.0,\n" + "        100.0\n" + "      ],\n" + "      [\n" + "        45.0,\n" + "        30.0,\n" + "        75.0\n" + "      ],\n" + "      [\n" + "        45.0,\n" + "        -30.0,\n" + "        77.0\n" + "      ],\n" + "      [\n" + "        -45.0,\n" + "        -30.0,\n" + "        101.0\n" + "      ],\n" + "      [\n" + "        -45.0,\n" + "        30.0,\n" + "        110.0\n" + "      ]\n" + "    ]\n" + "  ]\n" + "}";
    PolygonBuilder pb = new PolygonBuilder(new CoordinatesBuilder().coordinate(new Coordinate(-45, 30, 100)).coordinate(new Coordinate(45, 30, 75)).coordinate(new Coordinate(45, -30, 77)).coordinate(new Coordinate(-45, -30, 101)).coordinate(new Coordinate(-45, 30, 110)));
    assertEquals(expected, pb.toString());
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) OpenSearchGeoAssertions.assertMultiLineString(org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiLineString) LineString(org.locationtech.jts.geom.LineString) Matchers.containsString(org.hamcrest.Matchers.containsString) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder)

Example 4 with PolygonBuilder

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

the class ShapeBuilderTests method testInvalidSelfCrossingPolygon.

public void testInvalidSelfCrossingPolygon() {
    PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(0, 0).coordinate(0, 2).coordinate(1, 1.9).coordinate(0.5, 1.8).coordinate(1.5, 1.8).coordinate(1, 1.9).coordinate(2, 2).coordinate(2, 0).coordinate(0, 0));
    Exception e = expectThrows(InvalidShapeException.class, () -> builder.close().buildS4J());
    assertThat(e.getMessage(), containsString("Self-intersection at or near point ["));
    assertThat(e.getMessage(), not(containsString("NaN")));
    e = expectThrows(InvalidShapeException.class, () -> buildGeometry(builder.close()));
    assertThat(e.getMessage(), containsString("Self-intersection at or near point ["));
    assertThat(e.getMessage(), not(containsString("NaN")));
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException)

Example 5 with PolygonBuilder

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

the class ShapeBuilderTests method testPolygonWrapping.

public void testPolygonWrapping() {
    PolygonBuilder pb = new PolygonBuilder(new CoordinatesBuilder().coordinate(-150.0, 65.0).coordinate(-250.0, 65.0).coordinate(-250.0, -65.0).coordinate(-150.0, -65.0).close());
    assertMultiPolygon(pb.buildS4J(), true);
    assertMultiPolygon(buildGeometry(pb), false);
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder)

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