Search in sources :

Example 1 with LegacyGeoShapeFieldMapper

use of org.opensearch.index.mapper.LegacyGeoShapeFieldMapper 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 2 with LegacyGeoShapeFieldMapper

use of org.opensearch.index.mapper.LegacyGeoShapeFieldMapper 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 3 with LegacyGeoShapeFieldMapper

use of org.opensearch.index.mapper.LegacyGeoShapeFieldMapper in project OpenSearch by opensearch-project.

the class GeoJsonShapeParserTests method testParse3DPolygon.

public void testParse3DPolygon() throws IOException, ParseException {
    XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(100.0).value(1.0).value(10.0).endArray().startArray().value(101.0).value(1.0).value(10.0).endArray().startArray().value(101.0).value(0.0).value(10.0).endArray().startArray().value(100.0).value(0.0).value(10.0).endArray().startArray().value(100.0).value(1.0).value(10.0).endArray().endArray().endArray().endObject();
    List<Coordinate> shellCoordinates = new ArrayList<>();
    shellCoordinates.add(new Coordinate(100, 0, 10));
    shellCoordinates.add(new Coordinate(101, 0, 10));
    shellCoordinates.add(new Coordinate(101, 1, 10));
    shellCoordinates.add(new Coordinate(100, 1, 10));
    shellCoordinates.add(new Coordinate(100, 0, 10));
    Coordinate[] coordinates = shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]);
    Version randomVersion = VersionUtils.randomIndexCompatibleVersion(random());
    Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, randomVersion).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    LinearRing shell = GEOMETRY_FACTORY.createLinearRing(shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]));
    Polygon expected = GEOMETRY_FACTORY.createPolygon(shell, null);
    Mapper.BuilderContext mockBuilderContext = new Mapper.BuilderContext(indexSettings, new ContentPath());
    final LegacyGeoShapeFieldMapper mapperBuilder = (LegacyGeoShapeFieldMapper) (new LegacyGeoShapeFieldMapper.Builder("test").ignoreZValue(true).build(mockBuilderContext));
    try (XContentParser parser = createParser(polygonGeoJson)) {
        parser.nextToken();
        OpenSearchGeoAssertions.assertEquals(jtsGeom(expected), ShapeParser.parse(parser, mapperBuilder).buildS4J());
    }
    org.opensearch.geometry.Polygon p = new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(Arrays.stream(coordinates).mapToDouble(i -> i.x).toArray(), Arrays.stream(coordinates).mapToDouble(i -> i.y).toArray()));
    assertGeometryEquals(p, polygonGeoJson, false);
}
Also used : Arrays(java.util.Arrays) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShapeParser(org.opensearch.common.geo.parsers.ShapeParser) LinearRing(org.locationtech.jts.geom.LinearRing) ContentPath(org.opensearch.index.mapper.ContentPath) Coordinate(org.locationtech.jts.geom.Coordinate) Version(org.opensearch.Version) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) Strings(org.opensearch.common.Strings) MultiPoint(org.opensearch.geometry.MultiPoint) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser) Rectangle(org.locationtech.spatial4j.shape.Rectangle) SPATIAL_CONTEXT(org.opensearch.common.geo.builders.ShapeBuilder.SPATIAL_CONTEXT) VersionUtils(org.opensearch.test.VersionUtils) XContentFactory(org.opensearch.common.xcontent.XContentFactory) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) UUIDs(org.opensearch.common.UUIDs) ParseException(java.text.ParseException) ShapeCollection(org.locationtech.spatial4j.shape.ShapeCollection) MultiLine(org.opensearch.geometry.MultiLine) MultiLineString(org.locationtech.jts.geom.MultiLineString) GeometryCollection(org.opensearch.geometry.GeometryCollection) Circle(org.locationtech.spatial4j.shape.Circle) OpenSearchParseException(org.opensearch.OpenSearchParseException) Settings(org.opensearch.common.settings.Settings) Point(org.locationtech.jts.geom.Point) IOException(java.io.IOException) Geometry(org.opensearch.geometry.Geometry) Mapper(org.opensearch.index.mapper.Mapper) Shape(org.locationtech.spatial4j.shape.Shape) GeoShapeIndexer(org.opensearch.index.mapper.GeoShapeIndexer) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) LineString(org.locationtech.jts.geom.LineString) List(java.util.List) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) Polygon(org.locationtech.jts.geom.Polygon) OpenSearchGeoAssertions(org.opensearch.test.hamcrest.OpenSearchGeoAssertions) Line(org.opensearch.geometry.Line) Collections(java.util.Collections) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ArrayList(java.util.ArrayList) ContentPath(org.opensearch.index.mapper.ContentPath) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) Coordinate(org.locationtech.jts.geom.Coordinate) Version(org.opensearch.Version) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Settings(org.opensearch.common.settings.Settings) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 4 with LegacyGeoShapeFieldMapper

use of org.opensearch.index.mapper.LegacyGeoShapeFieldMapper in project OpenSearch by opensearch-project.

the class GeoWKTShapeParserTests method testParseMixedDimensionPolyWithHoleStoredZ.

public void testParseMixedDimensionPolyWithHoleStoredZ() throws IOException {
    List<Coordinate> shellCoordinates = new ArrayList<>();
    shellCoordinates.add(new Coordinate(100, 0));
    shellCoordinates.add(new Coordinate(101, 0));
    shellCoordinates.add(new Coordinate(101, 1));
    shellCoordinates.add(new Coordinate(100, 1));
    shellCoordinates.add(new Coordinate(100, 0));
    // add 3d point to test ISSUE #10501
    List<Coordinate> holeCoordinates = new ArrayList<>();
    holeCoordinates.add(new Coordinate(100.2, 0.2, 15.0));
    holeCoordinates.add(new Coordinate(100.8, 0.2));
    holeCoordinates.add(new Coordinate(100.8, 0.8));
    holeCoordinates.add(new Coordinate(100.2, 0.8, 10.0));
    holeCoordinates.add(new Coordinate(100.2, 0.2));
    PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoordinates));
    builder.hole(new LineStringBuilder(holeCoordinates));
    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));
    // test store z disabled
    OpenSearchException e = expectThrows(OpenSearchException.class, () -> ShapeParser.parse(parser, mapperBuilder));
    assertThat(e, hasToString(containsString("unable to add coordinate to CoordinateBuilder: coordinate dimensions do not match")));
}
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) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) 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) OpenSearchException(org.opensearch.OpenSearchException) 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)

Aggregations

Settings (org.opensearch.common.settings.Settings)4 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)4 XContentParser (org.opensearch.common.xcontent.XContentParser)4 ContentPath (org.opensearch.index.mapper.ContentPath)4 LegacyGeoShapeFieldMapper (org.opensearch.index.mapper.LegacyGeoShapeFieldMapper)4 Mapper (org.opensearch.index.mapper.Mapper)4 ArrayList (java.util.ArrayList)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)3 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)3 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)3 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)3 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)3 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)3 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)3 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)3 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)3 GeoShapeFieldMapper (org.opensearch.index.mapper.GeoShapeFieldMapper)3 LineString (org.locationtech.jts.geom.LineString)2