use of org.opensearch.geometry.Circle in project OpenSearch by opensearch-project.
the class GeoJson method createGeometry.
private static Geometry createGeometry(String type, List<Geometry> geometries, CoordinateNode coordinates, Boolean orientation, boolean defaultOrientation, boolean coerce, DistanceUnit.Distance radius) {
ShapeType shapeType;
if ("bbox".equalsIgnoreCase(type)) {
shapeType = ShapeType.ENVELOPE;
} else {
shapeType = ShapeType.forName(type);
}
if (shapeType == ShapeType.GEOMETRYCOLLECTION) {
if (geometries == null) {
throw new OpenSearchParseException("geometries not included");
}
if (coordinates != null) {
throw new OpenSearchParseException("parameter coordinates is not supported for type " + type);
}
verifyNulls(type, null, orientation, radius);
return new GeometryCollection<>(geometries);
}
// We expect to have coordinates for all the rest
if (coordinates == null) {
throw new OpenSearchParseException("coordinates not included");
}
switch(shapeType) {
case CIRCLE:
if (radius == null) {
throw new OpenSearchParseException("radius is not specified");
}
verifyNulls(type, geometries, orientation, null);
Point point = coordinates.asPoint();
return new Circle(point.getX(), point.getY(), point.getZ(), radius.convert(DistanceUnit.METERS).value);
case POINT:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asPoint();
case MULTIPOINT:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asMultiPoint();
case LINESTRING:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asLineString(coerce);
case MULTILINESTRING:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asMultiLineString(coerce);
case POLYGON:
verifyNulls(type, geometries, null, radius);
// handle possible null in orientation
return coordinates.asPolygon(orientation != null ? orientation : defaultOrientation, coerce);
case MULTIPOLYGON:
verifyNulls(type, geometries, null, radius);
// handle possible null in orientation
return coordinates.asMultiPolygon(orientation != null ? orientation : defaultOrientation, coerce);
case ENVELOPE:
verifyNulls(type, geometries, orientation, radius);
return coordinates.asRectangle();
default:
throw new OpenSearchParseException("unsupported shape type " + type);
}
}
use of org.opensearch.geometry.Circle in project OpenSearch by opensearch-project.
the class LegacyGeoShapeIntegrationIT method testLegacyCircle.
/**
* Test that the circle is still supported for the legacy shapes
*/
public void testLegacyCircle() throws Exception {
// create index
assertAcked(client().admin().indices().prepareCreate("test").addMapping("geometry", "shape", "type=geo_shape,strategy=recursive,tree=geohash").get());
ensureGreen();
indexRandom(true, client().prepareIndex("test").setId("0").setSource("shape", (ToXContent) (builder, params) -> {
builder.startObject().field("type", "circle").startArray("coordinates").value(30).value(50).endArray().field("radius", "77km").endObject();
return builder;
}));
// test self crossing of circles
SearchResponse searchResponse = client().prepareSearch("test").setQuery(geoShapeQuery("shape", new Circle(30, 50, 77000))).get();
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
}
use of org.opensearch.geometry.Circle in project OpenSearch by opensearch-project.
the class WellKnownText method parseCircle.
private Circle parseCircle(StreamTokenizer stream) throws IOException, ParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return Circle.EMPTY;
}
double lon = nextNumber(stream);
double lat = nextNumber(stream);
double radius = nextNumber(stream);
double alt = Double.NaN;
if (isNumberNext(stream)) {
alt = nextNumber(stream);
}
Circle circle = new Circle(lon, lat, alt, radius);
nextCloser(stream);
return circle;
}
use of org.opensearch.geometry.Circle in project OpenSearch by opensearch-project.
the class LegacyGeoShapeIntegrationIT method testDisallowExpensiveQueries.
public void testDisallowExpensiveQueries() throws InterruptedException, IOException {
try {
// create index
assertAcked(client().admin().indices().prepareCreate("test").addMapping("_doc", "shape", "type=geo_shape,strategy=recursive,tree=geohash").get());
ensureGreen();
indexRandom(true, client().prepareIndex("test").setId("0").setSource("shape", (ToXContent) (builder, params) -> {
builder.startObject().field("type", "circle").startArray("coordinates").value(30).value(50).endArray().field("radius", "77km").endObject();
return builder;
}));
refresh();
// Execute with search.allow_expensive_queries = null => default value = false => success
SearchResponse searchResponse = client().prepareSearch("test").setQuery(geoShapeQuery("shape", new Circle(30, 50, 77000))).get();
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest();
updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false));
assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());
// Set search.allow_expensive_queries to "false" => assert failure
OpenSearchException e = expectThrows(OpenSearchException.class, () -> client().prepareSearch("test").setQuery(geoShapeQuery("shape", new Circle(30, 50, 77000))).get());
assertEquals("[geo-shape] queries on [PrefixTree geo shapes] cannot be executed when " + "'search.allow_expensive_queries' is set to false.", e.getCause().getMessage());
// Set search.allow_expensive_queries to "true" => success
updateSettingsRequest = new ClusterUpdateSettingsRequest();
updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true));
assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());
searchResponse = client().prepareSearch("test").setQuery(geoShapeQuery("shape", new Circle(30, 50, 77000))).get();
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
} finally {
ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest();
updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null));
assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());
}
}
use of org.opensearch.geometry.Circle in project OpenSearch by opensearch-project.
the class GeoJsonParserTests method testParseCircle.
public void testParseCircle() throws IOException {
XContentBuilder multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "circle").startArray("coordinates").value(100.0).value(0.0).endArray().field("radius", "200m").endObject();
Circle expected = new Circle(100.0, 0.0, 200);
assertGeometryEquals(expected, multilinesGeoJson);
}
Aggregations