Search in sources :

Example 1 with WellKnownText

use of org.opensearch.geometry.utils.WellKnownText in project OpenSearch by opensearch-project.

the class CircleTests method testBasicSerialization.

public void testBasicSerialization() throws IOException, ParseException {
    WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true));
    assertEquals("CIRCLE (20.0 10.0 15.0)", wkt.toWKT(new Circle(20, 10, 15)));
    assertEquals(new Circle(20, 10, 15), wkt.fromWKT("circle (20.0 10.0 15.0)"));
    assertEquals("CIRCLE (20.0 10.0 15.0 25.0)", wkt.toWKT(new Circle(20, 10, 25, 15)));
    assertEquals(new Circle(20, 10, 25, 15), wkt.fromWKT("circle (20.0 10.0 15.0 25.0)"));
    assertEquals("CIRCLE EMPTY", wkt.toWKT(Circle.EMPTY));
    assertEquals(Circle.EMPTY, wkt.fromWKT("CIRCLE EMPTY)"));
}
Also used : GeographyValidator(org.opensearch.geometry.utils.GeographyValidator) WellKnownText(org.opensearch.geometry.utils.WellKnownText)

Example 2 with WellKnownText

use of org.opensearch.geometry.utils.WellKnownText in project OpenSearch by opensearch-project.

the class GeometryValidatorTests method testOneValidator.

public void testOneValidator() throws Exception {
    WellKnownText parser = new WellKnownText(true, new OneValidator());
    parser.fromWKT("POINT (0 1)");
    parser.fromWKT("POINT (0 1 0.5)");
    IllegalArgumentException ex;
    ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("CIRCLE (1 2 3)"));
    assertEquals("invalid latitude 2.0; must be between -1.0 and 1.0", ex.getMessage());
    ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("POINT (2 1)"));
    assertEquals("invalid longitude 2.0; must be between -1.0 and 1.0", ex.getMessage());
    ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("LINESTRING (1 -1 0, 0 0 2)"));
    assertEquals("invalid altitude 2.0; must be between -1.0 and 1.0", ex.getMessage());
    ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("POLYGON ((0.3 0.1, 0.4 0.2, 5 0.3, 0.3 0.1))"));
    assertEquals("invalid longitude 5.0; must be between -1.0 and 1.0", ex.getMessage());
    ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("POLYGON ((0.3 0.1, 0.4 0.2, 0.5 0.3, 0.3 0.1), (0.5 1.5, 2.5 1.5, 2.0 1.0))"));
    assertEquals("invalid latitude 1.5; must be between -1.0 and 1.0", ex.getMessage());
    ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("MULTIPOINT (0 1, -2 1)"));
    assertEquals("invalid longitude -2.0; must be between -1.0 and 1.0", ex.getMessage());
}
Also used : WellKnownText(org.opensearch.geometry.utils.WellKnownText)

Example 3 with WellKnownText

use of org.opensearch.geometry.utils.WellKnownText in project OpenSearch by opensearch-project.

the class GeometryValidatorTests method testNoopValidator.

public void testNoopValidator() throws Exception {
    WellKnownText parser = new WellKnownText(true, new NoopValidator());
    parser.fromWKT("CIRCLE (10000 20000 30000)");
    parser.fromWKT("POINT (10000 20000)");
    parser.fromWKT("LINESTRING (10000 20000, 0 0)");
    parser.fromWKT("POLYGON ((300 100, 400 200, 500 300, 300 100), (50 150, 250 150, 200 100))");
    parser.fromWKT("MULTIPOINT (10000 20000, 20000 30000)");
}
Also used : WellKnownText(org.opensearch.geometry.utils.WellKnownText)

Example 4 with WellKnownText

use of org.opensearch.geometry.utils.WellKnownText in project OpenSearch by opensearch-project.

the class MultiPolygonTests method testBasicSerialization.

public void testBasicSerialization() throws IOException, ParseException {
    WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true));
    assertEquals("MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))", wkt.toWKT(new MultiPolygon(Collections.singletonList(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))))));
    assertEquals(new MultiPolygon(Collections.singletonList(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })))), wkt.fromWKT("MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))"));
    assertEquals("MULTIPOLYGON EMPTY", wkt.toWKT(MultiPolygon.EMPTY));
    assertEquals(MultiPolygon.EMPTY, wkt.fromWKT("MULTIPOLYGON EMPTY)"));
}
Also used : GeographyValidator(org.opensearch.geometry.utils.GeographyValidator) WellKnownText(org.opensearch.geometry.utils.WellKnownText)

Example 5 with WellKnownText

use of org.opensearch.geometry.utils.WellKnownText in project OpenSearch by opensearch-project.

the class PolygonTests method testBasicSerialization.

public void testBasicSerialization() throws IOException, ParseException {
    WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true));
    assertEquals("POLYGON ((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0))", wkt.toWKT(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))));
    assertEquals(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })), wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1))"));
    assertEquals("POLYGON ((3.0 1.0 5.0, 4.0 2.0 4.0, 5.0 3.0 3.0, 3.0 1.0 5.0))", wkt.toWKT(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 }))));
    assertEquals(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 })), wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3, 3 1 5))"));
    // Auto closing in coerce mode
    assertEquals(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })), wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3))"));
    assertEquals(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 })), wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3))"));
    assertEquals(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }), Collections.singletonList(new LinearRing(new double[] { 0.5, 2.5, 2.0, 0.5 }, new double[] { 1.5, 1.5, 1.0, 1.5 }))), wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))"));
    assertEquals("POLYGON EMPTY", wkt.toWKT(Polygon.EMPTY));
    assertEquals(Polygon.EMPTY, wkt.fromWKT("POLYGON EMPTY)"));
}
Also used : GeographyValidator(org.opensearch.geometry.utils.GeographyValidator) WellKnownText(org.opensearch.geometry.utils.WellKnownText)

Aggregations

WellKnownText (org.opensearch.geometry.utils.WellKnownText)17 GeographyValidator (org.opensearch.geometry.utils.GeographyValidator)15 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 LatLonPoint (org.apache.lucene.document.LatLonPoint)1 OpenSearchException (org.opensearch.OpenSearchException)1 OpenSearchParseException (org.opensearch.OpenSearchParseException)1 EffectivePoint (org.opensearch.common.geo.GeoUtils.EffectivePoint)1 Geometry (org.opensearch.geometry.Geometry)1 Point (org.opensearch.geometry.Point)1