Search in sources :

Example 16 with Geometry

use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.

the class GeoWKTShapeParserTests method testParseMultiLineString.

@Override
public void testParseMultiLineString() throws IOException, ParseException {
    int numLineStrings = randomIntBetween(0, 8);
    List<LineString> lineStrings = new ArrayList<>(numLineStrings);
    MultiLineStringBuilder builder = new MultiLineStringBuilder();
    for (int j = 0; j < numLineStrings; ++j) {
        List<Coordinate> lsc = randomLineStringCoords();
        Coordinate[] coords = lsc.toArray(new Coordinate[lsc.size()]);
        lineStrings.add(GEOMETRY_FACTORY.createLineString(coords));
        builder.linestring(new LineStringBuilder(lsc));
    }
    List<Line> lines = new ArrayList<>(lineStrings.size());
    for (int j = 0; j < lineStrings.size(); ++j) {
        Coordinate[] c = lineStrings.get(j).getCoordinates();
        lines.add(new Line(Arrays.stream(c).mapToDouble(i -> i.x).toArray(), Arrays.stream(c).mapToDouble(i -> i.y).toArray()));
    }
    Geometry expectedGeom;
    if (lines.isEmpty()) {
        expectedGeom = GeometryCollection.EMPTY;
    } else if (lines.size() == 1) {
        expectedGeom = new Line(lines.get(0).getX(), lines.get(0).getY());
    } else {
        expectedGeom = new MultiLine(lines);
    }
    assertExpected(expectedGeom, builder, false);
    assertMalformed(builder);
    MultiLineString expected = GEOMETRY_FACTORY.createMultiLineString(lineStrings.toArray(new LineString[lineStrings.size()]));
    assumeTrue("JTS test path cannot handle empty multilinestrings", numLineStrings > 1);
    assertExpected(jtsGeom(expected), builder, true);
}
Also used : Matchers.hasToString(org.hamcrest.Matchers.hasToString) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) Arrays(java.util.Arrays) GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) ShapeParser(org.opensearch.common.geo.parsers.ShapeParser) ContentPath(org.opensearch.index.mapper.ContentPath) Coordinate(org.locationtech.jts.geom.Coordinate) Version(org.opensearch.Version) OpenSearchException(org.opensearch.OpenSearchException) RandomShapeGenerator(org.opensearch.test.geo.RandomShapeGenerator) MultiPoint(org.opensearch.geometry.MultiPoint) XContentParser(org.opensearch.common.xcontent.XContentParser) SPATIAL_CONTEXT(org.opensearch.common.geo.builders.ShapeBuilder.SPATIAL_CONTEXT) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) XContentFactory(org.opensearch.common.xcontent.XContentFactory) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) ParseException(java.text.ParseException) ShapeCollection(org.locationtech.spatial4j.shape.ShapeCollection) GeoShapeFieldMapper(org.opensearch.index.mapper.GeoShapeFieldMapper) MultiLineString(org.locationtech.jts.geom.MultiLineString) OpenSearchParseException(org.opensearch.OpenSearchParseException) Settings(org.opensearch.common.settings.Settings) Point(org.locationtech.jts.geom.Point) Geometry(org.opensearch.geometry.Geometry) Shape(org.locationtech.spatial4j.shape.Shape) GeoShapeIndexer(org.opensearch.index.mapper.GeoShapeIndexer) List(java.util.List) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) Polygon(org.locationtech.jts.geom.Polygon) Line(org.opensearch.geometry.Line) Matchers.containsString(org.hamcrest.Matchers.containsString) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) GeoTestUtil(org.apache.lucene.geo.GeoTestUtil) LinearRing(org.locationtech.jts.geom.LinearRing) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) LegacyGeoShapeFieldMapper(org.opensearch.index.mapper.LegacyGeoShapeFieldMapper) ArrayList(java.util.ArrayList) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) Rectangle(org.locationtech.spatial4j.shape.Rectangle) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) UUIDs(org.opensearch.common.UUIDs) MultiLine(org.opensearch.geometry.MultiLine) GeometryCollection(org.opensearch.geometry.GeometryCollection) IOException(java.io.IOException) Mapper(org.opensearch.index.mapper.Mapper) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) GeoWKTParser(org.opensearch.common.geo.parsers.GeoWKTParser) LineString(org.locationtech.jts.geom.LineString) Collections(java.util.Collections) MultiLineString(org.locationtech.jts.geom.MultiLineString) ArrayList(java.util.ArrayList) MultiPoint(org.opensearch.geometry.MultiPoint) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.jts.geom.Point) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) Line(org.opensearch.geometry.Line) MultiLine(org.opensearch.geometry.MultiLine) Geometry(org.opensearch.geometry.Geometry) MultiLine(org.opensearch.geometry.MultiLine) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder)

Example 17 with Geometry

use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.

the class GeometryIndexerTests method testRectangle.

public void testRectangle() {
    Rectangle indexed = new Rectangle(-179, -178, 10, -10);
    Geometry processed = indexer.prepareForIndexing(indexed);
    assertEquals(indexed, processed);
    // a rectangle is broken into two triangles
    List<IndexableField> fields = indexer.indexShape(null, indexed);
    assertEquals(fields.size(), 2);
    indexed = new Rectangle(179, -179, 10, -10);
    processed = indexer.prepareForIndexing(indexed);
    assertEquals(indexed, processed);
    // a rectangle crossing the dateline is broken into 4 triangles
    fields = indexer.indexShape(null, indexed);
    assertEquals(fields.size(), 4);
}
Also used : Geometry(org.opensearch.geometry.Geometry) IndexableField(org.apache.lucene.index.IndexableField) Rectangle(org.opensearch.geometry.Rectangle)

Example 18 with Geometry

use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.

the class GeometryIndexerTests method testLine.

public void testLine() {
    Line line = new Line(new double[] { 3, 4 }, new double[] { 1, 2 });
    Geometry indexed = line;
    assertEquals(indexed, indexer.prepareForIndexing(line));
    line = new Line(new double[] { 160, 200 }, new double[] { 10, 20 });
    indexed = new MultiLine(Arrays.asList(new Line(new double[] { 160, 180 }, new double[] { 10, 15 }), new Line(new double[] { -180, -160 }, new double[] { 15, 20 })));
    assertEquals(indexed, indexer.prepareForIndexing(line));
    line = new Line(new double[] { 200, 160 }, new double[] { 10, 20 });
    indexed = new MultiLine(Arrays.asList(new Line(new double[] { -160, -180 }, new double[] { 10, 15 }), new Line(new double[] { 180, 160 }, new double[] { 15, 20 })));
    assertEquals(indexed, indexer.prepareForIndexing(line));
    line = new Line(new double[] { 160, 200, 160 }, new double[] { 0, 10, 20 });
    indexed = new MultiLine(Arrays.asList(new Line(new double[] { 160, 180 }, new double[] { 0, 5 }), new Line(new double[] { -180, -160, -180 }, new double[] { 5, 10, 15 }), new Line(new double[] { 180, 160 }, new double[] { 15, 20 })));
    assertEquals(indexed, indexer.prepareForIndexing(line));
    line = new Line(new double[] { 0, 720 }, new double[] { 0, 20 });
    indexed = new MultiLine(Arrays.asList(new Line(new double[] { 0, 180 }, new double[] { 0, 5 }), new Line(new double[] { -180, 180 }, new double[] { 5, 15 }), new Line(new double[] { -180, 0 }, new double[] { 15, 20 })));
    assertEquals(indexed, indexer.prepareForIndexing(line));
    line = new Line(new double[] { 160, 180, 180, 200, 160, 140 }, new double[] { 0, 10, 20, 30, 30, 40 });
    indexed = new MultiLine(Arrays.asList(new Line(new double[] { 160, 180 }, new double[] { 0, 10 }), new Line(new double[] { -180, -180, -160, -180 }, new double[] { 10, 20, 30, 30 }), new Line(new double[] { 180, 160, 140 }, new double[] { 30, 30, 40 })));
    assertEquals(indexed, indexer.prepareForIndexing(line));
    line = new Line(new double[] { -70, 180, 900 }, new double[] { 0, 0, 4 });
    indexed = new MultiLine(Arrays.asList(new Line(new double[] { -70, 180 }, new double[] { 0, 0 }), new Line(new double[] { -180, 180 }, new double[] { 0, 2 }), new Line(new double[] { -180, 180 }, new double[] { 2, 4 })));
    assertEquals(indexed, indexer.prepareForIndexing(line));
    line = new Line(new double[] { 160, 200, 160, 200, 160, 200 }, new double[] { 0, 10, 20, 30, 40, 50 });
    indexed = new MultiLine(Arrays.asList(new Line(new double[] { 160, 180 }, new double[] { 0, 5 }), new Line(new double[] { -180, -160, -180 }, new double[] { 5, 10, 15 }), new Line(new double[] { 180, 160, 180 }, new double[] { 15, 20, 25 }), new Line(new double[] { -180, -160, -180 }, new double[] { 25, 30, 35 }), new Line(new double[] { 180, 160, 180 }, new double[] { 35, 40, 45 }), new Line(new double[] { -180, -160 }, new double[] { 45, 50 })));
    assertEquals(indexed, indexer.prepareForIndexing(line));
}
Also used : MultiLine(org.opensearch.geometry.MultiLine) Line(org.opensearch.geometry.Line) Geometry(org.opensearch.geometry.Geometry) MultiLine(org.opensearch.geometry.MultiLine)

Example 19 with Geometry

use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.

the class GeoJsonSerializationTests method testToMap.

public void testToMap() throws IOException {
    for (int i = 0; i < 10; i++) {
        Geometry geometry = GeometryTestUtils.randomGeometry(randomBoolean());
        XContentBuilder builder = XContentFactory.jsonBuilder();
        GeoJson.toXContent(geometry, builder, ToXContent.EMPTY_PARAMS);
        StreamInput input = BytesReference.bytes(builder).streamInput();
        try (XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, input)) {
            Map<String, Object> map = GeoJson.toMap(geometry);
            assertThat(parser.map(), equalTo(map));
        }
    }
}
Also used : Geometry(org.opensearch.geometry.Geometry) StreamInput(org.opensearch.common.io.stream.StreamInput) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) GeometryTestUtils.randomPoint(org.opensearch.geo.GeometryTestUtils.randomPoint) GeometryTestUtils.randomMultiPoint(org.opensearch.geo.GeometryTestUtils.randomMultiPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 20 with Geometry

use of org.opensearch.geometry.Geometry in project OpenSearch by opensearch-project.

the class GeoBoundingBox method parseBoundingBox.

/**
 * Parses the bounding box and returns bottom, top, left, right coordinates
 */
public static GeoBoundingBox parseBoundingBox(XContentParser parser) throws IOException, OpenSearchParseException {
    XContentParser.Token token = parser.currentToken();
    if (token != XContentParser.Token.START_OBJECT) {
        throw new OpenSearchParseException("failed to parse bounding box. Expected start object but found [{}]", token);
    }
    double top = Double.NaN;
    double bottom = Double.NaN;
    double left = Double.NaN;
    double right = Double.NaN;
    String currentFieldName;
    GeoPoint sparse = new GeoPoint();
    Rectangle envelope = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
            token = parser.nextToken();
            if (WKT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                try {
                    Geometry geometry = WKT_PARSER.fromWKT(parser.text());
                    if (ShapeType.ENVELOPE.equals(geometry.type()) == false) {
                        throw new OpenSearchParseException("failed to parse WKT bounding box. [" + geometry.type() + "] found. expected [" + ShapeType.ENVELOPE + "]");
                    }
                    envelope = (Rectangle) geometry;
                } catch (ParseException | IllegalArgumentException e) {
                    throw new OpenSearchParseException("failed to parse WKT bounding box", e);
                }
            } else if (TOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                top = parser.doubleValue();
            } else if (BOTTOM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                bottom = parser.doubleValue();
            } else if (LEFT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                left = parser.doubleValue();
            } else if (RIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                right = parser.doubleValue();
            } else {
                if (TOP_LEFT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.TOP_LEFT);
                    top = sparse.getLat();
                    left = sparse.getLon();
                } else if (BOTTOM_RIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.BOTTOM_RIGHT);
                    bottom = sparse.getLat();
                    right = sparse.getLon();
                } else if (TOP_RIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.TOP_RIGHT);
                    top = sparse.getLat();
                    right = sparse.getLon();
                } else if (BOTTOM_LEFT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.BOTTOM_LEFT);
                    bottom = sparse.getLat();
                    left = sparse.getLon();
                } else {
                    throw new OpenSearchParseException("failed to parse bounding box. unexpected field [{}]", currentFieldName);
                }
            }
        } else {
            throw new OpenSearchParseException("failed to parse bounding box. field name expected but [{}] found", token);
        }
    }
    if (envelope != null) {
        if (Double.isNaN(top) == false || Double.isNaN(bottom) == false || Double.isNaN(left) == false || Double.isNaN(right) == false) {
            throw new OpenSearchParseException("failed to parse bounding box. Conflicting definition found " + "using well-known text and explicit corners.");
        }
        GeoPoint topLeft = new GeoPoint(envelope.getMaxLat(), envelope.getMinLon());
        GeoPoint bottomRight = new GeoPoint(envelope.getMinLat(), envelope.getMaxLon());
        return new GeoBoundingBox(topLeft, bottomRight);
    }
    GeoPoint topLeft = new GeoPoint(top, left);
    GeoPoint bottomRight = new GeoPoint(bottom, right);
    return new GeoBoundingBox(topLeft, bottomRight);
}
Also used : Geometry(org.opensearch.geometry.Geometry) OpenSearchParseException(org.opensearch.OpenSearchParseException) Rectangle(org.opensearch.geometry.Rectangle) OpenSearchParseException(org.opensearch.OpenSearchParseException) ParseException(java.text.ParseException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

Geometry (org.opensearch.geometry.Geometry)28 MultiPoint (org.opensearch.geometry.MultiPoint)10 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 Point (org.opensearch.geometry.Point)9 Coordinate (org.locationtech.jts.geom.Coordinate)7 GeometryCollection (org.opensearch.geometry.GeometryCollection)7 Line (org.opensearch.geometry.Line)7 MultiLine (org.opensearch.geometry.MultiLine)7 ArrayList (java.util.ArrayList)6 XContentParser (org.opensearch.common.xcontent.XContentParser)6 IOException (java.io.IOException)5 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)5 ParseException (java.text.ParseException)4 OpenSearchParseException (org.opensearch.OpenSearchParseException)4 Point (org.locationtech.jts.geom.Point)3 Shape (org.locationtech.spatial4j.shape.Shape)3 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)3