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