Search in sources :

Example 1 with CoordinatesBuilder

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

the class GeoWKTParser method parseCoordinateList.

private static List<Coordinate> parseCoordinateList(StreamTokenizer stream, final boolean ignoreZValue, final boolean coerce) throws IOException, OpenSearchParseException {
    CoordinatesBuilder coordinates = new CoordinatesBuilder();
    boolean isOpenParen = false;
    if (isNumberNext(stream) || (isOpenParen = nextWord(stream).equals(LPAREN))) {
        coordinates.coordinate(parseCoordinate(stream, ignoreZValue, coerce));
    }
    if (isOpenParen && nextCloser(stream).equals(RPAREN) == false) {
        throw new OpenSearchParseException("expected: [{}]" + RPAREN + " but found: [{}]" + tokenString(stream), stream.lineno());
    }
    while (nextCloserOrComma(stream).equals(COMMA)) {
        isOpenParen = false;
        if (isNumberNext(stream) || (isOpenParen = nextWord(stream).equals(LPAREN))) {
            coordinates.coordinate(parseCoordinate(stream, ignoreZValue, coerce));
        }
        if (isOpenParen && nextCloser(stream).equals(RPAREN) == false) {
            throw new OpenSearchParseException("expected: " + RPAREN + " but found: " + tokenString(stream), stream.lineno());
        }
    }
    return coordinates.build();
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) OpenSearchParseException(org.opensearch.OpenSearchParseException)

Example 2 with CoordinatesBuilder

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

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

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

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

the class ShapeBuilderTests method testLineStringWrapping.

public void testLineStringWrapping() {
    LineStringBuilder lsb = new LineStringBuilder(new CoordinatesBuilder().coordinate(-150.0, 65.0).coordinate(-250.0, 65.0).coordinate(-250.0, -65.0).coordinate(-150.0, -65.0).close());
    assertMultiLineString(lsb.buildS4J(), true);
    assertMultiLineString(buildGeometry(lsb), false);
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder)

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