Search in sources :

Example 1 with MultiLine

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

the class MultiLineStringBuilder method buildGeometry.

@Override
public org.opensearch.geometry.Geometry buildGeometry() {
    if (lines.isEmpty()) {
        return MultiLine.EMPTY;
    }
    List<Line> linestrings = new ArrayList<>(lines.size());
    for (int i = 0; i < lines.size(); ++i) {
        LineStringBuilder lsb = lines.get(i);
        linestrings.add(new Line(lsb.coordinates.stream().mapToDouble(c -> c.x).toArray(), lsb.coordinates.stream().mapToDouble(c -> c.y).toArray()));
    }
    return new MultiLine(linestrings);
}
Also used : Line(org.opensearch.geometry.Line) MultiLine(org.opensearch.geometry.MultiLine) StreamInput(org.opensearch.common.io.stream.StreamInput) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) Iterator(java.util.Iterator) ShapeParser(org.opensearch.common.geo.parsers.ShapeParser) GeoShapeType(org.opensearch.common.geo.GeoShapeType) Coordinate(org.locationtech.jts.geom.Coordinate) StreamOutput(org.opensearch.common.io.stream.StreamOutput) IOException(java.io.IOException) ArrayList(java.util.ArrayList) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Objects(java.util.Objects) GeoWKTParser(org.opensearch.common.geo.parsers.GeoWKTParser) LineString(org.locationtech.jts.geom.LineString) List(java.util.List) Geometry(org.locationtech.jts.geom.Geometry) Line(org.opensearch.geometry.Line) MultiLine(org.opensearch.geometry.MultiLine) MultiLine(org.opensearch.geometry.MultiLine) ArrayList(java.util.ArrayList)

Example 2 with MultiLine

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

the class GeometryIndexerTests method testCollection.

public void testCollection() {
    assertEquals(GeometryCollection.EMPTY, indexer.prepareForIndexing(GeometryCollection.EMPTY));
    GeometryCollection<Geometry> collection = new GeometryCollection<>(Collections.singletonList(new Point(2, 1)));
    Geometry indexed = new Point(2, 1);
    assertEquals(indexed, indexer.prepareForIndexing(collection));
    collection = new GeometryCollection<>(Arrays.asList(new Point(2, 1), new Point(4, 3), new Line(new double[] { 160, 200 }, new double[] { 10, 20 })));
    indexed = new GeometryCollection<>(Arrays.asList(new Point(2, 1), new Point(4, 3), new MultiLine(Arrays.asList(new Line(new double[] { 160, 180 }, new double[] { 10, 15 }), new Line(new double[] { -180, -160 }, new double[] { 15, 20 })))));
    assertEquals(indexed, indexer.prepareForIndexing(collection));
}
Also used : Geometry(org.opensearch.geometry.Geometry) GeometryCollection(org.opensearch.geometry.GeometryCollection) MultiLine(org.opensearch.geometry.MultiLine) Line(org.opensearch.geometry.Line) MultiLine(org.opensearch.geometry.MultiLine) MultiPoint(org.opensearch.geometry.MultiPoint) Point(org.opensearch.geometry.Point)

Example 3 with MultiLine

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

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

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

Aggregations

MultiLine (org.opensearch.geometry.MultiLine)11 Line (org.opensearch.geometry.Line)10 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 Geometry (org.opensearch.geometry.Geometry)5 MultiPoint (org.opensearch.geometry.MultiPoint)5 ArrayList (java.util.ArrayList)4 Coordinate (org.locationtech.jts.geom.Coordinate)3 Point (org.opensearch.geometry.Point)3 IOException (java.io.IOException)2 List (java.util.List)2 LineString (org.locationtech.jts.geom.LineString)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)2 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)2 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)2 ParseException (java.text.ParseException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 Objects (java.util.Objects)1