Search in sources :

Example 1 with Polygon

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

the class WellKnownText method parsePolygon.

private Polygon parsePolygon(StreamTokenizer stream) throws IOException, ParseException {
    if (nextEmptyOrOpen(stream).equals(EMPTY)) {
        return Polygon.EMPTY;
    }
    nextOpener(stream);
    ArrayList<Double> lats = new ArrayList<>();
    ArrayList<Double> lons = new ArrayList<>();
    ArrayList<Double> alts = new ArrayList<>();
    parseCoordinates(stream, lats, lons, alts);
    ArrayList<LinearRing> holes = new ArrayList<>();
    while (nextCloserOrComma(stream).equals(COMMA)) {
        holes.add(parsePolygonHole(stream));
    }
    closeLinearRingIfCoerced(lats, lons, alts);
    LinearRing shell;
    if (alts.isEmpty()) {
        shell = new LinearRing(toArray(lons), toArray(lats));
    } else {
        shell = new LinearRing(toArray(lons), toArray(lats), toArray(alts));
    }
    if (holes.isEmpty()) {
        return new Polygon(shell);
    } else {
        return new Polygon(shell, Collections.unmodifiableList(holes));
    }
}
Also used : ArrayList(java.util.ArrayList) LinearRing(org.opensearch.geometry.LinearRing) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon)

Example 2 with Polygon

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

the class GeometryIO method readPolygon.

private static Polygon readPolygon(StreamInput in) throws IOException {
    double[][] shellComponents = readLineComponents(in);
    boolean orientation = in.readBoolean();
    LinearRing shell = buildLinearRing(shellComponents, orientation);
    int numberOfHoles = in.readVInt();
    if (numberOfHoles > 0) {
        List<LinearRing> holes = new ArrayList<>(numberOfHoles);
        for (int i = 0; i < numberOfHoles; i++) {
            holes.add(buildLinearRing(readLineComponents(in), orientation));
        }
        return new Polygon(shell, holes);
    } else {
        return new Polygon(shell);
    }
}
Also used : ArrayList(java.util.ArrayList) LinearRing(org.opensearch.geometry.LinearRing) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint)

Example 3 with Polygon

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

the class GeometryIO method readMultiPolygon.

private static MultiPolygon readMultiPolygon(StreamInput in) throws IOException {
    // orientation for BWC
    in.readBoolean();
    int size = in.readVInt();
    List<Polygon> polygons = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        polygons.add(readPolygon(in));
    }
    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)

Example 4 with Polygon

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

the class GeometryParserTests method testBasics.

public void testBasics() {
    GeometryParser parser = new GeometryParser(true, randomBoolean(), randomBoolean());
    // point
    Point expectedPoint = new Point(-122.084110, 37.386637);
    testBasics(parser, mapOf("lat", 37.386637, "lon", -122.084110), expectedPoint);
    testBasics(parser, "37.386637, -122.084110", expectedPoint);
    testBasics(parser, "POINT (-122.084110 37.386637)", expectedPoint);
    testBasics(parser, Arrays.asList(-122.084110, 37.386637), expectedPoint);
    testBasics(parser, mapOf("type", "Point", "coordinates", Arrays.asList(-122.084110, 37.386637)), expectedPoint);
    // line
    Line expectedLine = new Line(new double[] { 0, 1 }, new double[] { 0, 1 });
    testBasics(parser, "LINESTRING(0 0, 1 1)", expectedLine);
    testBasics(parser, mapOf("type", "LineString", "coordinates", Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 1))), expectedLine);
    // polygon
    Polygon expectedPolygon = new Polygon(new LinearRing(new double[] { 0, 1, 1, 0, 0 }, new double[] { 0, 0, 1, 1, 0 }));
    testBasics(parser, "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))", expectedPolygon);
    testBasics(parser, mapOf("type", "Polygon", "coordinates", Arrays.asList(Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 0), Arrays.asList(1, 1), Arrays.asList(0, 1), Arrays.asList(0, 0)))), expectedPolygon);
    // geometry collection
    testBasics(parser, Arrays.asList(Arrays.asList(-122.084110, 37.386637), "37.386637, -122.084110", "POINT (-122.084110 37.386637)", mapOf("type", "Point", "coordinates", Arrays.asList(-122.084110, 37.386637)), mapOf("type", "LineString", "coordinates", Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 1))), "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))"), new GeometryCollection<>(Arrays.asList(expectedPoint, expectedPoint, expectedPoint, expectedPoint, expectedLine, expectedPolygon)));
    expectThrows(OpenSearchParseException.class, () -> testBasics(parser, "not a geometry", null));
}
Also used : Line(org.opensearch.geometry.Line) Point(org.opensearch.geometry.Point) Polygon(org.opensearch.geometry.Polygon) LinearRing(org.opensearch.geometry.LinearRing)

Example 5 with Polygon

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

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