Search in sources :

Example 1 with GeometryCollection

use of org.opensearch.geometry.GeometryCollection 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 GeometryCollection

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

the class GeometryIndexerTests method testCollection.

public void testCollection() {
    assertEquals(GeometryCollection.EMPTY, indexer.prepareForIndexing(GeometryCollection.EMPTY));
    GeometryCollection<Geometry> collection = new GeometryCollection<>(Collections.singletonList(new Point(2, 1)));
    Geometry indexed = new Point(2, 1);
    assertEquals(indexed, indexer.prepareForIndexing(collection));
    collection = new GeometryCollection<>(Arrays.asList(new Point(2, 1), new Point(4, 3), new Line(new double[] { 160, 200 }, new double[] { 10, 20 })));
    indexed = new GeometryCollection<>(Arrays.asList(new Point(2, 1), new Point(4, 3), 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(collection));
}
Also used : Geometry(org.opensearch.geometry.Geometry) GeometryCollection(org.opensearch.geometry.GeometryCollection) MultiLine(org.opensearch.geometry.MultiLine) Line(org.opensearch.geometry.Line) MultiLine(org.opensearch.geometry.MultiLine) MultiPoint(org.opensearch.geometry.MultiPoint) Point(org.opensearch.geometry.Point)

Example 3 with GeometryCollection

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

the class GeoJsonShapeParserTests method testParseGeometryCollection.

@Override
public void testParseGeometryCollection() throws IOException, ParseException {
    XContentBuilder geometryCollectionGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "GeometryCollection").startArray("geometries").startObject().field("type", "LineString").startArray("coordinates").startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().endArray().endObject().startObject().field("type", "Point").startArray("coordinates").value(102.0).value(2.0).endArray().endObject().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(-177.0).value(10.0).endArray().startArray().value(176.0).value(15.0).endArray().startArray().value(172.0).value(0.0).endArray().startArray().value(176.0).value(-15.0).endArray().startArray().value(-177.0).value(-10.0).endArray().startArray().value(-177.0).value(10.0).endArray().endArray().endArray().endObject().endArray().endObject();
    ArrayList<Coordinate> shellCoordinates1 = new ArrayList<>();
    shellCoordinates1.add(new Coordinate(180.0, -12.142857142857142));
    shellCoordinates1.add(new Coordinate(180.0, 12.142857142857142));
    shellCoordinates1.add(new Coordinate(176.0, 15.0));
    shellCoordinates1.add(new Coordinate(172.0, 0.0));
    shellCoordinates1.add(new Coordinate(176.0, -15));
    shellCoordinates1.add(new Coordinate(180.0, -12.142857142857142));
    ArrayList<Coordinate> shellCoordinates2 = new ArrayList<>();
    shellCoordinates2.add(new Coordinate(-180.0, 12.142857142857142));
    shellCoordinates2.add(new Coordinate(-180.0, -12.142857142857142));
    shellCoordinates2.add(new Coordinate(-177.0, -10.0));
    shellCoordinates2.add(new Coordinate(-177.0, 10.0));
    shellCoordinates2.add(new Coordinate(-180.0, 12.142857142857142));
    Shape[] expected = new Shape[3];
    LineString expectedLineString = GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(100, 0), new Coordinate(101, 1) });
    expected[0] = jtsGeom(expectedLineString);
    Point expectedPoint = GEOMETRY_FACTORY.createPoint(new Coordinate(102.0, 2.0));
    expected[1] = new JtsPoint(expectedPoint, SPATIAL_CONTEXT);
    LinearRing shell1 = GEOMETRY_FACTORY.createLinearRing(shellCoordinates1.toArray(new Coordinate[shellCoordinates1.size()]));
    LinearRing shell2 = GEOMETRY_FACTORY.createLinearRing(shellCoordinates2.toArray(new Coordinate[shellCoordinates2.size()]));
    MultiPolygon expectedMultiPoly = GEOMETRY_FACTORY.createMultiPolygon(new Polygon[] { GEOMETRY_FACTORY.createPolygon(shell1), GEOMETRY_FACTORY.createPolygon(shell2) });
    expected[2] = jtsGeom(expectedMultiPoly);
    // equals returns true only if geometries are in the same order
    assertGeometryEquals(shapeCollection(expected), geometryCollectionGeoJson, true);
    GeometryCollection<Geometry> geometryExpected = new GeometryCollection<>(Arrays.asList(new Line(new double[] { 100d, 101d }, new double[] { 0d, 1d }), new org.opensearch.geometry.Point(102d, 2d), new org.opensearch.geometry.MultiPolygon(Arrays.asList(new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(new double[] { 180d, 180d, 176d, 172d, 176d, 180d }, new double[] { -12.142857142857142d, 12.142857142857142d, 15d, 0d, -15d, -12.142857142857142d })), new org.opensearch.geometry.Polygon(new org.opensearch.geometry.LinearRing(new double[] { -180d, -180d, -177d, -177d, -180d }, new double[] { 12.142857142857142d, -12.142857142857142d, -10d, 10d, 12.142857142857142d }))))));
    assertGeometryEquals(geometryExpected, geometryCollectionGeoJson, false);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) ArrayList(java.util.ArrayList) MultiPoint(org.opensearch.geometry.MultiPoint) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.jts.geom.Point) Geometry(org.opensearch.geometry.Geometry) GeometryCollection(org.opensearch.geometry.GeometryCollection) MultiLine(org.opensearch.geometry.MultiLine) Line(org.opensearch.geometry.Line) Coordinate(org.locationtech.jts.geom.Coordinate) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 4 with GeometryCollection

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

the class GeoJsonParserTests method testParseGeometryCollection.

@Override
public void testParseGeometryCollection() throws IOException {
    XContentBuilder geometryCollectionGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "GeometryCollection").startArray("geometries").startObject().field("type", "LineString").startArray("coordinates").startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().endArray().endObject().startObject().field("type", "Point").startArray("coordinates").value(102.0).value(2.0).endArray().endObject().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(-177.0).value(10.0).endArray().startArray().value(176.0).value(15.0).endArray().startArray().value(172.0).value(0.0).endArray().startArray().value(176.0).value(-15.0).endArray().startArray().value(-177.0).value(-10.0).endArray().startArray().value(-177.0).value(10.0).endArray().endArray().endArray().endObject().endArray().endObject();
    GeometryCollection<Geometry> geometryExpected = new GeometryCollection<>(Arrays.asList(new Line(new double[] { 100d, 101d }, new double[] { 0d, 1d }), new Point(102d, 2d), new Polygon(new LinearRing(new double[] { -177, 176, 172, 176, -177, -177 }, new double[] { 10, 15, 0, -15, -10, 10 }))));
    assertGeometryEquals(geometryExpected, geometryCollectionGeoJson);
}
Also used : Geometry(org.opensearch.geometry.Geometry) GeometryCollection(org.opensearch.geometry.GeometryCollection) Line(org.opensearch.geometry.Line) MultiLine(org.opensearch.geometry.MultiLine) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) LinearRing(org.opensearch.geometry.LinearRing) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 5 with GeometryCollection

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

the class GeometryParser method parseGeometry.

/**
 * Parses the value as a {@link Geometry}. The following types of values are supported:
 * <p>
 * Object: has to contain either lat and lon or geohash fields
 * <p>
 * String: expected to be in "latitude, longitude" format, a geohash or WKT
 * <p>
 * Array: two or more elements, the first element is longitude, the second is latitude, the rest is ignored if ignoreZValue is true
 * <p>
 * Json structure: valid geojson definition
 */
public Geometry parseGeometry(Object value) throws OpenSearchParseException {
    if (value instanceof List) {
        List<?> values = (List<?>) value;
        if (values.size() == 2 && values.get(0) instanceof Number) {
            GeoPoint point = GeoUtils.parseGeoPoint(values, ignoreZValue);
            return new Point(point.lon(), point.lat());
        } else {
            List<Geometry> geometries = new ArrayList<>(values.size());
            for (Object object : values) {
                geometries.add(parseGeometry(object));
            }
            return new GeometryCollection<>(geometries);
        }
    }
    try (XContentParser parser = new MapXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Collections.singletonMap("null_value", value), null)) {
        // start object
        parser.nextToken();
        // field name
        parser.nextToken();
        // field value
        parser.nextToken();
        if (isPoint(value)) {
            GeoPoint point = GeoUtils.parseGeoPoint(parser, new GeoPoint(), ignoreZValue);
            return new Point(point.lon(), point.lat());
        } else {
            return parse(parser);
        }
    } catch (IOException | ParseException ex) {
        throw new OpenSearchParseException("error parsing geometry ", ex);
    }
}
Also used : MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser) ArrayList(java.util.ArrayList) Point(org.opensearch.geometry.Point) IOException(java.io.IOException) Geometry(org.opensearch.geometry.Geometry) GeometryCollection(org.opensearch.geometry.GeometryCollection) OpenSearchParseException(org.opensearch.OpenSearchParseException) ArrayList(java.util.ArrayList) List(java.util.List) OpenSearchParseException(org.opensearch.OpenSearchParseException) ParseException(java.text.ParseException) MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

GeometryCollection (org.opensearch.geometry.GeometryCollection)7 Geometry (org.opensearch.geometry.Geometry)6 MultiPoint (org.opensearch.geometry.MultiPoint)6 Point (org.opensearch.geometry.Point)6 ArrayList (java.util.ArrayList)4 Line (org.opensearch.geometry.Line)3 MultiLine (org.opensearch.geometry.MultiLine)3 OpenSearchParseException (org.opensearch.OpenSearchParseException)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 List (java.util.List)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 LineString (org.locationtech.jts.geom.LineString)1 LinearRing (org.locationtech.jts.geom.LinearRing)1 MultiLineString (org.locationtech.jts.geom.MultiLineString)1 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)1 Point (org.locationtech.jts.geom.Point)1 Polygon (org.locationtech.jts.geom.Polygon)1 Shape (org.locationtech.spatial4j.shape.Shape)1