Skip to content

Commit

Permalink
remove dependency on legacy geo code from test (#74609) (#74667)
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase committed Jun 30, 2021
1 parent 1ffe77e commit 13980e3
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
import org.apache.lucene.document.StringField;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geometry.Point;

import java.io.IOException;

import static org.elasticsearch.test.geo.RandomShapeGenerator.randomPoint;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
Expand All @@ -26,8 +27,8 @@ public abstract class AbstractGeoFieldDataTestCase extends AbstractFieldDataImpl
protected abstract String getFieldDataType();

protected Field randomGeoPointField(String fieldName, Field.Store store) {
GeoPoint point = randomPoint(random());
return new LatLonDocValuesField(fieldName, point.lat(), point.lon());
Point point = GeometryTestUtils.randomPoint();
return new LatLonDocValuesField(fieldName, point.getLat(), point.getLon());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import org.elasticsearch.Version;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geometry.Rectangle;
import org.elasticsearch.geometry.utils.Geohash;
import org.elasticsearch.index.mapper.GeoPointFieldMapper;
import org.elasticsearch.index.mapper.GeoShapeFieldMapper;
import org.elasticsearch.index.mapper.LegacyGeoShapeFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.test.geo.RandomShapeGenerator;
import org.locationtech.spatial4j.io.GeohashUtils;
import org.locationtech.spatial4j.shape.Rectangle;

import java.io.IOException;

Expand All @@ -40,7 +40,9 @@ public class GeoBoundingBoxQueryBuilderTests extends AbstractQueryTestCase<GeoBo
protected GeoBoundingBoxQueryBuilder doCreateTestQueryBuilder() {
String fieldName = randomFrom(GEO_POINT_FIELD_NAME, GEO_POINT_ALIAS_FIELD_NAME, GEO_SHAPE_FIELD_NAME);
GeoBoundingBoxQueryBuilder builder = new GeoBoundingBoxQueryBuilder(fieldName);
Rectangle box = RandomShapeGenerator.xRandomRectangle(random(), RandomShapeGenerator.xRandomPoint(random()));
// make sure that minX != maxX after geohash encoding
Rectangle box = randomValueOtherThanMany((r) ->
Math.abs(r.getMaxX() - r.getMinX()) < 0.1 || Math.abs(r.getMaxY() - r.getMinY()) < 0.1, GeometryTestUtils::randomRectangle);

if (randomBoolean()) {
// check the top-left/bottom-right combination of setters
Expand All @@ -53,8 +55,8 @@ protected GeoBoundingBoxQueryBuilder doCreateTestQueryBuilder() {
break;
case 1:
builder.setCorners(
GeohashUtils.encodeLatLon(box.getMaxY(), box.getMinX()),
GeohashUtils.encodeLatLon(box.getMinY(), box.getMaxX()));
Geohash.stringEncode(box.getMinX(), box.getMaxY()),
Geohash.stringEncode(box.getMaxX(), box.getMinY()));
break;
default:
builder.setCorners(box.getMaxY(), box.getMinX(), box.getMinY(), box.getMaxX());
Expand All @@ -67,8 +69,8 @@ protected GeoBoundingBoxQueryBuilder doCreateTestQueryBuilder() {
new GeoPoint(box.getMaxY(), box.getMaxX()));
} else {
builder.setCornersOGC(
GeohashUtils.encodeLatLon(box.getMinY(), box.getMinX()),
GeohashUtils.encodeLatLon(box.getMaxY(), box.getMaxX()));
Geohash.stringEncode(box.getMinX(), box.getMinY()),
Geohash.stringEncode(box.getMaxX(), box.getMaxY()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.index.mapper.GeoPointFieldMapper;
import org.elasticsearch.index.mapper.GeoShapeFieldMapper;
import org.elasticsearch.index.mapper.LegacyGeoShapeFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.test.geo.RandomShapeGenerator;
import org.locationtech.spatial4j.shape.Point;

import java.io.IOException;

Expand Down Expand Up @@ -56,8 +55,7 @@ protected GeoDistanceQueryBuilder doCreateTestQueryBuilder() {
break;
}

Point p = RandomShapeGenerator.xRandomPoint(random());
qb.point(new GeoPoint(p.getY(), p.getX()));
qb.point(new GeoPoint(GeometryTestUtils.randomLat(), GeometryTestUtils.randomLon()));

if (randomBoolean()) {
qb.setValidationMethod(randomFrom(GeoValidationMethod.values()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geometry.LinearRing;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.test.geo.RandomShapeGenerator;
import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.spatial4j.shape.jts.JtsGeometry;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -102,18 +99,10 @@ private void assertDeprecationWarning() {
}

private static List<GeoPoint> randomPolygon() {
ShapeBuilder<?, ?, ?> shapeBuilder = null;
// This is a temporary fix because sometimes the RandomShapeGenerator
// returns null. This is if there is an error generating the polygon. So
// in this case keep trying until we successfully generate one
while (shapeBuilder == null) {
shapeBuilder = RandomShapeGenerator.createShapeWithin(random(), null, ShapeType.POLYGON);
}
JtsGeometry shape = (JtsGeometry) shapeBuilder.buildS4J();
Coordinate[] coordinates = shape.getGeom().getCoordinates();
ArrayList<GeoPoint> polygonPoints = new ArrayList<>();
for (Coordinate coord : coordinates) {
polygonPoints.add(new GeoPoint(coord.y, coord.x));
LinearRing linearRing = GeometryTestUtils.randomPolygon(false).getPolygon();
List<GeoPoint> polygonPoints = new ArrayList<>(linearRing.length());
for (int i = 0; i < linearRing.length(); i++) {
polygonPoints.add(new GeoPoint(linearRing.getLat(i), linearRing.getLon(i)));
}
return polygonPoints;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import org.elasticsearch.common.xcontent.XContentParseException;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geometry.Point;
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
import org.elasticsearch.search.aggregations.bucket.range.GeoDistanceAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.range.GeoDistanceAggregationBuilder.Range;
import org.elasticsearch.test.geo.RandomShapeGenerator;

import java.io.IOException;

Expand All @@ -29,8 +30,9 @@ public class GeoDistanceRangeTests extends BaseAggregationTestCase<GeoDistanceAg
@Override
protected GeoDistanceAggregationBuilder createTestAggregatorBuilder() {
int numRanges = randomIntBetween(1, 10);
GeoPoint origin = RandomShapeGenerator.randomPoint(random());
GeoDistanceAggregationBuilder factory = new GeoDistanceAggregationBuilder(randomAlphaOfLengthBetween(3, 10), origin);
Point origin = GeometryTestUtils.randomPoint();
GeoDistanceAggregationBuilder factory =
new GeoDistanceAggregationBuilder(randomAlphaOfLengthBetween(3, 10), new GeoPoint(origin.getLat(), origin.getLon()));
for (int i = 0; i < numRanges; i++) {
String key = null;
if (randomBoolean()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@

public class GeoPointShapeQueryTests extends GeoQueryTests {

@Override
protected XContentBuilder createTypedMapping() throws Exception {
XContentBuilder xcb = XContentFactory.jsonBuilder().startObject().startObject("type1")
.startObject("properties").startObject("location")
.field("type", "geo_point")
.endObject().endObject().endObject().endObject();

return xcb;
}

@Override
protected XContentBuilder createDefaultMapping() throws Exception {
XContentBuilder xcb = XContentFactory.jsonBuilder().startObject()
Expand Down

0 comments on commit 13980e3

Please sign in to comment.