Search in sources :

Example 11 with Polygon

use of org.opensearch.geometry.Polygon 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 12 with Polygon

use of org.opensearch.geometry.Polygon 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 13 with Polygon

use of org.opensearch.geometry.Polygon 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 14 with Polygon

use of org.opensearch.geometry.Polygon 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)

Example 15 with Polygon

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

the class GeoPolygonDecomposer method buildPolygon.

private static Polygon buildPolygon(List<Point[]> polygon) {
    List<LinearRing> holes;
    Point[] shell = polygon.get(0);
    if (polygon.size() > 1) {
        holes = new ArrayList<>(polygon.size() - 1);
        for (int i = 1; i < polygon.size(); ++i) {
            Point[] coords = polygon.get(i);
            // We do not have holes on the dateline as they get eliminated
            // when breaking the polygon around it.
            double[] x = new double[coords.length];
            double[] y = new double[coords.length];
            for (int c = 0; c < coords.length; ++c) {
                x[c] = normalizeLon(coords[c].getX());
                y[c] = normalizeLat(coords[c].getY());
            }
            holes.add(new LinearRing(x, y));
        }
    } else {
        holes = Collections.emptyList();
    }
    double[] x = new double[shell.length];
    double[] y = new double[shell.length];
    for (int i = 0; i < shell.length; ++i) {
        // Lucene Tessellator treats different +180 and -180 and we should keep the sign.
        // normalizeLon method excludes -180.
        x[i] = normalizeLonMinus180Inclusive(shell[i].getX());
        y[i] = normalizeLat(shell[i].getY());
    }
    return new Polygon(new LinearRing(x, y), holes);
}
Also used : Point(org.opensearch.geometry.Point) LinearRing(org.opensearch.geometry.LinearRing) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) Point(org.opensearch.geometry.Point)

Aggregations

Polygon (org.opensearch.geometry.Polygon)16 LinearRing (org.opensearch.geometry.LinearRing)14 MultiPolygon (org.opensearch.geometry.MultiPolygon)14 Point (org.opensearch.geometry.Point)8 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)7 ArrayList (java.util.ArrayList)5 MultiPoint (org.opensearch.geometry.MultiPoint)5 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 GeoTestUtil (org.apache.lucene.geo.GeoTestUtil)1 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)1 GeometryCollection (org.opensearch.geometry.GeometryCollection)1 MultiLine (org.opensearch.geometry.MultiLine)1 GeographyValidator (org.opensearch.geometry.utils.GeographyValidator)1