use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoPolygonQueryBuilderTests method doAssertLuceneQuery.
@Override
protected void doAssertLuceneQuery(GeoPolygonQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
if (fieldType == null) {
assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery);
} else {
// TODO: Test case when there are no docValues
Query indexQuery = ((IndexOrDocValuesQuery) query).getIndexQuery();
String expectedFieldName = expectedFieldName(queryBuilder.fieldName());
List<GeoPoint> points = queryBuilder.points();
double[] lats = new double[points.size()];
double[] lons = new double[points.size()];
for (int i = 0; i < points.size(); i++) {
lats[i] = points.get(i).getLat();
lons[i] = points.get(i).getLon();
}
org.apache.lucene.geo.Polygon polygon = new org.apache.lucene.geo.Polygon(lats, lons);
assertEquals(LatLonPoint.newPolygonQuery(expectedFieldName, polygon), indexQuery);
Query dvQuery = ((IndexOrDocValuesQuery) query).getRandomAccessQuery();
assertEquals(LatLonDocValuesField.newSlowPolygonQuery(expectedFieldName, polygon), dvQuery);
}
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoUtilsTests method testParseGeoPoint.
public void testParseGeoPoint() throws IOException {
for (int i = 0; i < 100; i++) {
double lat = randomDouble() * 180 - 90 + randomIntBetween(-1000, 1000) * 180;
double lon = randomDouble() * 360 - 180 + randomIntBetween(-1000, 1000) * 360;
XContentBuilder json = jsonBuilder().startObject().field("lat", lat).field("lon", lon).endObject();
try (XContentParser parser = createParser(json)) {
parser.nextToken();
GeoPoint point = GeoUtils.parseGeoPoint(parser);
assertThat(point, equalTo(new GeoPoint(lat, lon)));
assertThat(parser.currentToken(), is(Token.END_OBJECT));
assertNull(parser.nextToken());
}
json = jsonBuilder().startObject().field("lat", String.valueOf(lat)).field("lon", String.valueOf(lon)).endObject();
try (XContentParser parser = createParser(json)) {
parser.nextToken();
GeoPoint point = GeoUtils.parseGeoPoint(parser);
assertThat(point, equalTo(new GeoPoint(lat, lon)));
}
json = jsonBuilder().startObject().startArray("foo").value(lon).value(lat).endArray().endObject();
try (XContentParser parser = createParser(json)) {
while (parser.currentToken() != Token.START_ARRAY) {
parser.nextToken();
}
GeoPoint point = GeoUtils.parseGeoPoint(parser);
assertThat(point, equalTo(new GeoPoint(lat, lon)));
}
json = jsonBuilder().startObject().field("foo", lat + "," + lon).endObject();
try (XContentParser parser = createParser(json)) {
while (parser.currentToken() != Token.VALUE_STRING) {
parser.nextToken();
}
GeoPoint point = GeoUtils.parseGeoPoint(parser);
assertThat(point, equalTo(new GeoPoint(lat, lon)));
}
}
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoUtilsTests method testParseGeoPointStringZValueError.
public void testParseGeoPointStringZValueError() throws IOException {
double lat = randomDouble() * 180 - 90 + randomIntBetween(-1000, 1000) * 180;
double lon = randomDouble() * 360 - 180 + randomIntBetween(-1000, 1000) * 360;
double alt = randomDouble() * 1000;
XContentBuilder json = jsonBuilder().startObject().field("foo", lat + "," + lon + "," + alt).endObject();
try (XContentParser parser = createParser(json)) {
while (parser.currentToken() != Token.VALUE_STRING) {
parser.nextToken();
}
Exception e = expectThrows(OpenSearchParseException.class, () -> GeoUtils.parseGeoPoint(parser, new GeoPoint(), false));
assertThat(e.getMessage(), containsString("but [ignore_z_value] parameter is [false]"));
}
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoUtilsTests method testNormalizePointInNormalRange.
public void testNormalizePointInNormalRange() {
for (int i = 0; i < 100; i++) {
double testLat = (randomDouble() * 180.0) - 90.0;
double testLon = (randomDouble() * 360.0) - 180.0;
GeoPoint testPoint = new GeoPoint(testLat, testLon);
assertNormalizedPoint(testPoint, testPoint);
}
}
use of org.opensearch.common.geo.GeoPoint in project OpenSearch by opensearch-project.
the class GeoUtilsTests method testParseGeoPointGeohashPositions.
public void testParseGeoPointGeohashPositions() throws IOException {
assertNormalizedPoint(parseGeohash("drt5", GeoUtils.EffectivePoint.TOP_LEFT), new GeoPoint(42.890625, -71.71875));
assertNormalizedPoint(parseGeohash("drt5", GeoUtils.EffectivePoint.TOP_RIGHT), new GeoPoint(42.890625, -71.3671875));
assertNormalizedPoint(parseGeohash("drt5", GeoUtils.EffectivePoint.BOTTOM_LEFT), new GeoPoint(42.71484375, -71.71875));
assertNormalizedPoint(parseGeohash("drt5", GeoUtils.EffectivePoint.BOTTOM_RIGHT), new GeoPoint(42.71484375, -71.3671875));
assertNormalizedPoint(parseGeohash("drtk", GeoUtils.EffectivePoint.BOTTOM_LEFT), new GeoPoint(42.890625, -71.3671875));
}
Aggregations