Search in sources :

Example 1 with MultiPoint

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

the class GeoJson method createGeometry.

private static Geometry createGeometry(String type, List<Geometry> geometries, CoordinateNode coordinates, Boolean orientation, boolean defaultOrientation, boolean coerce, DistanceUnit.Distance radius) {
    ShapeType shapeType;
    if ("bbox".equalsIgnoreCase(type)) {
        shapeType = ShapeType.ENVELOPE;
    } else {
        shapeType = ShapeType.forName(type);
    }
    if (shapeType == ShapeType.GEOMETRYCOLLECTION) {
        if (geometries == null) {
            throw new OpenSearchParseException("geometries not included");
        }
        if (coordinates != null) {
            throw new OpenSearchParseException("parameter coordinates is not supported for type " + type);
        }
        verifyNulls(type, null, orientation, radius);
        return new GeometryCollection<>(geometries);
    }
    // We expect to have coordinates for all the rest
    if (coordinates == null) {
        throw new OpenSearchParseException("coordinates not included");
    }
    switch(shapeType) {
        case CIRCLE:
            if (radius == null) {
                throw new OpenSearchParseException("radius is not specified");
            }
            verifyNulls(type, geometries, orientation, null);
            Point point = coordinates.asPoint();
            return new Circle(point.getX(), point.getY(), point.getZ(), radius.convert(DistanceUnit.METERS).value);
        case POINT:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asPoint();
        case MULTIPOINT:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asMultiPoint();
        case LINESTRING:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asLineString(coerce);
        case MULTILINESTRING:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asMultiLineString(coerce);
        case POLYGON:
            verifyNulls(type, geometries, null, radius);
            // handle possible null in orientation
            return coordinates.asPolygon(orientation != null ? orientation : defaultOrientation, coerce);
        case MULTIPOLYGON:
            verifyNulls(type, geometries, null, radius);
            // handle possible null in orientation
            return coordinates.asMultiPolygon(orientation != null ? orientation : defaultOrientation, coerce);
        case ENVELOPE:
            verifyNulls(type, geometries, orientation, radius);
            return coordinates.asRectangle();
        default:
            throw new OpenSearchParseException("unsupported shape type " + type);
    }
}
Also used : GeometryCollection(org.opensearch.geometry.GeometryCollection) Circle(org.opensearch.geometry.Circle) OpenSearchParseException(org.opensearch.OpenSearchParseException) ShapeType(org.opensearch.geometry.ShapeType) MultiPoint(org.opensearch.geometry.MultiPoint) Point(org.opensearch.geometry.Point)

Example 2 with MultiPoint

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

the class MultiPointBuilder method buildS4J.

@Override
public XShapeCollection<Point> buildS4J() {
    // Could wrap JtsGeometry but probably slower due to conversions to/from JTS in relate()
    // MultiPoint geometry = FACTORY.createMultiPoint(points.toArray(new Coordinate[points.size()]));
    List<Point> shapes = new ArrayList<>(coordinates.size());
    for (Coordinate coord : coordinates) {
        shapes.add(SPATIAL_CONTEXT.makePoint(coord.x, coord.y));
    }
    XShapeCollection<Point> multiPoints = new XShapeCollection<>(shapes, SPATIAL_CONTEXT);
    multiPoints.setPointsOnly(true);
    return multiPoints;
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) XShapeCollection(org.opensearch.common.geo.XShapeCollection) ArrayList(java.util.ArrayList) Point(org.locationtech.spatial4j.shape.Point) MultiPoint(org.opensearch.geometry.MultiPoint)

Example 3 with MultiPoint

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

the class GeometryIndexerTests method testMultiPoint.

public void testMultiPoint() {
    MultiPoint multiPoint = MultiPoint.EMPTY;
    Geometry indexed = multiPoint;
    assertEquals(indexed, indexer.prepareForIndexing(multiPoint));
    multiPoint = new MultiPoint(Collections.singletonList(new Point(2, 1)));
    indexed = new Point(2, 1);
    assertEquals(indexed, indexer.prepareForIndexing(multiPoint));
    multiPoint = new MultiPoint(Arrays.asList(new Point(2, 1), new Point(4, 3)));
    indexed = multiPoint;
    assertEquals(indexed, indexer.prepareForIndexing(multiPoint));
    multiPoint = new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(4, 3, 10)));
    assertEquals(indexed, indexer.prepareForIndexing(multiPoint));
}
Also used : MultiPoint(org.opensearch.geometry.MultiPoint) Geometry(org.opensearch.geometry.Geometry) MultiPoint(org.opensearch.geometry.MultiPoint) Point(org.opensearch.geometry.Point)

Example 4 with MultiPoint

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

the class GeometryIndexerTests method testRandomLine.

/**
 * A randomized test that generates a random lines crossing anti-merdian and checks that the decomposed segments of this line
 * have the same total length (measured using Euclidean distances between neighboring points) as the original line.
 *
 * It also extracts all points from these lines, performs normalization of these points and then compares that the resulting
 * points of line normalization match the points of points normalization with the exception of points that were created on the
 * antimeridian as the result of line decomposition.
 */
public void testRandomLine() {
    int size = randomIntBetween(2, 20);
    int shift = randomIntBetween(-2, 2);
    double[] originalLats = new double[size];
    double[] originalLons = new double[size];
    // Generate a random line that goes over poles and stretches beyond -180 and +180
    for (int i = 0; i < size; i++) {
        // from time to time go over poles
        originalLats[i] = randomInt(4) == 0 ? GeometryTestUtils.randomLat() : GeometryTestUtils.randomLon();
        originalLons[i] = GeometryTestUtils.randomLon() + shift * 360;
        if (randomInt(3) == 0) {
            shift += randomFrom(-2, -1, 1, 2);
        }
    }
    Line original = new Line(originalLons, originalLats);
    // Check that the length of original and decomposed lines is the same
    Geometry decomposed = indexer.prepareForIndexing(original);
    double decomposedLength = 0;
    if (decomposed instanceof Line) {
        decomposedLength = length((Line) decomposed);
    } else {
        assertThat(decomposed, instanceOf(MultiLine.class));
        MultiLine lines = (MultiLine) decomposed;
        for (int i = 0; i < lines.size(); i++) {
            decomposedLength += length(lines.get(i));
        }
    }
    assertEquals("Different Lengths between " + original + " and " + decomposed, length(original), decomposedLength, 0.001);
    // Check that normalized linestring generates the same points as the normalized multipoint based on the same set of points
    MultiPoint decomposedViaLines = remove180s(GeometryTestUtils.toMultiPoint(decomposed));
    MultiPoint originalPoints = GeometryTestUtils.toMultiPoint(original);
    MultiPoint decomposedViaPoint = remove180s(GeometryTestUtils.toMultiPoint(indexer.prepareForIndexing(originalPoints)));
    assertEquals(decomposedViaPoint.size(), decomposedViaLines.size());
    for (int i = 0; i < decomposedViaPoint.size(); i++) {
        assertEquals("Difference between decomposing lines " + decomposedViaLines + " and points " + decomposedViaPoint + " at the position " + i, decomposedViaPoint.get(i).getLat(), decomposedViaLines.get(i).getLat(), 0.0001);
        assertEquals("Difference between decomposing lines " + decomposedViaLines + " and points " + decomposedViaPoint + " at the position " + i, decomposedViaPoint.get(i).getLon(), decomposedViaLines.get(i).getLon(), 0.0001);
    }
}
Also used : MultiLine(org.opensearch.geometry.MultiLine) Line(org.opensearch.geometry.Line) Geometry(org.opensearch.geometry.Geometry) MultiLine(org.opensearch.geometry.MultiLine) MultiPoint(org.opensearch.geometry.MultiPoint) MultiPoint(org.opensearch.geometry.MultiPoint) Point(org.opensearch.geometry.Point)

Example 5 with MultiPoint

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

the class GeoJsonShapeParserTests method testParseMultiPoint.

@Override
public void testParseMultiPoint() throws IOException, ParseException {
    XContentBuilder multiPointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "MultiPoint").startArray("coordinates").startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().endArray().endObject();
    ShapeCollection<?> expected = shapeCollection(SPATIAL_CONTEXT.makePoint(100, 0), SPATIAL_CONTEXT.makePoint(101, 1.0));
    assertGeometryEquals(expected, multiPointGeoJson, true);
    assertGeometryEquals(new MultiPoint(Arrays.asList(new org.opensearch.geometry.Point(100, 0), new org.opensearch.geometry.Point(101, 1))), multiPointGeoJson, false);
}
Also used : MultiPoint(org.opensearch.geometry.MultiPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

MultiPoint (org.opensearch.geometry.MultiPoint)11 Point (org.opensearch.geometry.Point)7 ArrayList (java.util.ArrayList)5 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)3 Geometry (org.opensearch.geometry.Geometry)3 Coordinate (org.locationtech.jts.geom.Coordinate)2 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)2 Point (org.locationtech.jts.geom.Point)1 Point (org.locationtech.spatial4j.shape.Point)1 Shape (org.locationtech.spatial4j.shape.Shape)1 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)1 OpenSearchParseException (org.opensearch.OpenSearchParseException)1 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)1 XShapeCollection (org.opensearch.common.geo.XShapeCollection)1 Circle (org.opensearch.geometry.Circle)1 GeometryCollection (org.opensearch.geometry.GeometryCollection)1 Line (org.opensearch.geometry.Line)1 MultiLine (org.opensearch.geometry.MultiLine)1 ShapeType (org.opensearch.geometry.ShapeType)1