Search in sources :

Example 6 with Polygon

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

the class GeometryIndexerTests method testInvalidSelfCrossingPolygon.

public void testInvalidSelfCrossingPolygon() {
    Polygon polygon = new Polygon(new LinearRing(new double[] { 0, 0, 1, 0.5, 1.5, 1, 2, 2, 0 }, new double[] { 0, 2, 1.9, 1.8, 1.8, 1.9, 2, 0, 0 }));
    Exception e = expectThrows(InvalidShapeException.class, () -> indexer.prepareForIndexing(polygon));
    assertThat(e.getMessage(), containsString("Self-intersection at or near point ["));
    assertThat(e.getMessage(), not(containsString("NaN")));
}
Also used : Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) LinearRing(org.opensearch.geometry.LinearRing) ParseException(java.text.ParseException) IOException(java.io.IOException) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException)

Example 7 with Polygon

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

the class GeoJsonParserTests method testParseGeometryCollection.

@Override
public void testParseGeometryCollection() throws IOException {
    XContentBuilder geometryCollectionGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "GeometryCollection").startArray("geometries").startObject().field("type", "LineString").startArray("coordinates").startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().endArray().endObject().startObject().field("type", "Point").startArray("coordinates").value(102.0).value(2.0).endArray().endObject().startObject().field("type", "Polygon").startArray("coordinates").startArray().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().startArray().value(-177.0).value(-10.0).endArray().startArray().value(-177.0).value(10.0).endArray().endArray().endArray().endObject().endArray().endObject();
    GeometryCollection<Geometry> geometryExpected = new GeometryCollection<>(Arrays.asList(new Line(new double[] { 100d, 101d }, new double[] { 0d, 1d }), new Point(102d, 2d), new Polygon(new LinearRing(new double[] { -177, 176, 172, 176, -177, -177 }, new double[] { 10, 15, 0, -15, -10, 10 }))));
    assertGeometryEquals(geometryExpected, geometryCollectionGeoJson);
}
Also used : Geometry(org.opensearch.geometry.Geometry) GeometryCollection(org.opensearch.geometry.GeometryCollection) Line(org.opensearch.geometry.Line) MultiLine(org.opensearch.geometry.MultiLine) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) LinearRing(org.opensearch.geometry.LinearRing) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 8 with Polygon

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

the class GeoJsonParserTests method testParse3DPolygon.

public void testParse3DPolygon() throws IOException {
    XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(100.0).value(1.0).value(10.0).endArray().startArray().value(101.0).value(1.0).value(10.0).endArray().startArray().value(101.0).value(0.0).value(10.0).endArray().startArray().value(100.0).value(0.0).value(10.0).endArray().startArray().value(100.0).value(1.0).value(10.0).endArray().endArray().endArray().endObject();
    Polygon expected = new Polygon(new LinearRing(new double[] { 100.0, 101.0, 101.0, 100.0, 100.0 }, new double[] { 1.0, 1.0, 0.0, 0.0, 1.0 }, new double[] { 10.0, 10.0, 10.0, 10.0, 10.0 }));
    try (XContentParser parser = createParser(polygonGeoJson)) {
        parser.nextToken();
        assertEquals(expected, new GeoJson(true, false, new GeographyValidator(true)).fromXContent(parser));
    }
}
Also used : GeographyValidator(org.opensearch.geometry.utils.GeographyValidator) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) LinearRing(org.opensearch.geometry.LinearRing) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 9 with Polygon

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

the class GeometryTestUtils method randomPolygon.

public static Polygon randomPolygon(boolean hasAlt) {
    org.apache.lucene.geo.Polygon lucenePolygon = randomValueOtherThanMany(p -> area(p) == 0, GeoTestUtil::nextPolygon);
    if (lucenePolygon.numHoles() > 0) {
        org.apache.lucene.geo.Polygon[] luceneHoles = lucenePolygon.getHoles();
        List<LinearRing> holes = new ArrayList<>();
        for (int i = 0; i < lucenePolygon.numHoles(); i++) {
            org.apache.lucene.geo.Polygon poly = luceneHoles[i];
            holes.add(linearRing(poly.getPolyLons(), poly.getPolyLats(), hasAlt));
        }
        return new Polygon(linearRing(lucenePolygon.getPolyLons(), lucenePolygon.getPolyLats(), hasAlt), holes);
    }
    return new Polygon(linearRing(lucenePolygon.getPolyLons(), lucenePolygon.getPolyLats(), hasAlt));
}
Also used : GeoTestUtil(org.apache.lucene.geo.GeoTestUtil) ArrayList(java.util.ArrayList) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) LinearRing(org.opensearch.geometry.LinearRing) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint)

Example 10 with Polygon

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

the class GeometryTestUtils method randomMultiPolygon.

public static MultiPolygon randomMultiPolygon(boolean hasAlt) {
    int size = OpenSearchTestCase.randomIntBetween(3, 10);
    List<Polygon> polygons = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        polygons.add(randomPolygon(hasAlt));
    }
    return new MultiPolygon(polygons);
}
Also used : MultiPolygon(org.opensearch.geometry.MultiPolygon) ArrayList(java.util.ArrayList) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint)

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