use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.
the class GeoWKTShapeParserTests method testParseMultiLineString.
@Override
public void testParseMultiLineString() throws IOException, ParseException {
int numLineStrings = randomIntBetween(0, 8);
List<LineString> lineStrings = new ArrayList<>(numLineStrings);
MultiLineStringBuilder builder = new MultiLineStringBuilder();
for (int j = 0; j < numLineStrings; ++j) {
List<Coordinate> lsc = randomLineStringCoords();
Coordinate[] coords = lsc.toArray(new Coordinate[lsc.size()]);
lineStrings.add(GEOMETRY_FACTORY.createLineString(coords));
builder.linestring(new LineStringBuilder(lsc));
}
List<Line> lines = new ArrayList<>(lineStrings.size());
for (int j = 0; j < lineStrings.size(); ++j) {
Coordinate[] c = lineStrings.get(j).getCoordinates();
lines.add(new Line(Arrays.stream(c).mapToDouble(i -> i.x).toArray(), Arrays.stream(c).mapToDouble(i -> i.y).toArray()));
}
Geometry expectedGeom;
if (lines.isEmpty()) {
expectedGeom = GeometryCollection.EMPTY;
} else if (lines.size() == 1) {
expectedGeom = new Line(lines.get(0).getX(), lines.get(0).getY());
} else {
expectedGeom = new MultiLine(lines);
}
assertExpected(expectedGeom, builder, false);
assertMalformed(builder);
MultiLineString expected = GEOMETRY_FACTORY.createMultiLineString(lineStrings.toArray(new LineString[lineStrings.size()]));
assumeTrue("JTS test path cannot handle empty multilinestrings", numLineStrings > 1);
assertExpected(jtsGeom(expected), builder, true);
}
use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.
the class GeometryIndexerTests method testRectangle.
public void testRectangle() {
Rectangle indexed = new Rectangle(-179, -178, 10, -10);
Geometry processed = indexer.prepareForIndexing(indexed);
assertEquals(indexed, processed);
// a rectangle is broken into two triangles
List<IndexableField> fields = indexer.indexShape(null, indexed);
assertEquals(fields.size(), 2);
indexed = new Rectangle(179, -179, 10, -10);
processed = indexer.prepareForIndexing(indexed);
assertEquals(indexed, processed);
// a rectangle crossing the dateline is broken into 4 triangles
fields = indexer.indexShape(null, indexed);
assertEquals(fields.size(), 4);
}
use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.
the class GeometryIndexerTests method testLine.
public void testLine() {
Line line = new Line(new double[] { 3, 4 }, new double[] { 1, 2 });
Geometry indexed = line;
assertEquals(indexed, indexer.prepareForIndexing(line));
line = new Line(new double[] { 160, 200 }, new double[] { 10, 20 });
indexed = 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(line));
line = new Line(new double[] { 200, 160 }, new double[] { 10, 20 });
indexed = 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(line));
line = new Line(new double[] { 160, 200, 160 }, new double[] { 0, 10, 20 });
indexed = new MultiLine(Arrays.asList(new Line(new double[] { 160, 180 }, new double[] { 0, 5 }), new Line(new double[] { -180, -160, -180 }, new double[] { 5, 10, 15 }), new Line(new double[] { 180, 160 }, new double[] { 15, 20 })));
assertEquals(indexed, indexer.prepareForIndexing(line));
line = new Line(new double[] { 0, 720 }, new double[] { 0, 20 });
indexed = new MultiLine(Arrays.asList(new Line(new double[] { 0, 180 }, new double[] { 0, 5 }), new Line(new double[] { -180, 180 }, new double[] { 5, 15 }), new Line(new double[] { -180, 0 }, new double[] { 15, 20 })));
assertEquals(indexed, indexer.prepareForIndexing(line));
line = new Line(new double[] { 160, 180, 180, 200, 160, 140 }, new double[] { 0, 10, 20, 30, 30, 40 });
indexed = new MultiLine(Arrays.asList(new Line(new double[] { 160, 180 }, new double[] { 0, 10 }), new Line(new double[] { -180, -180, -160, -180 }, new double[] { 10, 20, 30, 30 }), new Line(new double[] { 180, 160, 140 }, new double[] { 30, 30, 40 })));
assertEquals(indexed, indexer.prepareForIndexing(line));
line = new Line(new double[] { -70, 180, 900 }, new double[] { 0, 0, 4 });
indexed = new MultiLine(Arrays.asList(new Line(new double[] { -70, 180 }, new double[] { 0, 0 }), new Line(new double[] { -180, 180 }, new double[] { 0, 2 }), new Line(new double[] { -180, 180 }, new double[] { 2, 4 })));
assertEquals(indexed, indexer.prepareForIndexing(line));
line = new Line(new double[] { 160, 200, 160, 200, 160, 200 }, new double[] { 0, 10, 20, 30, 40, 50 });
indexed = new MultiLine(Arrays.asList(new Line(new double[] { 160, 180 }, new double[] { 0, 5 }), new Line(new double[] { -180, -160, -180 }, new double[] { 5, 10, 15 }), new Line(new double[] { 180, 160, 180 }, new double[] { 15, 20, 25 }), new Line(new double[] { -180, -160, -180 }, new double[] { 25, 30, 35 }), new Line(new double[] { 180, 160, 180 }, new double[] { 35, 40, 45 }), new Line(new double[] { -180, -160 }, new double[] { 45, 50 })));
assertEquals(indexed, indexer.prepareForIndexing(line));
}
use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.
the class GeoJsonSerializationTests method testToMap.
public void testToMap() throws IOException {
for (int i = 0; i < 10; i++) {
Geometry geometry = GeometryTestUtils.randomGeometry(randomBoolean());
XContentBuilder builder = XContentFactory.jsonBuilder();
GeoJson.toXContent(geometry, builder, ToXContent.EMPTY_PARAMS);
StreamInput input = BytesReference.bytes(builder).streamInput();
try (XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, input)) {
Map<String, Object> map = GeoJson.toMap(geometry);
assertThat(parser.map(), equalTo(map));
}
}
}
use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.
the class GeoBoundingBox method parseBoundingBox.
/**
* Parses the bounding box and returns bottom, top, left, right coordinates
*/
public static GeoBoundingBox parseBoundingBox(XContentParser parser) throws IOException, OpenSearchParseException {
XContentParser.Token token = parser.currentToken();
if (token != XContentParser.Token.START_OBJECT) {
throw new OpenSearchParseException("failed to parse bounding box. Expected start object but found [{}]", token);
}
double top = Double.NaN;
double bottom = Double.NaN;
double left = Double.NaN;
double right = Double.NaN;
String currentFieldName;
GeoPoint sparse = new GeoPoint();
Rectangle envelope = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
token = parser.nextToken();
if (WKT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
try {
Geometry geometry = WKT_PARSER.fromWKT(parser.text());
if (ShapeType.ENVELOPE.equals(geometry.type()) == false) {
throw new OpenSearchParseException("failed to parse WKT bounding box. [" + geometry.type() + "] found. expected [" + ShapeType.ENVELOPE + "]");
}
envelope = (Rectangle) geometry;
} catch (ParseException | IllegalArgumentException e) {
throw new OpenSearchParseException("failed to parse WKT bounding box", e);
}
} else if (TOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
top = parser.doubleValue();
} else if (BOTTOM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
bottom = parser.doubleValue();
} else if (LEFT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
left = parser.doubleValue();
} else if (RIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
right = parser.doubleValue();
} else {
if (TOP_LEFT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.TOP_LEFT);
top = sparse.getLat();
left = sparse.getLon();
} else if (BOTTOM_RIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.BOTTOM_RIGHT);
bottom = sparse.getLat();
right = sparse.getLon();
} else if (TOP_RIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.TOP_RIGHT);
top = sparse.getLat();
right = sparse.getLon();
} else if (BOTTOM_LEFT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.BOTTOM_LEFT);
bottom = sparse.getLat();
left = sparse.getLon();
} else {
throw new OpenSearchParseException("failed to parse bounding box. unexpected field [{}]", currentFieldName);
}
}
} else {
throw new OpenSearchParseException("failed to parse bounding box. field name expected but [{}] found", token);
}
}
if (envelope != null) {
if (Double.isNaN(top) == false || Double.isNaN(bottom) == false || Double.isNaN(left) == false || Double.isNaN(right) == false) {
throw new OpenSearchParseException("failed to parse bounding box. Conflicting definition found " + "using well-known text and explicit corners.");
}
GeoPoint topLeft = new GeoPoint(envelope.getMaxLat(), envelope.getMinLon());
GeoPoint bottomRight = new GeoPoint(envelope.getMinLat(), envelope.getMaxLon());
return new GeoBoundingBox(topLeft, bottomRight);
}
GeoPoint topLeft = new GeoPoint(top, left);
GeoPoint bottomRight = new GeoPoint(bottom, right);
return new GeoBoundingBox(topLeft, bottomRight);
}
Aggregations