Search in sources :

Example 16 with Polygon

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

the class GeometryParserTests method testGeoJsonParsing.

public void testGeoJsonParsing() throws Exception {
    XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Point").startArray("coordinates").value(100.0).value(0.0).endArray().endObject();
    try (XContentParser parser = createParser(pointGeoJson)) {
        parser.nextToken();
        GeometryFormat format = new GeometryParser(true, randomBoolean(), randomBoolean()).geometryFormat(parser);
        assertEquals(new Point(100, 0), format.fromXContent(parser));
        XContentBuilder newGeoJson = XContentFactory.jsonBuilder();
        format.toXContent(new Point(100, 10), newGeoJson, ToXContent.EMPTY_PARAMS);
        assertEquals("{\"type\":\"Point\",\"coordinates\":[100.0,10.0]}", Strings.toString(newGeoJson));
    }
    XContentBuilder pointGeoJsonWithZ = XContentFactory.jsonBuilder().startObject().field("type", "Point").startArray("coordinates").value(100.0).value(0.0).value(10.0).endArray().endObject();
    try (XContentParser parser = createParser(pointGeoJsonWithZ)) {
        parser.nextToken();
        assertEquals(new Point(100, 0, 10.0), new GeometryParser(true, randomBoolean(), true).parse(parser));
    }
    try (XContentParser parser = createParser(pointGeoJsonWithZ)) {
        parser.nextToken();
        expectThrows(IllegalArgumentException.class, () -> new GeometryParser(true, randomBoolean(), false).parse(parser));
    }
    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().endArray().endArray().endObject();
    Polygon p = new Polygon(new LinearRing(new double[] { 100d, 101d, 101d, 100d, 100d }, new double[] { 1d, 1d, 0d, 0d, 1d }));
    try (XContentParser parser = createParser(polygonGeoJson)) {
        parser.nextToken();
        // Coerce should automatically close the polygon
        assertEquals(p, new GeometryParser(true, true, randomBoolean()).parse(parser));
    }
    try (XContentParser parser = createParser(polygonGeoJson)) {
        parser.nextToken();
        // No coerce - the polygon parsing should fail
        expectThrows(XContentParseException.class, () -> new GeometryParser(true, false, randomBoolean()).parse(parser));
    }
}
Also used : Point(org.opensearch.geometry.Point) Polygon(org.opensearch.geometry.Polygon) LinearRing(org.opensearch.geometry.LinearRing) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

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