Search in sources :

Example 1 with Rectangle

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

the class GeoHashTests method testNorthPoleBoundingBox.

public void testNorthPoleBoundingBox() {
    // Bounding box with maximum precision touching north pole
    Rectangle bbox = Geohash.toBoundingBox("zzbxfpgzupbx");
    // Should be 90 degrees
    assertEquals(90.0, bbox.getMaxY(), 0.0000001);
}
Also used : Rectangle(org.opensearch.geometry.Rectangle)

Example 2 with Rectangle

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

the class GeoHashTests method testBboxFromHash.

public void testBboxFromHash() {
    String hash = randomGeohash(1, 12);
    int level = hash.length();
    Rectangle bbox = Geohash.toBoundingBox(hash);
    // check that the length is as expected
    double expectedLonDiff = 360.0 / (Math.pow(8.0, (level + 1) / 2) * Math.pow(4.0, level / 2));
    double expectedLatDiff = 180.0 / (Math.pow(4.0, (level + 1) / 2) * Math.pow(8.0, level / 2));
    assertEquals(expectedLonDiff, bbox.getMaxX() - bbox.getMinX(), 0.00001);
    assertEquals(expectedLatDiff, bbox.getMaxY() - bbox.getMinY(), 0.00001);
    assertEquals(hash, Geohash.stringEncode(bbox.getMinX(), bbox.getMinY(), level));
}
Also used : Rectangle(org.opensearch.geometry.Rectangle) GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 3 with Rectangle

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

the class GeoJsonParserTests method testParseEnvelope.

@Override
public void testParseEnvelope() throws IOException {
    // test #1: envelope with expected coordinate order (TopLeft, BottomRight)
    XContentBuilder multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", randomBoolean() ? "envelope" : "bbox").startArray("coordinates").startArray().value(-50).value(30).endArray().startArray().value(50).value(-30).endArray().endArray().endObject();
    Rectangle expected = new Rectangle(-50, 50, 30, -30);
    assertGeometryEquals(expected, multilinesGeoJson);
    // test #2: envelope that spans dateline
    multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", randomBoolean() ? "envelope" : "bbox").startArray("coordinates").startArray().value(50).value(30).endArray().startArray().value(-50).value(-30).endArray().endArray().endObject();
    expected = new Rectangle(50, -50, 30, -30);
    assertGeometryEquals(expected, multilinesGeoJson);
    // test #3: "envelope" (actually a triangle) with invalid number of coordinates (TopRight, BottomLeft, BottomRight)
    multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", randomBoolean() ? "envelope" : "bbox").startArray("coordinates").startArray().value(50).value(30).endArray().startArray().value(-50).value(-30).endArray().startArray().value(50).value(-39).endArray().endArray().endObject();
    try (XContentParser parser = createParser(multilinesGeoJson)) {
        parser.nextToken();
        expectThrows(XContentParseException.class, () -> new GeoJson(false, false, new GeographyValidator(false)).fromXContent(parser));
        assertNull(parser.nextToken());
    }
    // test #4: "envelope" with empty coordinates
    multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", randomBoolean() ? "envelope" : "bbox").startArray("coordinates").endArray().endObject();
    try (XContentParser parser = createParser(multilinesGeoJson)) {
        parser.nextToken();
        expectThrows(XContentParseException.class, () -> new GeoJson(false, false, new GeographyValidator(false)).fromXContent(parser));
        assertNull(parser.nextToken());
    }
}
Also used : Rectangle(org.opensearch.geometry.Rectangle) GeographyValidator(org.opensearch.geometry.utils.GeographyValidator) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 4 with Rectangle

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

the class GeoTileGridParserTests method testParseValidBounds.

public void testParseValidBounds() throws Exception {
    Rectangle bbox = GeometryTestUtils.randomRectangle();
    XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\": 5, \"size\": 500, \"shard_size\": 550," + "\"bounds\": { " + "\"top\": " + bbox.getMaxY() + "," + "\"bottom\": " + bbox.getMinY() + "," + "\"left\": " + bbox.getMinX() + "," + "\"right\": " + bbox.getMaxX() + "}" + "}");
    XContentParser.Token token = stParser.nextToken();
    assertSame(XContentParser.Token.START_OBJECT, token);
    // can create a factory
    assertNotNull(GeoTileGridAggregationBuilder.PARSER.parse(stParser, "geotile_grid"));
}
Also used : Rectangle(org.opensearch.geometry.Rectangle) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with Rectangle

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

the class GeoQueryTests method testRectangleSpanningDateline.

public void testRectangleSpanningDateline() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-169 0)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-179 0)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(171 0)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    Rectangle rectangle = new Rectangle(169, -178, 1, -1);
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", rectangle);
    SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertHitCount(response, 2);
    SearchHits searchHits = response.getHits();
    assertThat(searchHits.getAt(0).getId(), not(equalTo("1")));
    assertThat(searchHits.getAt(1).getId(), not(equalTo("1")));
}
Also used : GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) Rectangle(org.opensearch.geometry.Rectangle) SearchHits(org.opensearch.search.SearchHits) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

Rectangle (org.opensearch.geometry.Rectangle)13 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)4 XContentParser (org.opensearch.common.xcontent.XContentParser)4 GeoPoint (org.opensearch.common.geo.GeoPoint)3 SearchResponse (org.opensearch.action.search.SearchResponse)2 Geometry (org.opensearch.geometry.Geometry)2 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)2 ParseException (java.text.ParseException)1 IndexableField (org.apache.lucene.index.IndexableField)1 OpenSearchParseException (org.opensearch.OpenSearchParseException)1 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)1 ShapeRelation (org.opensearch.common.geo.ShapeRelation)1 Point (org.opensearch.geometry.Point)1 GeographyValidator (org.opensearch.geometry.utils.GeographyValidator)1 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)1 SearchHits (org.opensearch.search.SearchHits)1 GeoTileUtils.hashToGeoPoint (org.opensearch.search.aggregations.bucket.geogrid.GeoTileUtils.hashToGeoPoint)1 GeoTileUtils.keyToGeoPoint (org.opensearch.search.aggregations.bucket.geogrid.GeoTileUtils.keyToGeoPoint)1