Search in sources :

Example 16 with LinearRing

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

the class GeoPolygonDecomposer method buildPolygon.

private static Polygon buildPolygon(List<Point[]> polygon) {
    List<LinearRing> holes;
    Point[] shell = polygon.get(0);
    if (polygon.size() > 1) {
        holes = new ArrayList<>(polygon.size() - 1);
        for (int i = 1; i < polygon.size(); ++i) {
            Point[] coords = polygon.get(i);
            // We do not have holes on the dateline as they get eliminated
            // when breaking the polygon around it.
            double[] x = new double[coords.length];
            double[] y = new double[coords.length];
            for (int c = 0; c < coords.length; ++c) {
                x[c] = normalizeLon(coords[c].getX());
                y[c] = normalizeLat(coords[c].getY());
            }
            holes.add(new LinearRing(x, y));
        }
    } else {
        holes = Collections.emptyList();
    }
    double[] x = new double[shell.length];
    double[] y = new double[shell.length];
    for (int i = 0; i < shell.length; ++i) {
        // Lucene Tessellator treats different +180 and -180 and we should keep the sign.
        // normalizeLon method excludes -180.
        x[i] = normalizeLonMinus180Inclusive(shell[i].getX());
        y[i] = normalizeLat(shell[i].getY());
    }
    return new Polygon(new LinearRing(x, y), holes);
}
Also used : Point(org.opensearch.geometry.Point) LinearRing(org.opensearch.geometry.LinearRing) Polygon(org.opensearch.geometry.Polygon) MultiPolygon(org.opensearch.geometry.MultiPolygon) Point(org.opensearch.geometry.Point)

Example 17 with LinearRing

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

the class GeometryParserTests method testGeoJsonParsing.

public void testGeoJsonParsing() throws Exception {
    XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Point").startArray("coordinates").value(100.0).value(0.0).endArray().endObject();
    try (XContentParser parser = createParser(pointGeoJson)) {
        parser.nextToken();
        GeometryFormat format = new GeometryParser(true, randomBoolean(), randomBoolean()).geometryFormat(parser);
        assertEquals(new Point(100, 0), format.fromXContent(parser));
        XContentBuilder newGeoJson = XContentFactory.jsonBuilder();
        format.toXContent(new Point(100, 10), newGeoJson, ToXContent.EMPTY_PARAMS);
        assertEquals("{\"type\":\"Point\",\"coordinates\":[100.0,10.0]}", Strings.toString(newGeoJson));
    }
    XContentBuilder pointGeoJsonWithZ = XContentFactory.jsonBuilder().startObject().field("type", "Point").startArray("coordinates").value(100.0).value(0.0).value(10.0).endArray().endObject();
    try (XContentParser parser = createParser(pointGeoJsonWithZ)) {
        parser.nextToken();
        assertEquals(new Point(100, 0, 10.0), new GeometryParser(true, randomBoolean(), true).parse(parser));
    }
    try (XContentParser parser = createParser(pointGeoJsonWithZ)) {
        parser.nextToken();
        expectThrows(IllegalArgumentException.class, () -> new GeometryParser(true, randomBoolean(), false).parse(parser));
    }
    XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(100.0).value(1.0).endArray().startArray().value(101.0).value(1.0).endArray().startArray().value(101.0).value(0.0).endArray().startArray().value(100.0).value(0.0).endArray().endArray().endArray().endObject();
    Polygon p = new Polygon(new LinearRing(new double[] { 100d, 101d, 101d, 100d, 100d }, new double[] { 1d, 1d, 0d, 0d, 1d }));
    try (XContentParser parser = createParser(polygonGeoJson)) {
        parser.nextToken();
        // Coerce should automatically close the polygon
        assertEquals(p, new GeometryParser(true, true, randomBoolean()).parse(parser));
    }
    try (XContentParser parser = createParser(polygonGeoJson)) {
        parser.nextToken();
        // No coerce - the polygon parsing should fail
        expectThrows(XContentParseException.class, () -> new GeometryParser(true, false, randomBoolean()).parse(parser));
    }
}
Also used : Point(org.opensearch.geometry.Point) Polygon(org.opensearch.geometry.Polygon) LinearRing(org.opensearch.geometry.LinearRing) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 18 with LinearRing

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

the class GeoPointShapeQueryTests method testQueryLinearRing.

public void testQueryLinearRing() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    LinearRing linearRing = new LinearRing(new double[] { -25, -35, -25 }, new double[] { -25, -35, -25 });
    try {
        // LinearRing extends Line implements Geometry: expose the build process
        GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder(defaultGeoFieldName, linearRing);
        SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE);
        searchRequestBuilder.setQuery(queryBuilder);
        searchRequestBuilder.setIndices("test");
        searchRequestBuilder.get();
    } catch (SearchPhaseExecutionException e) {
        assertThat(e.getCause().getMessage(), containsString("Field [" + defaultGeoFieldName + "] does not support LINEARRING queries"));
    }
}
Also used : GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) LinearRing(org.opensearch.geometry.LinearRing) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

LinearRing (org.opensearch.geometry.LinearRing)18 Polygon (org.opensearch.geometry.Polygon)14 MultiPolygon (org.opensearch.geometry.MultiPolygon)12 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)8 Point (org.opensearch.geometry.Point)6 ArrayList (java.util.ArrayList)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 LineString (org.locationtech.jts.geom.LineString)3 Polygon (org.locationtech.jts.geom.Polygon)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)3 MultiPoint (org.opensearch.geometry.MultiPoint)3 OpenSearchGeoAssertions.assertMultiLineString (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiLineString)3 OpenSearchGeoAssertions.assertMultiPolygon (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiPolygon)3 OpenSearchGeoAssertions.assertPolygon (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertPolygon)3 XContentParser (org.opensearch.common.xcontent.XContentParser)2 Geometry (org.opensearch.geometry.Geometry)2 Line (org.opensearch.geometry.Line)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1