Search in sources :

Example 11 with LinearRing

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

the class ShapeBuilderTests method testNewPolygon_coordinate.

public void testNewPolygon_coordinate() {
    PolygonBuilder pb = new PolygonBuilder(new CoordinatesBuilder().coordinate(new Coordinate(-45, 30)).coordinate(new Coordinate(45, 30)).coordinate(new Coordinate(45, -30)).coordinate(new Coordinate(-45, -30)).coordinate(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 12 with LinearRing

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

the class GeoJsonParserTests method testParsePolygonWithHole.

public void testParsePolygonWithHole() throws IOException {
    XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(100.0).value(1.0).endArray().startArray().value(101.0).value(1.0).endArray().startArray().value(101.0).value(0.0).endArray().startArray().value(100.0).value(0.0).endArray().startArray().value(100.0).value(1.0).endArray().endArray().startArray().startArray().value(100.2).value(0.8).endArray().startArray().value(100.2).value(0.2).endArray().startArray().value(100.8).value(0.2).endArray().startArray().value(100.8).value(0.8).endArray().startArray().value(100.2).value(0.8).endArray().endArray().endArray().endObject();
    LinearRing hole = new LinearRing(new double[] { 100.2d, 100.2d, 100.8d, 100.8d, 100.2d }, new double[] { 0.8d, 0.2d, 0.2d, 0.8d, 0.8d });
    Polygon p = new Polygon(new LinearRing(new double[] { 100d, 101d, 101d, 100d, 100d }, new double[] { 1d, 1d, 0d, 0d, 1d }), Collections.singletonList(hole));
    assertGeometryEquals(p, polygonGeoJson);
}
Also used : LinearRing(org.opensearch.geometry.LinearRing) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 13 with LinearRing

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

the class GeoJsonParserTests method testParsePolygon.

@Override
public void testParsePolygon() throws IOException {
    XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(100.0).value(1.0).endArray().startArray().value(101.0).value(1.0).endArray().startArray().value(101.0).value(0.0).endArray().startArray().value(100.0).value(0.0).endArray().startArray().value(100.0).value(1.0).endArray().endArray().endArray().endObject();
    Polygon p = new Polygon(new LinearRing(new double[] { 100d, 101d, 101d, 100d, 100d }, new double[] { 1d, 1d, 0d, 0d, 1d }));
    assertGeometryEquals(p, polygonGeoJson);
}
Also used : Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) LinearRing(org.opensearch.geometry.LinearRing) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 14 with LinearRing

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

the class GeoJsonParserTests method testParseMultiPolygon.

@Override
public void testParseMultiPolygon() throws IOException {
    // two polygons; one without hole, one with hole
    XContentBuilder multiPolygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "MultiPolygon").startArray("coordinates").startArray().startArray().startArray().value(102.0).value(2.0).endArray().startArray().value(103.0).value(2.0).endArray().startArray().value(103.0).value(3.0).endArray().startArray().value(102.0).value(3.0).endArray().startArray().value(102.0).value(2.0).endArray().endArray().endArray().startArray().startArray().startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().startArray().value(100.0).value(1.0).endArray().startArray().value(100.0).value(0.0).endArray().endArray().startArray().startArray().value(100.2).value(0.8).endArray().startArray().value(100.2).value(0.2).endArray().startArray().value(100.8).value(0.2).endArray().startArray().value(100.8).value(0.8).endArray().startArray().value(100.2).value(0.8).endArray().endArray().endArray().endArray().endObject();
    LinearRing hole = new LinearRing(new double[] { 100.2d, 100.2d, 100.8d, 100.8d, 100.2d }, new double[] { 0.8d, 0.2d, 0.2d, 0.8d, 0.8d });
    MultiPolygon polygons = new MultiPolygon(Arrays.asList(new Polygon(new LinearRing(new double[] { 102d, 103d, 103d, 102d, 102d }, new double[] { 2d, 2d, 3d, 3d, 2d })), new Polygon(new LinearRing(new double[] { 100d, 101d, 101d, 100d, 100d }, new double[] { 0d, 0d, 1d, 1d, 0d }), Collections.singletonList(hole))));
    assertGeometryEquals(polygons, multiPolygonGeoJson);
}
Also used : MultiPolygon(org.opensearch.geometry.MultiPolygon) LinearRing(org.opensearch.geometry.LinearRing) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 15 with LinearRing

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

the class GeoJsonParserTests method testParseOrientationOption.

public void testParseOrientationOption() throws IOException {
    // test 1: valid ccw (right handed system) poly not crossing dateline (with 'right' field)
    XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").field("orientation", randomFrom("ccw", "right")).startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(-177.0).value(10.0).endArray().startArray().value(-177.0).value(-10.0).endArray().startArray().value(176.0).value(-15.0).endArray().startArray().value(172.0).value(0.0).endArray().startArray().value(176.0).value(15.0).endArray().endArray().startArray().startArray().value(-172.0).value(8.0).endArray().startArray().value(174.0).value(10.0).endArray().startArray().value(-172.0).value(-8.0).endArray().startArray().value(-172.0).value(8.0).endArray().endArray().endArray().endObject();
    Polygon expected = new Polygon(new LinearRing(new double[] { 176.0, -177.0, -177.0, 176.0, 172.0, 176.0 }, new double[] { 15.0, 10.0, -10.0, -15.0, 0.0, 15.0 }), Collections.singletonList(new LinearRing(new double[] { -172.0, 174.0, -172.0, -172.0 }, new double[] { 8.0, 10.0, -8.0, 8.0 })));
    assertGeometryEquals(expected, polygonGeoJson);
    // test 2: valid cw poly
    polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").field("orientation", randomFrom("cw", "left")).startArray("coordinates").startArray().startArray().value(176.0).value(15.0).endArray().startArray().value(-177.0).value(10.0).endArray().startArray().value(-177.0).value(-10.0).endArray().startArray().value(176.0).value(-15.0).endArray().startArray().value(172.0).value(0.0).endArray().startArray().value(176.0).value(15.0).endArray().endArray().startArray().startArray().value(-172.0).value(8.0).endArray().startArray().value(174.0).value(10.0).endArray().startArray().value(-172.0).value(-8.0).endArray().startArray().value(-172.0).value(8.0).endArray().endArray().endArray().endObject();
    expected = new Polygon(new LinearRing(new double[] { 176.0, 172.0, 176.0, -177.0, -177.0, 176.0 }, new double[] { 15.0, 0.0, -15.0, -10.0, 10.0, 15.0 }), Collections.singletonList(new LinearRing(new double[] { -172.0, -172.0, 174.0, -172.0 }, new double[] { 8.0, -8.0, 10.0, 8.0 })));
    assertGeometryEquals(expected, polygonGeoJson);
}
Also used : Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) LinearRing(org.opensearch.geometry.LinearRing) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

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