Search in sources :

Example 6 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoJsonParser method parse.

protected static ShapeBuilder parse(XContentParser parser, AbstractShapeGeometryFieldMapper shapeMapper) throws IOException {
    GeoShapeType shapeType = null;
    DistanceUnit.Distance radius = null;
    CoordinateNode coordinateNode = null;
    GeometryCollectionBuilder geometryCollections = null;
    Orientation orientation = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.ORIENTATION.value() : shapeMapper.orientation();
    Explicit<Boolean> coerce = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.COERCE : shapeMapper.coerce();
    Explicit<Boolean> ignoreZValue = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.IGNORE_Z_VALUE : shapeMapper.ignoreZValue();
    String malformedException = null;
    XContentParser.Token token;
    try (XContentParser subParser = new XContentSubParser(parser)) {
        while ((token = subParser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                String fieldName = subParser.currentName();
                if (ShapeParser.FIELD_TYPE.match(fieldName, subParser.getDeprecationHandler())) {
                    subParser.nextToken();
                    final GeoShapeType type = GeoShapeType.forName(subParser.text());
                    if (shapeType != null && shapeType.equals(type) == false) {
                        malformedException = ShapeParser.FIELD_TYPE + " already parsed as [" + shapeType + "] cannot redefine as [" + type + "]";
                    } else {
                        shapeType = type;
                    }
                } else if (ShapeParser.FIELD_COORDINATES.match(fieldName, subParser.getDeprecationHandler())) {
                    subParser.nextToken();
                    CoordinateNode tempNode = parseCoordinates(subParser, ignoreZValue.value());
                    if (coordinateNode != null && tempNode.numDimensions() != coordinateNode.numDimensions()) {
                        throw new OpenSearchParseException("Exception parsing coordinates: " + "number of dimensions do not match");
                    }
                    coordinateNode = tempNode;
                } else if (ShapeParser.FIELD_GEOMETRIES.match(fieldName, subParser.getDeprecationHandler())) {
                    if (shapeType == null) {
                        shapeType = GeoShapeType.GEOMETRYCOLLECTION;
                    } else if (shapeType.equals(GeoShapeType.GEOMETRYCOLLECTION) == false) {
                        malformedException = "cannot have [" + ShapeParser.FIELD_GEOMETRIES + "] with type set to [" + shapeType + "]";
                    }
                    subParser.nextToken();
                    geometryCollections = parseGeometries(subParser, shapeMapper);
                } else if (CircleBuilder.FIELD_RADIUS.match(fieldName, subParser.getDeprecationHandler())) {
                    if (shapeType == null) {
                        shapeType = GeoShapeType.CIRCLE;
                    } else if (shapeType != null && shapeType.equals(GeoShapeType.CIRCLE) == false) {
                        malformedException = "cannot have [" + CircleBuilder.FIELD_RADIUS + "] with type set to [" + shapeType + "]";
                    }
                    subParser.nextToken();
                    radius = DistanceUnit.Distance.parseDistance(subParser.text());
                } else if (ShapeParser.FIELD_ORIENTATION.match(fieldName, subParser.getDeprecationHandler())) {
                    if (shapeType != null && (shapeType.equals(GeoShapeType.POLYGON) || shapeType.equals(GeoShapeType.MULTIPOLYGON)) == false) {
                        malformedException = "cannot have [" + ShapeParser.FIELD_ORIENTATION + "] with type set to [" + shapeType + "]";
                    }
                    subParser.nextToken();
                    orientation = ShapeBuilder.Orientation.fromString(subParser.text());
                } else {
                    subParser.nextToken();
                    subParser.skipChildren();
                }
            }
        }
    }
    if (malformedException != null) {
        throw new OpenSearchParseException(malformedException);
    } else if (shapeType == null) {
        throw new OpenSearchParseException("shape type not included");
    } else if (coordinateNode == null && GeoShapeType.GEOMETRYCOLLECTION != shapeType) {
        throw new OpenSearchParseException("coordinates not included");
    } else if (geometryCollections == null && GeoShapeType.GEOMETRYCOLLECTION == shapeType) {
        throw new OpenSearchParseException("geometries not included");
    } else if (radius != null && GeoShapeType.CIRCLE != shapeType) {
        throw new OpenSearchParseException("field [{}] is supported for [{}] only", CircleBuilder.FIELD_RADIUS, CircleBuilder.TYPE);
    }
    if (shapeType.equals(GeoShapeType.GEOMETRYCOLLECTION)) {
        return geometryCollections;
    }
    return shapeType.getBuilder(coordinateNode, radius, orientation, coerce.value());
}
Also used : Orientation(org.opensearch.common.geo.builders.ShapeBuilder.Orientation) GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) XContentSubParser(org.opensearch.common.xcontent.XContentSubParser) OpenSearchParseException(org.opensearch.OpenSearchParseException) DistanceUnit(org.opensearch.common.unit.DistanceUnit) GeoShapeType(org.opensearch.common.geo.GeoShapeType) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 7 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoQueryTests method testIndexPointsCircle.

public void testIndexPointsCircle() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate(defaultIndexName).addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultGeoFieldName, "POINT(-30 -30)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultGeoFieldName, "POINT(-45 -50)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    CircleBuilder shape = new CircleBuilder().center(new Coordinate(-30, -30)).radius("100m");
    GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(shape);
    Geometry geometry = builder.buildGeometry().get(0);
    try {
        client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry).relation(ShapeRelation.INTERSECTS)).get();
    } catch (Exception e) {
        assertThat(e.getCause().getMessage(), containsString("failed to create query: " + GeoShapeType.CIRCLE + " geometry is not supported"));
    }
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) Geometry(org.opensearch.geometry.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) CircleBuilder(org.opensearch.common.geo.builders.CircleBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 8 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoQueryTests method testIndexPointsFilterRectangle.

public void testIndexPointsFilterRectangle() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate(defaultIndexName).addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultGeoFieldName, "POINT(-30 -30)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultGeoFieldName, "POINT(-45 -50)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    EnvelopeBuilder shape = new EnvelopeBuilder(new Coordinate(-45, 45), new Coordinate(45, -45));
    GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(shape);
    Geometry geometry = builder.buildGeometry().get(0);
    SearchResponse searchResponse = client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry).relation(ShapeRelation.INTERSECTS)).get();
    assertSearchResponse(searchResponse);
    assertHitCount(searchResponse, 1);
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
    // default query, without specifying relation (expect intersects)
    searchResponse = client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry)).get();
    assertSearchResponse(searchResponse);
    assertHitCount(searchResponse, 1);
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) Geometry(org.opensearch.geometry.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 9 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoQueryTests method testIndexPointsMultiPolygon.

public void testIndexPointsMultiPolygon() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate(defaultIndexName).addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultGeoFieldName, "POINT(-30 -30)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultGeoFieldName, "POINT(-40 -40)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field("name", "Document 3").field(defaultGeoFieldName, "POINT(-50 -50)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    CoordinatesBuilder encloseDocument1Cb = new CoordinatesBuilder();
    encloseDocument1Cb.coordinate(new Coordinate(-35, -35)).coordinate(new Coordinate(-35, -25)).coordinate(new Coordinate(-25, -25)).coordinate(new Coordinate(-25, -35)).coordinate(new Coordinate(-35, -35));
    PolygonBuilder encloseDocument1Shape = new PolygonBuilder(encloseDocument1Cb);
    CoordinatesBuilder encloseDocument2Cb = new CoordinatesBuilder();
    encloseDocument2Cb.coordinate(new Coordinate(-55, -55)).coordinate(new Coordinate(-55, -45)).coordinate(new Coordinate(-45, -45)).coordinate(new Coordinate(-45, -55)).coordinate(new Coordinate(-55, -55));
    PolygonBuilder encloseDocument2Shape = new PolygonBuilder(encloseDocument2Cb);
    MultiPolygonBuilder mp = new MultiPolygonBuilder();
    mp.polygon(encloseDocument1Shape).polygon(encloseDocument2Shape);
    GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(mp);
    Geometry geometry = builder.buildGeometry();
    SearchResponse searchResponse = client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry).relation(ShapeRelation.INTERSECTS)).get();
    assertSearchResponse(searchResponse);
    assertHitCount(searchResponse, 2);
    assertThat(searchResponse.getHits().getAt(0).getId(), not(equalTo("2")));
    assertThat(searchResponse.getHits().getAt(1).getId(), not(equalTo("2")));
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) Geometry(org.opensearch.geometry.Geometry) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 10 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoShapeQueryTests method testQueryRandomGeoCollection.

public void testQueryRandomGeoCollection() throws Exception {
    // Create a random geometry collection.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
    org.apache.lucene.geo.Polygon randomPoly = GeoTestUtil.nextPolygon();
    CoordinatesBuilder cb = new CoordinatesBuilder();
    for (int i = 0; i < randomPoly.numPoints(); ++i) {
        cb.coordinate(randomPoly.getPolyLon(i), randomPoly.getPolyLat(i));
    }
    gcb.shape(new PolygonBuilder(cb));
    XContentBuilder builder = createRandomMapping();
    client().admin().indices().prepareCreate("test").addMapping("type", builder).get();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = (gcb.getShapeAt(gcb.numShapes() - 1));
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", filterShape);
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertSearchResponse(result);
    assumeTrue("Skipping the check for the polygon with a degenerated dimension until " + " https://issues.apache.org/jira/browse/LUCENE-8634 is fixed", randomPoly.maxLat - randomPoly.minLat > 8.4e-8 && randomPoly.maxLon - randomPoly.minLon > 8.4e-8);
    assertHitCount(result, 1);
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) RandomShapeGenerator.xRandomPoint(org.opensearch.test.geo.RandomShapeGenerator.xRandomPoint) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)15 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)11 SearchResponse (org.opensearch.action.search.SearchResponse)10 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)10 Coordinate (org.locationtech.jts.geom.Coordinate)6 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)5 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)5 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)5 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)4 Geometry (org.opensearch.geometry.Geometry)4 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)3 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)3 RandomShapeGenerator.xRandomPoint (org.opensearch.test.geo.RandomShapeGenerator.xRandomPoint)3 OpenSearchParseException (org.opensearch.OpenSearchParseException)2 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)2 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)2 XContentParser (org.opensearch.common.xcontent.XContentParser)2 Point (org.locationtech.spatial4j.shape.Point)1 Rectangle (org.locationtech.spatial4j.shape.Rectangle)1 Shape (org.locationtech.spatial4j.shape.Shape)1