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);
}
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));
}
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());
}
}
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"));
}
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")));
}
Aggregations