Search in sources :

Example 1 with LinearRing

use of org.opensearch.geometry.LinearRing in project OpenSearch by opensearch-project.

the class WellKnownText method parsePolygon.

private Polygon parsePolygon(StreamTokenizer stream) throws IOException, ParseException {
    if (nextEmptyOrOpen(stream).equals(EMPTY)) {
        return Polygon.EMPTY;
    }
    nextOpener(stream);
    ArrayList<Double> lats = new ArrayList<>();
    ArrayList<Double> lons = new ArrayList<>();
    ArrayList<Double> alts = new ArrayList<>();
    parseCoordinates(stream, lats, lons, alts);
    ArrayList<LinearRing> holes = new ArrayList<>();
    while (nextCloserOrComma(stream).equals(COMMA)) {
        holes.add(parsePolygonHole(stream));
    }
    closeLinearRingIfCoerced(lats, lons, alts);
    LinearRing shell;
    if (alts.isEmpty()) {
        shell = new LinearRing(toArray(lons), toArray(lats));
    } else {
        shell = new LinearRing(toArray(lons), toArray(lats), toArray(alts));
    }
    if (holes.isEmpty()) {
        return new Polygon(shell);
    } else {
        return new Polygon(shell, Collections.unmodifiableList(holes));
    }
}
Also used : ArrayList(java.util.ArrayList) LinearRing(org.opensearch.geometry.LinearRing) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon)

Example 2 with LinearRing

use of org.opensearch.geometry.LinearRing in project OpenSearch by opensearch-project.

the class GeometryIO method readPolygon.

private static Polygon readPolygon(StreamInput in) throws IOException {
    double[][] shellComponents = readLineComponents(in);
    boolean orientation = in.readBoolean();
    LinearRing shell = buildLinearRing(shellComponents, orientation);
    int numberOfHoles = in.readVInt();
    if (numberOfHoles > 0) {
        List<LinearRing> holes = new ArrayList<>(numberOfHoles);
        for (int i = 0; i < numberOfHoles; i++) {
            holes.add(buildLinearRing(readLineComponents(in), orientation));
        }
        return new Polygon(shell, holes);
    } else {
        return new Polygon(shell);
    }
}
Also used : ArrayList(java.util.ArrayList) LinearRing(org.opensearch.geometry.LinearRing) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint)

Example 3 with LinearRing

use of org.opensearch.geometry.LinearRing 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 4 with LinearRing

use of org.opensearch.geometry.LinearRing 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 5 with LinearRing

use of org.opensearch.geometry.LinearRing in project OpenSearch by opensearch-project.

the class GeometryParserTests method testBasics.

public void testBasics() {
    GeometryParser parser = new GeometryParser(true, randomBoolean(), randomBoolean());
    // point
    Point expectedPoint = new Point(-122.084110, 37.386637);
    testBasics(parser, mapOf("lat", 37.386637, "lon", -122.084110), expectedPoint);
    testBasics(parser, "37.386637, -122.084110", expectedPoint);
    testBasics(parser, "POINT (-122.084110 37.386637)", expectedPoint);
    testBasics(parser, Arrays.asList(-122.084110, 37.386637), expectedPoint);
    testBasics(parser, mapOf("type", "Point", "coordinates", Arrays.asList(-122.084110, 37.386637)), expectedPoint);
    // line
    Line expectedLine = new Line(new double[] { 0, 1 }, new double[] { 0, 1 });
    testBasics(parser, "LINESTRING(0 0, 1 1)", expectedLine);
    testBasics(parser, mapOf("type", "LineString", "coordinates", Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 1))), expectedLine);
    // polygon
    Polygon expectedPolygon = new Polygon(new LinearRing(new double[] { 0, 1, 1, 0, 0 }, new double[] { 0, 0, 1, 1, 0 }));
    testBasics(parser, "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))", expectedPolygon);
    testBasics(parser, mapOf("type", "Polygon", "coordinates", Arrays.asList(Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 0), Arrays.asList(1, 1), Arrays.asList(0, 1), Arrays.asList(0, 0)))), expectedPolygon);
    // geometry collection
    testBasics(parser, Arrays.asList(Arrays.asList(-122.084110, 37.386637), "37.386637, -122.084110", "POINT (-122.084110 37.386637)", mapOf("type", "Point", "coordinates", Arrays.asList(-122.084110, 37.386637)), mapOf("type", "LineString", "coordinates", Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 1))), "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))"), new GeometryCollection<>(Arrays.asList(expectedPoint, expectedPoint, expectedPoint, expectedPoint, expectedLine, expectedPolygon)));
    expectThrows(OpenSearchParseException.class, () -> testBasics(parser, "not a geometry", null));
}
Also used : Line(org.opensearch.geometry.Line) Point(org.opensearch.geometry.Point) Polygon(org.opensearch.geometry.Polygon) LinearRing(org.opensearch.geometry.LinearRing)

Aggregations

LinearRing (org.opensearch.geometry.LinearRing)18 Polygon (org.opensearch.geometry.Polygon)14 MultiPolygon (org.opensearch.geometry.MultiPolygon)12 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)8 Point (org.opensearch.geometry.Point)6 ArrayList (java.util.ArrayList)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 LineString (org.locationtech.jts.geom.LineString)3 Polygon (org.locationtech.jts.geom.Polygon)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)3 MultiPoint (org.opensearch.geometry.MultiPoint)3 OpenSearchGeoAssertions.assertMultiLineString (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiLineString)3 OpenSearchGeoAssertions.assertMultiPolygon (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiPolygon)3 OpenSearchGeoAssertions.assertPolygon (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertPolygon)3 XContentParser (org.opensearch.common.xcontent.XContentParser)2 Geometry (org.opensearch.geometry.Geometry)2 Line (org.opensearch.geometry.Line)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1