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);
}
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));
}
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));
}
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);
}
}
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);
}
Aggregations