Search in sources :

Example 6 with Geometry

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

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

the class GeometryIndexerTests method testMultiLine.

public void testMultiLine() {
    Line line = new Line(new double[] { 3, 4 }, new double[] { 1, 2 });
    MultiLine multiLine = new MultiLine(Collections.singletonList(line));
    Geometry indexed = line;
    assertEquals(indexed, indexer.prepareForIndexing(multiLine));
    multiLine = new MultiLine(Arrays.asList(line, new Line(new double[] { 160, 200 }, new double[] { 10, 20 })));
    indexed = new MultiLine(Arrays.asList(line, new Line(new double[] { 160, 180 }, new double[] { 10, 15 }), new Line(new double[] { -180, -160 }, new double[] { 15, 20 })));
    assertEquals(indexed, indexer.prepareForIndexing(multiLine));
}
Also used : MultiLine(org.opensearch.geometry.MultiLine) Line(org.opensearch.geometry.Line) MultiLine(org.opensearch.geometry.MultiLine) Geometry(org.opensearch.geometry.Geometry)

Example 8 with Geometry

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

the class GeometryIndexerTests method testRandomLine.

/**
 * A randomized test that generates a random lines crossing anti-merdian and checks that the decomposed segments of this line
 * have the same total length (measured using Euclidean distances between neighboring points) as the original line.
 *
 * It also extracts all points from these lines, performs normalization of these points and then compares that the resulting
 * points of line normalization match the points of points normalization with the exception of points that were created on the
 * antimeridian as the result of line decomposition.
 */
public void testRandomLine() {
    int size = randomIntBetween(2, 20);
    int shift = randomIntBetween(-2, 2);
    double[] originalLats = new double[size];
    double[] originalLons = new double[size];
    // Generate a random line that goes over poles and stretches beyond -180 and +180
    for (int i = 0; i < size; i++) {
        // from time to time go over poles
        originalLats[i] = randomInt(4) == 0 ? GeometryTestUtils.randomLat() : GeometryTestUtils.randomLon();
        originalLons[i] = GeometryTestUtils.randomLon() + shift * 360;
        if (randomInt(3) == 0) {
            shift += randomFrom(-2, -1, 1, 2);
        }
    }
    Line original = new Line(originalLons, originalLats);
    // Check that the length of original and decomposed lines is the same
    Geometry decomposed = indexer.prepareForIndexing(original);
    double decomposedLength = 0;
    if (decomposed instanceof Line) {
        decomposedLength = length((Line) decomposed);
    } else {
        assertThat(decomposed, instanceOf(MultiLine.class));
        MultiLine lines = (MultiLine) decomposed;
        for (int i = 0; i < lines.size(); i++) {
            decomposedLength += length(lines.get(i));
        }
    }
    assertEquals("Different Lengths between " + original + " and " + decomposed, length(original), decomposedLength, 0.001);
    // Check that normalized linestring generates the same points as the normalized multipoint based on the same set of points
    MultiPoint decomposedViaLines = remove180s(GeometryTestUtils.toMultiPoint(decomposed));
    MultiPoint originalPoints = GeometryTestUtils.toMultiPoint(original);
    MultiPoint decomposedViaPoint = remove180s(GeometryTestUtils.toMultiPoint(indexer.prepareForIndexing(originalPoints)));
    assertEquals(decomposedViaPoint.size(), decomposedViaLines.size());
    for (int i = 0; i < decomposedViaPoint.size(); i++) {
        assertEquals("Difference between decomposing lines " + decomposedViaLines + " and points " + decomposedViaPoint + " at the position " + i, decomposedViaPoint.get(i).getLat(), decomposedViaLines.get(i).getLat(), 0.0001);
        assertEquals("Difference between decomposing lines " + decomposedViaLines + " and points " + decomposedViaPoint + " at the position " + i, decomposedViaPoint.get(i).getLon(), decomposedViaLines.get(i).getLon(), 0.0001);
    }
}
Also used : MultiLine(org.opensearch.geometry.MultiLine) Line(org.opensearch.geometry.Line) Geometry(org.opensearch.geometry.Geometry) MultiLine(org.opensearch.geometry.MultiLine) MultiPoint(org.opensearch.geometry.MultiPoint) MultiPoint(org.opensearch.geometry.MultiPoint) Point(org.opensearch.geometry.Point)

Example 9 with Geometry

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

the class GeoJsonShapeParserTests method testParseGeometryCollection.

@Override
public void testParseGeometryCollection() throws IOException, ParseException {
    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();
    ArrayList<Coordinate> shellCoordinates1 = new ArrayList<>();
    shellCoordinates1.add(new Coordinate(180.0, -12.142857142857142));
    shellCoordinates1.add(new Coordinate(180.0, 12.142857142857142));
    shellCoordinates1.add(new Coordinate(176.0, 15.0));
    shellCoordinates1.add(new Coordinate(172.0, 0.0));
    shellCoordinates1.add(new Coordinate(176.0, -15));
    shellCoordinates1.add(new Coordinate(180.0, -12.142857142857142));
    ArrayList<Coordinate> shellCoordinates2 = new ArrayList<>();
    shellCoordinates2.add(new Coordinate(-180.0, 12.142857142857142));
    shellCoordinates2.add(new Coordinate(-180.0, -12.142857142857142));
    shellCoordinates2.add(new Coordinate(-177.0, -10.0));
    shellCoordinates2.add(new Coordinate(-177.0, 10.0));
    shellCoordinates2.add(new Coordinate(-180.0, 12.142857142857142));
    Shape[] expected = new Shape[3];
    LineString expectedLineString = GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(100, 0), new Coordinate(101, 1) });
    expected[0] = jtsGeom(expectedLineString);
    Point expectedPoint = GEOMETRY_FACTORY.createPoint(new Coordinate(102.0, 2.0));
    expected[1] = new JtsPoint(expectedPoint, SPATIAL_CONTEXT);
    LinearRing shell1 = GEOMETRY_FACTORY.createLinearRing(shellCoordinates1.toArray(new Coordinate[shellCoordinates1.size()]));
    LinearRing shell2 = GEOMETRY_FACTORY.createLinearRing(shellCoordinates2.toArray(new Coordinate[shellCoordinates2.size()]));
    MultiPolygon expectedMultiPoly = GEOMETRY_FACTORY.createMultiPolygon(new Polygon[] { GEOMETRY_FACTORY.createPolygon(shell1), GEOMETRY_FACTORY.createPolygon(shell2) });
    expected[2] = jtsGeom(expectedMultiPoly);
    // equals returns true only if geometries are in the same order
    assertGeometryEquals(shapeCollection(expected), geometryCollectionGeoJson, true);
    GeometryCollection<Geometry> geometryExpected = new GeometryCollection<>(Arrays.asList(new Line(new double[] { 100d, 101d }, new double[] { 0d, 1d }), new org.opensearch.geometry.Point(102d, 2d), new org.opensearch.geometry.MultiPolygon(Arrays.asList(new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(new double[] { 180d, 180d, 176d, 172d, 176d, 180d }, new double[] { -12.142857142857142d, 12.142857142857142d, 15d, 0d, -15d, -12.142857142857142d })), new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(new double[] { -180d, -180d, -177d, -177d, -180d }, new double[] { 12.142857142857142d, -12.142857142857142d, -10d, 10d, 12.142857142857142d }))))));
    assertGeometryEquals(geometryExpected, geometryCollectionGeoJson, false);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) ArrayList(java.util.ArrayList) MultiPoint(org.opensearch.geometry.MultiPoint) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.jts.geom.Point) Geometry(org.opensearch.geometry.Geometry) GeometryCollection(org.opensearch.geometry.GeometryCollection) MultiLine(org.opensearch.geometry.MultiLine) Line(org.opensearch.geometry.Line) Coordinate(org.locationtech.jts.geom.Coordinate) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 10 with Geometry

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

Aggregations

Geometry (org.opensearch.geometry.Geometry)28 MultiPoint (org.opensearch.geometry.MultiPoint)10 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 Point (org.opensearch.geometry.Point)9 Coordinate (org.locationtech.jts.geom.Coordinate)7 GeometryCollection (org.opensearch.geometry.GeometryCollection)7 Line (org.opensearch.geometry.Line)7 MultiLine (org.opensearch.geometry.MultiLine)7 ArrayList (java.util.ArrayList)6 XContentParser (org.opensearch.common.xcontent.XContentParser)6 IOException (java.io.IOException)5 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)5 ParseException (java.text.ParseException)4 OpenSearchParseException (org.opensearch.OpenSearchParseException)4 Point (org.locationtech.jts.geom.Point)3 Shape (org.locationtech.spatial4j.shape.Shape)3 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)3