Search in sources :

Example 11 with CoordinatesBuilder

use of org.opensearch.common.geo.builders.CoordinatesBuilder 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 12 with CoordinatesBuilder

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

the class GeoWKTShapeParserTests method testParseMixedDimensionPolyWithHole.

public void testParseMixedDimensionPolyWithHole() throws IOException, ParseException {
    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 GeoShapeFieldMapper mapperBuilder = (GeoShapeFieldMapper) (new GeoShapeFieldMapper.Builder("test").ignoreZValue(false).build(mockBuilderContext));
    // test store z disabled
    OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> ShapeParser.parse(parser, mapperBuilder));
    assertThat(e, hasToString(containsString("but [ignore_z_value] parameter is [false]")));
}
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) OpenSearchParseException(org.opensearch.OpenSearchParseException) Coordinate(org.locationtech.jts.geom.Coordinate) GeoShapeFieldMapper(org.opensearch.index.mapper.GeoShapeFieldMapper) 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 13 with CoordinatesBuilder

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

the class GeoWKTShapeParserTests method testParsePolygonWithHole.

public void testParsePolygonWithHole() throws IOException, ParseException {
    // add 3d point to test ISSUE #10501
    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));
    List<Coordinate> holeCoordinates = new ArrayList<>();
    holeCoordinates.add(new Coordinate(100.2, 0.2));
    holeCoordinates.add(new Coordinate(100.8, 0.2));
    holeCoordinates.add(new Coordinate(100.8, 0.8));
    holeCoordinates.add(new Coordinate(100.2, 0.8));
    holeCoordinates.add(new Coordinate(100.2, 0.2));
    PolygonBuilder polygonWithHole = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoordinates));
    polygonWithHole.hole(new LineStringBuilder(holeCoordinates));
    LinearRing shell = GEOMETRY_FACTORY.createLinearRing(shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]));
    LinearRing[] holes = new LinearRing[1];
    holes[0] = GEOMETRY_FACTORY.createLinearRing(holeCoordinates.toArray(new Coordinate[holeCoordinates.size()]));
    Polygon expected = GEOMETRY_FACTORY.createPolygon(shell, holes);
    assertExpected(jtsGeom(expected), polygonWithHole, true);
    org.opensearch.geometry.LinearRing hole = new org.opensearch.geometry.LinearRing(new double[] { 100.2d, 100.8d, 100.8d, 100.2d, 100.2d }, new double[] { 0.8d, 0.8d, 0.2d, 0.2d, 0.8d });
    org.opensearch.geometry.Polygon p = new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(new double[] { 101d, 101d, 100d, 100d, 101d }, new double[] { 0d, 1d, 1d, 0d, 0d }), Collections.singletonList(hole));
    assertExpected(p, polygonWithHole, false);
    assertMalformed(polygonWithHole);
}
Also used : ArrayList(java.util.ArrayList) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder)

Example 14 with CoordinatesBuilder

use of org.opensearch.common.geo.builders.CoordinatesBuilder 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)

Example 15 with CoordinatesBuilder

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

the class GeoQueryTests method testIndexPointsPolygon.

public void testIndexPointsPolygon() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate(defaultIndexName).addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-30 -30)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-45 -50)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    CoordinatesBuilder cb = new CoordinatesBuilder();
    cb.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 shape = new PolygonBuilder(cb);
    GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(shape);
    Geometry geometry = builder.buildGeometry();
    SearchResponse searchResponse = client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry).relation(ShapeRelation.INTERSECTS)).get();
    assertSearchResponse(searchResponse);
    assertHitCount(searchResponse, 1);
    SearchHits searchHits = searchResponse.getHits();
    assertThat(searchHits.getAt(0).getId(), equalTo("1"));
}
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) SearchHits(org.opensearch.search.SearchHits) 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)

Aggregations

CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)42 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)37 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)21 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)18 Coordinate (org.locationtech.jts.geom.Coordinate)12 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)12 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)11 SearchResponse (org.opensearch.action.search.SearchResponse)8 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)8 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)7 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)7 ArrayList (java.util.ArrayList)5 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)5 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)5 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)5 LineString (org.locationtech.jts.geom.LineString)4 Polygon (org.locationtech.jts.geom.Polygon)4 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)4 Settings (org.opensearch.common.settings.Settings)4 OpenSearchGeoAssertions.assertMultiLineString (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiLineString)4