Search in sources :

Example 6 with LinearRing

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

the class GeometryIndexerTests method testPolygon.

public void testPolygon() {
    Polygon polygon = new Polygon(new LinearRing(new double[] { 160, 200, 200, 160, 160 }, new double[] { 10, 10, 20, 20, 10 }));
    Geometry indexed = new MultiPolygon(Arrays.asList(new Polygon(new LinearRing(new double[] { 180, 180, 160, 160, 180 }, new double[] { 10, 20, 20, 10, 10 })), new Polygon(new LinearRing(new double[] { -180, -180, -160, -160, -180 }, new double[] { 20, 10, 10, 20, 20 }))));
    assertEquals(indexed, indexer.prepareForIndexing(polygon));
    polygon = new Polygon(new LinearRing(new double[] { 160, 200, 200, 160, 160 }, new double[] { 10, 10, 20, 20, 10 }), Collections.singletonList(new LinearRing(new double[] { 165, 165, 195, 195, 165 }, new double[] { 12, 18, 18, 12, 12 })));
    indexed = new MultiPolygon(Arrays.asList(new Polygon(new LinearRing(new double[] { 180, 180, 165, 165, 180, 180, 160, 160, 180 }, new double[] { 10, 12, 12, 18, 18, 20, 20, 10, 10 })), new Polygon(new LinearRing(new double[] { -180, -180, -160, -160, -180, -180, -165, -165, -180 }, new double[] { 12, 10, 10, 20, 20, 18, 18, 12, 12 }))));
    assertEquals(indexed, indexer.prepareForIndexing(polygon));
}
Also used : Geometry(org.opensearch.geometry.Geometry) MultiPolygon(org.opensearch.geometry.MultiPolygon) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) LinearRing(org.opensearch.geometry.LinearRing)

Example 7 with LinearRing

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

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

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

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

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