use of org.opensearch.geometry.LinearRing in project OpenSearch by opensearch-project.
the class GeoPolygonDecomposer method buildPolygon.
private static Polygon buildPolygon(List<Point[]> polygon) {
List<LinearRing> holes;
Point[] shell = polygon.get(0);
if (polygon.size() > 1) {
holes = new ArrayList<>(polygon.size() - 1);
for (int i = 1; i < polygon.size(); ++i) {
Point[] coords = polygon.get(i);
// We do not have holes on the dateline as they get eliminated
// when breaking the polygon around it.
double[] x = new double[coords.length];
double[] y = new double[coords.length];
for (int c = 0; c < coords.length; ++c) {
x[c] = normalizeLon(coords[c].getX());
y[c] = normalizeLat(coords[c].getY());
}
holes.add(new LinearRing(x, y));
}
} else {
holes = Collections.emptyList();
}
double[] x = new double[shell.length];
double[] y = new double[shell.length];
for (int i = 0; i < shell.length; ++i) {
// Lucene Tessellator treats different +180 and -180 and we should keep the sign.
// normalizeLon method excludes -180.
x[i] = normalizeLonMinus180Inclusive(shell[i].getX());
y[i] = normalizeLat(shell[i].getY());
}
return new Polygon(new LinearRing(x, y), holes);
}
use of org.opensearch.geometry.LinearRing in project OpenSearch by opensearch-project.
the class GeometryParserTests method testGeoJsonParsing.
public void testGeoJsonParsing() throws Exception {
XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Point").startArray("coordinates").value(100.0).value(0.0).endArray().endObject();
try (XContentParser parser = createParser(pointGeoJson)) {
parser.nextToken();
GeometryFormat format = new GeometryParser(true, randomBoolean(), randomBoolean()).geometryFormat(parser);
assertEquals(new Point(100, 0), format.fromXContent(parser));
XContentBuilder newGeoJson = XContentFactory.jsonBuilder();
format.toXContent(new Point(100, 10), newGeoJson, ToXContent.EMPTY_PARAMS);
assertEquals("{\"type\":\"Point\",\"coordinates\":[100.0,10.0]}", Strings.toString(newGeoJson));
}
XContentBuilder pointGeoJsonWithZ = XContentFactory.jsonBuilder().startObject().field("type", "Point").startArray("coordinates").value(100.0).value(0.0).value(10.0).endArray().endObject();
try (XContentParser parser = createParser(pointGeoJsonWithZ)) {
parser.nextToken();
assertEquals(new Point(100, 0, 10.0), new GeometryParser(true, randomBoolean(), true).parse(parser));
}
try (XContentParser parser = createParser(pointGeoJsonWithZ)) {
parser.nextToken();
expectThrows(IllegalArgumentException.class, () -> new GeometryParser(true, randomBoolean(), false).parse(parser));
}
XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(100.0).value(1.0).endArray().startArray().value(101.0).value(1.0).endArray().startArray().value(101.0).value(0.0).endArray().startArray().value(100.0).value(0.0).endArray().endArray().endArray().endObject();
Polygon p = new Polygon(new LinearRing(new double[] { 100d, 101d, 101d, 100d, 100d }, new double[] { 1d, 1d, 0d, 0d, 1d }));
try (XContentParser parser = createParser(polygonGeoJson)) {
parser.nextToken();
// Coerce should automatically close the polygon
assertEquals(p, new GeometryParser(true, true, randomBoolean()).parse(parser));
}
try (XContentParser parser = createParser(polygonGeoJson)) {
parser.nextToken();
// No coerce - the polygon parsing should fail
expectThrows(XContentParseException.class, () -> new GeometryParser(true, false, randomBoolean()).parse(parser));
}
}
use of org.opensearch.geometry.LinearRing in project OpenSearch by opensearch-project.
the class GeoPointShapeQueryTests method testQueryLinearRing.
public void testQueryLinearRing() throws Exception {
XContentBuilder xcb = createDefaultMapping();
client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
ensureGreen();
LinearRing linearRing = new LinearRing(new double[] { -25, -35, -25 }, new double[] { -25, -35, -25 });
try {
// LinearRing extends Line implements Geometry: expose the build process
GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder(defaultGeoFieldName, linearRing);
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE);
searchRequestBuilder.setQuery(queryBuilder);
searchRequestBuilder.setIndices("test");
searchRequestBuilder.get();
} catch (SearchPhaseExecutionException e) {
assertThat(e.getCause().getMessage(), containsString("Field [" + defaultGeoFieldName + "] does not support LINEARRING queries"));
}
}
Aggregations