Search in sources :

Example 1 with MultiLineStringBuilder

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

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

the class ShapeBuilderTests method testMultiLineString.

public void testMultiLineString() {
    MultiLineStringBuilder mlsb = new MultiLineStringBuilder().linestring(new LineStringBuilder(new CoordinatesBuilder().coordinate(-100.0, 50.0).coordinate(50.0, 50.0).coordinate(50.0, 20.0).coordinate(-100.0, 20.0))).linestring(new LineStringBuilder(new CoordinatesBuilder().coordinate(-100.0, 20.0).coordinate(50.0, 20.0).coordinate(50.0, 0.0).coordinate(-100.0, 0.0)));
    mlsb.buildS4J();
    buildGeometry(mlsb);
    // LineString that needs to be wrapped
    new MultiLineStringBuilder().linestring(new LineStringBuilder(new CoordinatesBuilder().coordinate(150.0, 60.0).coordinate(200.0, 60.0).coordinate(200.0, 40.0).coordinate(150.0, 40.0))).linestring(new LineStringBuilder(new CoordinatesBuilder().coordinate(150.0, 20.0).coordinate(200.0, 20.0).coordinate(200.0, 0.0).coordinate(150.0, 0.0)));
    mlsb.buildS4J();
    buildGeometry(mlsb);
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder)

Example 3 with MultiLineStringBuilder

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

the class GeoWKTParser method parseMultiLine.

private static MultiLineStringBuilder parseMultiLine(StreamTokenizer stream, final boolean ignoreZValue, final boolean coerce) throws IOException, OpenSearchParseException {
    String token = nextEmptyOrOpen(stream);
    if (token.equals(EMPTY)) {
        return new MultiLineStringBuilder();
    }
    MultiLineStringBuilder builder = new MultiLineStringBuilder();
    builder.linestring(parseLine(stream, ignoreZValue, coerce));
    while (nextCloserOrComma(stream).equals(COMMA)) {
        builder.linestring(parseLine(stream, ignoreZValue, coerce));
    }
    return builder;
}
Also used : MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder)

Example 4 with MultiLineStringBuilder

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

the class GeoPointShapeQueryTests method testQueryMultiLine.

public void testQueryMultiLine() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    CoordinatesBuilder coords1 = new CoordinatesBuilder().coordinate(-35, -35).coordinate(-25, -25);
    CoordinatesBuilder coords2 = new CoordinatesBuilder().coordinate(-15, -15).coordinate(-5, -5);
    LineStringBuilder lsb1 = new LineStringBuilder(coords1);
    LineStringBuilder lsb2 = new LineStringBuilder(coords2);
    MultiLineStringBuilder mlb = new MultiLineStringBuilder().linestring(lsb1).linestring(lsb2);
    MultiLine multiline = (MultiLine) mlb.buildGeometry();
    try {
        client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiline)).get();
    } catch (Exception e) {
        assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.MULTILINESTRING + " queries"));
    }
}
Also used : MultiLine(org.opensearch.geometry.MultiLine) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException)

Example 5 with MultiLineStringBuilder

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

the class RandomShapeGenerator method createShape.

/**
 * Creates a random shape useful for randomized testing, NOTE: exercise caution when using this to build random GeometryCollections
 * as creating a large random number of random shapes can result in massive resource consumption
 * see: {@link GeoShapeQueryTests#testQueryRandomGeoCollection()}
 *
 * The following options are included
 * @param nearPoint Create a shape near a provided point
 * @param within Create a shape within the provided rectangle (note: if not null this will override the provided point)
 * @param st Create a random shape of the provided type
 * @return the ShapeBuilder for a random shape
 */
private static ShapeBuilder createShape(Random r, Point nearPoint, Rectangle within, ShapeType st, boolean validate) throws InvalidShapeException {
    if (st == null) {
        st = ShapeType.randomType(r);
    }
    if (within == null) {
        within = xRandomRectangle(r, nearPoint);
    }
    // inside non overlapping bounding rectangles
    switch(st) {
        case POINT:
            Point p = xRandomPointIn(r, within);
            PointBuilder pb = new PointBuilder().coordinate(new Coordinate(p.getX(), p.getY(), Double.NaN));
            return pb;
        case MULTIPOINT:
        case LINESTRING:
            // for random testing having a maximum number of 10 points for a line string is more than sufficient
            // if this number gets out of hand, the number of self intersections for a linestring can become
            // (n^2-n)/2 and computing the relation intersection matrix will become NP-Hard
            int numPoints = RandomNumbers.randomIntBetween(r, 3, 10);
            CoordinatesBuilder coordinatesBuilder = new CoordinatesBuilder();
            for (int i = 0; i < numPoints; ++i) {
                p = xRandomPointIn(r, within);
                coordinatesBuilder.coordinate(p.getX(), p.getY());
            }
            ShapeBuilder pcb = (st == ShapeType.MULTIPOINT) ? new MultiPointBuilder(coordinatesBuilder.build()) : new LineStringBuilder(coordinatesBuilder);
            return pcb;
        case MULTILINESTRING:
            MultiLineStringBuilder mlsb = new MultiLineStringBuilder();
            for (int i = 0; i < RandomNumbers.randomIntBetween(r, 1, 10); ++i) {
                mlsb.linestring((LineStringBuilder) createShape(r, nearPoint, within, ShapeType.LINESTRING, false));
            }
            return mlsb;
        case POLYGON:
            numPoints = RandomNumbers.randomIntBetween(r, 5, 25);
            Coordinate[] coordinates = new Coordinate[numPoints];
            for (int i = 0; i < numPoints; ++i) {
                p = (Point) createShape(r, nearPoint, within, ShapeType.POINT, false).buildS4J();
                coordinates[i] = new Coordinate(p.getX(), p.getY());
            }
            // random point order or random linestrings can lead to invalid self-crossing polygons,
            // compute the convex hull for a set of points to ensure polygon does not self cross
            Geometry shell = new ConvexHull(coordinates, ctx.getGeometryFactory()).getConvexHull();
            Coordinate[] shellCoords = shell.getCoordinates();
            // when all else fails, use the bounding box as the polygon
            if (shellCoords.length < 3) {
                shellCoords = new Coordinate[4];
                shellCoords[0] = new Coordinate(within.getMinX(), within.getMinY());
                shellCoords[1] = new Coordinate(within.getMinX(), within.getMaxY());
                shellCoords[2] = new Coordinate(within.getMaxX(), within.getMaxY());
                shellCoords[3] = new Coordinate(within.getMaxX(), within.getMinY());
            }
            PolygonBuilder pgb = new PolygonBuilder(new CoordinatesBuilder().coordinates(shellCoords).close());
            if (validate) {
                // The validate flag will check for these possibilities and bail if an incorrect geometry is created
                try {
                    pgb.buildS4J();
                } catch (AssertionError | InvalidShapeException e) {
                    // or InvalidShapeException
                    return null;
                }
            }
            return pgb;
        default:
            throw new OpenSearchException("Unable to create shape of type [" + st + "]");
    }
}
Also used : ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) ConvexHull(org.locationtech.jts.algorithm.ConvexHull) Point(org.locationtech.spatial4j.shape.Point) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) Point(org.locationtech.spatial4j.shape.Point) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) Geometry(org.locationtech.jts.geom.Geometry) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) OpenSearchException(org.opensearch.OpenSearchException) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder)

Aggregations

MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)5 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)4 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)4 Coordinate (org.locationtech.jts.geom.Coordinate)2 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)2 OpenSearchException (org.opensearch.OpenSearchException)2 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)2 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)2 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)2 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 MultiLine (org.opensearch.geometry.MultiLine)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 GeoTestUtil (org.apache.lucene.geo.GeoTestUtil)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1