Search in sources :

Example 1 with Circle

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);
    }
}
Also used : GeometryCollection(org.opensearch.geometry.GeometryCollection) Circle(org.opensearch.geometry.Circle) OpenSearchParseException(org.opensearch.OpenSearchParseException) ShapeType(org.opensearch.geometry.ShapeType) MultiPoint(org.opensearch.geometry.MultiPoint) Point(org.opensearch.geometry.Point)

Example 2 with Circle

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));
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) Circle(org.opensearch.geometry.Circle) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with Circle

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;
}
Also used : Circle(org.opensearch.geometry.Circle)

Example 4 with 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());
    }
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) Circle(org.opensearch.geometry.Circle) OpenSearchException(org.opensearch.OpenSearchException) ClusterUpdateSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with Circle

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);
}
Also used : Circle(org.opensearch.geometry.Circle) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

Circle (org.opensearch.geometry.Circle)6 SearchResponse (org.opensearch.action.search.SearchResponse)2 ToXContent (org.opensearch.common.xcontent.ToXContent)2 OpenSearchException (org.opensearch.OpenSearchException)1 OpenSearchParseException (org.opensearch.OpenSearchParseException)1 ClusterUpdateSettingsRequest (org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest)1 DistanceUnit (org.opensearch.common.unit.DistanceUnit)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 GeometryCollection (org.opensearch.geometry.GeometryCollection)1 MultiPoint (org.opensearch.geometry.MultiPoint)1 Point (org.opensearch.geometry.Point)1 ShapeType (org.opensearch.geometry.ShapeType)1