Search in sources :

Example 1 with ShapeBuilder

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

the class GeoWKTParser method parseExpectedType.

/**
 * throws an exception if the parsed geometry type does not match the expected shape type
 */
public static ShapeBuilder parseExpectedType(XContentParser parser, final GeoShapeType shapeType, final AbstractShapeGeometryFieldMapper shapeMapper) throws IOException, OpenSearchParseException {
    try (StringReader reader = new StringReader(parser.text())) {
        Explicit<Boolean> ignoreZValue = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.IGNORE_Z_VALUE : shapeMapper.ignoreZValue();
        Explicit<Boolean> coerce = (shapeMapper == null) ? AbstractShapeGeometryFieldMapper.Defaults.COERCE : shapeMapper.coerce();
        // setup the tokenizer; configured to read words w/o numbers
        StreamTokenizer tokenizer = new StreamTokenizer(reader);
        tokenizer.resetSyntax();
        tokenizer.wordChars('a', 'z');
        tokenizer.wordChars('A', 'Z');
        tokenizer.wordChars(128 + 32, 255);
        tokenizer.wordChars('0', '9');
        tokenizer.wordChars('-', '-');
        tokenizer.wordChars('+', '+');
        tokenizer.wordChars('.', '.');
        tokenizer.whitespaceChars(0, ' ');
        tokenizer.commentChar('#');
        ShapeBuilder builder = parseGeometry(tokenizer, shapeType, ignoreZValue.value(), coerce.value());
        checkEOF(tokenizer);
        return builder;
    }
}
Also used : ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) StringReader(java.io.StringReader) StreamTokenizer(java.io.StreamTokenizer)

Example 2 with ShapeBuilder

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

the class GeoWKTShapeParserTests method testParsePolyWithStoredZ.

public void testParsePolyWithStoredZ() throws IOException {
    List<Coordinate> shellCoordinates = new ArrayList<>();
    shellCoordinates.add(new Coordinate(100, 0, 0));
    shellCoordinates.add(new Coordinate(101, 0, 0));
    shellCoordinates.add(new Coordinate(101, 1, 0));
    shellCoordinates.add(new Coordinate(100, 1, 5));
    shellCoordinates.add(new Coordinate(100, 0, 5));
    PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoordinates));
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().value(builder.toWKT());
    XContentParser parser = createParser(xContentBuilder);
    parser.nextToken();
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    Mapper.BuilderContext mockBuilderContext = new Mapper.BuilderContext(indexSettings, new ContentPath());
    final LegacyGeoShapeFieldMapper mapperBuilder = (LegacyGeoShapeFieldMapper) (new LegacyGeoShapeFieldMapper.Builder("test").ignoreZValue(true).build(mockBuilderContext));
    ShapeBuilder<?, ?, ?> shapeBuilder = ShapeParser.parse(parser, mapperBuilder);
    assertEquals(shapeBuilder.numDimensions(), 3);
}
Also used : MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ArrayList(java.util.ArrayList) ContentPath(org.opensearch.index.mapper.ContentPath) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) GeoShapeFieldMapper(org.opensearch.index.mapper.GeoShapeFieldMapper) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) Coordinate(org.locationtech.jts.geom.Coordinate) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) Settings(org.opensearch.common.settings.Settings)

Example 3 with ShapeBuilder

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

the class GeoWKTShapeParserTests method testParseOpenPolygon.

public void testParseOpenPolygon() throws IOException {
    String openPolygon = "POLYGON ((100 5, 100 10, 90 10, 90 5))";
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().value(openPolygon);
    XContentParser parser = createParser(xContentBuilder);
    parser.nextToken();
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    Mapper.BuilderContext mockBuilderContext = new Mapper.BuilderContext(indexSettings, new ContentPath());
    final LegacyGeoShapeFieldMapper defaultMapperBuilder = (LegacyGeoShapeFieldMapper) (new LegacyGeoShapeFieldMapper.Builder("test").coerce(false).build(mockBuilderContext));
    OpenSearchParseException exception = expectThrows(OpenSearchParseException.class, () -> ShapeParser.parse(parser, defaultMapperBuilder));
    assertEquals("invalid LinearRing found (coordinates are not closed)", exception.getMessage());
    final LegacyGeoShapeFieldMapper coercingMapperBuilder = (LegacyGeoShapeFieldMapper) (new LegacyGeoShapeFieldMapper.Builder("test").coerce(true).build(mockBuilderContext));
    ShapeBuilder<?, ?, ?> shapeBuilder = ShapeParser.parse(parser, coercingMapperBuilder);
    assertNotNull(shapeBuilder);
    assertEquals("polygon ((100.0 5.0, 100.0 10.0, 90.0 10.0, 90.0 5.0, 100.0 5.0))", shapeBuilder.toWKT());
}
Also used : GeoShapeFieldMapper(org.opensearch.index.mapper.GeoShapeFieldMapper) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) OpenSearchParseException(org.opensearch.OpenSearchParseException) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Matchers.hasToString(org.hamcrest.Matchers.hasToString) MultiLineString(org.locationtech.jts.geom.MultiLineString) Matchers.containsString(org.hamcrest.Matchers.containsString) LineString(org.locationtech.jts.geom.LineString) ContentPath(org.opensearch.index.mapper.ContentPath) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) Settings(org.opensearch.common.settings.Settings)

Example 4 with ShapeBuilder

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

the class GeoShapeQueryTests method testIndexRectangleSpanningDateLine.

public void testIndexRectangleSpanningDateLine() throws Exception {
    String mapping = Strings.toString(createRandomMapping());
    client().admin().indices().prepareCreate("test").addMapping("type1", mapping, XContentType.JSON).get();
    ensureGreen();
    EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder(new Coordinate(178, 10), new Coordinate(-178, -10));
    XContentBuilder docSource = envelopeBuilder.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = new PointBuilder(179, 0);
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", filterShape);
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
}
Also used : ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with ShapeBuilder

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

the class GeoShapeQueryTests method testPointsOnlyExplicit.

public void testPointsOnlyExplicit() throws Exception {
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("geo").field("type", "geo_shape").field("tree", randomBoolean() ? "quadtree" : "geohash").field("tree_levels", "6").field("distance_error_pct", "0.01").field("points_only", true).endObject().endObject().endObject().endObject());
    client().admin().indices().prepareCreate("geo_points_only").addMapping("type1", mapping, XContentType.JSON).get();
    ensureGreen();
    // MULTIPOINT
    ShapeBuilder shape = RandomShapeGenerator.createShape(random(), RandomShapeGenerator.ShapeType.MULTIPOINT);
    client().prepareIndex("geo_points_only").setId("1").setSource(jsonBuilder().startObject().field("geo", shape).endObject()).setRefreshPolicy(IMMEDIATE).get();
    // POINT
    shape = RandomShapeGenerator.createShape(random(), RandomShapeGenerator.ShapeType.POINT);
    client().prepareIndex("geo_points_only").setId("2").setSource(jsonBuilder().startObject().field("geo", shape).endObject()).setRefreshPolicy(IMMEDIATE).get();
    // test that point was inserted
    SearchResponse response = client().prepareSearch("geo_points_only").setQuery(matchAllQuery()).get();
    assertHitCount(response, 2);
}
Also used : ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)13 SearchResponse (org.opensearch.action.search.SearchResponse)6 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)6 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)6 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)6 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Coordinate (org.locationtech.jts.geom.Coordinate)4 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)4 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)4 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)4 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)4 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)3 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)3 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)2 Point (org.locationtech.spatial4j.shape.Point)2 OpenSearchParseException (org.opensearch.OpenSearchParseException)2 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)2