Search in sources :

Example 6 with Line

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

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

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

the class GeoJsonShapeParserTests method testParseMultiLineString.

@Override
public void testParseMultiLineString() throws IOException, ParseException {
    XContentBuilder multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "MultiLineString").startArray("coordinates").startArray().startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().endArray().startArray().startArray().value(102.0).value(2.0).endArray().startArray().value(103.0).value(3.0).endArray().endArray().endArray().endObject();
    MultiLineString expected = GEOMETRY_FACTORY.createMultiLineString(new LineString[] { GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(100, 0), new Coordinate(101, 1) }), GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(102, 2), new Coordinate(103, 3) }) });
    assertGeometryEquals(jtsGeom(expected), multilinesGeoJson, true);
    assertGeometryEquals(new MultiLine(Arrays.asList(new Line(new double[] { 100d, 101d }, new double[] { 0d, 1d }), new Line(new double[] { 102d, 103d }, new double[] { 2d, 3d }))), multilinesGeoJson, false);
}
Also used : MultiLine(org.opensearch.geometry.MultiLine) MultiLine(org.opensearch.geometry.MultiLine) Line(org.opensearch.geometry.Line) MultiLineString(org.locationtech.jts.geom.MultiLineString) Coordinate(org.locationtech.jts.geom.Coordinate) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 9 with Line

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

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

Line (org.opensearch.geometry.Line)19 MultiLine (org.opensearch.geometry.MultiLine)17 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 MultiPoint (org.opensearch.geometry.MultiPoint)9 Point (org.opensearch.geometry.Point)8 Geometry (org.opensearch.geometry.Geometry)7 ArrayList (java.util.ArrayList)5 Coordinate (org.locationtech.jts.geom.Coordinate)5 LineString (org.locationtech.jts.geom.LineString)4 MultiLineString (org.locationtech.jts.geom.MultiLineString)4 Point (org.locationtech.jts.geom.Point)3 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)3 GeometryCollection (org.opensearch.geometry.GeometryCollection)3 IOException (java.io.IOException)2 List (java.util.List)2 LinearRing (org.locationtech.jts.geom.LinearRing)2 Polygon (org.locationtech.jts.geom.Polygon)2 Shape (org.locationtech.spatial4j.shape.Shape)2 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)2 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)2